@@ -18,6 +18,7 @@ export async function handleUploadFile(req: express.Request, res: express.Respon
18
18
const parent = req . query ?. path as string || "/"
19
19
const bucket_name = req . params . bucket as string
20
20
const token = req . query ?. token as string
21
+ const auto_name = Number ( req . query ?. auto ?? 0 )
21
22
22
23
// check given params
23
24
if ( ! req . file ) {
@@ -30,10 +31,12 @@ export async function handleUploadFile(req: express.Request, res: express.Respon
30
31
return res . status ( 400 ) . send ( 'bucket not found' )
31
32
}
32
33
34
+ const filename = auto_name ? req . file . filename : req . file . originalname
35
+
33
36
// check file permissions
34
- const filename = path . join ( parent , req . file . originalname )
37
+ const filename_full = path . join ( parent , filename )
35
38
if ( bucket . mode !== BucketMode . PUBLIC_READ_WRITE ) {
36
- const [ code , message ] = checkFileOperationToken ( bucket , token , FS_OPERATION . WRITE , filename )
39
+ const [ code , message ] = checkFileOperationToken ( bucket , token , FS_OPERATION . WRITE , filename_full )
37
40
if ( code ) {
38
41
return res . status ( code ) . send ( message )
39
42
}
@@ -45,19 +48,19 @@ export async function handleUploadFile(req: express.Request, res: express.Respon
45
48
}
46
49
47
50
// check if file already exist
48
- if ( await pathExists ( bucket_name , filename ) ) {
51
+ if ( await pathExists ( bucket_name , filename_full ) ) {
49
52
return res . send ( { code : 'ALREADY_EXISTED' , error : "file already exists" } )
50
53
}
51
54
52
55
// construct file metadata
53
56
const metadata : FileItemMeta = {
54
57
contentType : req . file . mimetype ,
55
58
parent,
56
- name : req . file . originalname ,
59
+ name : filename ,
57
60
}
58
61
59
62
// start save
60
63
const storage = new GridFSStorage ( bucket_name , DatabaseAgent . db )
61
- const data = await storage . save ( req . file . path , filename , metadata )
64
+ const data = await storage . save ( req . file . path , filename_full , metadata )
62
65
return res . send ( { code : 0 , data } )
63
66
}
0 commit comments