@@ -33,5 +33,49 @@ module.exports = {
33
33
delete ( ) {
34
34
return Promise . resolve ( { } )
35
35
}
36
+ } ,
37
+ files : {
38
+ statByPath ( pathToCheck ) {
39
+ // check this path in ./data
40
+ return new Promise ( ( resolve , reject ) => {
41
+ log ( `Checking if ${ pathToCheck } exists` )
42
+ const realpath = path . join ( './data' , pathToCheck )
43
+ log ( `Real path : ${ realpath } ` )
44
+ if ( fs . existsSync ( realpath ) ) {
45
+ resolve ( { _id : pathToCheck } )
46
+ } else {
47
+ reject ( new Error ( `${ pathToCheck } does not exist` ) )
48
+ }
49
+ } )
50
+ } ,
51
+ create ( file , options ) {
52
+ return new Promise ( ( resolve , reject ) => {
53
+ log ( `Creating new file ${ options . name } ` )
54
+ const finalPath = path . join ( './data' , options . dirID , options . name )
55
+ log ( `Real path : ${ finalPath } ` )
56
+ let writeStream = fs . createWriteStream ( finalPath )
57
+ file . pipe ( writeStream )
58
+
59
+ file . on ( 'end' , ( ) => {
60
+ log ( `File ${ finalPath } created` )
61
+ resolve ( )
62
+ } )
63
+
64
+ writeStream . on ( 'error' , err => {
65
+ log ( `Error : ${ err } ` )
66
+ reject ( new Error ( err ) )
67
+ } )
68
+ } )
69
+ } ,
70
+ createDirectory ( options ) {
71
+ return new Promise ( ( resolve , reject ) => {
72
+ log ( `Creating new directory ${ options . name } ` )
73
+ const finalPath = path . join ( './data' , options . dirID , options . name )
74
+ log ( `Real path : ${ finalPath } ` )
75
+ let result = fs . mkdir ( finalPath )
76
+ if ( result ) resolve ( )
77
+ else reject ( new Error ( `Could not create ${ finalPath } ` ) )
78
+ } )
79
+ }
36
80
}
37
81
}
0 commit comments