const createTempDirectory = function () { const self = this; return new Promise((resolve, reject) => { Fs.mkdtemp(`/tmp/${PackageMeta.name}-`, (err, folder) => { if (err) { return reject(err); } self.tmpFolderPath = folder; self.tmpChartPath = `${folder}/${self.fileName}`; self.tmpIndexPath = `${folder}/index.yaml`; return resolve(); }); }); }
/** * Create a TemporaryStorage entry and directory. * @param {TemporaryStorageEntry} data * @returns {Promise<TemporaryStorageEntry>} */ async create (data) { await this.createStorage(); const prefix = data.prefix || data.type.charAt(0).toLowerCase(); const tempPath = data.id ? await fs.promises.mkdir(path.join(this._temporaryStorage, `${prefix}-${data.id}`)) : await fs.promises.mkdtemp(path.join(this._temporaryStorage, `${prefix}-`)); if (!data.prefix) data.prefix = prefix; if (!data.id) data.id = tempPath.substring(tempPath.length - 6); await this._createMetadataEntry(data); data.path = path.join(this._temporaryStorage, `${prefix}-${data.id}`); return data; }
/** * Renames the uploaded file names which receive random unique names to the original uploaded file name, * this is done by creating an intermediated unique name folder for each of the upload files then * renaming them back to the original name. * This method is called when `uploadPreserveName` returns true * * @param {string} uploadFile - input file name * @return {string} output file name * @private */ async _keepOriginalUploadName(uploadFile){ const uploadFolder = await mkdtemp(path.join(this.option('uploadDirectory'), 'file-')); const finalName = path.join(uploadFolder, uploadFile.name.replace(/[^a-zA-Z0-9 _.-]/g, '_')); await rename(uploadFile.path, finalName); // temporary folders removed at the end of the request this[_temporaryFolders].push(uploadFolder); return finalName; }