11import { readFileSync , unlinkSync } from 'fs' ;
2- import { debug , error } from './log' ;
2+ import logger from './log' ;
33
44const files = { } ;
55
@@ -20,7 +20,7 @@ const remove = (filename) => {
2020 unlinkSync ( file . filepath ) ;
2121 }
2222 delete files [ filename ] ;
23- debug ( 'File removed' , filename ) ;
23+ logger . 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- debug ( 'File saved' , files [ filename ] ) ;
32+ logger . debug ( 'File saved' , files [ filename ] ) ;
3333} ;
3434
35- const getFileSize = ( filename ) => {
35+ export const getFileSize = ( filename ) => {
3636 if ( exists ( filename ) ) {
3737 const fileSize = size ( filename ) ;
38- debug ( 'Request size of' , filename , 'is' , fileSize ) ;
38+ logger . debug ( 'Request size of' , filename , 'is' , fileSize ) ;
3939 return result ( 200 , { fileSize } ) ;
4040 }
41- debug ( 'Request size of' , filename , 'not found' ) ;
41+ logger . debug ( 'Request size of' , filename , 'not found' ) ;
4242 return result ( 404 ) ;
4343} ;
4444
45- const readFile = ( filename ) => {
45+ export const readFile = ( filename ) => {
4646 if ( exists ( filename ) ) {
47- debug ( 'Streaming' , filename ) ;
47+ logger . debug ( 'Streaming' , filename ) ;
4848 return result ( 200 , read ( filename ) ) ;
4949 }
50- debug ( 'Streaming' , filename , 'not found' ) ;
50+ logger . debug ( 'Streaming' , filename , 'not found' ) ;
5151 return result ( 404 ) ;
5252} ;
5353
54- const writeFile = ( file ) => {
55- debug ( 'Storing' , file . originalname ) ;
54+ export const writeFile = ( file ) => {
55+ logger . debug ( 'Storing' , file . originalname ) ;
5656 write ( file . originalname , file . buffer , file . size , file . path ) ;
5757 return result ( 200 ) ;
5858} ;
59- const writeFileChunk = ( filename , buffer , chunkNumber ) => {
60- debug ( 'Storing' , filename , 'chunk' , chunkNumber ) ;
59+ export const writeFileChunk = ( filename , buffer , chunkNumber ) => {
60+ logger . debug ( 'Storing' , filename , 'chunk' , chunkNumber ) ;
6161 write ( `${ filename } .${ chunkNumber } .chunk` , buffer ) ;
6262 return result ( 200 ) ;
6363} ;
64- const assembleFileChunks = ( filename , requestTotalSize ) => {
65- debug ( 'Assembling' , filename , 'total size' , requestTotalSize ) ;
64+ export const assembleFileChunks = ( filename , requestTotalSize ) => {
65+ logger . 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- debug ( 'Testing' , chunkName , 'with size' , fileSize ) ;
72+ logger . debug ( 'Testing' , chunkName , 'with size' , fileSize ) ;
7373 chunkNumber += 1 ;
7474 totalSize += fileSize ;
7575 } else {
76- error ( 'Testing' , chunkName , 'not found' ) ;
76+ logger . error ( 'Testing' , chunkName , 'not found' ) ;
7777 break ;
7878 }
7979 }
8080 if ( requestTotalSize !== totalSize ) {
81- error ( 'Request total size' , requestTotalSize , 'not equal to calculated total size' , totalSize ) ;
81+ logger . error ( 'Request total size' , requestTotalSize , 'not equal to calculated total size' , totalSize ) ;
8282 return result ( 412 ) ;
8383 }
84- debug ( 'Request total size' , requestTotalSize , 'equal to calculated total size' , totalSize ) ;
84+ logger . debug ( 'Request total size' , requestTotalSize , 'equal to calculated total size' , totalSize ) ;
8585 let buffer = null ;
8686 chunkNumber = 1 ;
8787 while ( true ) {
@@ -97,21 +97,12 @@ const assembleFileChunks = (filename, requestTotalSize) => {
9797 return result ( 200 ) ;
9898} ;
9999
100- const removeFile = ( filename ) => {
100+ export const removeFile = ( filename ) => {
101101 if ( exists ( filename ) ) {
102- debug ( 'Removing file' , filename ) ;
102+ logger . debug ( 'Removing file' , filename ) ;
103103 remove ( filename ) ;
104104 return result ( 200 ) ;
105105 }
106- error ( 'Removing' , filename , 'not found' ) ;
106+ logger . error ( 'Removing' , filename , 'not found' ) ;
107107 return result ( 404 ) ;
108108} ;
109-
110- export default {
111- assembleFileChunks,
112- getFileSize,
113- readFile,
114- removeFile,
115- writeFile,
116- writeFileChunk,
117- } ;
0 commit comments