@@ -23,6 +23,7 @@ let uploadTmpDir = process.env.UPLOAD_TMP_DIR || uploadDir;
2323let token = process . env . TOKEN || false ;
2424let pathMatchRegExp = ( process . env . PATH_REGEXP ) ? new RegExp ( process . env . PATH_REGEXP ) : / ^ [ a - z A - Z 0 - 9 - _ / ] * $ / ;
2525let maxFileSize = ( parseInt ( process . env . MAX_FILE_SIZE , 10 ) || 200 ) * 1024 * 1024 ;
26+ let enableFolderCreation = ! ! process . env . ENABLE_FOLDER_CREATION ;
2627
2728console . log ( 'HTTP Server Upload' ) ;
2829
@@ -61,6 +62,8 @@ Argument | Environmen variable
6162 to outside the upload directory. [/^[a-zA-Z0-9-_/]*$/]
6263--disable-auto-port | DISABLE_AUTO_PORT
6364 Disable automatic port increase if the port is nor available. [Not set]
65+ --enable-folder-creation | ENABLE_FOLDER_CREATION
66+ Enable automatic folder creation when uploading file to inexisting folder. [Not set]
6467--help or -h
6568 Show this help text.
6669
@@ -70,7 +73,7 @@ PORT=9000 UPLOAD_DIR=~/uploads/ UPLOAD_TMP_DIR=/tmp/ TOKEN=my-super-secret-token
7073
7174http-server-upload --port=9000 --upload-dir="c:\\users\\peter\\Path With Whitespaces\\"
7275
73- PORT=9000 http-server-upload --disable-auto-port ./
76+ PORT=9000 http-server-upload --disable-auto-port --enable-folder-creation ./
7477
7578Additional information:
7679 https://github.com/crycode-de/http-server-upload
@@ -90,6 +93,10 @@ while (myArgs.length > 0) {
9093 disableAutoPort = true ;
9194 continue ;
9295 }
96+ if ( key === '--enable-folder-creation' ) {
97+ enableFolderCreation = true ;
98+ continue ;
99+ }
93100
94101 // options with values - get value from next arg if not provided by --arg=val
95102 if ( typeof val === 'undefined' ) {
@@ -186,11 +193,32 @@ server.on('request', (req, res) => {
186193 fields . path = '' ;
187194 }
188195
189- fs . stat ( path . join ( uploadDir , fields . path ) , ( err ) => {
196+ let targetPath = path . join ( uploadDir , fields . path ) ;
197+ fs . stat ( targetPath , ( err ) => {
190198 if ( err ) {
191- res . write ( 'Path does not exist!' ) ;
192- files . uploads . forEach ( ( file ) => file && fs . unlink ( file . filepath ) ) ;
193- return res . end ( ) ;
199+ if ( enableFolderCreation ) {
200+ console . log ( `Target path folder ${ targetPath } does not exist, creating it...` ) ;
201+ fs . mkdir ( targetPath , ( err ) => {
202+ if ( err ) {
203+ console . log ( `Unable to create target path folder ${ targetPath } !` ) ;
204+ res . write ( 'Unable to create target path folder!' ) ;
205+ files . uploads . forEach ( ( file ) => file && fs . unlink ( file . filepath , ( err ) => {
206+ if ( err ) {
207+ console . log ( 'Error removing temporary file!' ) ;
208+ }
209+ } ) ) ;
210+ return res . end ( ) ;
211+ }
212+ } ) ;
213+ } else {
214+ res . write ( 'Path does not exist!' ) ;
215+ files . uploads . forEach ( ( file ) => file && fs . unlink ( file . filepath , ( err ) => {
216+ if ( err ) {
217+ console . log ( 'Error removing temporary file!' ) ;
218+ }
219+ } ) ) ;
220+ return res . end ( ) ;
221+ }
194222 }
195223
196224 let count = 0 ;
0 commit comments