@adminforth/storage-adapter-local 1.0.7 → 1.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +17 -19
- package/index.ts +17 -20
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -9,6 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
};
|
|
10
10
|
import fs from "fs/promises";
|
|
11
11
|
import path from "path";
|
|
12
|
+
import { afLogger } from "adminforth";
|
|
12
13
|
import crypto from "crypto";
|
|
13
14
|
import { createWriteStream } from 'fs';
|
|
14
15
|
import { Level } from 'level';
|
|
@@ -73,13 +74,13 @@ class AdminForthStorageAdapterLocalFilesystem {
|
|
|
73
74
|
}
|
|
74
75
|
markKeyForDeletation(key) {
|
|
75
76
|
return __awaiter(this, void 0, void 0, function* () {
|
|
76
|
-
|
|
77
|
+
afLogger.error("Method \"markKeyForDeletation\" is deprecated, use markKeyForDeletion instead");
|
|
77
78
|
const metadata = yield this.metadataDb.get(key).catch((e) => {
|
|
78
|
-
|
|
79
|
+
afLogger.error(`Could not read metadata from db: ${e}`);
|
|
79
80
|
throw new Error(`Could not read metadata from db: ${e}`);
|
|
80
81
|
});
|
|
81
82
|
if (!metadata) {
|
|
82
|
-
|
|
83
|
+
afLogger.error(`Metadata for key ${key} not found`);
|
|
83
84
|
return;
|
|
84
85
|
}
|
|
85
86
|
const metadataParsed = JSON.parse(metadata);
|
|
@@ -95,14 +96,14 @@ class AdminForthStorageAdapterLocalFilesystem {
|
|
|
95
96
|
yield this.candidatesForDeletionDb.put(key, metadataParsed.createdAt);
|
|
96
97
|
}
|
|
97
98
|
catch (e) {
|
|
98
|
-
|
|
99
|
+
afLogger.error(`Could not write metadata to db: ${e}`);
|
|
99
100
|
throw new Error(`Could not write metadata to db: ${e}`);
|
|
100
101
|
}
|
|
101
102
|
});
|
|
102
103
|
}
|
|
103
104
|
markKeyForNotDeletation(key) {
|
|
104
105
|
return __awaiter(this, void 0, void 0, function* () {
|
|
105
|
-
|
|
106
|
+
afLogger.error("Method \"markKeyForNotDeletation\" is deprecated, use markKeyForNotDeletion instead");
|
|
106
107
|
try {
|
|
107
108
|
// if key exists, delete it
|
|
108
109
|
yield this.candidatesForDeletionDb.del(key);
|
|
@@ -115,11 +116,11 @@ class AdminForthStorageAdapterLocalFilesystem {
|
|
|
115
116
|
markKeyForDeletion(key) {
|
|
116
117
|
return __awaiter(this, void 0, void 0, function* () {
|
|
117
118
|
const metadata = yield this.metadataDb.get(key).catch((e) => {
|
|
118
|
-
|
|
119
|
+
afLogger.error(`Could not read metadata from db: ${e}`);
|
|
119
120
|
throw new Error(`Could not read metadata from db: ${e}`);
|
|
120
121
|
});
|
|
121
122
|
if (!metadata) {
|
|
122
|
-
|
|
123
|
+
afLogger.error(`Metadata for key ${key} not found`);
|
|
123
124
|
return;
|
|
124
125
|
}
|
|
125
126
|
const metadataParsed = JSON.parse(metadata);
|
|
@@ -135,7 +136,7 @@ class AdminForthStorageAdapterLocalFilesystem {
|
|
|
135
136
|
yield this.candidatesForDeletionDb.put(key, metadataParsed.createdAt);
|
|
136
137
|
}
|
|
137
138
|
catch (e) {
|
|
138
|
-
|
|
139
|
+
afLogger.error(`Could not write metadata to db: ${e}`);
|
|
139
140
|
throw new Error(`Could not write metadata to db: ${e}`);
|
|
140
141
|
}
|
|
141
142
|
});
|
|
@@ -197,12 +198,12 @@ class AdminForthStorageAdapterLocalFilesystem {
|
|
|
197
198
|
this.expressBase = `${slashedPrefix}uploaded-static/${userUniqueIntanceId}`;
|
|
198
199
|
}
|
|
199
200
|
else {
|
|
200
|
-
if (AdminForthStorageAdapterLocalFilesystem.registredPrexises.includes(this.options.adminServeBaseUrl)) {
|
|
201
|
+
if (AdminForthStorageAdapterLocalFilesystem.registredPrexises.includes(this.options.adminServeBaseUrl) || AdminForthStorageAdapterLocalFilesystem.registredPrexises.includes(`/${this.options.adminServeBaseUrl}`)) {
|
|
201
202
|
throw new Error(`adminServeBaseUrl ${this.options.adminServeBaseUrl} already registered, by another instance of local filesystem adapter.
|
|
202
203
|
Each adapter instahce should have unique adminServeBaseUrl by design.
|
|
203
204
|
`);
|
|
204
205
|
}
|
|
205
|
-
AdminForthStorageAdapterLocalFilesystem.registredPrexises.push(this.
|
|
206
|
+
AdminForthStorageAdapterLocalFilesystem.registredPrexises.push(this.options.adminServeBaseUrl);
|
|
206
207
|
this.expressBase = `${slashedPrefix}${this.options.adminServeBaseUrl}`;
|
|
207
208
|
}
|
|
208
209
|
// add express PUT endpoint for uploading files
|
|
@@ -225,7 +226,6 @@ class AdminForthStorageAdapterLocalFilesystem {
|
|
|
225
226
|
const payload = {
|
|
226
227
|
contentType: contentType,
|
|
227
228
|
};
|
|
228
|
-
console.log(`👐👐👐 verify sign for ${key}|${expires}|${JSON.stringify(payload)}`);
|
|
229
229
|
const expectedSignature = this.sign(`${this.expressBase}/${key}`, expires, payload);
|
|
230
230
|
if (signature !== expectedSignature) {
|
|
231
231
|
return res.status(403).send("Invalid signature");
|
|
@@ -263,17 +263,15 @@ class AdminForthStorageAdapterLocalFilesystem {
|
|
|
263
263
|
createdAt: +Date.now(),
|
|
264
264
|
size: writeStream.bytesWritten,
|
|
265
265
|
})).catch((e) => {
|
|
266
|
-
|
|
266
|
+
afLogger.error(`Could not write metadata to db: ${e}`);
|
|
267
267
|
throw new Error(`Could not write metadata to db: ${e}`);
|
|
268
268
|
});
|
|
269
269
|
this.markKeyForDeletation(key);
|
|
270
270
|
res.status(200).send("File uploaded");
|
|
271
271
|
});
|
|
272
272
|
}));
|
|
273
|
-
console.log(`🎉🎉🎉 registring get endpoint for ${this.expressBase}/*`);
|
|
274
273
|
// add express GET endpoint for downloading files
|
|
275
274
|
expressInstance.get(`${this.expressBase}/*`, (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
276
|
-
console.log(`🎉🎉🎉 GET ${req.url}`, res, typeof res, Object.keys(res));
|
|
277
275
|
const key = req.params[0];
|
|
278
276
|
const filePath = path.resolve(this.options.fileSystemFolder, key);
|
|
279
277
|
// Ensure filePath is within fileSystemFolder
|
|
@@ -306,7 +304,7 @@ class AdminForthStorageAdapterLocalFilesystem {
|
|
|
306
304
|
},
|
|
307
305
|
}, (err) => {
|
|
308
306
|
if (err) {
|
|
309
|
-
|
|
307
|
+
afLogger.error(`Could not send file ${filePath}: ${err}`);
|
|
310
308
|
res.status(500).send("Could not send file");
|
|
311
309
|
}
|
|
312
310
|
});
|
|
@@ -348,7 +346,7 @@ class AdminForthStorageAdapterLocalFilesystem {
|
|
|
348
346
|
const keys = yield this.candidatesForDeletionDb.keys().all();
|
|
349
347
|
for (const key of keys) {
|
|
350
348
|
const createdAt = yield this.candidatesForDeletionDb.get(key).catch((e) => {
|
|
351
|
-
|
|
349
|
+
afLogger.error(`Could not read metadata from db: ${e}`);
|
|
352
350
|
throw new Error(`Could not read metadata from db: ${e}`);
|
|
353
351
|
});
|
|
354
352
|
if (now - +createdAt > 24 * 60 * 60 * 1000) {
|
|
@@ -357,7 +355,7 @@ class AdminForthStorageAdapterLocalFilesystem {
|
|
|
357
355
|
yield fs.unlink(path.resolve(this.options.fileSystemFolder, key));
|
|
358
356
|
}
|
|
359
357
|
catch (e) {
|
|
360
|
-
|
|
358
|
+
afLogger.error(`Could not delete file ${key}: ${e}`);
|
|
361
359
|
throw new Error(`Could not delete file ${key}: ${e}`);
|
|
362
360
|
}
|
|
363
361
|
// delete metadata
|
|
@@ -365,7 +363,7 @@ class AdminForthStorageAdapterLocalFilesystem {
|
|
|
365
363
|
yield this.metadataDb.del(key);
|
|
366
364
|
}
|
|
367
365
|
catch (e) {
|
|
368
|
-
|
|
366
|
+
afLogger.error(`Could not delete metadata from db: ${e}`);
|
|
369
367
|
throw new Error(`Could not delete metadata from db: ${e}`);
|
|
370
368
|
}
|
|
371
369
|
}
|
|
@@ -419,7 +417,7 @@ class AdminForthStorageAdapterLocalFilesystem {
|
|
|
419
417
|
const fileBuffer = yield fs.readFile(filePath);
|
|
420
418
|
const base64 = fileBuffer.toString("base64");
|
|
421
419
|
const metadata = yield this.metadataDb.get(key).catch((e) => {
|
|
422
|
-
|
|
420
|
+
afLogger.error(`Could not read metadata from db: ${e}`);
|
|
423
421
|
throw new Error(`Could not read metadata from db: ${e}`);
|
|
424
422
|
});
|
|
425
423
|
if (!metadata) {
|
package/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import fs from "fs/promises";
|
|
2
2
|
import path from "path";
|
|
3
|
-
import AdminForth, { StorageAdapter } from "adminforth";
|
|
3
|
+
import AdminForth, { StorageAdapter, afLogger } from "adminforth";
|
|
4
4
|
import crypto from "crypto";
|
|
5
5
|
import { createWriteStream } from 'fs';
|
|
6
6
|
import { Level } from 'level';
|
|
@@ -99,13 +99,13 @@ export default class AdminForthStorageAdapterLocalFilesystem implements StorageA
|
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
async markKeyForDeletation(key: string): Promise<void> {
|
|
102
|
-
|
|
102
|
+
afLogger.error("Method \"markKeyForDeletation\" is deprecated, use markKeyForDeletion instead");
|
|
103
103
|
const metadata = await this.metadataDb.get(key).catch((e) => {
|
|
104
|
-
|
|
104
|
+
afLogger.error(`Could not read metadata from db: ${e}`);
|
|
105
105
|
throw new Error(`Could not read metadata from db: ${e}`);
|
|
106
106
|
});
|
|
107
107
|
if (!metadata) {
|
|
108
|
-
|
|
108
|
+
afLogger.error(`Metadata for key ${key} not found`);
|
|
109
109
|
return;
|
|
110
110
|
}
|
|
111
111
|
const metadataParsed = JSON.parse(metadata);
|
|
@@ -120,13 +120,13 @@ export default class AdminForthStorageAdapterLocalFilesystem implements StorageA
|
|
|
120
120
|
try {
|
|
121
121
|
await this.candidatesForDeletionDb.put(key, metadataParsed.createdAt)
|
|
122
122
|
} catch (e) {
|
|
123
|
-
|
|
123
|
+
afLogger.error(`Could not write metadata to db: ${e}`);
|
|
124
124
|
throw new Error(`Could not write metadata to db: ${e}`);
|
|
125
125
|
}
|
|
126
126
|
}
|
|
127
127
|
|
|
128
128
|
async markKeyForNotDeletation(key: string): Promise<void> {
|
|
129
|
-
|
|
129
|
+
afLogger.error("Method \"markKeyForNotDeletation\" is deprecated, use markKeyForNotDeletion instead");
|
|
130
130
|
try {
|
|
131
131
|
// if key exists, delete it
|
|
132
132
|
await this.candidatesForDeletionDb.del(key);
|
|
@@ -137,11 +137,11 @@ export default class AdminForthStorageAdapterLocalFilesystem implements StorageA
|
|
|
137
137
|
|
|
138
138
|
async markKeyForDeletion(key: string): Promise<void> {
|
|
139
139
|
const metadata = await this.metadataDb.get(key).catch((e) => {
|
|
140
|
-
|
|
140
|
+
afLogger.error(`Could not read metadata from db: ${e}`);
|
|
141
141
|
throw new Error(`Could not read metadata from db: ${e}`);
|
|
142
142
|
});
|
|
143
143
|
if (!metadata) {
|
|
144
|
-
|
|
144
|
+
afLogger.error(`Metadata for key ${key} not found`);
|
|
145
145
|
return;
|
|
146
146
|
}
|
|
147
147
|
const metadataParsed = JSON.parse(metadata);
|
|
@@ -156,7 +156,7 @@ export default class AdminForthStorageAdapterLocalFilesystem implements StorageA
|
|
|
156
156
|
try {
|
|
157
157
|
await this.candidatesForDeletionDb.put(key, metadataParsed.createdAt)
|
|
158
158
|
} catch (e) {
|
|
159
|
-
|
|
159
|
+
afLogger.error(`Could not write metadata to db: ${e}`);
|
|
160
160
|
throw new Error(`Could not write metadata to db: ${e}`);
|
|
161
161
|
}
|
|
162
162
|
}
|
|
@@ -217,13 +217,13 @@ export default class AdminForthStorageAdapterLocalFilesystem implements StorageA
|
|
|
217
217
|
if (!this.options.adminServeBaseUrl) {
|
|
218
218
|
this.expressBase = `${slashedPrefix}uploaded-static/${userUniqueIntanceId}`
|
|
219
219
|
} else {
|
|
220
|
-
if (AdminForthStorageAdapterLocalFilesystem.registredPrexises.includes(this.options.adminServeBaseUrl)) {
|
|
220
|
+
if (AdminForthStorageAdapterLocalFilesystem.registredPrexises.includes(this.options.adminServeBaseUrl) || AdminForthStorageAdapterLocalFilesystem.registredPrexises.includes(`/${this.options.adminServeBaseUrl}`)) {
|
|
221
221
|
throw new Error(`adminServeBaseUrl ${this.options.adminServeBaseUrl} already registered, by another instance of local filesystem adapter.
|
|
222
222
|
Each adapter instahce should have unique adminServeBaseUrl by design.
|
|
223
223
|
`);
|
|
224
224
|
}
|
|
225
225
|
|
|
226
|
-
AdminForthStorageAdapterLocalFilesystem.registredPrexises.push(this.
|
|
226
|
+
AdminForthStorageAdapterLocalFilesystem.registredPrexises.push(this.options.adminServeBaseUrl);
|
|
227
227
|
this.expressBase = `${slashedPrefix}${this.options.adminServeBaseUrl}`;
|
|
228
228
|
|
|
229
229
|
}
|
|
@@ -253,7 +253,6 @@ export default class AdminForthStorageAdapterLocalFilesystem implements StorageA
|
|
|
253
253
|
const payload = {
|
|
254
254
|
contentType: contentType,
|
|
255
255
|
}
|
|
256
|
-
console.log(`👐👐👐 verify sign for ${key}|${expires}|${JSON.stringify(payload)}`)
|
|
257
256
|
|
|
258
257
|
const expectedSignature = this.sign(
|
|
259
258
|
`${this.expressBase}/${key}`, expires, payload);
|
|
@@ -294,7 +293,7 @@ export default class AdminForthStorageAdapterLocalFilesystem implements StorageA
|
|
|
294
293
|
size: writeStream.bytesWritten,
|
|
295
294
|
})
|
|
296
295
|
).catch((e) => {
|
|
297
|
-
|
|
296
|
+
afLogger.error(`Could not write metadata to db: ${e}`);
|
|
298
297
|
throw new Error(`Could not write metadata to db: ${e}`);
|
|
299
298
|
});
|
|
300
299
|
|
|
@@ -304,10 +303,8 @@ export default class AdminForthStorageAdapterLocalFilesystem implements StorageA
|
|
|
304
303
|
});
|
|
305
304
|
});
|
|
306
305
|
|
|
307
|
-
console.log(`🎉🎉🎉 registring get endpoint for ${this.expressBase}/*`)
|
|
308
306
|
// add express GET endpoint for downloading files
|
|
309
307
|
expressInstance.get(`${this.expressBase}/*`, async (req: any, res: any) => {
|
|
310
|
-
console.log(`🎉🎉🎉 GET ${req.url}`, res, typeof res, Object.keys(res));
|
|
311
308
|
const key = req.params[0];
|
|
312
309
|
const filePath = path.resolve(this.options.fileSystemFolder, key);
|
|
313
310
|
|
|
@@ -345,7 +342,7 @@ export default class AdminForthStorageAdapterLocalFilesystem implements StorageA
|
|
|
345
342
|
},
|
|
346
343
|
(err) => {
|
|
347
344
|
if (err) {
|
|
348
|
-
|
|
345
|
+
afLogger.error(`Could not send file ${filePath}: ${err}`);
|
|
349
346
|
res.status(500).send("Could not send file");
|
|
350
347
|
}
|
|
351
348
|
}
|
|
@@ -396,7 +393,7 @@ export default class AdminForthStorageAdapterLocalFilesystem implements StorageA
|
|
|
396
393
|
const keys = await this.candidatesForDeletionDb.keys().all();
|
|
397
394
|
for (const key of keys) {
|
|
398
395
|
const createdAt = await this.candidatesForDeletionDb.get(key).catch((e) => {
|
|
399
|
-
|
|
396
|
+
afLogger.error(`Could not read metadata from db: ${e}`);
|
|
400
397
|
throw new Error(`Could not read metadata from db: ${e}`);
|
|
401
398
|
});
|
|
402
399
|
if (now - +createdAt > 24 * 60 * 60 * 1000) {
|
|
@@ -404,14 +401,14 @@ export default class AdminForthStorageAdapterLocalFilesystem implements StorageA
|
|
|
404
401
|
try {
|
|
405
402
|
await fs.unlink(path.resolve(this.options.fileSystemFolder, key));
|
|
406
403
|
} catch (e) {
|
|
407
|
-
|
|
404
|
+
afLogger.error(`Could not delete file ${key}: ${e}`);
|
|
408
405
|
throw new Error(`Could not delete file ${key}: ${e}`);
|
|
409
406
|
}
|
|
410
407
|
// delete metadata
|
|
411
408
|
try {
|
|
412
409
|
await this.metadataDb.del(key);
|
|
413
410
|
} catch (e) {
|
|
414
|
-
|
|
411
|
+
afLogger.error(`Could not delete metadata from db: ${e}`);
|
|
415
412
|
throw new Error(`Could not delete metadata from db: ${e}`);
|
|
416
413
|
}
|
|
417
414
|
}
|
|
@@ -467,7 +464,7 @@ export default class AdminForthStorageAdapterLocalFilesystem implements StorageA
|
|
|
467
464
|
const fileBuffer = await fs.readFile(filePath);
|
|
468
465
|
const base64 = fileBuffer.toString("base64");
|
|
469
466
|
const metadata = await this.metadataDb.get(key).catch((e) => {
|
|
470
|
-
|
|
467
|
+
afLogger.error(`Could not read metadata from db: ${e}`);
|
|
471
468
|
throw new Error(`Could not read metadata from db: ${e}`);
|
|
472
469
|
});
|
|
473
470
|
if (!metadata) {
|