Optimized Car Catalog image
MODIFY
src/server/rest/v1/service/CarService.ts
src/server/rest/v1/service/CarService.ts
@@ -39,22 +39,20 @@ export default class CarService {
}
// Filter
const filteredRequest = CarSecurity.filterCarCatalogsRequest(req.query);
- const projectFields = [ 'id', 'vehicleModel', 'vehicleMake', 'vehicleModelVersion', 'batteryCapacityFull', 'fastchargeChargeSpeed', 'performanceTopspeed',
- 'performanceAcceleration', 'rangeWLTP', 'rangeReal', 'efficiencyReal',
- 'chargeStandardPower', 'chargeStandardPhase', 'chargeStandardPhaseAmp', 'chargeAlternativePower', 'chargeOptionPower',
- 'chargeOptionPhaseAmp', 'chargeOptionPhase', 'chargeAlternativePhaseAmp', 'chargeAlternativePhase', 'chargePlug', 'fastChargePlug', 'fastChargePowerMax', 'drivetrainPowerHP' ];
- // Get the cars
+ // Get the Cars
const carCatalogs = await CarStorage.getCarCatalogs(
{
search: filteredRequest.Search,
carMaker: filteredRequest.CarMaker ? filteredRequest.CarMaker.split('|') : null
},
{ limit: filteredRequest.Limit, skip: filteredRequest.Skip, sort: filteredRequest.Sort, onlyRecordCount: filteredRequest.OnlyRecordCount },
- projectFields
+ [
+ 'id', 'vehicleModel', 'vehicleMake', 'vehicleModelVersion', 'batteryCapacityFull', 'fastchargeChargeSpeed', 'performanceTopspeed',
+ 'performanceAcceleration', 'rangeWLTP', 'rangeReal', 'efficiencyReal', 'image',
+ 'chargeStandardPower', 'chargeStandardPhase', 'chargeStandardPhaseAmp', 'chargeAlternativePower', 'chargeOptionPower',
+ 'chargeOptionPhaseAmp', 'chargeOptionPhase', 'chargeAlternativePhaseAmp', 'chargeAlternativePhase', 'chargePlug', 'fastChargePlug', 'fastChargePowerMax', 'drivetrainPowerHP'
+ ]
);
- // Filter
- CarSecurity.filterCarCatalogsResponse(carCatalogs, req.user);
- // Return
res.json(carCatalogs);
next();
}
@@ -102,12 +100,13 @@ export default class CarService {
const filteredRequest = CarSecurity.filterCarCatalogRequest(req.query);
UtilsService.assertIdIsProvided(action, filteredRequest.ID, MODULE_NAME, 'handleGetCarCatalogImage', req.user);
// Get the car Image
- const carCatalog = await CarStorage.getCarCatalog(filteredRequest.ID, ['image']);
+ const carCatalog = await CarStorage.getCarCatalogImage(filteredRequest.ID);
// Return
+ if (carCatalog?.image) {
+ // Remove encoding header
let header = 'image';
let encoding: BufferEncoding = 'base64';
- // Remove encoding header
- if (carCatalog.image.startsWith('data:image/')) {
+ if (carCatalog?.image.startsWith('data:image/')) {
header = carCatalog.image.substring(5, carCatalog.image.indexOf(';'));
encoding = carCatalog.image.substring(carCatalog.image.indexOf(';') + 1, carCatalog.image.indexOf(',')) as BufferEncoding;
carCatalog.image = carCatalog.image.substring(carCatalog.image.indexOf(',') + 1);
@@ -115,6 +114,9 @@ export default class CarService {
// Revert to binary
res.setHeader('content-type', header);
res.send(Buffer.from(carCatalog.image, encoding));
+ } else {
+ res.send(null);
+ }
next();
}

MODIFY
src/server/rest/v1/service/security/CarSecurity.ts
src/server/rest/v1/service/security/CarSecurity.ts
@@ -42,7 +42,7 @@ export default class CarSecurity {
public static filterCarCatalogRequest(request: any): HttpCarCatalogByIDRequest {
const filteredRequest: HttpCarCatalogByIDRequest = {
- ID: sanitize(request.ID),
+ ID: Utils.convertToInt(sanitize(request.ID)),
} as HttpCarCatalogByIDRequest;
return filteredRequest;
}

MODIFY
src/storage/mongodb/CarStorage.ts
src/storage/mongodb/CarStorage.ts
@@ -55,9 +55,7 @@ export default class CarStorage {
if (params.carMaker) {
aggregation.push({
$match: {
- 'vehicleMake': {
- $in: params.carMaker
- }
+ 'vehicleMake': { $in: params.carMaker }
}
});
}
@@ -100,6 +98,19 @@ export default class CarStorage {
aggregation.push({
$limit: (dbParams.limit > 0 && dbParams.limit < Constants.DB_RECORD_COUNT_CEIL) ? dbParams.limit : Constants.DB_RECORD_COUNT_CEIL
});
+ // Car Image
+ aggregation.push({
+ $addFields: {
+ image: {
+ $concat: [
+ `${Utils.buildRestServerURL()}/client/util/CarCatalogImage?ID=`,
+ { $toString: '$_id' },
+ '&LastChangedOn=',
+ { $toString: '$lastChangedOn' }
+ ]
+ }
+ }
+ });
// Add Created By / Last Changed By
DatabaseUtils.pushCreatedLastChangedInAggregation(Constants.DEFAULT_TENANT, aggregation);
// Handle the ID
@@ -319,6 +330,20 @@ export default class CarStorage {
Logging.traceEnd(Constants.DEFAULT_TENANT, MODULE_NAME, 'saveCarImages', uniqueTimerID, carImagesToSave);
}
+ public static async getCarCatalogImage(id: number): Promise<{ id: number; image: string }> {
+ // Debug
+ const uniqueTimerID = Logging.traceStart(Constants.DEFAULT_TENANT, MODULE_NAME, 'getCarCatalogImage');
+ // Read DB
+ const carCatalogImageMDB = await global.database.getCollection<{ _id: number; image: string }>(Constants.DEFAULT_TENANT, 'carcatalogs')
+ .findOne({ _id: id });
+ // Debug
+ Logging.traceEnd(Constants.DEFAULT_TENANT, MODULE_NAME, 'getCarCatalogImage', uniqueTimerID, carCatalogImageMDB);
+ return {
+ id: id,
+ image: carCatalogImageMDB ? carCatalogImageMDB.image : null
+ };
+ }
+
public static async getCarCatalogImages(id: number = Constants.UNKNOWN_NUMBER_ID, dbParams?: DbParams): Promise<DataResult<Image>> {
// Debug
const uniqueTimerID = Logging.traceStart(Constants.DEFAULT_TENANT, MODULE_NAME, 'getCarCatalogImages');

Optimized Car Catalog image