Skip to content

Commit

Permalink
♻️ reafactor: move upload process to a separate service
Browse files Browse the repository at this point in the history
  • Loading branch information
david-vaclavek committed Oct 5, 2023
1 parent b0ce91c commit 27defe9
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 158 deletions.
1 change: 0 additions & 1 deletion admin/src/pages/Upload/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ function Upload(props) {
setIsUploading(false);
setShowUploadFinishedModal(true);
});
setUploadResult(result);

// track upload
ProductAnalyticsService.trackUploadToLocalazy(
Expand Down
164 changes: 7 additions & 157 deletions server/controllers/localazy-transfer-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const RequestInitiatorHelper = require('../utils/request-initiator-helper');
const PluginSettingsServiceHelper = require('../services/helpers/plugin-settings-service-helper');
const generateRandomId = require('../utils/generate-random-id');
const { LOCALAZY_PLUGIN_CHANNEL } = require('../constants/channels');
const { UPLOAD_EVENT, UPLOAD_FINISHED_EVENT } = require('../constants/events');
const { UPLOAD_FINISHED_EVENT } = require('../constants/events');

const getFilteredLanguagesCodesForDownload = async (languagesCodes) => {
const pluginSettingsServiceHelper = new PluginSettingsServiceHelper(strapi);
Expand Down Expand Up @@ -52,166 +52,16 @@ module.exports = {
try {
const streamIdentifier = generateRandomId();

const func = async () => {
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${UPLOAD_EVENT}:${streamIdentifier}`, {
message: 'Upload started',
});

let success = true;

// Strapi Service
const StrapiService = strapi
.plugin("localazy")
.service("strapiService");

// Strapi i18n Service
const StrapiI18nService = strapi
.plugin("localazy")
.service("strapiI18nService");

// Localazy Upload Service
const LocalazyUploadService = strapi
.plugin("localazy")
.service("localazyUploadService");

// get content transfer setup
const contentTransferSetup = await strapi
.plugin("localazy")
.service("pluginSettingsService")
.getContentTransferSetup();

if (!contentTransferSetup.has_setup) {
const message = "Content transfer setup is not set up.";
success = false;
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${UPLOAD_FINISHED_EVENT}:${streamIdentifier}`, {
success,
message,
});
strapi.log.info(message);
ctx.body = {
success,
};
return;
}

const { setup } = contentTransferSetup;
const collectionsNames = getCollectionsNames(setup);
const models = await StrapiService.getModels();
// flatten Strapi content
let flattenContent = {};

for (const collectionName of collectionsNames) {
const currentModel = models.find(
(model) => model.collectionName === collectionName
);
const modelUid = currentModel.uid;
if (!currentModel) {
strapi.log.warn(`Model with uid ${modelUid} is not found.`);
continue;
}

const transferSetupModel = findSetupModelByCollectionName(
setup,
collectionName
);

if (!isCollectionTransferEnabled(setup, collectionName)) {
const message = `Collection ${collectionName} transfer is disabled.`;
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${UPLOAD_EVENT}:${streamIdentifier}`, {
message,
});
strapi.log.info(message);
continue;
}

// get only enabled fields paths
const currentTransferSetupModel = transferSetupModel[collectionName];
const pickPaths = getPickPathsWithComponents(currentTransferSetupModel);
if (!pickPaths.length) {
const message = `No fields for collection ${collectionName} transfer are enabled.`;
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${UPLOAD_EVENT}:${streamIdentifier}`, {
message,
});
strapi.log.warn(message);
continue;
}
const pickPathsWithUid = pickPaths.map(
(pickPath) => `${modelUid}.${pickPath}`
);

let entries = await strapi.entityService.findMany(modelUid, {
populate: "deep",
});
entries = omitDeep(entries, [
// "__component",
"locale",
"localizations",
"createdAt",
"createdBy",
"updatedAt",
"updatedBy",
"publishedAt",
]);

if (!entries) {
strapi.log.info(`No entries found for model ${modelUid}`);
continue;
}

if (!Array.isArray(entries)) {
entries = [entries];
}
const LocalazyTransferUploadService = strapi
.plugin("localazy")
.service("localazyTransferUploadService");

entries.forEach((entry) => {
const flatten = flattenObject({
[modelUid]: entry,
});
// get only enabled fields; "__component" will be filtered out inside of the function
const pickedFlatten = pickEntries(flatten, pickPathsWithUid);

flattenContent = {
...flattenContent,
...pickedFlatten,
};
});
}

// get Strapi default language and convert it to Localazy language code
const strapiLocales = await StrapiI18nService.getLocales(ctx);
const defaultLocale = strapiLocales.find((locale) => locale.isDefault);

const locale = defaultLocale
? isoStrapiToLocalazy(defaultLocale.code)
: config.LOCALAZY_DEFAULT_LOCALE;

const chunks = LocalazyUploadService.splitToChunks(flattenContent);
const importFile = LocalazyUploadService.createImportFileRepresentation(
config.LOCALAZY_DEFAULT_FILE_NAME,
config.LOCALAZY_DEFAULT_FILE_PATH,
config.LOCALAZY_DEFAULT_FILE_EXTENSION,
locale,
chunks
);
// Use `deprecate: "file"` if there is one chunk of transferred data only!
const hasMoreTransferFilesChunks = importFile.length > 1;
const uploadConfig = !hasMoreTransferFilesChunks ? { deprecate: "file" } : {};
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${UPLOAD_EVENT}:${streamIdentifier}`, {
message: "Uploading collections to Localazy...",
});
await LocalazyUploadService.upload(
importFile,
uploadConfig
);
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${UPLOAD_FINISHED_EVENT}:${streamIdentifier}`, {
success,
message: "Upload finished",
});
};
/**
* start executing the function after 2 seconds
* start executing the function after a delay
* (to let the client receive and subscribe to the messages stream)
*/
setTimeout(func, 2000);
// TODO: let the client send a message to the server to start the upload (that it's subscribed to the stream )
setTimeout(() => (LocalazyTransferUploadService.upload(streamIdentifier, ctx)), 1000);

