UNPKG

@crowdin/app-project-module

Version:

Module that generates for you all common endpoints for serving standalone Crowdin App

141 lines (140 loc) 7.03 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const util_1 = require("../../../util"); const types_1 = require("../types"); const files_1 = require("../util/files"); function handle(baseConfig, config, folderName) { const folderPath = config.filesFolder || baseConfig.dbFolder; if (!fs_1.default.existsSync(path_1.default.join(folderPath, folderName))) { fs_1.default.mkdirSync(path_1.default.join(folderPath, folderName), { recursive: true }); } return (0, util_1.runAsyncWrapper)((req, res) => __awaiter(this, void 0, void 0, function* () { var _a; const response = {}; const baseFilesUrl = `${baseConfig.baseUrl}/file/download/${folderName}`; const body = req.body; // Skip assets (e.g., images, videos, or other media files) as they typically don't need to be processed if (((_a = body === null || body === void 0 ? void 0 : body.file) === null || _a === void 0 ? void 0 : _a.type) === 'assets' && !('processAssets' in config && config.processAssets)) { res.sendStatus(304); return; } let processingError; let fileContent; let rawContent; if (body.stringsUrl) { fileContent = yield (0, files_1.getFileStrings)(body.stringsUrl); } else if (body.strings) { fileContent = body.strings; } else if (body.file.contentUrl) { rawContent = yield (0, files_1.getFileContent)(body.file.contentUrl); fileContent = rawContent; } else if (body.file.content) { rawContent = body.file.content; fileContent = Buffer.from(rawContent, 'base64'); } if (body.jobType === types_1.ProcessFileJobType.PRE_IMPORT || body.jobType === types_1.ProcessFileJobType.POST_EXPORT) { body.getRawContent = (encoding) => __awaiter(this, void 0, void 0, function* () { if (typeof rawContent === 'string') { return Buffer.from(rawContent, 'base64').toString(encoding); } return rawContent; }); } const fileProcessResult = yield config.fileProcess(body, fileContent, req.crowdinApiClient, req.crowdinContext, req.crowdinContext.jwtPayload.context.project_id); if (fileProcessResult.notModified) { res.sendStatus(304); return; } switch (body.jobType) { case types_1.ProcessFileJobType.PRE_IMPORT: case types_1.ProcessFileJobType.POST_EXPORT: const { contentFile, fileName, fileType, error: contentFileError, } = fileProcessResult; if (contentFile) { if (Buffer.byteLength(contentFile) < files_1.MAX_BODY_SIZE) { response.content = contentFile.toString('base64'); } else { let url; if (config.storeFile) { url = yield config.storeFile(contentFile); } else { const storedFile = yield (0, files_1.storeFile)(contentFile, path_1.default.join(folderPath, folderName)); url = `${baseFilesUrl}?file=${storedFile}`; } response.contentUrl = url; } } if (fileName) { response.fileName = fileName; } if (fileType) { response.fileType = fileType; } processingError = contentFileError; break; case types_1.ProcessFileJobType.POST_IMPORT: case types_1.ProcessFileJobType.PRE_EXPORT: const { strings, error: stringsFileError, previewFile } = fileProcessResult; let maxSize = files_1.MAX_BODY_SIZE; if (strings && previewFile && body.jobType === types_1.ProcessFileJobType.POST_IMPORT) { maxSize = maxSize / 2; } if (previewFile && body.jobType === types_1.ProcessFileJobType.POST_IMPORT) { if (Buffer.byteLength(previewFile) < maxSize) { response.preview = previewFile.toString('base64'); } else { let url; if (config.storeFile) { url = yield config.storeFile(previewFile); } else { const storedFile = yield (0, files_1.storeFile)(previewFile, path_1.default.join(folderPath, folderName)); url = `${baseFilesUrl}?file=${storedFile}`; } response.previewUrl = url; } } if (strings) { const stringsNDJson = strings.map((s) => JSON.stringify(s)).join('\n\r'); const bufferData = Buffer.from(stringsNDJson, 'utf-8'); if (Buffer.byteLength(bufferData) < maxSize) { response.strings = strings; } else { let url; if (config.storeFile) { url = yield config.storeFile(bufferData); } else { const storedFile = yield (0, files_1.storeFile)(bufferData, path_1.default.join(folderPath, folderName)); url = `${baseFilesUrl}?file=${storedFile}`; } response.stringsUrl = url; } } processingError = stringsFileError; break; } res.send({ data: response, error: processingError ? { message: processingError } : undefined }); })); } exports.default = handle;