@@ -25,7 +25,7 @@ const fileTypes = require("file-type")
25
25
// Custom errors we throw
26
26
const { NotFoundError, BadRequestError, FileExistsError, MissingParamError } = require ( "../errors.js" )
27
27
// Used to generate platform-independent file/folder paths
28
- const { diskPath, sortFiles } = require ( "../utils.js" )
28
+ const { diskPath, sortFiles, log , json } = require ( "../utils.js" )
29
29
30
30
// Import the default Provider class we need to extend
31
31
const Provider = require ( "./provider.js" ) . default
@@ -45,10 +45,14 @@ class HardDriveDataProvider extends Provider {
45
45
// If it doesn't exist, error out
46
46
throw new MissingParamError ( "Expected base path to be part of request body" )
47
47
}
48
+
48
49
// Get the folder path in the URL
49
50
const folderPath = params [ "folderPath" ] . replace ( basePath , "" )
50
51
let { compareWith, operator, value, orderBy, direction} = queries
51
52
53
+ // Log it
54
+ log ( "hard_drive" , `Base path => ${ basePath } ; folder path => ${ folderPath } ; queries => ${ json ( queries , true ) } ` )
55
+
52
56
// Don't allow relative paths, let clients do that
53
57
if ( [ basePath , folderPath ] . join ( "/" ) . indexOf ( "/.." ) !== - 1 ) {
54
58
throw new BadRequestError ( `Folder paths must not contain relative paths` )
@@ -61,11 +65,17 @@ class HardDriveDataProvider extends Provider {
61
65
62
66
// List the files and folders at that location
63
67
let files = await fs . readdir ( diskPath ( basePath , folderPath ) )
68
+ // Log it
69
+ log ( "hard_drive" , `Read dir => ${ json ( files , true ) } at ${ diskPath ( basePath , folderPath ) } ` )
70
+
64
71
let fileObjs = [ ]
65
72
66
73
// Then loop through the list of files
67
74
for ( let i = 0 , length = files . length ; i < length ; i ++ ) {
68
75
const fileName = files [ i ]
76
+ // Log it
77
+ log ( "hard_drive" , `Fetching stats for file => ${ fileName } ` )
78
+
69
79
// Get the statistics related to that file, `fs.readdir` only gives the name
70
80
const statistics = await fs . stat ( diskPath ( basePath , folderPath , fileName ) ) // Change to lstat if you want to support sym links
71
81
@@ -83,6 +93,9 @@ class HardDriveDataProvider extends Provider {
83
93
const lastModifiedTime = statistics [ "mtime" ] // Last time the file or its metadata was changed
84
94
const contentURI = "file://" + diskPath ( basePath , folderPath , fileName ) . replace ( / \ / g, "%20" ) // Content URI, allows the file to be downloaded
85
95
96
+ // Log it
97
+ log ( "hard_drive" , `Adding file to results (Parsed name => ${ name } ; kind => ${ kind } ; path => ${ path } ; mimeType => ${ mimeType } ; size => ${ size } ; createdAtTime => ${ createdAtTime } ; lastModifiedTime => ${ lastModifiedTime } ; contentURI => ${ contentURI } )` )
98
+
86
99
// Append to a final array that will be returned
87
100
fileObjs . push ( {
88
101
name, kind, path, mimeType, size, createdAtTime, lastModifiedTime, contentURI
@@ -92,6 +105,9 @@ class HardDriveDataProvider extends Provider {
92
105
// Sort the array now
93
106
fileObjs = sortFiles ( compareWith , operator , value , orderBy , direction , fileObjs )
94
107
108
+ // Log it
109
+ log ( "hard_drive" , `Sorted files, final result => ${ json ( fileObjs , true ) } ` )
110
+
95
111
// Return all the files as a final array
96
112
return fileObjs
97
113
}
@@ -105,6 +121,9 @@ class HardDriveDataProvider extends Provider {
105
121
// Get the file name in the URL
106
122
const fileName = params [ "fileName" ]
107
123
124
+ // Log it
125
+ log ( "hard_drive" , `Base path => ${ basePath } ; folder path => ${ folderPath } ; file name => ${ fileName } ` )
126
+
108
127
// Don't allow relative paths, let clients do that
109
128
if ( [ basePath , folderPath ] . join ( "/" ) . indexOf ( "/.." ) !== - 1 ) {
110
129
throw new BadRequestError ( `Folder paths must not contain relative paths` )
@@ -115,6 +134,9 @@ class HardDriveDataProvider extends Provider {
115
134
throw new NotFoundError ( `Folder ${ diskPath ( basePath , folderPath ) } was not found` )
116
135
}
117
136
137
+ // Log it
138
+ log ( "hard_drive" , `Fetching stats for ${ diskPath ( basePath , folderPath , fileName ) } ` )
139
+
118
140
// Get the statistics related to that file, `fs.readdir` only gives the name
119
141
const statistics = await fs . stat ( diskPath ( basePath , folderPath , fileName ) ) // Change to lstat if you want to support sym links
120
142
@@ -131,6 +153,10 @@ class HardDriveDataProvider extends Provider {
131
153
const createdAtTime = statistics [ "birthTime" ] // When it was created
132
154
const lastModifiedTime = statistics [ "mtime" ] // Last time the file or its metadata was changed
133
155
const contentURI = "file://" + diskPath ( basePath , folderPath , fileName ) . replace ( / \ / g, "%20" ) // Content URI, allows the file to be downloaded
156
+
157
+ // Log it
158
+ log ( "hard_drive" , `Returning file (Parsed name => ${ name } ; kind => ${ kind } ; path => ${ path } ; mimeType => ${ mimeType } ; size => ${ size } ; createdAtTime => ${ createdAtTime } ; lastModifiedTime => ${ lastModifiedTime } ; contentURI => ${ contentURI } )` )
159
+
134
160
return { name, kind, path, mimeType, size, createdAtTime, lastModifiedTime, contentURI} // Return it as an object
135
161
}
136
162
@@ -143,6 +169,9 @@ class HardDriveDataProvider extends Provider {
143
169
// Get the file name in the URL
144
170
const fileName = params [ "fileName" ]
145
171
172
+ // Log it
173
+ log ( "hard_drive" , `Base path => ${ basePath } ; folder path => ${ folderPath } ; file name => ${ fileName } ` )
174
+
146
175
// Don't allow relative paths, let clients do that
147
176
if ( [ basePath , folderPath ] . join ( "/" ) . indexOf ( "/.." ) !== - 1 ) {
148
177
throw new BadRequestError ( `Folder paths must not contain relative paths` )
@@ -159,13 +188,19 @@ class HardDriveDataProvider extends Provider {
159
188
throw new FileExistsError ( `File ${ diskPath ( basePath , folderPath , fileName ) } already exists` )
160
189
}
161
190
191
+ // Log it
192
+ log ( "hard_drive" , `Moving file ${ fileMeta . path } to ${ diskPath ( basePath , folderPath , fileName ) } ` )
193
+
162
194
// `fileMeta` is passed to us by multer, and contains the path, size and mime type of the file
163
195
// uploaded. Move the file from that path to the specified one.
164
196
await fs . move ( fileMeta . path , diskPath ( basePath , folderPath , fileName ) )
165
197
166
198
// Check if the user passed fields to set values in
167
199
// We can only set lastModifiedTime (mtime), not createAtTime
168
200
if ( body [ "lastModifiedTime" ] ) {
201
+ // Log it
202
+ log ( "hard_drive" , `Setting lastModifiedTime to ${ body [ "lastModifiedTime" ] } ` )
203
+ // Convert it to a date object
169
204
const mtime = new Date ( body [ "lastModifiedTime" ] )
170
205
// Set the lastModifiedTime
171
206
await fs . utimes ( fileMeta . path , mtime , mtime )
@@ -188,6 +223,9 @@ class HardDriveDataProvider extends Provider {
188
223
const lastModifiedTime = statistics [ "mtime" ] // Last time the file or its metadata was changed
189
224
const contentURI = "file://" + diskPath ( basePath , folderPath , fileName ) . replace ( / \ / g, "%20" ) // Content URI, allows the file to be downloaded
190
225
226
+ // Log it
227
+ log ( "hard_drive" , `Returning file (Parsed name => ${ name } ; kind => ${ kind } ; path => ${ path } ; mimeType => ${ mimeType } ; size => ${ size } ; createdAtTime => ${ createdAtTime } ; lastModifiedTime => ${ lastModifiedTime } ; contentURI => ${ contentURI } )` )
228
+
191
229
return { name, kind, path, mimeType, size, createdAtTime, lastModifiedTime, contentURI} // Return it as an object
192
230
}
193
231
@@ -212,6 +250,8 @@ class HardDriveDataProvider extends Provider {
212
250
213
251
// If there is some file data specified, update the file with it
214
252
if ( fileMeta ) {
253
+ // Log it
254
+ log ( "hard_drive" , `Updating file contents with contents of file => ${ fileMeta . path } ` )
215
255
// `fileMeta` is passed to us by multer, and contains the path, size and mime type of the file
216
256
// uploaded. Move the file from that path to the specified one and overwrite it.
217
257
await fs . move ( fileMeta . path , diskPath ( basePath , folderPath , fileName ) , { overwrite : true } )
@@ -220,10 +260,14 @@ class HardDriveDataProvider extends Provider {
220
260
// Check if the user passed fields to set values in
221
261
// We can only set name, path, and lastModifiedTime (mtime), not createdAtTime (birthTime)
222
262
if ( body [ "name" ] ) {
263
+ // Log it
264
+ log ( "hard_drive" , `Setting name to ${ body [ "name" ] } ` )
223
265
await fs . rename ( diskPath ( basePath , folderPath , fileName ) , diskPath ( basePath , folderPath , body [ "name" ] ) )
224
266
fileName = body [ "name" ]
225
267
}
226
268
if ( body [ "path" ] ) {
269
+ // Log it
270
+ log ( "hard_drive" , `Moving file to ${ body [ "path" ] } ` )
227
271
// Don't allow relative paths, let clients do that
228
272
if ( body [ "path" ] . indexOf ( "/.." ) !== - 1 ) {
229
273
throw new BadRequestError ( `Folder paths must not contain relative paths` )
@@ -232,6 +276,8 @@ class HardDriveDataProvider extends Provider {
232
276
folderPath = body [ "path" ]
233
277
}
234
278
if ( body [ "lastModifiedTime" ] ) {
279
+ // Log it
280
+ log ( "hard_drive" , `Setting lastModifiedTime to ${ body [ "lastModifiedTime" ] } ` )
235
281
const mtime = new Date ( body [ "lastModifiedTime" ] )
236
282
// Set the lastModifiedTime
237
283
await fs . utimes ( diskPath ( basePath , folderPath , fileName ) , mtime , mtime )
@@ -254,6 +300,9 @@ class HardDriveDataProvider extends Provider {
254
300
const lastModifiedTime = statistics [ "mtime" ] // Last time the file or its metadata was changed
255
301
const contentURI = "file://" + diskPath ( basePath , folderPath , fileName ) . replace ( / \ / g, "%20" ) // Content URI, allows the file to be downloaded
256
302
303
+ // Log it
304
+ log ( "hard_drive" , `Returning file (Parsed name => ${ name } ; kind => ${ kind } ; path => ${ path } ; mimeType => ${ mimeType } ; size => ${ size } ; createdAtTime => ${ createdAtTime } ; lastModifiedTime => ${ lastModifiedTime } ; contentURI => ${ contentURI } )` )
305
+
257
306
return { name, kind, path, mimeType, size, createdAtTime, lastModifiedTime, contentURI} // Return it as an object
258
307
}
259
308
@@ -266,8 +315,14 @@ class HardDriveDataProvider extends Provider {
266
315
// Get the file name in the URL
267
316
const fileName = params [ "fileName" ]
268
317
318
+ // Log it
319
+ log ( "hard_drive" , `Base path => ${ basePath } ; folder path => ${ folderPath } ; file name => ${ fileName } ` )
320
+
269
321
if ( folderPath && fileName ) {
270
322
// If there is a file name provided, delete the file
323
+ // Log it
324
+ log ( "hard_drive" , `Deleting file => ${ diskPath ( folderPath , fileName ) } ` )
325
+
271
326
// Don't allow relative paths, let clients do that
272
327
if ( [ basePath , folderPath ] . join ( "/" ) . indexOf ( "/.." ) !== - 1 ) {
273
328
throw new BadRequestError ( `Folder paths must not contain relative paths` )
@@ -282,6 +337,9 @@ class HardDriveDataProvider extends Provider {
282
337
return await fs . unlink ( diskPath ( basePath , folderPath , fileName ) )
283
338
} else if ( folderPath && ! fileName ) {
284
339
// If there is only a folder name provided, delete the folder and its contents
340
+ // Log it
341
+ log ( "hard_drive" , `Deleting folder => ${ folderPath } ` )
342
+
285
343
// Don't allow relative paths, let clients do that
286
344
if ( [ basePath , folderPath ] . join ( "/" ) . indexOf ( "/.." ) !== - 1 ) {
287
345
throw new BadRequestError ( `Folder paths must not contain relative paths` )
0 commit comments