UNPKG

alapa

Version:

A cutting-edge web development framework designed to revolutionize the way developers build modern web applications.

44 lines (43 loc) 1.61 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.LocalFileUploaderDisk = void 0; const path_1 = __importDefault(require("path")); const fs_1 = __importDefault(require("fs")); class LocalFileUploaderDisk { option; constructor(options) { this.option = options; } save(file, name, options) { name = name || file.name; const dir = this.option?.uploadDir ?? options?.directory ?? "static/uploads"; if (!fs_1.default.existsSync(path_1.default.resolve(dir))) { fs_1.default.mkdirSync(path_1.default.resolve(dir), { recursive: true, }); } const destPath = path_1.default.resolve(dir, name); if (options?.maxSize && file.size > options.maxSize) { throw new Error(""); } if (options?.overwrite && fs_1.default.existsSync(destPath)) { fs_1.default.unlinkSync(destPath); } if (options?.allowEmptyFiles && file.size === 0) { throw new Error(""); } // if (options?.allowExtensions && !checkExtensions(file.name,options.allowExtensions??[])) { // throw new Error(""); // } if (options?.allowedMimeTypes && !options.allowedMimeTypes.includes(file.mimetype)) { throw new Error(""); } file.mv(destPath); return destPath; } } exports.LocalFileUploaderDisk = LocalFileUploaderDisk;