Skip to content

Commit

Permalink
update MF
Browse files Browse the repository at this point in the history
  • Loading branch information
Xziy committed Jul 21, 2023
1 parent bb3b21e commit 138e65a
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 50 deletions.
5 changes: 3 additions & 2 deletions adapters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ exports.Adapter = exports.OTP = exports.Captcha = exports.Payment = exports.Map
const RMSAdapter_1 = require("./rms/RMSAdapter");
const pow_1 = require("./captcha/default/pow");
const defaultOTP_1 = require("./otp/default/defaultOTP");
const local_1 = require("./mediafile/default/local");
const MediaFileAdapter_1 = require("./mediafile/MediaFileAdapter");
const fs = require("fs");
const discountAdapter_1 = require("./discount/default/discountAdapter");
const path = require("path");
// import DiscountAdapter from "./discount/AbstractDiscountAdapter";
const WEBRESTO_MODULES_PATH = process.env.WEBRESTO_MODULES_PATH === undefined ? "@webresto" : process.env.WEBRESTO_MODULES_PATH;
/**
Expand Down Expand Up @@ -238,7 +238,8 @@ class Adapter {
if (!adapterName) {
adapterName = await Settings.get("DEFAULT_MEDIAFILE_ADAPTER");
if (!adapterName) {
adapterLocation = path.resolve(__dirname, "mediafile/default/local");
this.instanceMF = new local_1.default(initParams);
return this.instanceMF;
}
}
if (!adapterLocation) {
Expand Down
4 changes: 3 additions & 1 deletion adapters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import MapAdapter from "./map/MapAdapter";
import CaptchaAdapter from "./captcha/CaptchaAdapter";
import { POW } from "./captcha/default/pow";
import { DefaultOTP } from "./otp/default/defaultOTP";
import LocalMediaFileAdapter from "./mediafile/default/local";
import OTPAdapter from "./otp/OneTimePasswordAdapter"
import MediaFileAdapter, { ConfigMediaFileAdapter } from "./mediafile/MediaFileAdapter";
import PaymentAdapter from "./payment/PaymentAdapter";
Expand Down Expand Up @@ -274,7 +275,8 @@ export class Adapter {
if(!adapterName) {
adapterName = await Settings.get("DEFAULT_MEDIAFILE_ADAPTER") as string;
if (!adapterName) {
adapterLocation = path.resolve(__dirname, "mediafile/default/local")
this.instanceMF = new LocalMediaFileAdapter(initParams);
return this.instanceMF;
}
}

Expand Down
9 changes: 6 additions & 3 deletions adapters/mediafile/MediaFileAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ class MediaFileAdapter {
async toDownload(url, target, type, force = false) {
await this.wait();
let imageId = (0, uuid_1.v5)(url, this.UUID_NAMESPACE);
const mediaFile = await MediaFile.findOne({ id: imageId });
let mediaFile = await MediaFile.findOne({ id: imageId });
let loadConfig;
if (target && this.config[target]) {
if (target && this.config && this.config[target]) {
loadConfig = this.config[target];
}
// image
if (mediaFile === undefined || force) {
mediaFile = {
id: imageId
};
switch (type) {
case "image":
mediaFile.images = this.load(url, "image", loadConfig);
Expand All @@ -43,7 +46,7 @@ class MediaFileAdapter {
throw `mediaFile type not known ${type}`;
break;
}
// DOWNLOAD
mediaFile = await MediaFile.create(mediaFile).fetch();
}
return mediaFile;
}
Expand Down
12 changes: 7 additions & 5 deletions adapters/mediafile/MediaFileAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,19 @@ export default abstract class MediaFileAdapter {
public async toDownload(url: string, target: string, type: MediaFileTypes, force: boolean = false): Promise<MediaFile> {
await this.wait()
let imageId = uuidv5(url, this.UUID_NAMESPACE);
const mediaFile = await MediaFile.findOne({ id: imageId });
let mediaFile = await MediaFile.findOne({ id: imageId });

let loadConfig: BaseConfigProperty;
if (target && this.config[target]){
if (target && this.config && this.config[target]){
loadConfig = this.config[target];
}


// image
if (mediaFile === undefined || force) {
mediaFile = {
id: imageId
};
switch (type)
{
case "image":
Expand All @@ -68,10 +71,9 @@ export default abstract class MediaFileAdapter {
throw `mediaFile type not known ${type}`
break;
}

// DOWNLOAD
mediaFile = await MediaFile.create(mediaFile).fetch()
}

return mediaFile;
};

Expand Down
67 changes: 36 additions & 31 deletions adapters/mediafile/default/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,39 +134,44 @@ export default class LocalMediaFileAdapter extends MediaFileAdapter {
}

private async loadMediaFiles() {
while (this.loadMediaFilesProcessQueue.length) {
const loadMediaFilesProcess = this.loadMediaFilesProcessQueue.shift();
const MEDIAFILE_PARALEL_TO_DOWNLOAD = await Settings.get('MEDIAFILE_PARALEL_TO_DOWNLOAD') as number ?? 3;

while (this.loadMediaFilesProcessQueue.length) {
const loadMediaFilesProcesses = this.loadMediaFilesProcessQueue.splice(0, MEDIAFILE_PARALEL_TO_DOWNLOAD);

const downloadPromises = loadMediaFilesProcesses.map(async (loadMediaFilesProcess) => {
const prefix = await this.download(loadMediaFilesProcess);

switch (loadMediaFilesProcess.type) {
case "image":
for (let size in loadMediaFilesProcess.config.resize) {
if (size === "origin") continue;
const mediafileItem = <Size>loadMediaFilesProcess.config.resize[size];
if (!mediafileItem.width && !mediafileItem.height) {
throw "Not valid mediafile config. Must have name (key) and one of width or height";
}
mediafileItem.width = mediafileItem.width || mediafileItem.height;
mediafileItem.height = mediafileItem.height || mediafileItem.width;

await resizeMediaFile({
srcPath: path.normalize(prefix + loadMediaFilesProcess.name.origin),
dstPath: path.normalize(prefix + loadMediaFilesProcess.name[size]),
width: mediafileItem.width,
height: mediafileItem.height,
customArgs: ["-background", loadMediaFilesProcess.config.background || "white", "-flatten"],
});
}
break;
default:
break;
}
});

const prefix = await this.download(loadMediaFilesProcess);
// Wait for all downloads and processing to complete
await Promise.all(downloadPromises);
}

switch (loadMediaFilesProcess.type)
{
case "image":
for (let size in loadMediaFilesProcess.config.resize) {
if (size === "origin") continue;

const mediafileItem = <Size>loadMediaFilesProcess.config.resize[size];
if (!mediafileItem.width && !mediafileItem.height) {
throw "Not valid mediafile config. Must have name (key) and one of width or height";
}

mediafileItem.width = mediafileItem.width || mediafileItem.height;
mediafileItem.height = mediafileItem.height || mediafileItem.width;

await resizeMediaFile({
srcPath: path.normalize(prefix + loadMediaFilesProcess.name.origin),
dstPath: path.normalize(prefix + loadMediaFilesProcess.name[size]),
width: mediafileItem.width,
height: mediafileItem.height,
customArgs: ["-background", loadMediaFilesProcess.config.background || "white", "-flatten"],
});
}
break;
default:
break;
}
}
setTimeout(this.loadMediaFiles.bind(this), 60000);
setTimeout(this.loadMediaFiles.bind(this), 10000);
}
}

Expand Down
15 changes: 11 additions & 4 deletions adapters/rms/RMSAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ class RMSAdapter {
return;
}
this.syncProductsExecuted = true;
const rootGroupsToSync = await Settings.get("ROOT_GROUPS_RMS_TO_SYNC");
let rootGroupsToSync = await Settings.get("ROOT_GROUPS_RMS_TO_SYNC");
if (typeof rootGroupsToSync === "string")
rootGroupsToSync = rootGroupsToSync.split(";");
if (!rootGroupsToSync)
rootGroupsToSync = [];
const rmsAdapter = await Adapter.getRMSAdapter();
if (await rmsAdapter.nomenclatureHasUpdated() || force) {
const currentRMSGroupsFlatTree = await rmsAdapter.loadNomenclatureTree(rootGroupsToSync);
Expand All @@ -77,14 +81,17 @@ class RMSAdapter {
allProductIds = allProductIds.concat(productIds);
for (let product of productsToUpdate) {
emitter.emit("rms-sync:before-each-product-item", product);
const SKIP_LOAD_PRODUCT_IMAGES = await Settings.get("SKIP_LOAD_PRODUCT_IMAGES") ?? false;
// Load images
if (product.images && product.images.length) {
if (product.images && product.images.length && !SKIP_LOAD_PRODUCT_IMAGES) {
console.log(product.images);
const isURL = (str) => /^(https?:\/\/)?[\w.-]+(\.[\w.-]+)+[\w\-._~:/?#[\]@!$&'()*+,;=]+$/.test(str);
for (let image of product.images) {
if (isURL(image)) {
// load image
let mfAdater = await Adapter.getMediaFileAdapter();
let mediaFileImage = await mfAdater.toDownload(image, 'dish', 'image');
const mfAdater = await Adapter.getMediaFileAdapter();
const mediaFileImage = await mfAdater.toDownload(image, 'dish', 'image');
console.log(mediaFileImage);
await Dish.addToCollection(product.id, 'images').members([mediaFileImage.id]);
}
else {
Expand Down
14 changes: 10 additions & 4 deletions adapters/rms/RMSAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ export default abstract class RMSAdapter {
}
this.syncProductsExecuted = true;

const rootGroupsToSync = await Settings.get("ROOT_GROUPS_RMS_TO_SYNC") as string[];
let rootGroupsToSync = await Settings.get("ROOT_GROUPS_RMS_TO_SYNC") as string | string[];
if(typeof rootGroupsToSync === "string") rootGroupsToSync = rootGroupsToSync.split(";");
if (!rootGroupsToSync) rootGroupsToSync = [];

const rmsAdapter = await Adapter.getRMSAdapter();

if (await rmsAdapter.nomenclatureHasUpdated() || force) {
Expand Down Expand Up @@ -123,14 +126,17 @@ export default abstract class RMSAdapter {
for (let product of productsToUpdate) {
emitter.emit("rms-sync:before-each-product-item", product);

const SKIP_LOAD_PRODUCT_IMAGES = await Settings.get("SKIP_LOAD_PRODUCT_IMAGES") as boolean ?? false
// Load images
if(product.images && product.images.length) {
if(product.images && product.images.length && !SKIP_LOAD_PRODUCT_IMAGES) {
console.log(product.images)
const isURL = (str) => /^(https?:\/\/)?[\w.-]+(\.[\w.-]+)+[\w\-._~:/?#[\]@!$&'()*+,;=]+$/.test(str);
for (let image of product.images){
if (isURL(image)){
// load image
let mfAdater = await Adapter.getMediaFileAdapter()
let mediaFileImage = await mfAdater.toDownload(image as string, 'dish', 'image');
const mfAdater = await Adapter.getMediaFileAdapter()
const mediaFileImage = await mfAdater.toDownload(image as string, 'dish', 'image');
console.log(mediaFileImage)
await Dish.addToCollection(product.id, 'images').members([mediaFileImage.id]);
} else {
sails.log.debug(`Image not url on sync products ${image}`);
Expand Down

0 comments on commit 138e65a

Please sign in to comment.