From 9c2dee315a6fda31e9247ed6c8ebdf6b82ea73e2 Mon Sep 17 00:00:00 2001 From: david-vaclavek Date: Thu, 5 Oct 2023 14:36:40 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20implement=20non-blocking=20?= =?UTF-8?q?download=20into=20UI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- admin/src/modules/alerts/constants/events.js | 2 + .../services/download-alerts-service.js | 12 + admin/src/pages/Download/index.js | 40 +- server/constants/events.js | 2 + .../localazy-transfer-controller.js | 608 +++++++++--------- .../localazy-transfer-upload-service.js | 3 - 6 files changed, 362 insertions(+), 305 deletions(-) create mode 100644 admin/src/modules/localazy-download/services/download-alerts-service.js diff --git a/admin/src/modules/alerts/constants/events.js b/admin/src/modules/alerts/constants/events.js index d4f70b7..29e8fbb 100644 --- a/admin/src/modules/alerts/constants/events.js +++ b/admin/src/modules/alerts/constants/events.js @@ -1,2 +1,4 @@ export const UPLOAD_EVENT = "upload"; export const UPLOAD_FINISHED_EVENT = "upload:finished"; +export const DOWNLOAD_EVENT = "download"; +export const DOWNLOAD_FINISHED_EVENT = "download:finished"; diff --git a/admin/src/modules/localazy-download/services/download-alerts-service.js b/admin/src/modules/localazy-download/services/download-alerts-service.js new file mode 100644 index 0000000..15849fb --- /dev/null +++ b/admin/src/modules/localazy-download/services/download-alerts-service.js @@ -0,0 +1,12 @@ +import AlertsService from "../../alerts/services/alerts-service"; +import { DOWNLOAD_EVENT, DOWNLOAD_FINISHED_EVENT } from "../../alerts/constants/events"; + +export default class DownloadAlertsService extends AlertsService { + onDownload(callback) { + this.on(DOWNLOAD_EVENT, callback); + } + + onDownloadFinished(callback) { + this.on(DOWNLOAD_FINISHED_EVENT, callback); + } +} diff --git a/admin/src/pages/Download/index.js b/admin/src/pages/Download/index.js index 4df3e77..df34733 100644 --- a/admin/src/pages/Download/index.js +++ b/admin/src/pages/Download/index.js @@ -24,6 +24,10 @@ import PluginSettingsService from "../../modules/plugin-settings/services/plugin import { PLUGIN_ROUTES } from "../../modules/@common/utils/redirect-to-plugin-route"; import LanguagesSelector from "../../modules/@common/components/LanguagesSelector"; import ProjectService from "../../modules/@common/services/project-service"; +import DownloadAlertsService from "../../modules/localazy-download/services/download-alerts-service"; + +const downloadAlertsService = new DownloadAlertsService(); +downloadAlertsService.subscribe(); function Download(props) { const { t } = useTranslation(); @@ -36,7 +40,10 @@ function Download(props) { const [localesIncompatible, setLocalesIncompatible] = useState(false); const [showDownloadFinishedModal, setshowDownloadFinishedModal] = useState(false); - const [downloadResult, setDownloadResult] = useState({}); + const [downloadResult, setDownloadResult] = useState({ + success: false, + report: [], + }); const [isDownloading, setIsDownloading] = useState(false); /** @@ -72,8 +79,34 @@ function Download(props) { const onDownloadClick = async () => { setIsDownloading(true); + setshowDownloadFinishedModal(false); + setDownloadResult({ + success: false, + report: [], + }); const result = await LocalazyDownloadService.download(); - setDownloadResult(result); + const { streamIdentifier } = result; + downloadAlertsService.setStreamIdentifier(streamIdentifier); + downloadAlertsService.onDownload((data) => { + setDownloadResult((old) => ({ + success: data.success, + report: [ + ...old.report || [], + data.message, + ], + })); + }); + downloadAlertsService.onDownloadFinished((data) => { + setDownloadResult((old) => ({ + success: data.success, + report: [ + ...old.report || [], + data.message, + ], + })); + setIsDownloading(false); + setshowDownloadFinishedModal(true); + }); // track download ProductAnalyticsService.trackDownloadToStrapi( @@ -83,9 +116,6 @@ function Download(props) { "Target Languages Codes": "all", } ); - - setIsDownloading(false); - setshowDownloadFinishedModal(true); }; useEffect(() => { diff --git a/server/constants/events.js b/server/constants/events.js index b100b8f..5f4885f 100644 --- a/server/constants/events.js +++ b/server/constants/events.js @@ -1,4 +1,6 @@ module.exports = { UPLOAD_EVENT: "upload", UPLOAD_FINISHED_EVENT: "upload:finished", + DOWNLOAD_EVENT: "download", + DOWNLOAD_FINISHED_EVENT: "download:finished", }; diff --git a/server/controllers/localazy-transfer-controller.js b/server/controllers/localazy-transfer-controller.js index a1f5df4..76e0e13 100644 --- a/server/controllers/localazy-transfer-controller.js +++ b/server/controllers/localazy-transfer-controller.js @@ -1,28 +1,20 @@ "use strict"; -const flattenObject = require("../utils/flatten-object"); const { - getCollectionsNames, - findSetupModelByCollectionName, findSetupModelByCollectionUid, - isCollectionTransferEnabled, - getPickPathsWithComponents, } = require("../utils/transfer-setup-utils"); -const pickEntries = require("../utils/pick-entries"); const config = require("../config").default; const { isoLocalazyToStrapi, - isoStrapiToLocalazy, } = require("../utils/iso-locales-utils"); const shouldSetDownloadedProperty = require("../functions/should-set-downloaded-property"); const set = require("lodash/set"); const isEmpty = require("lodash/isEmpty"); -const omitDeep = require("../utils/omit-deep"); 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_FINISHED_EVENT } = require('../constants/events'); +const { UPLOAD_FINISHED_EVENT, DOWNLOAD_EVENT, DOWNLOAD_FINISHED_EVENT } = require('../constants/events'); const getFilteredLanguagesCodesForDownload = async (languagesCodes) => { const pluginSettingsServiceHelper = new PluginSettingsServiceHelper(strapi); @@ -60,7 +52,7 @@ module.exports = { * start executing the function after a delay * (to let the client receive and subscribe to the messages stream) */ - // TODO: let the client send a message to the server to start the upload (that it's subscribed to the stream ) + // 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 = { @@ -70,7 +62,7 @@ module.exports = { } catch (e) { strapi.log.error(e.message); strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${UPLOAD_FINISHED_EVENT}:${streamIdentifier}`, { - success, + success: false, message: e.message, }); ctx.throw(400, e); @@ -78,334 +70,356 @@ module.exports = { }, async download(ctx) { - let success = true; - const messageReport = []; - - // Strapi service - const StrapiService = strapi - .plugin("localazy") - .service("strapiService"); - - // Strapi i18n Service - const StrapiI18nService = strapi - .plugin("localazy") - .service("strapiI18nService"); - - const LocalazyUserService = strapi - .plugin("localazy") - .service("localazyUserService"); - const LocalazyPubApiService = strapi - .plugin("localazy") - .service("localazyPubApiService"); - const LocalazyDownloadService = strapi - .plugin("localazy") - .service("localazyDownloadService"); - const strapiLocalazyI18nService = strapi - .plugin("localazy") - .service("strapiLocalazyI18nService"); - - const contentTransferSetup = await strapi - .plugin("localazy") - .service("pluginSettingsService") - .getContentTransferSetup(); - - const user = await LocalazyUserService.getUser(); + const streamIdentifier = generateRandomId(); - /** - * Get Strapi File - */ - const strapiFile = await LocalazyPubApiService.getStrapiFile( - user.project.id - ); - if (!strapiFile) { - success = false; - const message = `File ${config.LOCALAZY_DEFAULT_FILE_NAME} not found`; - strapi.log.error(message); - messageReport.push(message); - ctx.body = { - success, - report: messageReport, - }; - return; - } + const func = async () => { + strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${DOWNLOAD_EVENT}:${streamIdentifier}`, { + message: 'Download started', + }); - /** - * Get Localazy Project (incl. languages) - */ - const project = await LocalazyPubApiService.getProject( - user.project.id, - false, - true - ); - if (!project) { - success = false; - const message = `Project ${user.project.id} not found`; - strapi.log.error(message); - messageReport.push(message); - ctx.body = { - success, - report: messageReport, - }; - return; - } + let success = true; + const messageReport = []; - /** - * Get Localazy project languages - */ - const projectLanguages = project.languages; - const projectSourceLanguageId = project.sourceLanguage; - const sourceLanguage = project.languages.find( - (language) => language.id === projectSourceLanguageId - ); - - if (!sourceLanguage) { - success = false; - const message = "Source language not found"; - strapi.log.error(message); - messageReport.push(message); - ctx.body = { - success, - report: messageReport, - }; - return; - } + // Strapi service + const StrapiService = strapi + .plugin("localazy") + .service("strapiService"); - const projectSourceLanguageCode = sourceLanguage.code; - const projectLanguagesWithoutSourceLanguage = projectLanguages.filter( - (language) => language.code !== projectSourceLanguageCode - ); + // Strapi i18n Service + const StrapiI18nService = strapi + .plugin("localazy") + .service("strapiI18nService"); - if (!projectLanguagesWithoutSourceLanguage.length) { - const message = "Your Localazy project is not translated to other languages."; - strapi.log.info(message); - messageReport.push(message); - ctx.body = { - success, - report: messageReport, - }; - return; - } + const LocalazyUserService = strapi + .plugin("localazy") + .service("localazyUserService"); + const LocalazyPubApiService = strapi + .plugin("localazy") + .service("localazyPubApiService"); + const LocalazyDownloadService = strapi + .plugin("localazy") + .service("localazyDownloadService"); + const strapiLocalazyI18nService = strapi + .plugin("localazy") + .service("strapiLocalazyI18nService"); - /** - * Get project languages codes - */ - let languagesCodes = projectLanguagesWithoutSourceLanguage.map( - (language) => language.code - ); - // process filtered languages only / keep all if empty! - languagesCodes = await getFilteredLanguagesCodesForDownload(languagesCodes); + const contentTransferSetup = await strapi + .plugin("localazy") + .service("pluginSettingsService") + .getContentTransferSetup(); - /** - * Iterate over languages and create the ones that are not present in Strapi - */ - const strapiUnsupportedLanguages = []; // do not iterate over these languages later - const strapiLocales = await StrapiI18nService.getLocales(ctx); - for (const isoLocalazy of languagesCodes) { - try { - const isoStrapi = isoLocalazyToStrapi(isoLocalazy); - const strapiLocale = strapiLocales.find( - (locale) => locale.code === isoStrapi - ); - // skip creating locale if it already exists - if (strapiLocale) { - strapi.log.info(`The Localazy locale ${isoLocalazy} already exists.`); - continue; - } + const user = await LocalazyUserService.getUser(); - const newLocaleCode = await StrapiI18nService.createStrapiLocale( - ctx, - isoLocalazy - ); - messageReport.push(`Created locale ${newLocaleCode}`); - } catch (e) { - strapi.log.error(e.message); - if (e.name === "ValidationError") { - // store unsupported language code - strapiUnsupportedLanguages.push(isoLocalazy); - messageReport.push(`Language ${isoLocalazy} is not supported by Strapi`); - } else { - messageReport.push(e.message); - } + /** + * Get Strapi File + */ + const strapiFile = await LocalazyPubApiService.getStrapiFile( + user.project.id + ); + if (!strapiFile) { + success = false; + const message = `File ${config.LOCALAZY_DEFAULT_FILE_NAME} not found`; + strapi.log.error(message); + strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${DOWNLOAD_FINISHED_EVENT}:${streamIdentifier}`, { + success, + message, + }); + return; } - } - /** - * Download all keys for Strapi-supported languages - */ - const supportedLanguages = languagesCodes.filter( - (languageCode) => !strapiUnsupportedLanguages.includes(languageCode) - ); - const localazyContent = {}; - for (const isoLocalazy of supportedLanguages) { - const isoStrapi = isoLocalazyToStrapi(isoLocalazy); - const result = await LocalazyDownloadService.download({ - projectId: user.project.id, - fileId: strapiFile.id, - lang: isoStrapi, - }); - if (!result.success) { - messageReport.push(result.message); + /** + * Get Localazy Project (incl. languages) + */ + const project = await LocalazyPubApiService.getProject( + user.project.id, + false, + true + ); + if (!project) { + success = false; + const message = `Project ${user.project.id} not found`; + strapi.log.error(message); + strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${DOWNLOAD_FINISHED_EVENT}:${streamIdentifier}`, { + success, + message, + }); + return; } - const langKeys = result.data; - localazyContent[isoLocalazy] = langKeys; - } - /** - * Parse Localazy content - */ - const parsedLocalazyContent = {}; - const strapiContentTypesModels = await StrapiService.getModels(); - for (const [isoLocalazy, keys] of Object.entries(localazyContent)) { - const isoStrapi = isoLocalazyToStrapi(isoLocalazy); - if (!isoStrapi) { - messageReport.push( - `Language ${isoLocalazy} is not supported by Strapi` - ); - continue; + /** + * Get Localazy project languages + */ + const projectLanguages = project.languages; + const projectSourceLanguageId = project.sourceLanguage; + const sourceLanguage = project.languages.find( + (language) => language.id === projectSourceLanguageId + ); + + if (!sourceLanguage) { + success = false; + const message = "Source language not found"; + strapi.log.error(message); + strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${DOWNLOAD_FINISHED_EVENT}:${streamIdentifier}`, { + success, + message, + }); + return; } - for (const localazyEntry of keys) { - const key = localazyEntry.key[0]; - const value = localazyEntry.value; + const projectSourceLanguageCode = sourceLanguage.code; + const projectLanguagesWithoutSourceLanguage = projectLanguages.filter( + (language) => language.code !== projectSourceLanguageCode + ); + + if (!projectLanguagesWithoutSourceLanguage.length) { + const message = "Your Localazy project is not translated to other languages."; + strapi.log.info(message); + strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${DOWNLOAD_FINISHED_EVENT}:${streamIdentifier}`, { + success, + message, + }); + return; + } - const parsedKey = StrapiI18nService.parseLocalazyKey(key); + /** + * Get project languages codes + */ + let languagesCodes = projectLanguagesWithoutSourceLanguage.map( + (language) => language.code + ); + // process filtered languages only / keep all if empty! + languagesCodes = await getFilteredLanguagesCodesForDownload(languagesCodes); - const modelContentTransferSetup = findSetupModelByCollectionUid( - contentTransferSetup, - strapiContentTypesModels, - parsedKey.uid - ); + /** + * Iterate over languages and create the ones that are not present in Strapi + */ + const strapiUnsupportedLanguages = []; // do not iterate over these languages later + const strapiLocales = await StrapiI18nService.getLocales(ctx); + for (const isoLocalazy of languagesCodes) { + try { + const isoStrapi = isoLocalazyToStrapi(isoLocalazy); + const strapiLocale = strapiLocales.find( + (locale) => locale.code === isoStrapi + ); + // skip creating locale if it already exists + if (strapiLocale) { + strapi.log.info(`The Localazy locale ${isoLocalazy} already exists.`); + continue; + } - if (typeof modelContentTransferSetup !== "undefined" && shouldSetDownloadedProperty(modelContentTransferSetup, parsedKey.rest)) { - const parsedKeyRestWithoutComponents = parsedKey.rest; - const setKey = [ - isoStrapi, - parsedKey.uid, - parsedKey.id, - ...parsedKeyRestWithoutComponents, - ]; + const newLocaleCode = await StrapiI18nService.createStrapiLocale( + ctx, + isoLocalazy + ); + messageReport.push(`Created locale ${newLocaleCode}`); + } catch (e) { + strapi.log.error(e.message); + if (e.name === "ValidationError") { + // store unsupported language code + strapiUnsupportedLanguages.push(isoLocalazy); + strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${DOWNLOAD_EVENT}:${streamIdentifier}`, { + message: `Language ${isoLocalazy} is not supported by Strapi`, + }); + } else { + strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${DOWNLOAD_EVENT}:${streamIdentifier}`, { + message: e.message, + }); + } + } + } - set(parsedLocalazyContent, setKey, value); + /** + * Download all keys for Strapi-supported languages + */ + const supportedLanguages = languagesCodes.filter( + (languageCode) => !strapiUnsupportedLanguages.includes(languageCode) + ); + const localazyContent = {}; + for (const isoLocalazy of supportedLanguages) { + const isoStrapi = isoLocalazyToStrapi(isoLocalazy); + const result = await LocalazyDownloadService.download({ + projectId: user.project.id, + fileId: strapiFile.id, + lang: isoStrapi, + }); + if (!result.success) { + messageReport.push(result.message); } + const langKeys = result.data; + localazyContent[isoLocalazy] = langKeys; } - } - /** - * Iterate over parsed Localazy content and insert/update content in Strapi - */ - for (const [isoStrapi, contentTypes] of Object.entries( - parsedLocalazyContent - )) { - for (const [uid, models] of Object.entries(contentTypes)) { - /** - * Model could not exist anymore in Strapi - */ - const strapiContentTypeModel = strapiContentTypesModels.find( - (model) => model.uid === uid - ); - if (!strapiContentTypeModel) { - strapi.log.info( - `Model ${uid} not found in Strapi, it has been deleted, skipping...` + /** + * Parse Localazy content + */ + const parsedLocalazyContent = {}; + const strapiContentTypesModels = await StrapiService.getModels(); + for (const [isoLocalazy, keys] of Object.entries(localazyContent)) { + const isoStrapi = isoLocalazyToStrapi(isoLocalazy); + if (!isoStrapi) { + messageReport.push( + `Language ${isoLocalazy} is not supported by Strapi` ); continue; } - for (const [id, translatedModel] of Object.entries(models)) { - try { - /** - * Get original source language entry - */ - const baseEntry = await strapi.entityService.findOne(uid, id, { - populate: "deep", - }); + for (const localazyEntry of keys) { + const key = localazyEntry.key[0]; + const value = localazyEntry.value; - if (isEmpty(baseEntry)) { - const message = `Source language entry ${uid}[${id}] does not exist anymore, skipping...`; - throw new Error(message); - } + const parsedKey = StrapiI18nService.parseLocalazyKey(key); - /** - * Get current language entry - */ - const baseEntryLocalizations = baseEntry.localizations; + const modelContentTransferSetup = findSetupModelByCollectionUid( + contentTransferSetup, + strapiContentTypesModels, + parsedKey.uid + ); - if (!Array.isArray(baseEntryLocalizations)) { - throw new Error( - `Content type ${uid} does not support localizations, skipping...` - ); - } + if (typeof modelContentTransferSetup !== "undefined" && shouldSetDownloadedProperty(modelContentTransferSetup, parsedKey.rest)) { + const parsedKeyRestWithoutComponents = parsedKey.rest; + const setKey = [ + isoStrapi, + parsedKey.uid, + parsedKey.id, + ...parsedKeyRestWithoutComponents, + ]; - const baseEntryCurrentLanguageLocalizationInfo = - baseEntryLocalizations.find( - (localization) => localization.locale === isoStrapi - ); - - if (!baseEntryCurrentLanguageLocalizationInfo) { - // create new entry - try { - const createdEntry = await strapiLocalazyI18nService.createEntry( - ctx, - uid, - strapiContentTypesModels, - translatedModel, - baseEntry, - isoStrapi, - ); + set(parsedLocalazyContent, setKey, value); + } + } + } - messageReport.push( - `Created new entry ${uid}[${createdEntry.id}] in language ${isoStrapi}` - ); - strapi.log.info(`Created new entry ${uid}[${createdEntry.id}] in language ${isoStrapi}`); - } catch (e) { - success = false; - strapi.log.error(e.message); - strapi.log.error(JSON.stringify(e.details?.errors || {})); - messageReport.push( - `Cannot create an entry in ${isoStrapi} for ${uid}[${baseEntry.id}]: ${e.message}` - ); + /** + * Iterate over parsed Localazy content and insert/update content in Strapi + */ + for (const [isoStrapi, contentTypes] of Object.entries( + parsedLocalazyContent + )) { + for (const [uid, models] of Object.entries(contentTypes)) { + /** + * Model could not exist anymore in Strapi + */ + const strapiContentTypeModel = strapiContentTypesModels.find( + (model) => model.uid === uid + ); + if (!strapiContentTypeModel) { + strapi.log.info( + `Model ${uid} not found in Strapi, it has been deleted, skipping...` + ); + continue; + } + + for (const [id, translatedModel] of Object.entries(models)) { + try { + /** + * Get original source language entry + */ + const baseEntry = await strapi.entityService.findOne(uid, id, { + populate: "deep", + }); + + if (isEmpty(baseEntry)) { + const message = `Source language entry ${uid}[${id}] does not exist anymore, skipping...`; + throw new Error(message); } - } else { - // update existing entry - try { - const localizedEntryId = - baseEntryCurrentLanguageLocalizationInfo.id; - - const updatedEntry = await strapiLocalazyI18nService.updateEntry( - uid, - localizedEntryId, - strapiContentTypesModels, - translatedModel, - baseEntry, - isoStrapi, - ); - messageReport.push( - `Updated ${uid}[${updatedEntry.id}] (${isoStrapi})` + /** + * Get current language entry + */ + const baseEntryLocalizations = baseEntry.localizations; + + if (!Array.isArray(baseEntryLocalizations)) { + throw new Error( + `Content type ${uid} does not support localizations, skipping...` ); - strapi.log.info(`Updated ${uid}[${updatedEntry.id}] (${isoStrapi})`); - } catch (e) { - success = false; - strapi.log.error(e.message); - strapi.log.error(JSON.stringify(e.details?.errors || {})); - messageReport.push( - `Cannot update an ${uid}[${baseEntryCurrentLanguageLocalizationInfo.id}] (${isoStrapi}): ${e.message}` + } + + const baseEntryCurrentLanguageLocalizationInfo = + baseEntryLocalizations.find( + (localization) => localization.locale === isoStrapi ); + + if (!baseEntryCurrentLanguageLocalizationInfo) { + // create new entry + try { + const createdEntry = await strapiLocalazyI18nService.createEntry( + ctx, + uid, + strapiContentTypesModels, + translatedModel, + baseEntry, + isoStrapi, + ); + + const message = `Created new entry ${uid}[${createdEntry.id}] in language ${isoStrapi}`; + strapi.log.info(`Created new entry ${uid}[${createdEntry.id}] in language ${isoStrapi}`); + strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${DOWNLOAD_EVENT}:${streamIdentifier}`, { + message, + }); + } catch (e) { + success = false; + strapi.log.error(e.message); + strapi.log.error(JSON.stringify(e.details?.errors || {})); + strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${DOWNLOAD_EVENT}:${streamIdentifier}`, { + message: `Cannot create an entry in ${isoStrapi} for ${uid}[${baseEntry.id}]: ${e.message}`, + }); + } + } else { + // update existing entry + try { + const localizedEntryId = + baseEntryCurrentLanguageLocalizationInfo.id; + + const updatedEntry = await strapiLocalazyI18nService.updateEntry( + uid, + localizedEntryId, + strapiContentTypesModels, + translatedModel, + baseEntry, + isoStrapi, + ); + + const message = `Updated ${uid}[${updatedEntry.id}] (${isoStrapi})`; + strapi.log.info(message); + strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${DOWNLOAD_EVENT}:${streamIdentifier}`, { + message, + }); + } catch (e) { + success = false; + strapi.log.error(e.message); + strapi.log.error(JSON.stringify(e.details?.errors || {})); + strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${DOWNLOAD_EVENT}:${streamIdentifier}`, { + message: `Cannot update an ${uid}[${baseEntryCurrentLanguageLocalizationInfo.id}] (${isoStrapi}): ${e.message}`, + }); + } } + } catch (e) { + success = false; + strapi.log.error(e.message); + strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${DOWNLOAD_FINISHED_EVENT}:${streamIdentifier}`, { + success, + message: `An error occured while processing download: ${e.message}`, + }); } - } catch (e) { - success = false; - strapi.log.error(e.message); - messageReport.push( - `An error occured while processing download: ${e.message}` - ); } } } + + strapi.StrapIO.emitRaw(LOCALAZY_PLUGIN_CHANNEL, `${DOWNLOAD_FINISHED_EVENT}:${streamIdentifier}`, { + success, + message: 'Download finished', + }); } + /** + * start executing the function after a delay + * (to let the client receive and subscribe to the messages stream) + */ + // TODO: let the client send a message to the server to start the download (that it's subscribed to the stream) + setTimeout(() => (func()), 1000); + ctx.body = { - success, - report: messageReport, + streamIdentifier, }; }, }; diff --git a/server/services/localazy-transfer-upload-service.js b/server/services/localazy-transfer-upload-service.js index d480635..9cd9537 100644 --- a/server/services/localazy-transfer-upload-service.js +++ b/server/services/localazy-transfer-upload-service.js @@ -53,9 +53,6 @@ module.exports = ({ strapi }) => ({ message, }); strapi.log.info(message); - ctx.body = { - success, - }; return; }