ctx.body = {
streamIdentifier,
Expand Down
2 changes: 2 additions & 0 deletions server/services/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const localazyDownloadService = require("./localazy-download-service");
const localazyPubApiService = require("./localazy-pubapi-service");
const strapiI18nService = require("./strapi-i18n-service");
const strapiLocalazyI18nService = require("./strapi-localazy-i18n-service");
const localazyTransferUploadService = require("./localazy-transfer-upload-service");

module.exports = {
localazyUserService,
Expand All @@ -20,4 +21,5 @@ module.exports = {
localazyPubApiService,
strapiI18nService,
strapiLocalazyI18nService,
localazyTransferUploadService,
};
175 changes: 175 additions & 0 deletions server/services/localazy-transfer-upload-service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
"use strict";

const flattenObject = require("../utils/flatten-object");
const {
getCollectionsNames,
findSetupModelByCollectionName,
isCollectionTransferEnabled,
getPickPathsWithComponents,
} = require("../utils/transfer-setup-utils");
const pickEntries = require("../utils/pick-entries");
const config = require("../config").default;
const {
isoStrapiToLocalazy,
} = require("../utils/iso-locales-utils");
const omitDeep = require("../utils/omit-deep");
const { LOCALAZY_PLUGIN_CHANNEL } = require('../constants/channels');
const { UPLOAD_EVENT, UPLOAD_FINISHED_EVENT } = require('../constants/events');

module.exports = ({ strapi }) => ({
async upload(streamIdentifier, ctx) {
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${UPLOAD_EVENT}:${streamIdentifier}`, {
message: 'Upload started',
});

let success = true;

// Strapi Service
const StrapiService = strapi
.plugin("localazy")
.service("strapiService");

// Strapi i18n Service
const StrapiI18nService = strapi
.plugin("localazy")
.service("strapiI18nService");

// Localazy Upload Service
const LocalazyUploadService = strapi
.plugin("localazy")
.service("localazyUploadService");

// get content transfer setup
const contentTransferSetup = await strapi
.plugin("localazy")
.service("pluginSettingsService")
.getContentTransferSetup();

if (!contentTransferSetup.has_setup) {
const message = "Content transfer setup is not set up.";
success = false;
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${UPLOAD_FINISHED_EVENT}:${streamIdentifier}`, {
success,
message,
});
strapi.log.info(message);
ctx.body = {
success,
};
return;
}

const { setup } = contentTransferSetup;
const collectionsNames = getCollectionsNames(setup);
const models = await StrapiService.getModels();
// flatten Strapi content
let flattenContent = {};

for (const collectionName of collectionsNames) {
const currentModel = models.find(
(model) => model.collectionName === collectionName
);
const modelUid = currentModel.uid;
if (!currentModel) {
strapi.log.warn(`Model with uid ${modelUid} is not found.`);
continue;
}

const transferSetupModel = findSetupModelByCollectionName(
setup,
collectionName
);

if (!isCollectionTransferEnabled(setup, collectionName)) {
const message = `Collection ${collectionName} transfer is disabled.`;
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${UPLOAD_EVENT}:${streamIdentifier}`, {
message,
});
strapi.log.info(message);
continue;
}

// get only enabled fields paths
const currentTransferSetupModel = transferSetupModel[collectionName];
const pickPaths = getPickPathsWithComponents(currentTransferSetupModel);
if (!pickPaths.length) {
const message = `No fields for collection ${collectionName} transfer are enabled.`;
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${UPLOAD_EVENT}:${streamIdentifier}`, {
message,
});
strapi.log.warn(message);
continue;
}
const pickPathsWithUid = pickPaths.map(
(pickPath) => `${modelUid}.${pickPath}`
);

let entries = await strapi.entityService.findMany(modelUid, {
populate: "deep",
});
entries = omitDeep(entries, [
// "__component",
"locale",
"localizations",
"createdAt",
"createdBy",
"updatedAt",
"updatedBy",
"publishedAt",
]);

if (!entries) {
strapi.log.info(`No entries found for model ${modelUid}`);
continue;
}

if (!Array.isArray(entries)) {
entries = [entries];
}

entries.forEach((entry) => {
const flatten = flattenObject({
[modelUid]: entry,
});
// get only enabled fields; "__component" will be filtered out inside of the function
const pickedFlatten = pickEntries(flatten, pickPathsWithUid);

flattenContent = {
...flattenContent,
...pickedFlatten,
};
});
}

// get Strapi default language and convert it to Localazy language code
const strapiLocales = await StrapiI18nService.getLocales(ctx);
const defaultLocale = strapiLocales.find((locale) => locale.isDefault);

const locale = defaultLocale
? isoStrapiToLocalazy(defaultLocale.code)
: config.LOCALAZY_DEFAULT_LOCALE;

const chunks = LocalazyUploadService.splitToChunks(flattenContent);
const importFile = LocalazyUploadService.createImportFileRepresentation(
config.LOCALAZY_DEFAULT_FILE_NAME,
config.LOCALAZY_DEFAULT_FILE_PATH,
config.LOCALAZY_DEFAULT_FILE_EXTENSION,
locale,
chunks
);
// Use `deprecate: "file"` if there is one chunk of transferred data only!
const hasMoreTransferFilesChunks = importFile.length > 1;
const uploadConfig = !hasMoreTransferFilesChunks ? { deprecate: "file" } : {};
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${UPLOAD_EVENT}:${streamIdentifier}`, {
message: "Uploading collections to Localazy...",
});
await LocalazyUploadService.upload(
importFile,
uploadConfig
);
strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${UPLOAD_FINISHED_EVENT}:${streamIdentifier}`, {
success,
message: "Upload finished",
});
},
});

0 comments on commit 27defe9

Please sign in to comment.