1- const fs = require ( 'fs' ) ;
2- const logger = require ( '.. /log') ;
1+ import { readFileSync , unlinkSync } from 'fs' ;
2+ import { debug , error } from '. /log';
33
44const files = { } ;
55
@@ -12,15 +12,15 @@ const exists = filename => !!files[filename];
1212const size = filename => files [ filename ] . size ;
1313const read = filename => (
1414 files [ filename ] . buffer ||
15- fs . readFileSync ( files [ filename ] . filepath )
15+ readFileSync ( files [ filename ] . filepath )
1616) ;
1717const remove = ( filename ) => {
1818 const file = files [ filename ] ;
1919 if ( file . filepath ) {
20- fs . unlinkSync ( file . filepath ) ;
20+ unlinkSync ( file . filepath ) ;
2121 }
2222 delete files [ filename ] ;
23- logger . debug ( 'File removed' , filename ) ;
23+ debug ( 'File removed' , filename ) ;
2424} ;
2525const write = ( filename , buffer , filesize , filepath ) => {
2626 files [ filename ] = {
@@ -29,59 +29,59 @@ const write = (filename, buffer, filesize, filepath) => {
2929 filepath,
3030 size : ( buffer ? buffer . length : filesize ) ,
3131 } ;
32- logger . debug ( 'File saved' , files [ filename ] ) ;
32+ debug ( 'File saved' , files [ filename ] ) ;
3333} ;
3434
3535const getFileSize = ( filename ) => {
3636 if ( exists ( filename ) ) {
3737 const fileSize = size ( filename ) ;
38- logger . debug ( 'Request size of' , filename , 'is' , fileSize ) ;
38+ debug ( 'Request size of' , filename , 'is' , fileSize ) ;
3939 return result ( 200 , { fileSize } ) ;
4040 }
41- logger . debug ( 'Request size of' , filename , 'not found' ) ;
41+ debug ( 'Request size of' , filename , 'not found' ) ;
4242 return result ( 404 ) ;
4343} ;
4444
4545const readFile = ( filename ) => {
4646 if ( exists ( filename ) ) {
47- logger . debug ( 'Streaming' , filename ) ;
47+ debug ( 'Streaming' , filename ) ;
4848 return result ( 200 , read ( filename ) ) ;
4949 }
50- logger . debug ( 'Streaming' , filename , 'not found' ) ;
50+ debug ( 'Streaming' , filename , 'not found' ) ;
5151 return result ( 404 ) ;
5252} ;
5353
5454const writeFile = ( file ) => {
55- logger . debug ( 'Storing' , file . originalname ) ;
55+ debug ( 'Storing' , file . originalname ) ;
5656 write ( file . originalname , file . buffer , file . size , file . path ) ;
5757 return result ( 200 ) ;
5858} ;
5959const writeFileChunk = ( filename , buffer , chunkNumber ) => {
60- logger . debug ( 'Storing' , filename , 'chunk' , chunkNumber ) ;
60+ debug ( 'Storing' , filename , 'chunk' , chunkNumber ) ;
6161 write ( `${ filename } .${ chunkNumber } .chunk` , buffer ) ;
6262 return result ( 200 ) ;
6363} ;
6464const assembleFileChunks = ( filename , requestTotalSize ) => {
65- logger . debug ( 'Assembling' , filename , 'total size' , requestTotalSize ) ;
65+ debug ( 'Assembling' , filename , 'total size' , requestTotalSize ) ;
6666 let chunkNumber = 1 ;
6767 let totalSize = 0 ;
6868 while ( true ) {
6969 const chunkName = `${ filename } .${ chunkNumber } .chunk` ;
7070 if ( exists ( chunkName ) ) {
7171 const fileSize = size ( chunkName ) ;
72- logger . debug ( 'Testing' , chunkName , 'with size' , fileSize ) ;
72+ debug ( 'Testing' , chunkName , 'with size' , fileSize ) ;
7373 chunkNumber += 1 ;
7474 totalSize += fileSize ;
7575 } else {
76- logger . error ( 'Testing' , chunkName , 'not found' ) ;
76+ error ( 'Testing' , chunkName , 'not found' ) ;
7777 break ;
7878 }
7979 }
8080 if ( requestTotalSize !== totalSize ) {
81- logger . error ( 'Request total size' , requestTotalSize , 'not equal to calculated total size' , totalSize ) ;
81+ error ( 'Request total size' , requestTotalSize , 'not equal to calculated total size' , totalSize ) ;
8282 return result ( 412 ) ;
8383 }
84- logger . debug ( 'Request total size' , requestTotalSize , 'equal to calculated total size' , totalSize ) ;
84+ debug ( 'Request total size' , requestTotalSize , 'equal to calculated total size' , totalSize ) ;
8585 let buffer = null ;
8686 chunkNumber = 1 ;
8787 while ( true ) {
@@ -99,15 +99,15 @@ const assembleFileChunks = (filename, requestTotalSize) => {
9999
100100const removeFile = ( filename ) => {
101101 if ( exists ( filename ) ) {
102- logger . debug ( 'Removing file' , filename ) ;
102+ debug ( 'Removing file' , filename ) ;
103103 remove ( filename ) ;
104104 return result ( 200 ) ;
105105 }
106- logger . error ( 'Removing' , filename , 'not found' ) ;
106+ error ( 'Removing' , filename , 'not found' ) ;
107107 return result ( 404 ) ;
108108} ;
109109
110- module . exports = {
110+ export default {
111111 assembleFileChunks,
112112 getFileSize,
113113 readFile,
0 commit comments