@armi-wave/common 1.23.27 → 1.23.29
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.
- package/build/index.d.ts +1 -1
- package/build/index.js +5 -2
- package/build/models/TextPost.d.ts +1 -1
- package/build/models/TextPost.js +1 -1
- package/build/utils/multerConfig.d.ts +11 -2
- package/build/utils/multerConfig.js +118 -14
- package/package.json +1 -1
package/build/index.d.ts
CHANGED
|
@@ -25,4 +25,4 @@ export * from './middlewares/error-handler';
|
|
|
25
25
|
export * from './middlewares/require-auth';
|
|
26
26
|
export * from './middlewares/validate-request';
|
|
27
27
|
export { minioClient, ensureBucketExists } from './utils/minioClient';
|
|
28
|
-
export {
|
|
28
|
+
export { imageMiddleware, videoMiddleware, textPostMiddleware, createUploadMiddleware, MulterRequest, MulterFileRequest, } from './utils/multerConfig';
|
package/build/index.js
CHANGED
|
@@ -17,7 +17,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
17
17
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
18
|
};
|
|
19
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
-
exports.
|
|
20
|
+
exports.createUploadMiddleware = exports.textPostMiddleware = exports.videoMiddleware = exports.imageMiddleware = exports.ensureBucketExists = exports.minioClient = exports.RequestValidationError = exports.NotFoundError = exports.NotAuthorizedError = exports.CustomError = exports.DatabaseConnectionError = exports.BadRequestError = exports.PostReplyDownvote = exports.PostReplyUpvote = exports.PostReplyLike = exports.PostCommentDownvote = exports.PostCommentUpvote = exports.PostCommentLike = exports.Like = exports.Follow = exports.Reply = exports.Comment = exports.VideoPost = exports.TextPost = exports.Post = exports.User = void 0;
|
|
21
21
|
const User_1 = __importDefault(require("./models/User"));
|
|
22
22
|
exports.User = User_1.default;
|
|
23
23
|
const Post_1 = __importDefault(require("./models/Post"));
|
|
@@ -66,4 +66,7 @@ var minioClient_1 = require("./utils/minioClient");
|
|
|
66
66
|
Object.defineProperty(exports, "minioClient", { enumerable: true, get: function () { return minioClient_1.minioClient; } });
|
|
67
67
|
Object.defineProperty(exports, "ensureBucketExists", { enumerable: true, get: function () { return minioClient_1.ensureBucketExists; } });
|
|
68
68
|
var multerConfig_1 = require("./utils/multerConfig");
|
|
69
|
-
Object.defineProperty(exports, "
|
|
69
|
+
Object.defineProperty(exports, "imageMiddleware", { enumerable: true, get: function () { return multerConfig_1.imageMiddleware; } });
|
|
70
|
+
Object.defineProperty(exports, "videoMiddleware", { enumerable: true, get: function () { return multerConfig_1.videoMiddleware; } });
|
|
71
|
+
Object.defineProperty(exports, "textPostMiddleware", { enumerable: true, get: function () { return multerConfig_1.textPostMiddleware; } });
|
|
72
|
+
Object.defineProperty(exports, "createUploadMiddleware", { enumerable: true, get: function () { return multerConfig_1.createUploadMiddleware; } });
|
|
@@ -3,7 +3,7 @@ import { PostType } from '../enums/PostType';
|
|
|
3
3
|
import { Mention } from './Mention';
|
|
4
4
|
interface TextPost extends Document {
|
|
5
5
|
userId: mongoose.Types.ObjectId;
|
|
6
|
-
textPostId
|
|
6
|
+
textPostId?: mongoose.Types.ObjectId;
|
|
7
7
|
username: string;
|
|
8
8
|
profilePicUrl: string;
|
|
9
9
|
caption: string;
|
package/build/models/TextPost.js
CHANGED
|
@@ -44,8 +44,8 @@ const textPostSchema = new mongoose_1.Schema({
|
|
|
44
44
|
textPostId: {
|
|
45
45
|
type: mongoose_1.Schema.Types.ObjectId,
|
|
46
46
|
ref: 'TextPost',
|
|
47
|
-
required: true,
|
|
48
47
|
index: true,
|
|
48
|
+
required: false,
|
|
49
49
|
},
|
|
50
50
|
username: { type: String, required: true },
|
|
51
51
|
profilePicUrl: { type: String, default: '' },
|
|
@@ -2,5 +2,14 @@ import { Request, Response, NextFunction } from 'express';
|
|
|
2
2
|
interface MulterRequest extends Request {
|
|
3
3
|
file?: Express.Multer.File;
|
|
4
4
|
}
|
|
5
|
-
declare const
|
|
6
|
-
|
|
5
|
+
declare const imageMiddleware: (req: Request, res: Response, next: NextFunction) => void;
|
|
6
|
+
declare const videoMiddleware: (req: Request, res: Response, next: NextFunction) => void;
|
|
7
|
+
declare const textPostMiddleware: (req: Request, res: Response, next: NextFunction) => void;
|
|
8
|
+
declare const createUploadMiddleware: (type: "image" | "video" | "text") => (req: Request, res: Response, next: NextFunction) => void;
|
|
9
|
+
interface MulterFileRequest extends Request {
|
|
10
|
+
file?: Express.Multer.File;
|
|
11
|
+
files?: {
|
|
12
|
+
[fieldname: string]: Express.Multer.File[];
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export { imageMiddleware, videoMiddleware, textPostMiddleware, createUploadMiddleware, MulterRequest, MulterFileRequest, };
|
|
@@ -3,32 +3,136 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
6
|
+
exports.createUploadMiddleware = exports.textPostMiddleware = exports.videoMiddleware = exports.imageMiddleware = void 0;
|
|
7
7
|
const multer_1 = __importDefault(require("multer"));
|
|
8
8
|
// Use memory storage to avoid saving files locally
|
|
9
9
|
const storage = multer_1.default.memoryStorage();
|
|
10
|
-
//
|
|
11
|
-
const
|
|
10
|
+
// Maximum file sizes
|
|
11
|
+
const MAX_IMAGE_SIZE = 10 * 1024 * 1024; // 10MB
|
|
12
|
+
const MAX_VIDEO_SIZE = 100 * 1024 * 1024; // 100MB
|
|
13
|
+
const MAX_DOCUMENT_SIZE = 20 * 1024 * 1024; // 20MB
|
|
14
|
+
// File filter for images only
|
|
15
|
+
const imageFileFilter = (req, file, cb) => {
|
|
12
16
|
if (file.mimetype.startsWith('image/')) {
|
|
13
|
-
cb(null, true);
|
|
17
|
+
cb(null, true);
|
|
14
18
|
}
|
|
15
19
|
else {
|
|
16
20
|
cb(new Error('Invalid file type. Only images are allowed.'));
|
|
17
21
|
}
|
|
18
22
|
};
|
|
19
|
-
//
|
|
20
|
-
const
|
|
23
|
+
// File filter for videos only
|
|
24
|
+
const videoFileFilter = (req, file, cb) => {
|
|
25
|
+
if (file.mimetype.startsWith('video/')) {
|
|
26
|
+
cb(null, true);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
cb(new Error('Invalid file type. Only videos are allowed.'));
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
// File filter for text posts (allows images, videos, and documents)
|
|
33
|
+
const textPostFileFilter = (req, file, cb) => {
|
|
34
|
+
// Accept images, videos, and common document types
|
|
35
|
+
if (file.mimetype.startsWith('image/') ||
|
|
36
|
+
file.mimetype.startsWith('video/') ||
|
|
37
|
+
file.mimetype.startsWith('application/pdf') ||
|
|
38
|
+
file.mimetype === 'application/msword' ||
|
|
39
|
+
file.mimetype ===
|
|
40
|
+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document' ||
|
|
41
|
+
file.mimetype === 'application/vnd.ms-excel' ||
|
|
42
|
+
file.mimetype ===
|
|
43
|
+
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ||
|
|
44
|
+
file.mimetype === 'text/plain') {
|
|
45
|
+
cb(null, true);
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
cb(new Error('Invalid file type. Only images, videos, and documents are allowed.'));
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
// Set up multer configs for different post types
|
|
52
|
+
const imageUpload = (0, multer_1.default)({
|
|
53
|
+
storage,
|
|
54
|
+
limits: { fileSize: MAX_IMAGE_SIZE },
|
|
55
|
+
fileFilter: imageFileFilter,
|
|
56
|
+
});
|
|
57
|
+
const videoUpload = (0, multer_1.default)({
|
|
58
|
+
storage,
|
|
59
|
+
limits: { fileSize: MAX_VIDEO_SIZE },
|
|
60
|
+
fileFilter: videoFileFilter,
|
|
61
|
+
});
|
|
62
|
+
const textPostUpload = (0, multer_1.default)({
|
|
21
63
|
storage,
|
|
22
|
-
limits: { fileSize:
|
|
23
|
-
fileFilter,
|
|
64
|
+
limits: { fileSize: MAX_DOCUMENT_SIZE },
|
|
65
|
+
fileFilter: textPostFileFilter,
|
|
24
66
|
});
|
|
25
|
-
//
|
|
26
|
-
const
|
|
27
|
-
|
|
67
|
+
// Create middleware functions for each post type
|
|
68
|
+
const imageMiddleware = (req, res, next) => {
|
|
69
|
+
imageUpload.single('image')(req, res, (err) => {
|
|
70
|
+
if (err) {
|
|
71
|
+
if (err instanceof multer_1.default.MulterError) {
|
|
72
|
+
// Handle specific Multer errors
|
|
73
|
+
if (err.code === 'LIMIT_FILE_SIZE') {
|
|
74
|
+
return res.status(413).json({
|
|
75
|
+
error: `File too large. Maximum size is ${MAX_IMAGE_SIZE / (1024 * 1024)}MB`,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return res.status(400).json({ error: err.message });
|
|
80
|
+
}
|
|
81
|
+
next();
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
exports.imageMiddleware = imageMiddleware;
|
|
85
|
+
const videoMiddleware = (req, res, next) => {
|
|
86
|
+
videoUpload.single('video')(req, res, (err) => {
|
|
28
87
|
if (err) {
|
|
29
|
-
|
|
88
|
+
if (err instanceof multer_1.default.MulterError) {
|
|
89
|
+
if (err.code === 'LIMIT_FILE_SIZE') {
|
|
90
|
+
return res.status(413).json({
|
|
91
|
+
error: `File too large. Maximum size is ${MAX_VIDEO_SIZE / (1024 * 1024)}MB`,
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return res.status(400).json({ error: err.message });
|
|
30
96
|
}
|
|
31
|
-
next();
|
|
97
|
+
next();
|
|
32
98
|
});
|
|
33
99
|
};
|
|
34
|
-
exports.
|
|
100
|
+
exports.videoMiddleware = videoMiddleware;
|
|
101
|
+
const textPostMiddleware = (req, res, next) => {
|
|
102
|
+
// Use array to handle different field names based on file type
|
|
103
|
+
const uploadFields = [
|
|
104
|
+
{ name: 'image', maxCount: 1 },
|
|
105
|
+
{ name: 'video', maxCount: 1 },
|
|
106
|
+
{ name: 'document', maxCount: 1 },
|
|
107
|
+
];
|
|
108
|
+
textPostUpload.fields(uploadFields)(req, res, (err) => {
|
|
109
|
+
if (err) {
|
|
110
|
+
if (err instanceof multer_1.default.MulterError) {
|
|
111
|
+
if (err.code === 'LIMIT_FILE_SIZE') {
|
|
112
|
+
return res.status(413).json({
|
|
113
|
+
error: `File too large. Maximum size is ${MAX_DOCUMENT_SIZE / (1024 * 1024)}MB`,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return res.status(400).json({ error: err.message });
|
|
118
|
+
}
|
|
119
|
+
next();
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
exports.textPostMiddleware = textPostMiddleware;
|
|
123
|
+
// Or create a single configurable middleware (alternative approach)
|
|
124
|
+
const createUploadMiddleware = (type) => {
|
|
125
|
+
return (req, res, next) => {
|
|
126
|
+
switch (type) {
|
|
127
|
+
case 'image':
|
|
128
|
+
return imageMiddleware(req, res, next);
|
|
129
|
+
case 'video':
|
|
130
|
+
return videoMiddleware(req, res, next);
|
|
131
|
+
case 'text':
|
|
132
|
+
return textPostMiddleware(req, res, next);
|
|
133
|
+
default:
|
|
134
|
+
return next(new Error('Invalid upload type'));
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
|
+
exports.createUploadMiddleware = createUploadMiddleware;
|