@nasriya/hypercloud
Version:
Nasriya HyperCloud is a lightweight Node.js HTTP2 framework.
160 lines (159 loc) • 6.74 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const helpers_1 = __importDefault(require("../../utils/helpers"));
const limits_1 = __importDefault(require("./assets/limits"));
const utils_1 = __importDefault(require("./assets/utils"));
class Uploads {
#_limits;
#_config = {
maxFileSize: 100 * 1024 * 1024,
uploadDir: path_1.default.resolve('temp/uploads'),
limits: {
fileStream: 20 * 1024 * 1024,
images: 10 * 1024 * 1024, // 10 MB,
videos: 100 * 1024 * 1024, // 100 MB,
mimes: {}
}
};
constructor() {
const controller = {
fileStream: {
get: () => { return this.#_config.limits.fileStream; },
set: (limit) => {
if (typeof limit !== 'number') {
throw new TypeError('Limit must be a number.');
}
if (limit < 0) {
throw new RangeError('Limit must be a non-negative number.');
}
this.#_config.limits.fileStream = limit;
}
},
images: {
get: () => { return this.#_config.limits.images; },
set: (limit) => {
if (typeof limit !== 'number') {
throw new TypeError('Limit must be a number.');
}
if (limit < 0) {
throw new RangeError('Limit must be a non-negative number.');
}
this.#_config.limits.images = limit;
}
},
videos: {
get: () => { return this.#_config.limits.videos; },
set: (limit) => {
if (typeof limit !== 'number') {
throw new TypeError('Limit must be a number.');
}
if (limit < 0) {
throw new RangeError('Limit must be a non-negative number.');
}
this.#_config.limits.videos = limit;
}
},
mime: {
get: (mime) => {
if (typeof mime !== 'string') {
throw new TypeError('MIME type must be a string.');
}
if (helpers_1.default.isNot.validMime(mime)) {
throw new TypeError(`${mime} is not a valid mime`);
}
return this.#_config.limits.mimes[mime];
},
set: (mime, limit) => {
if (helpers_1.default.isNot.validString(mime)) {
throw new TypeError('MIME type must be a string.');
}
if (helpers_1.default.isNot.validMime(mime)) {
throw new TypeError(`${mime} is not a valid mime`);
}
if (typeof limit !== 'number') {
throw new TypeError('Limit must be a number.');
}
if (limit < 0) {
throw new RangeError('Limit must be a non-negative number.');
}
this.#_config.limits.mimes[mime] = limit;
}
}
};
this.#_limits = new limits_1.default(controller);
}
/**
* Gets the directory where uploads are stored.
* If the directory does not exist, it will be created.
* @returns {string} The directory path.
*/
get directory() {
const dir = this.#_config.uploadDir;
if (!fs_1.default.existsSync(dir)) {
fs_1.default.mkdirSync(dir, { recursive: true });
}
return dir;
}
/**
* Sets the directory where uploads are stored.
* Validates if the directory exists and creates it if necessary.
* @param {string} dir - The directory path to set.
*/
set directory(dir) {
if (!fs_1.default.existsSync(dir)) {
fs_1.default.mkdirSync(dir, { recursive: true });
}
this.#_config.uploadDir = dir;
}
/**
* Gets the maximum file size allowed for uploads.
* @returns {number} The maximum file size in bytes.
*/
get maxFileSize() { return this.#_config.maxFileSize; }
/**
* Sets the maximum file size allowed for uploads.
* Converts the provided value to a numerical limit if necessary.
* @param {number | StorageSize} value - The maximum file size to set.
*/
set maxFileSize(value) {
const limit = utils_1.default.getLimit(value);
this.#_config.maxFileSize = limit;
}
/**
* Manages and configures file uploads, including setting size limits, directories, and MIME type restrictions.
*
* The `uploads` instance provides a comprehensive interface for handling file uploads on the server. It allows you to define maximum file sizes for different types of uploads, configure the upload directory, and set specific limits based on MIME types. This utility ensures that your server handles file uploads efficiently and securely, with customizable parameters to suit your needs.
*
* **Example Usage:**
*
* ```js
* // Set the directory where uploads will be stored
* server.uploads.directory = '/path/to/uploads';
*
* // Set the maximum file size for uploads to 50 MB
* server.uploads.maxFileSize = { value: 50, unit: 'MB' };
*
* // Set the maximum file size for images to 10 MB
* server.uploads.limits.images = { value: 10, unit: 'MB' };
*
* // Retrieve the current maximum file size for images
* const maxImageSize = server.uploads.limits.images;
*
* // Set a specific limit for PDF files
* server.uploads.limits.mime.set('application/pdf', { value: 5, unit: 'MB' });
* ```
*
* **Properties:**
*
* - **directory**: Gets or sets the directory where uploads are stored. If the directory does not exist, it will be created automatically.
* - **maxFileSize**: Gets or sets the maximum file size allowed for uploads, either as a number in bytes or as a `StorageSize` object.
* - **limits**: Provides access to upload size limits for file streams, images, videos, and specific MIME types.
*/
get limits() { return this.#_limits; }
}
exports.default = Uploads;
;