Skip to content

Commit bbaa4ec

Browse files
committed
feat: add logging capability when DEBUG=true
- WARNING: logs may reveal sesitive information like file names, subject of emails, etc. - To enable debug logging, add DEBUG=true before the file name while running the program - misc bug fixes
1 parent 69dedbd commit bbaa4ec

File tree

8 files changed

+536
-67
lines changed

8 files changed

+536
-67
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
{
22
"name": "dabbu-server",
3-
"version": "1.5.2",
3+
"version": "1.5.3",
44
"description": "A unified API interface to access all your data online",
55
"directories": {
66
"doc": "docs"
77
},
88
"scripts": {
9-
"start": "node src/server.js start",
9+
"start": "node src/server.js",
10+
"debug": "clear && DEBUG=true node src/server.js",
1011
"clean": "rm -r dist; mkdir dist",
1112
"prebuild": "npm run clean",
1213
"build": "pkg --output dist/dabbu-server .",

src/modules/gmail.js

Lines changed: 181 additions & 31 deletions
Large diffs are not rendered by default.

src/modules/google_drive.js

Lines changed: 166 additions & 8 deletions
Large diffs are not rendered by default.

src/modules/hard_drive.js

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const fileTypes = require("file-type")
2525
// Custom errors we throw
2626
const { NotFoundError, BadRequestError, FileExistsError, MissingParamError } = require("../errors.js")
2727
// Used to generate platform-independent file/folder paths
28-
const { diskPath, sortFiles } = require("../utils.js")
28+
const { diskPath, sortFiles, log, json } = require("../utils.js")
2929

3030
// Import the default Provider class we need to extend
3131
const Provider = require("./provider.js").default
@@ -45,10 +45,14 @@ class HardDriveDataProvider extends Provider {
4545
// If it doesn't exist, error out
4646
throw new MissingParamError("Expected base path to be part of request body")
4747
}
48+
4849
// Get the folder path in the URL
4950
const folderPath = params["folderPath"].replace(basePath, "")
5051
let {compareWith, operator, value, orderBy, direction} = queries
5152

53+
// Log it
54+
log("hard_drive", `Base path => ${basePath}; folder path => ${folderPath}; queries => ${json(queries, true)}`)
55+
5256
// Don't allow relative paths, let clients do that
5357
if ([basePath, folderPath].join("/").indexOf("/..") !== -1) {
5458
throw new BadRequestError(`Folder paths must not contain relative paths`)
@@ -61,11 +65,17 @@ class HardDriveDataProvider extends Provider {
6165

6266
// List the files and folders at that location
6367
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+
6471
let fileObjs = []
6572

6673
// Then loop through the list of files
6774
for (let i = 0, length = files.length; i < length; i++) {
6875
const fileName = files[i]
76+
// Log it
77+
log("hard_drive", `Fetching stats for file => ${fileName}`)
78+
6979
// Get the statistics related to that file, `fs.readdir` only gives the name
7080
const statistics = await fs.stat(diskPath(basePath, folderPath, fileName)) // Change to lstat if you want to support sym links
7181

@@ -83,6 +93,9 @@ class HardDriveDataProvider extends Provider {
8393
const lastModifiedTime = statistics["mtime"] // Last time the file or its metadata was changed
8494
const contentURI = "file://" + diskPath(basePath, folderPath, fileName).replace(/\ /g, "%20") // Content URI, allows the file to be downloaded
8595

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+
8699
// Append to a final array that will be returned
87100
fileObjs.push({
88101
name, kind, path, mimeType, size, createdAtTime, lastModifiedTime, contentURI
@@ -92,6 +105,9 @@ class HardDriveDataProvider extends Provider {
92105
// Sort the array now
93106
fileObjs = sortFiles(compareWith, operator, value, orderBy, direction, fileObjs)
94107

108+
// Log it
109+
log("hard_drive", `Sorted files, final result => ${json(fileObjs, true)}`)
110+
95111
// Return all the files as a final array
96112
return fileObjs
97113
}
@@ -105,6 +121,9 @@ class HardDriveDataProvider extends Provider {
105121
// Get the file name in the URL
106122
const fileName = params["fileName"]
107123

124+
// Log it
125+
log("hard_drive", `Base path => ${basePath}; folder path => ${folderPath}; file name => ${fileName}`)
126+
108127
// Don't allow relative paths, let clients do that
109128
if ([basePath, folderPath].join("/").indexOf("/..") !== -1) {
110129
throw new BadRequestError(`Folder paths must not contain relative paths`)
@@ -115,6 +134,9 @@ class HardDriveDataProvider extends Provider {
115134
throw new NotFoundError(`Folder ${diskPath(basePath, folderPath)} was not found`)
116135
}
117136

137+
// Log it
138+
log("hard_drive", `Fetching stats for ${diskPath(basePath, folderPath, fileName)}`)
139+
118140
// Get the statistics related to that file, `fs.readdir` only gives the name
119141
const statistics = await fs.stat(diskPath(basePath, folderPath, fileName)) // Change to lstat if you want to support sym links
120142

@@ -131,6 +153,10 @@ class HardDriveDataProvider extends Provider {
131153
const createdAtTime = statistics["birthTime"] // When it was created
132154
const lastModifiedTime = statistics["mtime"] // Last time the file or its metadata was changed
133155
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+
134160
return {name, kind, path, mimeType, size, createdAtTime, lastModifiedTime, contentURI} // Return it as an object
135161
}
136162

@@ -143,6 +169,9 @@ class HardDriveDataProvider extends Provider {
143169
// Get the file name in the URL
144170
const fileName = params["fileName"]
145171

172+
// Log it
173+
log("hard_drive", `Base path => ${basePath}; folder path => ${folderPath}; file name => ${fileName}`)
174+
146175
// Don't allow relative paths, let clients do that
147176
if ([basePath, folderPath].join("/").indexOf("/..") !== -1) {
148177
throw new BadRequestError(`Folder paths must not contain relative paths`)
@@ -159,13 +188,19 @@ class HardDriveDataProvider extends Provider {
159188
throw new FileExistsError(`File ${diskPath(basePath, folderPath, fileName)} already exists`)
160189
}
161190

191+
// Log it
192+
log("hard_drive", `Moving file ${fileMeta.path} to ${diskPath(basePath, folderPath, fileName)}`)
193+
162194
// `fileMeta` is passed to us by multer, and contains the path, size and mime type of the file
163195
// uploaded. Move the file from that path to the specified one.
164196
await fs.move(fileMeta.path, diskPath(basePath, folderPath, fileName))
165197

166198
// Check if the user passed fields to set values in
167199
// We can only set lastModifiedTime (mtime), not createAtTime
168200
if (body["lastModifiedTime"]) {
201+
// Log it
202+
log("hard_drive", `Setting lastModifiedTime to ${body["lastModifiedTime"]}`)
203+
// Convert it to a date object
169204
const mtime = new Date(body["lastModifiedTime"])
170205
// Set the lastModifiedTime
171206
await fs.utimes(fileMeta.path, mtime, mtime)
@@ -188,6 +223,9 @@ class HardDriveDataProvider extends Provider {
188223
const lastModifiedTime = statistics["mtime"] // Last time the file or its metadata was changed
189224
const contentURI = "file://" + diskPath(basePath, folderPath, fileName).replace(/\ /g, "%20") // Content URI, allows the file to be downloaded
190225

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+
191229
return {name, kind, path, mimeType, size, createdAtTime, lastModifiedTime, contentURI} // Return it as an object
192230
}
193231

@@ -212,6 +250,8 @@ class HardDriveDataProvider extends Provider {
212250

213251
// If there is some file data specified, update the file with it
214252
if (fileMeta) {
253+
// Log it
254+
log("hard_drive", `Updating file contents with contents of file => ${fileMeta.path}`)
215255
// `fileMeta` is passed to us by multer, and contains the path, size and mime type of the file
216256
// uploaded. Move the file from that path to the specified one and overwrite it.
217257
await fs.move(fileMeta.path, diskPath(basePath, folderPath, fileName), { overwrite: true })
@@ -220,10 +260,14 @@ class HardDriveDataProvider extends Provider {
220260
// Check if the user passed fields to set values in
221261
// We can only set name, path, and lastModifiedTime (mtime), not createdAtTime (birthTime)
222262
if (body["name"]) {
263+
// Log it
264+
log("hard_drive", `Setting name to ${body["name"]}`)
223265
await fs.rename(diskPath(basePath, folderPath, fileName), diskPath(basePath, folderPath, body["name"]))
224266
fileName = body["name"]
225267
}
226268
if (body["path"]) {
269+
// Log it
270+
log("hard_drive", `Moving file to ${body["path"]}`)
227271
// Don't allow relative paths, let clients do that
228272
if (body["path"].indexOf("/..") !== -1) {
229273
throw new BadRequestError(`Folder paths must not contain relative paths`)
@@ -232,6 +276,8 @@ class HardDriveDataProvider extends Provider {
232276
folderPath = body["path"]
233277
}
234278
if (body["lastModifiedTime"]) {
279+
// Log it
280+
log("hard_drive", `Setting lastModifiedTime to ${body["lastModifiedTime"]}`)
235281
const mtime = new Date(body["lastModifiedTime"])
236282
// Set the lastModifiedTime
237283
await fs.utimes(diskPath(basePath, folderPath, fileName), mtime, mtime)
@@ -254,6 +300,9 @@ class HardDriveDataProvider extends Provider {
254300
const lastModifiedTime = statistics["mtime"] // Last time the file or its metadata was changed
255301
const contentURI = "file://" + diskPath(basePath, folderPath, fileName).replace(/\ /g, "%20") // Content URI, allows the file to be downloaded
256302

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+
257306
return {name, kind, path, mimeType, size, createdAtTime, lastModifiedTime, contentURI} // Return it as an object
258307
}
259308

@@ -266,8 +315,14 @@ class HardDriveDataProvider extends Provider {
266315
// Get the file name in the URL
267316
const fileName = params["fileName"]
268317

318+
// Log it
319+
log("hard_drive", `Base path => ${basePath}; folder path => ${folderPath}; file name => ${fileName}`)
320+
269321
if (folderPath && fileName) {
270322
// If there is a file name provided, delete the file
323+
// Log it
324+
log("hard_drive", `Deleting file => ${diskPath(folderPath, fileName)}`)
325+
271326
// Don't allow relative paths, let clients do that
272327
if ([basePath, folderPath].join("/").indexOf("/..") !== -1) {
273328
throw new BadRequestError(`Folder paths must not contain relative paths`)
@@ -282,6 +337,9 @@ class HardDriveDataProvider extends Provider {
282337
return await fs.unlink(diskPath(basePath, folderPath, fileName))
283338
} else if (folderPath && !fileName) {
284339
// 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+
285343
// Don't allow relative paths, let clients do that
286344
if ([basePath, folderPath].join("/").indexOf("/..") !== -1) {
287345
throw new BadRequestError(`Folder paths must not contain relative paths`)

0 commit comments

Comments
 (0)