1
- const fs = require ( 'fs' ) ;
2
- const logger = require ( '.. /log') ;
1
+ import { readFileSync , unlinkSync } from 'fs' ;
2
+ import { debug , error } from '. /log';
3
3
4
4
const files = { } ;
5
5
@@ -12,15 +12,15 @@ const exists = filename => !!files[filename];
12
12
const size = filename => files [ filename ] . size ;
13
13
const read = filename => (
14
14
files [ filename ] . buffer ||
15
- fs . readFileSync ( files [ filename ] . filepath )
15
+ readFileSync ( files [ filename ] . filepath )
16
16
) ;
17
17
const remove = ( filename ) => {
18
18
const file = files [ filename ] ;
19
19
if ( file . filepath ) {
20
- fs . unlinkSync ( file . filepath ) ;
20
+ unlinkSync ( file . filepath ) ;
21
21
}
22
22
delete files [ filename ] ;
23
- logger . debug ( 'File removed' , filename ) ;
23
+ debug ( 'File removed' , filename ) ;
24
24
} ;
25
25
const write = ( filename , buffer , filesize , filepath ) => {
26
26
files [ filename ] = {
@@ -29,59 +29,59 @@ const write = (filename, buffer, filesize, filepath) => {
29
29
filepath,
30
30
size : ( buffer ? buffer . length : filesize ) ,
31
31
} ;
32
- logger . debug ( 'File saved' , files [ filename ] ) ;
32
+ debug ( 'File saved' , files [ filename ] ) ;
33
33
} ;
34
34
35
35
const getFileSize = ( filename ) => {
36
36
if ( exists ( filename ) ) {
37
37
const fileSize = size ( filename ) ;
38
- logger . debug ( 'Request size of' , filename , 'is' , fileSize ) ;
38
+ debug ( 'Request size of' , filename , 'is' , fileSize ) ;
39
39
return result ( 200 , { fileSize } ) ;
40
40
}
41
- logger . debug ( 'Request size of' , filename , 'not found' ) ;
41
+ debug ( 'Request size of' , filename , 'not found' ) ;
42
42
return result ( 404 ) ;
43
43
} ;
44
44
45
45
const readFile = ( filename ) => {
46
46
if ( exists ( filename ) ) {
47
- logger . debug ( 'Streaming' , filename ) ;
47
+ debug ( 'Streaming' , filename ) ;
48
48
return result ( 200 , read ( filename ) ) ;
49
49
}
50
- logger . debug ( 'Streaming' , filename , 'not found' ) ;
50
+ debug ( 'Streaming' , filename , 'not found' ) ;
51
51
return result ( 404 ) ;
52
52
} ;
53
53
54
54
const writeFile = ( file ) => {
55
- logger . debug ( 'Storing' , file . originalname ) ;
55
+ debug ( 'Storing' , file . originalname ) ;
56
56
write ( file . originalname , file . buffer , file . size , file . path ) ;
57
57
return result ( 200 ) ;
58
58
} ;
59
59
const writeFileChunk = ( filename , buffer , chunkNumber ) => {
60
- logger . debug ( 'Storing' , filename , 'chunk' , chunkNumber ) ;
60
+ debug ( 'Storing' , filename , 'chunk' , chunkNumber ) ;
61
61
write ( `${ filename } .${ chunkNumber } .chunk` , buffer ) ;
62
62
return result ( 200 ) ;
63
63
} ;
64
64
const assembleFileChunks = ( filename , requestTotalSize ) => {
65
- logger . debug ( 'Assembling' , filename , 'total size' , requestTotalSize ) ;
65
+ debug ( 'Assembling' , filename , 'total size' , requestTotalSize ) ;
66
66
let chunkNumber = 1 ;
67
67
let totalSize = 0 ;
68
68
while ( true ) {
69
69
const chunkName = `${ filename } .${ chunkNumber } .chunk` ;
70
70
if ( exists ( chunkName ) ) {
71
71
const fileSize = size ( chunkName ) ;
72
- logger . debug ( 'Testing' , chunkName , 'with size' , fileSize ) ;
72
+ debug ( 'Testing' , chunkName , 'with size' , fileSize ) ;
73
73
chunkNumber += 1 ;
74
74
totalSize += fileSize ;
75
75
} else {
76
- logger . error ( 'Testing' , chunkName , 'not found' ) ;
76
+ error ( 'Testing' , chunkName , 'not found' ) ;
77
77
break ;
78
78
}
79
79
}
80
80
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 ) ;
82
82
return result ( 412 ) ;
83
83
}
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 ) ;
85
85
let buffer = null ;
86
86
chunkNumber = 1 ;
87
87
while ( true ) {
@@ -99,15 +99,15 @@ const assembleFileChunks = (filename, requestTotalSize) => {
99
99
100
100
const removeFile = ( filename ) => {
101
101
if ( exists ( filename ) ) {
102
- logger . debug ( 'Removing file' , filename ) ;
102
+ debug ( 'Removing file' , filename ) ;
103
103
remove ( filename ) ;
104
104
return result ( 200 ) ;
105
105
}
106
- logger . error ( 'Removing' , filename , 'not found' ) ;
106
+ error ( 'Removing' , filename , 'not found' ) ;
107
107
return result ( 404 ) ;
108
108
} ;
109
109
110
- module . exports = {
110
+ export default {
111
111
assembleFileChunks,
112
112
getFileSize,
113
113
readFile,
0 commit comments