@@ -4,6 +4,12 @@ import multerS3 from 'multer-s3';
4
4
import aws from 'aws-sdk' ;
5
5
import dotenv from 'dotenv' ;
6
6
dotenv . config ( ) ;
7
+
8
+ function sanitizeFileName ( fileName ) {
9
+ // Remove spaces and invalid characters
10
+ return fileName . replace ( / [ ^ a - z A - Z 0 - 9 . _ - ] / g, '' ) ;
11
+ }
12
+
7
13
async function uploadFile ( req , res ) {
8
14
try {
9
15
//--size extended to 100 mb
@@ -50,16 +56,28 @@ async function uploadFile(req, res) {
50
56
region : process . env . DO_REGION ,
51
57
} ) ;
52
58
53
- // const s3 = new aws.S3();
54
- const upload = multer ( {
55
- fileFilter : function ( req , file , cb ) {
56
- if ( accepted_extensions . some ( ext => file . originalname . toLowerCase ( ) . endsWith ( '.' + ext ) ) ) {
57
- return cb ( null , true ) ;
59
+ const parseBaseUrl = process . env . SERVER_URL ;
60
+ const parseAppId = process . env . APP_ID ;
61
+
62
+ if ( process . env . USE_LOCAL == "TRUE" ) {
63
+ var fileStorage = multer . diskStorage ( {
64
+ destination : function ( req , file , cb ) {
65
+ cb ( null , "files/files" ) ;
66
+ } ,
67
+ metadata : function ( req , file , cb ) {
68
+ cb ( null , { fieldName : 'OPENSIGN_METADATA' } ) ;
69
+ } ,
70
+ filename : function ( req , file , cb ) {
71
+ let filename = file . originalname ;
72
+ let newFileName = filename . split ( '.' ) [ 0 ] ;
73
+ let extension = filename . split ( '.' ) [ 1 ] ;
74
+ newFileName = sanitizeFileName ( newFileName + '_' + new Date ( ) . toISOString ( ) + '.' + extension )
75
+ console . log ( newFileName ) ;
76
+ cb ( null , newFileName ) ;
58
77
}
59
- // otherwise, return error
60
- return cb ( 'Only ' + accepted_extensions . join ( ', ' ) + ' files are allowed!' ) ;
61
- } ,
62
- storage : multerS3 ( {
78
+ } ) ;
79
+ } else {
80
+ var fileStorage = multerS3 ( {
63
81
acl : 'public-read' ,
64
82
s3,
65
83
bucket : DO_SPACE ,
@@ -69,14 +87,25 @@ async function uploadFile(req, res) {
69
87
key : function ( req , file , cb ) {
70
88
//console.log(file);
71
89
let filename = file . originalname ;
72
- let filenam = filename . split ( '.' ) [ 0 ] ;
90
+ let newFileName = filename . split ( '.' ) [ 0 ] ;
73
91
let extension = filename . split ( '.' ) [ 1 ] ;
74
- filenam = filenam + '_' + new Date ( ) . toISOString ( ) + '.' + extension ;
75
- console . log ( filenam ) ;
76
- cb ( null , filenam ) ;
77
- } ,
78
- } ) ,
92
+ newFileName = sanitizeFileName ( newFileName + '_' + new Date ( ) . toISOString ( ) + '.' + extension )
93
+ console . log ( newFileName ) ;
94
+ cb ( null , newFileName ) ;
95
+ }
96
+ } ) ;
97
+ }
79
98
99
+ // const s3 = new aws.S3();
100
+ const upload = multer ( {
101
+ fileFilter : function ( req , file , cb ) {
102
+ if ( accepted_extensions . some ( ext => file . originalname . toLowerCase ( ) . endsWith ( '.' + ext ) ) ) {
103
+ return cb ( null , true ) ;
104
+ }
105
+ // otherwise, return error
106
+ return cb ( 'Only ' + accepted_extensions . join ( ', ' ) + ' files are allowed!' ) ;
107
+ } ,
108
+ storage : fileStorage ,
80
109
limits : { fileSize : size } ,
81
110
} ) . single ( 'file' ) ;
82
111
@@ -93,7 +122,14 @@ async function uploadFile(req, res) {
93
122
const status = 'Success' ;
94
123
//res.header("Access-Control-Allow-Headers", "Content-Type");
95
124
//res.setHeader("Access-Control-Allow-Origin", "*");
96
- return res . json ( { status, imageUrl : req . file . location } ) ;
125
+ if ( process . env . USE_LOCAL == "TRUE" ) {
126
+ console . log ( req . file ) ;
127
+ var fileUrl = `${ parseBaseUrl } /files/${ parseAppId } /${ req . file . filename } ` ;
128
+ } else {
129
+ var fileUrl = req . file . location ;
130
+ }
131
+
132
+ return res . json ( { status, imageUrl : fileUrl } ) ;
97
133
} ) ;
98
134
} catch ( err ) {
99
135
console . log ( 'Exeption in query ' + err . stack ) ;
0 commit comments