UNPKG

@uploadx/core

Version:
63 lines (62 loc) 2.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LocalMetaStorage = void 0; const path_1 = require("path"); const utils_1 = require("../utils"); const meta_storage_1 = require("./meta-storage"); const os_1 = require("os"); /** * Stores upload metafiles on local disk */ class LocalMetaStorage extends meta_storage_1.MetaStorage { constructor(config) { super(config); /** * Returns metafile path * @param id - upload id */ this.getMetaPath = (id) => `${this.directory}/${this.prefix + id + this.suffix}`; /** * Returns upload id from metafile path * @internal */ this.getIdFromPath = (metaFilePath) => metaFilePath.slice(`${this.directory}/${this.prefix}`.length, -this.suffix.length); this.directory = (config?.directory || (0, path_1.join)((0, os_1.tmpdir)(), 'uploadx_meta')).replace(/\\/g, '/'); this.accessCheck().catch(err => { this.logger.error('Metadata storage access check failed: %O', err); }); } async save(id, file) { await utils_1.fsp.writeFile(this.getMetaPath(id), JSON.stringify(file, null, 2)); return file; } async touch(id, file) { const time = new Date(); await utils_1.fsp.utimes(this.getMetaPath(id), time, time); return file; } async get(id) { const json = await utils_1.fsp.readFile(this.getMetaPath(id), { encoding: 'utf8' }); return JSON.parse(json); } async delete(id) { await (0, utils_1.removeFile)(this.getMetaPath(id)); return; } async list(prefix = '') { const uploads = []; const files = await (0, utils_1.getFiles)(`${this.directory}/${this.prefix + prefix}`); for (const name of files) { if (name.endsWith(this.suffix)) { const { birthtime, ctime, mtime } = await utils_1.fsp.stat(name); const id = this.getIdFromPath(name); uploads.push({ id, createdAt: birthtime || ctime, modifiedAt: mtime }); } } return { items: uploads }; } accessCheck() { return (0, utils_1.accessCheck)(this.directory); } } exports.LocalMetaStorage = LocalMetaStorage;