-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathupload.ts
52 lines (39 loc) · 926 Bytes
/
upload.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, { StorageEngine } from 'multer';
import path from 'path';
import crypto from 'crypto';
const tmpFolder = path.resolve(__dirname, '..', '..', 'tmp');
interface IUploadConfig {
driver: 's3' | 'disk';
tmpFolder: string;
uploadsFolder: string;
multer: {
storage: StorageEngine;
};
config: {
disk: {};
aws: {
bucket: string;
};
};
}
export default {
driver: process.env.STORAGE_DRIVER,
tmpFolder,
uploadsFolder: path.resolve(tmpFolder, 'uploads'),
multer: {
storage: multer.diskStorage({
destination: tmpFolder,
filename(request, file, callback) {
const fileHash = crypto.randomBytes(10).toString('HEX');
const fileName = `${fileHash}-${file.originalname}`;
return callback(null, fileName);
},
}),
},
config: {
disk: {},
aws: {
bucket: 'gobarberapp-uploads',
},
},
} as IUploadConfig;