UNPKG

@filesrocket/cloudinary

Version:

Filesrocket service that manage the files of the Cloudinary

55 lines 1.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CustomFilename = exports.convertToExpression = void 0; const constants_1 = require("./constants"); const path_1 = require("path"); function convertToExpression(payload, join) { const keys = Object.keys(payload); if (!keys.length) return ''; const items = []; keys.forEach((key) => { const value = payload[key]; if (!value) return; if (key.match(/^_/)) { items.push(value); return; } items.push(`${key}=${value}`); }); return items.join(join); } exports.convertToExpression = convertToExpression; function formatFilename(filename) { const items = constants_1.exts.map(item => ({ [item]: item })); const dictionary = Object.assign({}, ...items); const { name, ext } = (0, path_1.parse)(filename); if (dictionary[ext]) filename = name; return filename; } /** * Generate a random filename with or without an * extension depending on the file type * * **Example** * - history.pptx -> history-186dgs.pptx * - image.jpg -> image-326gds * - video.mp4 -> video-434gas * - audio.jpg -> audio-149gds * * For more information visit: https://cloudinary.com/documentation/image_upload_api_reference#upload_optional_parameters */ function CustomFilename() { return function (target, key, descriptor) { const original = descriptor.value; descriptor.value = function (...args) { const data = args[0]; data.name = formatFilename(data.name); return original.apply(this, args); }; }; } exports.CustomFilename = CustomFilename; //# sourceMappingURL=utils.js.map