@duvdu-v1/duvdu 1.1.214 → 1.1.216
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,8 +1,16 @@
|
|
|
1
|
+
/// <reference types="qs" />
|
|
2
|
+
/// <reference types="express" />
|
|
1
3
|
import multer from 'multer';
|
|
2
4
|
interface UploadOptions {
|
|
3
5
|
fileTypes?: string[];
|
|
4
6
|
maxSize?: number;
|
|
5
7
|
fileFilter?(req: Request, file: Express.Multer.File, callback: multer.FileFilterCallback): void;
|
|
6
8
|
}
|
|
7
|
-
export declare const globalUploadMiddleware: (folder: string, options?: UploadOptions) =>
|
|
9
|
+
export declare const globalUploadMiddleware: (folder: string, options?: UploadOptions) => {
|
|
10
|
+
single: (fieldName: string) => import("express").RequestHandler<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>;
|
|
11
|
+
array: (fieldName: string, maxCount?: number | undefined) => import("express").RequestHandler<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>;
|
|
12
|
+
fields: (fields: readonly multer.Field[]) => import("express").RequestHandler<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>;
|
|
13
|
+
none: () => import("express").RequestHandler<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>;
|
|
14
|
+
any: () => import("express").RequestHandler<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>;
|
|
15
|
+
};
|
|
8
16
|
export {};
|
|
@@ -1,59 +1,63 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
// /* eslint-disable indent */
|
|
3
|
-
// import path from 'path';
|
|
4
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
5
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
6
4
|
};
|
|
7
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
6
|
exports.globalUploadMiddleware = void 0;
|
|
9
|
-
// import multer from 'multer';
|
|
10
|
-
// import { v4 } from 'uuid';
|
|
11
|
-
// import { BadRequestError } from '../errors/bad-request-error';
|
|
12
|
-
// interface uploadOptions {
|
|
13
|
-
// fileTypes?: string[];
|
|
14
|
-
// maxSize?: number;
|
|
15
|
-
// fileFilter?(req: Request, file: Express.Multer.File, callback: multer.FileFilterCallback): void;
|
|
16
|
-
// }
|
|
17
|
-
// export const globalUploadMiddleware = (folder: string, options?: uploadOptions) =>
|
|
18
|
-
// multer({
|
|
19
|
-
// storage: multer.diskStorage({
|
|
20
|
-
// destination: path.resolve(`media/${folder}`),
|
|
21
|
-
// filename(req, file, callback) {
|
|
22
|
-
// callback(null, `${v4()}.${file.originalname.split('.').at(-1)}`);
|
|
23
|
-
// },
|
|
24
|
-
// }),
|
|
25
|
-
// limits: { fileSize: options?.maxSize || 3 * 1024 * 1024 }, // 3MB
|
|
26
|
-
// fileFilter: options?.fileFilter
|
|
27
|
-
// ? (options.fileFilter as any)
|
|
28
|
-
// : function fileFilter(req, file, callback) {
|
|
29
|
-
// if (!options?.fileTypes) {
|
|
30
|
-
// if (!file.mimetype.startsWith('image'))
|
|
31
|
-
// return callback(new BadRequestError('invalid file format'));
|
|
32
|
-
// return callback(null, true);
|
|
33
|
-
// }
|
|
34
|
-
// if (options?.fileTypes?.some((type) => file.mimetype.startsWith(type)))
|
|
35
|
-
// return callback(null, true);
|
|
36
|
-
// else return callback(new BadRequestError('invalid file format'));
|
|
37
|
-
// },
|
|
38
|
-
// });
|
|
39
7
|
const multer_1 = __importDefault(require("multer"));
|
|
8
|
+
const uuid_1 = require("uuid");
|
|
40
9
|
const bad_request_error_1 = require("../errors/bad-request-error");
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
10
|
+
const generateUniqueFileName = (originalname) => {
|
|
11
|
+
const timestamp = Date.now();
|
|
12
|
+
const uuid = (0, uuid_1.v4)().slice(0, 8);
|
|
13
|
+
const extension = originalname.split('.').pop();
|
|
14
|
+
return `${timestamp}-${uuid}.${extension}`;
|
|
15
|
+
};
|
|
16
|
+
const globalUploadMiddleware = (folder, options) => {
|
|
17
|
+
const storage = multer_1.default.memoryStorage();
|
|
18
|
+
// Extend memory storage to include filename generation
|
|
19
|
+
const customStorage = Object.create(storage);
|
|
20
|
+
customStorage._handleFile = function (req, file, cb) {
|
|
21
|
+
// Generate unique filename before storing
|
|
22
|
+
file.filename = generateUniqueFileName(file.originalname);
|
|
23
|
+
// Call original memory storage handler
|
|
24
|
+
storage._handleFile(req, file, cb);
|
|
25
|
+
};
|
|
26
|
+
const upload = (0, multer_1.default)({
|
|
27
|
+
storage: customStorage,
|
|
28
|
+
limits: {
|
|
29
|
+
fileSize: (options === null || options === void 0 ? void 0 : options.maxSize) || 3 * 1024 * 1024 // 3MB default
|
|
57
30
|
},
|
|
58
|
-
|
|
31
|
+
fileFilter: (options === null || options === void 0 ? void 0 : options.fileFilter)
|
|
32
|
+
? options.fileFilter
|
|
33
|
+
: function fileFilter(req, file, callback) {
|
|
34
|
+
var _a;
|
|
35
|
+
if (!(options === null || options === void 0 ? void 0 : options.fileTypes)) {
|
|
36
|
+
if (!file.mimetype.startsWith('image')) {
|
|
37
|
+
return callback(new bad_request_error_1.BadRequestError({
|
|
38
|
+
en: 'Invalid file format',
|
|
39
|
+
ar: 'صيغة الملف غير صالحة'
|
|
40
|
+
}, 'en'));
|
|
41
|
+
}
|
|
42
|
+
return callback(null, true);
|
|
43
|
+
}
|
|
44
|
+
if ((_a = options === null || options === void 0 ? void 0 : options.fileTypes) === null || _a === void 0 ? void 0 : _a.some((type) => file.mimetype.startsWith(type))) {
|
|
45
|
+
return callback(null, true);
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
return callback(new bad_request_error_1.BadRequestError({
|
|
49
|
+
en: 'Invalid file format',
|
|
50
|
+
ar: 'صيغة الملف غير صالحة'
|
|
51
|
+
}, 'en'));
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
return {
|
|
56
|
+
single: upload.single.bind(upload),
|
|
57
|
+
array: upload.array.bind(upload),
|
|
58
|
+
fields: upload.fields.bind(upload),
|
|
59
|
+
none: upload.none.bind(upload),
|
|
60
|
+
any: upload.any.bind(upload)
|
|
61
|
+
};
|
|
62
|
+
};
|
|
59
63
|
exports.globalUploadMiddleware = globalUploadMiddleware;
|