forked from map-of-pi/map-of-pi-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulter.ts
52 lines (45 loc) · 1.25 KB
/
multer.ts
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
import multer from "multer";
import multerS3 from "multer-s3";
import { S3 } from "@aws-sdk/client-s3";
import path from "path";
import { env } from "./env";
// Set S3 endpoint to DigitalOcean Spaces
const s3 = new S3({
forcePathStyle: false, // Configures to use subdomain/virtual calling format.
endpoint: env.DIGITAL_OCEAN_BUCKET_ORIGIN_ENDPOINT,
region: "us-east-1",
credentials: {
accessKeyId: env.DIGITAL_OCEAN_BUCKET_ACCESS_KEY,
secretAccessKey: env.DIGITAL_OCEAN_BUCKET_SECRET_KEY
}
});
const storage = multerS3({
s3,
bucket: env.DIGITAL_OCEAN_BUCKET_NAME,
acl: 'public-read',
contentType: multerS3.AUTO_CONTENT_TYPE,
key: function (request: any, file: any, callback: any) {
callback(null, file.originalname);
}
});
const fileFilter = (
req: Express.Request,
file: Express.Multer.File,
cb: multer.FileFilterCallback
): void => {
const extension = path.extname(file.originalname).toLowerCase();
if (!(extension === ".jpg" || extension === ".jpeg" || extension === ".png")) {
const error: any = {
code: "INVALID_FILE_TYPE",
message: "Wrong format for file",
};
cb(new Error(error.message));
return;
}
cb(null, true);
};
const upload = multer({
storage,
fileFilter
});
export default upload;