1
1
'use strict'
2
+
3
+ const crypto = require ( 'crypto' )
4
+ const fs = require ( 'fs' )
2
5
const URL = require ( 'url' ) . URL
3
6
const path = require ( 'path' )
4
7
5
8
const config = require ( '../config' )
6
9
const logger = require ( '../logger' )
7
10
11
+ /**
12
+ * generate a random filename for uploaded image
13
+ */
14
+ function randomFilename ( ) {
15
+ const buf = crypto . randomBytes ( 16 )
16
+ return `upload_${ buf . toString ( 'hex' ) } `
17
+ }
18
+
19
+ /**
20
+ * pick a filename not exist in filesystem
21
+ * maximum attempt 5 times
22
+ */
23
+ function pickFilename ( defaultFilename ) {
24
+ let retryCounter = 5
25
+ let filename = defaultFilename
26
+ const extname = path . extname ( defaultFilename )
27
+ while ( retryCounter -- > 0 ) {
28
+ if ( fs . existsSync ( path . join ( config . uploadsPath , filename ) ) ) {
29
+ filename = `${ randomFilename ( ) } ${ extname } `
30
+ continue
31
+ }
32
+ return filename
33
+ }
34
+ throw new Error ( 'file exists.' )
35
+ }
36
+
8
37
exports . uploadImage = function ( imagePath , callback ) {
9
38
if ( ! imagePath || typeof imagePath !== 'string' ) {
10
39
callback ( new Error ( 'Image path is missing or wrong' ) , null )
@@ -16,11 +45,24 @@ exports.uploadImage = function (imagePath, callback) {
16
45
return
17
46
}
18
47
48
+ let filename = path . basename ( imagePath )
49
+ try {
50
+ filename = pickFilename ( path . basename ( imagePath ) )
51
+ } catch ( e ) {
52
+ return callback ( e , null )
53
+ }
54
+
55
+ try {
56
+ fs . copyFileSync ( imagePath , path . join ( config . uploadsPath , filename ) )
57
+ } catch ( e ) {
58
+ return callback ( e , null )
59
+ }
60
+
19
61
let url
20
62
try {
21
- url = ( new URL ( path . basename ( imagePath ) , config . serverURL + '/uploads/' ) ) . href
63
+ url = ( new URL ( filename , config . serverURL + '/uploads/' ) ) . href
22
64
} catch ( e ) {
23
- url = config . serverURL + '/uploads/' + path . basename ( imagePath )
65
+ url = config . serverURL + '/uploads/' + filename
24
66
}
25
67
26
68
callback ( null , url )
0 commit comments