@byline/storage-local 4.1.0 → 4.2.0
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.
|
@@ -34,7 +34,9 @@ export interface LocalStorageConfig {
|
|
|
34
34
|
* Create a local filesystem storage provider.
|
|
35
35
|
*
|
|
36
36
|
* Uploaded files are written under `uploadDir`, organised into sub-paths:
|
|
37
|
-
* `<collection>/<
|
|
37
|
+
* `<location|collection>/<filename>-<suffix>.<ext>`
|
|
38
|
+
* (e.g. `events/meeting-agenda-4fa35g.pdf` — a short base36 suffix keeps
|
|
39
|
+
* names friendly; availability is verified with a retry on collision).
|
|
38
40
|
*
|
|
39
41
|
* The provider generates a public URL by prepending `baseUrl` to the
|
|
40
42
|
* storage path. Pair this with a runtime file handler (Express
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
*
|
|
6
6
|
* Copyright (c) Infonomic Company Limited
|
|
7
7
|
*/
|
|
8
|
+
import { randomBytes } from 'node:crypto';
|
|
8
9
|
import fs from 'node:fs';
|
|
9
10
|
import path from 'node:path';
|
|
10
11
|
import { pipeline } from 'node:stream/promises';
|
|
@@ -27,19 +28,37 @@ function sanitiseFilename(filename) {
|
|
|
27
28
|
.replace(/^-|-$/g, '');
|
|
28
29
|
return `${safe || 'file'}${ext.toLowerCase()}`;
|
|
29
30
|
}
|
|
31
|
+
const SUFFIX_ALPHABET = 'abcdefghijklmnopqrstuvwxyz0123456789';
|
|
30
32
|
/**
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
+
* Short random base36 suffix appended to stored basenames. Six characters
|
|
34
|
+
* (36^6 ≈ 2.2 billion) keep filenames human-friendly while making
|
|
35
|
+
* collisions among same-named files in one scope vanishingly rare — and
|
|
36
|
+
* the upload path verifies availability with `exists()` and retries, so
|
|
37
|
+
* the residual risk is handled rather than merely improbable.
|
|
38
|
+
*/
|
|
39
|
+
function randomSuffix(length = 6) {
|
|
40
|
+
let out = '';
|
|
41
|
+
for (const byte of randomBytes(length)) {
|
|
42
|
+
out += SUFFIX_ALPHABET.charAt(byte % SUFFIX_ALPHABET.length);
|
|
43
|
+
}
|
|
44
|
+
return out;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Compose a candidate storage sub-path, e.g.:
|
|
48
|
+
* `events/meeting-agenda-4fa35g.pdf`
|
|
33
49
|
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
50
|
+
* `scope` is the field's `upload.location` when declared (may carry nested
|
|
51
|
+
* segments, e.g. `news/attachments`), else the collection path. The
|
|
52
|
+
* filename leads (arriving pre-slugified from `uploadField`'s configurable
|
|
53
|
+
* filename slugifier; `sanitiseFilename` re-applies the default rules as a
|
|
54
|
+
* safety net for direct callers) and the entropy rides as a short suffix
|
|
55
|
+
* before the extension — friendly to download as, browse, and log.
|
|
37
56
|
*/
|
|
38
|
-
function buildStoragePath(
|
|
39
|
-
const uid = uuidv4();
|
|
57
|
+
function buildStoragePath(scope, filename, suffix) {
|
|
40
58
|
const safe = sanitiseFilename(filename);
|
|
41
|
-
const
|
|
42
|
-
|
|
59
|
+
const ext = path.extname(safe);
|
|
60
|
+
const base = path.basename(safe, ext);
|
|
61
|
+
return `${scope ?? 'uploads'}/${base}-${suffix}${ext}`;
|
|
43
62
|
}
|
|
44
63
|
// ---------------------------------------------------------------------------
|
|
45
64
|
// Provider implementation
|
|
@@ -54,7 +73,7 @@ class LocalStorageProvider {
|
|
|
54
73
|
this.baseUrl = config.baseUrl.endsWith('/') ? config.baseUrl.slice(0, -1) : config.baseUrl;
|
|
55
74
|
}
|
|
56
75
|
async upload(stream, options) {
|
|
57
|
-
const storagePath = options.targetStoragePath ??
|
|
76
|
+
const storagePath = options.targetStoragePath ?? (await this.availableStoragePath(options));
|
|
58
77
|
const absolutePath = path.join(this.uploadDir, storagePath);
|
|
59
78
|
// Ensure the target directory exists.
|
|
60
79
|
fs.mkdirSync(path.dirname(absolutePath), { recursive: true });
|
|
@@ -71,6 +90,21 @@ class LocalStorageProvider {
|
|
|
71
90
|
storageUrl: this.getUrl(storagePath),
|
|
72
91
|
};
|
|
73
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* Pick an unclaimed storage path: compose the friendly key, verify it is
|
|
95
|
+
* free, and retry with a fresh suffix on collision. After three straight
|
|
96
|
+
* collisions (pathological — a same-named file avalanche) fall back to a
|
|
97
|
+
* full-entropy UUID suffix, which cannot realistically collide.
|
|
98
|
+
*/
|
|
99
|
+
async availableStoragePath(options) {
|
|
100
|
+
const scope = options.location ?? options.collection;
|
|
101
|
+
for (let attempt = 0; attempt < 3; attempt++) {
|
|
102
|
+
const candidate = buildStoragePath(scope, options.filename, randomSuffix());
|
|
103
|
+
if (!(await this.exists(candidate)))
|
|
104
|
+
return candidate;
|
|
105
|
+
}
|
|
106
|
+
return buildStoragePath(scope, options.filename, uuidv4());
|
|
107
|
+
}
|
|
74
108
|
async delete(storagePath) {
|
|
75
109
|
const absolutePath = path.join(this.uploadDir, storagePath);
|
|
76
110
|
try {
|
|
@@ -129,7 +163,9 @@ class LocalStorageProvider {
|
|
|
129
163
|
* Create a local filesystem storage provider.
|
|
130
164
|
*
|
|
131
165
|
* Uploaded files are written under `uploadDir`, organised into sub-paths:
|
|
132
|
-
* `<collection>/<
|
|
166
|
+
* `<location|collection>/<filename>-<suffix>.<ext>`
|
|
167
|
+
* (e.g. `events/meeting-agenda-4fa35g.pdf` — a short base36 suffix keeps
|
|
168
|
+
* names friendly; availability is verified with a retry on collision).
|
|
133
169
|
*
|
|
134
170
|
* The provider generates a public URL by prepending `baseUrl` to the
|
|
135
171
|
* storage path. Pair this with a runtime file handler (Express
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@byline/storage-local",
|
|
3
3
|
"private": false,
|
|
4
4
|
"license": "MPL-2.0",
|
|
5
|
-
"version": "4.
|
|
5
|
+
"version": "4.2.0",
|
|
6
6
|
"engines": {
|
|
7
7
|
"node": ">=20.9.0"
|
|
8
8
|
},
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
],
|
|
40
40
|
"dependencies": {
|
|
41
41
|
"uuid": "^14.0.1",
|
|
42
|
-
"@byline/core": "4.
|
|
42
|
+
"@byline/core": "4.2.0"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@biomejs/biome": "2.5.2",
|