This repository was archived by the owner on Nov 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuploadImage.middleware.ts
More file actions
184 lines (165 loc) · 5.25 KB
/
uploadImage.middleware.ts
File metadata and controls
184 lines (165 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import multer from "multer";
import { InternalServerError } from "../consts/errors.js";
import path from "node:path";
import sharp from "sharp";
import type { Request } from "express";
import fs from "node:fs";
import type { IImageMimeType, ISharpImageOptions } from "../types.js";
type IGetFilenameCallback = (req: Request, file: Express.Multer.File) => string;
interface IImageUploadOptions {
imageFilePrefix?: string;
imageDir?: string | (() => string);
imageOptions?: ISharpImageOptions;
allowedMimeTypes?: IImageMimeType[];
donatorOnlyMimeTypes?: IImageMimeType[];
fileSizeLimitMB?: number;
getFilenameCb?: IGetFilenameCallback;
}
class ImageUploadBuilder {
private _imageDir: NonNullable<IImageUploadOptions["imageDir"]>;
private imageOptions: ISharpImageOptions;
private allowedMimeTypes: IImageMimeType[];
private donatorOnlyMimeTypes: IImageMimeType[];
private fileSizeLimitMB: number;
private getFilename: IGetFilenameCallback;
constructor(options: IImageUploadOptions = {}) {
this._imageDir =
options.imageDir ||
path.join(process.cwd(), "public", "uploads", "avatars");
this.imageOptions = options.imageOptions || {
width: 512,
fileFormat: "webp",
quality: 85
};
this.allowedMimeTypes = options.allowedMimeTypes || [
"image/jpeg",
"image/jpg",
"image/png",
"image/webp",
"image/gif"
];
this.donatorOnlyMimeTypes = options.donatorOnlyMimeTypes || ["image/gif"];
this.fileSizeLimitMB = options.fileSizeLimitMB || 5;
this.getFilename =
options.getFilenameCb ||
((req, file) => {
const timestamp = process.hrtime.bigint().toString();
const prefix = options.imageFilePrefix || "img";
return `${prefix}_${timestamp}`;
});
// Required to ensure 'this' context is correct in fileFilter
this.fileFilter = this.fileFilter.bind(this);
}
private fileFilter(
req: Request,
file: Express.Multer.File,
cb: multer.FileFilterCallback
) {
if (!this.allowedMimeTypes.includes(file.mimetype as IImageMimeType)) {
return cb(
new InternalServerError(
"Invalid file type. Only JPEG, PNG, GIF and WEBP are allowed."
)
);
}
const isDonatorOrAdmin =
req.user?.role === "donator" || req.user?.role === "admin";
if (
!isDonatorOrAdmin &&
this.donatorOnlyMimeTypes.includes(file.mimetype as IImageMimeType)
) {
return cb(
new InternalServerError(
"Only donators can upload GIF avatars. Please upgrade your account."
)
);
}
cb(null, true);
}
private imageDir(): string {
const path =
typeof this._imageDir === "function" ? this._imageDir() : this._imageDir;
fs.mkdirSync(path, { recursive: true });
return path;
}
private storage: multer.StorageEngine = {
_handleFile: (req, file, callback) => {
const filename = `${this.getFilename(req, file)}.${
this.imageOptions.fileFormat
}`;
const outPath = path.join(this.imageDir(), filename);
const transform = sharp({ pages: -1 })
.resize(this.imageOptions.width, this.imageOptions.width, {
fit: "cover"
})
.toFormat(this.imageOptions.fileFormat, {
quality: this.imageOptions.quality
})
.toFile(outPath, (err, info) => {
if (err) {
console.error("Error processing image:", err);
return callback(new InternalServerError("Error processing image."));
}
callback(null, {
path: outPath
});
});
file.stream.pipe(transform);
transform.on("error", (err) => {
console.error("Sharp transformation error:", err);
callback(new InternalServerError("Error processing image."));
});
},
_removeFile: (req, file, callback) => {
fs.promises
.unlink(file.path)
.then(() => callback(null))
.catch(callback);
}
};
build() {
return multer({
limits: { fileSize: this.fileSizeLimitMB * 1024 * 1024, files: 1 },
fileFilter: this.fileFilter,
storage: this.storage
});
}
}
const uploadAvatarMiddleware = new ImageUploadBuilder({
imageDir: path.join(process.cwd(), "public", "uploads", "avatars"),
imageFilePrefix: "avatar_",
imageOptions: {
width: 32,
fileFormat: "webp",
quality: 75
},
getFilenameCb: (req) => `avatar_${req.user?.id}`
}).build();
const uploadPostImageMiddleware = new ImageUploadBuilder({
imageDir: () => {
// Date based subdirectory (e.g., "2024/06" - "year/month")
const now = new Date();
const year = now.getFullYear().toString();
const month = String(now.getMonth() + 1).padStart(2, "0");
return path.join(process.cwd(), "public", "uploads", "posts", year, month);
},
imageFilePrefix: "post_",
imageOptions: {
width: 1080,
fileFormat: "webp",
quality: 85
},
fileSizeLimitMB: 13,
getFilenameCb: (req, file) => {
const timestamp = process.hrtime.bigint().toString();
const safeOriginalName = file.originalname
.replace(/\s+/g, "_")
.replace(/[^a-zA-Z0-9._-]/g, "");
return `post_${timestamp}_${safeOriginalName}`;
}
}).build();
export {
ImageUploadBuilder,
uploadAvatarMiddleware,
uploadPostImageMiddleware
};