Save Car Catalogs Images one by one
MODIFY
src/integration/car/CarIntegration.ts
src/integration/car/CarIntegration.ts
@@ -19,6 +19,7 @@ export default abstract class CarIntegration {
for (const externalCar of externalCars) {
try {
const internalCar = await CarStorage.getCarCatalog(externalCar.id);
+ let image = '', imagesCount = 0;
if (!internalCar) {
// New Car: Create it
externalCar.hash = Cypher.hash(JSON.stringify(externalCar));
@@ -28,13 +29,15 @@ export default abstract class CarIntegration {
externalCar.image = await this.getCarCatalogThumb(externalCar);
for (const imageURL of externalCar.imageURLs) {
// Get images
- // externalCar.images = await this.getCarCatalogImages(externalCar, imageURL);
+ image = await this.getCarCatalogImage(externalCar, imageURL);
+ await CarStorage.saveCarImage(externalCar.id, image);
+ imagesCount++;
}
// Create the Hash
- externalCar.imagesHash = (externalCar.imageURLs.length > 0 && externalCar.imageURLs.length === externalCar.images.length && externalCar.image) ?
+ externalCar.imagesHash = (externalCar.imageURLs.length > 0 && externalCar.imageURLs.length === imagesCount && externalCar.image) ?
Cypher.hash(externalCar.imageURLs.toString()) : null;
// Save
- externalCar.id = await CarStorage.saveCarCatalog(externalCar, true);
+ externalCar.id = await CarStorage.saveCarCatalog(externalCar);
actionsDone.inSuccess++;
// Log
await Logging.logDebug({
@@ -53,25 +56,25 @@ export default abstract class CarIntegration {
if (!internalCar.imagesHash || (Cypher.hash(externalCar.imageURLs.toString()) !== internalCar.imagesHash)) {
// Get image
externalCar.image = await this.getCarCatalogThumb(externalCar);
+ // Delete old car catalogs images
+ await CarStorage.deleteCarImages(externalCar.id);
// Get images
- let i = 0;
for (const imageURL of externalCar.imageURLs) {
// Get images
- const image = await this.getCarCatalogImages(externalCar, imageURL);
- await CarStorage.saveCarImages(externalCar.id, [image]);
- i++;
+ image = await this.getCarCatalogImage(externalCar, imageURL);
+ await CarStorage.saveCarImage(externalCar.id, image);
+ imagesCount++;
}
// Create the Hash
- externalCar.imagesHash = (externalCar.imageURLs.length > 0 && externalCar.imageURLs.length === i && externalCar.image) ?
+ externalCar.imagesHash = (externalCar.imageURLs.length > 0 && externalCar.imageURLs.length === imagesCount
+ && externalCar.image) ?
Cypher.hash(externalCar.imageURLs.toString()) : null;
- // Save
- await CarStorage.saveCarCatalog(externalCar, true);
} else {
externalCar.image = internalCar.image;
externalCar.imagesHash = internalCar.imagesHash;
- // Save
- await CarStorage.saveCarCatalog(externalCar, false);
}
+ // Save
+ await CarStorage.saveCarCatalog(externalCar);
actionsDone.inSuccess++;
// Log
await Logging.logDebug({
@@ -109,5 +112,5 @@ export default abstract class CarIntegration {
public abstract getCarCatalogThumb(carCatalog: CarCatalog): Promise<string>;
- public abstract getCarCatalogImages(carCatalog: CarCatalog, imageURL: string): Promise<string>;
+ public abstract getCarCatalogImage(carCatalog: CarCatalog, imageURL: string): Promise<string>;
}

MODIFY
src/integration/car/ev-database/EVDatabaseCarIntegration.ts
src/integration/car/ev-database/EVDatabaseCarIntegration.ts
@@ -232,28 +232,22 @@ export default class EVDatabaseCarIntegration extends CarIntegration {
return image;
}
- public async getCarCatalogImages(carCatalog: CarCatalog, imageURL:string): Promise<string> {
- const images: string[] = [];
- // Retrieve all images
- // for (const imageURL of carCatalog.imageURLs) {
+ public async getCarCatalogImage(carCatalog: CarCatalog, imageURL:string): Promise<string> {
try {
const response = await this.axiosInstance.get(imageURL, { responseType: 'arraybuffer' });
const base64Image = Buffer.from(response.data).toString('base64');
const encodedImage = 'data:' + response.headers['content-type'] + ';base64,' + base64Image;
- images.push(encodedImage);
return encodedImage;
} catch (error) {
await Logging.logError({
tenantID: Constants.DEFAULT_TENANT,
source: Constants.CENTRAL_SERVER,
action: ServerAction.SYNCHRONIZE_CAR_CATALOGS,
- module: MODULE_NAME, method: 'getCarCatalogImages',
+ module: MODULE_NAME, method: 'getCarCatalogImage',
message: `${carCatalog.id} - ${carCatalog.vehicleMake} - ${carCatalog.vehicleModel} - Cannot retrieve image from URL '${imageURL}'`,
detailedMessages: { error: error.message, stack: error.stack }
});
}
- // }
- // return images;
}
private convertToThumbImage(image: string): string {

MODIFY
src/migration/tasks/ImportLocalCarCatalogTask.ts
src/migration/tasks/ImportLocalCarCatalogTask.ts
@@ -189,17 +189,9 @@ export default class ImportLocalCarCatalogTask extends MigrationTask {
images: [],
videos: car.Videos,
};
- for (const imageURL of carCatalog.imageURLs) {
- const imageURLPath = `${global.appRoot}/assets/cars/img/${imageURL}`;
- const image = fs.readFileSync(imageURLPath);
- const contentType = await FileType.fromFile(imageURLPath);
- const base64Image = Buffer.from(image).toString('base64');
- const encodedImage = 'data:' + contentType.mime + ';base64,' + base64Image;
- carCatalog.images.push(encodedImage);
- }
- const imageURLPath = `${global.appRoot}/assets/cars/img/${carCatalog.imageURLs[0]}`;
+ let imageURLPath = `${global.appRoot}/assets/cars/img/${carCatalog.imageURLs[0]}`;
const base64ThumbImage = (await sharp(imageURLPath).resize(200, 150).toBuffer()).toString('base64');
- const contentType = await FileType.fromFile(imageURLPath);
+ let contentType = await FileType.fromFile(imageURLPath);
carCatalog.image = 'data:' + contentType.mime + ';base64,' + base64ThumbImage;
await global.database.getCollection<any>(Constants.DEFAULT_TENANT, 'carcatalogs').deleteOne({
_id: carCatalog.id
@@ -209,14 +201,23 @@ export default class ImportLocalCarCatalogTask extends MigrationTask {
});
carCatalog.createdOn = new Date();
carCatalog.lastChangedOn = carCatalog.createdOn;
+ for (const imageURL of carCatalog.imageURLs) {
+ imageURLPath = `${global.appRoot}/assets/cars/img/${imageURL}`;
+ const image = fs.readFileSync(imageURLPath);
+ contentType = await FileType.fromFile(imageURLPath);
+ const base64Image = Buffer.from(image).toString('base64');
+ const encodedImage = 'data:' + contentType.mime + ';base64,' + base64Image;
+ // Save car catalog images
+ await CarStorage.saveCarImage(carCatalog.id, encodedImage);
+ }
// Save
- await CarStorage.saveCarCatalog(carCatalog, true);
+ await CarStorage.saveCarCatalog(carCatalog);
created++;
}
}
} catch (error) {
- Logging.logError({
+ await Logging.logError({
tenantID: Constants.DEFAULT_TENANT,
module: MODULE_NAME, method: 'migrate',
action: ServerAction.CAR_CATALOG_SYNCHRONIZATION,
@@ -225,7 +226,7 @@ export default class ImportLocalCarCatalogTask extends MigrationTask {
});
} // Log in the default tenant
if (created > 0) {
- Logging.logDebug({
+ await Logging.logDebug({
tenantID: Constants.DEFAULT_TENANT,
action: ServerAction.MIGRATION,
module: MODULE_NAME, method: 'migrateTenant',

MODIFY
src/migration/tasks/ResetCarCatalogsHashTask.ts
src/migration/tasks/ResetCarCatalogsHashTask.ts
@@ -39,7 +39,7 @@ export default class ResetCarCatalogsHashTask extends MigrationTask {
}
getVersion(): string {
- return '2.5';
+ return '1.1';
}
isAsynchronous(): boolean {

MODIFY
src/storage/mongodb/CarStorage.ts
src/storage/mongodb/CarStorage.ts
@@ -143,7 +143,7 @@ export default class CarStorage {
};
}
- public static async saveCarCatalog(carToSave: CarCatalog, saveImage = false): Promise<number> {
+ public static async saveCarCatalog(carToSave: CarCatalog): Promise<number> {
// Debug
const uniqueTimerID = Logging.traceStart(Constants.DEFAULT_TENANT, MODULE_NAME, 'saveCarCatalog');
// Build Request
@@ -312,31 +312,33 @@ export default class CarStorage {
carMDB,
{ upsert: true }
);
- if (saveImage) {
- await CarStorage.saveCarImages(carToSave.id, carToSave.images);
- }
// Debug
await Logging.traceEnd(Constants.DEFAULT_TENANT, MODULE_NAME, 'saveCarCatalog', uniqueTimerID, carMDB);
return carToSave.id;
}
- public static async saveCarImages(carID: number, carImagesToSave: string[]): Promise<void> {
+ public static async saveCarImage(carID: number, carImageToSave: string): Promise<void> {
// Debug
- const uniqueTimerID = Logging.traceStart(Constants.DEFAULT_TENANT, MODULE_NAME, 'saveCarImages');
- // Delete old images
- await global.database.getCollection<any>(Constants.DEFAULT_TENANT, 'carcatalogimages').deleteMany(
- { carID: Utils.convertToInt(carID) }
- );
+ const uniqueTimerID = Logging.traceStart(Constants.DEFAULT_TENANT, MODULE_NAME, 'saveCarImage');
// Save new images
- for (const carImageToSave of carImagesToSave) {
await global.database.getCollection<any>(Constants.DEFAULT_TENANT, 'carcatalogimages').findOneAndReplace(
{ _id: Cypher.hash(`${carImageToSave}~${carID}`), },
{ carID: Utils.convertToInt(carID), image: carImageToSave },
{ upsert: true }
);
+ // Debug
+ await Logging.traceEnd(Constants.DEFAULT_TENANT, MODULE_NAME, 'saveCarImage', uniqueTimerID, carImageToSave);
}
+
+ public static async deleteCarImages(carID: number): Promise<void> {
+ // Debug
+ const uniqueTimerID = Logging.traceStart(Constants.DEFAULT_TENANT, MODULE_NAME, 'deleteCarImages');
+ // Delete car catalogs images
+ await global.database.getCollection<any>(Constants.DEFAULT_TENANT, 'carcatalogimages').deleteMany(
+ { carID: Utils.convertToInt(carID) }
+ );
// Debug
- await Logging.traceEnd(Constants.DEFAULT_TENANT, MODULE_NAME, 'saveCarImages', uniqueTimerID, carImagesToSave);
+ await Logging.traceEnd(Constants.DEFAULT_TENANT, MODULE_NAME, 'deleteCarImages', uniqueTimerID, carID);
}
public static async getCarCatalogImage(id: number): Promise<{ id: number; image: string }> {

Save Car Catalogs Images one by one