@objectstack/service-storage 4.0.4 → 4.1.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.
- package/README.md +108 -401
- package/dist/index.cjs +1343 -66
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +7645 -22
- package/dist/index.d.ts +7645 -22
- package/dist/index.js +1335 -66
- package/dist/index.js.map +1 -1
- package/package.json +43 -6
- package/.turbo/turbo-build.log +0 -22
- package/CHANGELOG.md +0 -177
- package/src/index.ts +0 -8
- package/src/local-storage-adapter.test.ts +0 -91
- package/src/local-storage-adapter.ts +0 -100
- package/src/s3-storage-adapter.ts +0 -88
- package/src/storage-service-plugin.ts +0 -66
- package/tsconfig.json +0 -17
package/dist/index.cjs
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __esm = (fn, res) => function __init() {
|
|
9
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
10
|
+
};
|
|
6
11
|
var __export = (target, all) => {
|
|
7
12
|
for (var name in all)
|
|
8
13
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -15,51 +20,349 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
20
|
}
|
|
16
21
|
return to;
|
|
17
22
|
};
|
|
23
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
24
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
25
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
26
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
27
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
28
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
29
|
+
mod
|
|
30
|
+
));
|
|
18
31
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
32
|
|
|
33
|
+
// src/s3-storage-adapter.ts
|
|
34
|
+
var s3_storage_adapter_exports = {};
|
|
35
|
+
__export(s3_storage_adapter_exports, {
|
|
36
|
+
S3StorageAdapter: () => S3StorageAdapter
|
|
37
|
+
});
|
|
38
|
+
async function streamToBuffer(stream) {
|
|
39
|
+
if (Buffer.isBuffer(stream)) return stream;
|
|
40
|
+
if (stream instanceof Uint8Array) return Buffer.from(stream);
|
|
41
|
+
const chunks = [];
|
|
42
|
+
if (typeof stream[Symbol.asyncIterator] === "function") {
|
|
43
|
+
for await (const chunk of stream) {
|
|
44
|
+
chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk);
|
|
45
|
+
}
|
|
46
|
+
} else if (stream.getReader) {
|
|
47
|
+
const reader = stream.getReader();
|
|
48
|
+
let done = false;
|
|
49
|
+
while (!done) {
|
|
50
|
+
const result = await reader.read();
|
|
51
|
+
done = result.done;
|
|
52
|
+
if (result.value) chunks.push(result.value);
|
|
53
|
+
}
|
|
54
|
+
} else {
|
|
55
|
+
throw new Error("Cannot convert stream to buffer");
|
|
56
|
+
}
|
|
57
|
+
return Buffer.concat(chunks);
|
|
58
|
+
}
|
|
59
|
+
var S3StorageAdapter;
|
|
60
|
+
var init_s3_storage_adapter = __esm({
|
|
61
|
+
"src/s3-storage-adapter.ts"() {
|
|
62
|
+
"use strict";
|
|
63
|
+
S3StorageAdapter = class {
|
|
64
|
+
constructor(options) {
|
|
65
|
+
this.options = options;
|
|
66
|
+
this.clientPromise = null;
|
|
67
|
+
// ---------------------------------------------------------------------------
|
|
68
|
+
// Internal upload key tracking
|
|
69
|
+
// ---------------------------------------------------------------------------
|
|
70
|
+
this._uploadKeys = /* @__PURE__ */ new Map();
|
|
71
|
+
this.bucket = options.bucket;
|
|
72
|
+
this.region = options.region;
|
|
73
|
+
this.endpoint = options.endpoint;
|
|
74
|
+
this.forcePathStyle = options.forcePathStyle ?? false;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Lazily resolve the AWS S3 client to avoid crashing at import time when
|
|
78
|
+
* `@aws-sdk/client-s3` isn't installed.
|
|
79
|
+
*/
|
|
80
|
+
async getClient() {
|
|
81
|
+
if (!this.clientPromise) {
|
|
82
|
+
this.clientPromise = (async () => {
|
|
83
|
+
let s3Mod;
|
|
84
|
+
try {
|
|
85
|
+
s3Mod = await import("@aws-sdk/client-s3");
|
|
86
|
+
} catch {
|
|
87
|
+
throw new Error(
|
|
88
|
+
"S3StorageAdapter requires @aws-sdk/client-s3. Install it with: pnpm add @aws-sdk/client-s3 @aws-sdk/s3-request-presigner"
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
const { S3Client } = s3Mod;
|
|
92
|
+
const clientOpts = { region: this.region };
|
|
93
|
+
if (this.endpoint) clientOpts.endpoint = this.endpoint;
|
|
94
|
+
if (this.forcePathStyle) clientOpts.forcePathStyle = true;
|
|
95
|
+
if (this.options.accessKeyId && this.options.secretAccessKey) {
|
|
96
|
+
clientOpts.credentials = {
|
|
97
|
+
accessKeyId: this.options.accessKeyId,
|
|
98
|
+
secretAccessKey: this.options.secretAccessKey
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
return new S3Client(clientOpts);
|
|
102
|
+
})();
|
|
103
|
+
}
|
|
104
|
+
return this.clientPromise;
|
|
105
|
+
}
|
|
106
|
+
async s3Mod() {
|
|
107
|
+
try {
|
|
108
|
+
return await import("@aws-sdk/client-s3");
|
|
109
|
+
} catch {
|
|
110
|
+
throw new Error("S3StorageAdapter requires @aws-sdk/client-s3");
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
async presignerMod() {
|
|
114
|
+
try {
|
|
115
|
+
return await import("@aws-sdk/s3-request-presigner");
|
|
116
|
+
} catch {
|
|
117
|
+
throw new Error("S3StorageAdapter requires @aws-sdk/s3-request-presigner");
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
// ---------------------------------------------------------------------------
|
|
121
|
+
// Basic operations
|
|
122
|
+
// ---------------------------------------------------------------------------
|
|
123
|
+
async upload(key, data, options) {
|
|
124
|
+
const client = await this.getClient();
|
|
125
|
+
const s3 = await this.s3Mod();
|
|
126
|
+
const body = data instanceof Buffer ? data : await streamToBuffer(data);
|
|
127
|
+
const cmd = new s3.PutObjectCommand({
|
|
128
|
+
Bucket: this.bucket,
|
|
129
|
+
Key: key,
|
|
130
|
+
Body: body,
|
|
131
|
+
ContentType: options?.contentType,
|
|
132
|
+
Metadata: options?.metadata,
|
|
133
|
+
ACL: options?.acl === "public-read" ? "public-read" : void 0
|
|
134
|
+
});
|
|
135
|
+
await client.send(cmd);
|
|
136
|
+
}
|
|
137
|
+
async download(key) {
|
|
138
|
+
const client = await this.getClient();
|
|
139
|
+
const s3 = await this.s3Mod();
|
|
140
|
+
const cmd = new s3.GetObjectCommand({ Bucket: this.bucket, Key: key });
|
|
141
|
+
const res = await client.send(cmd);
|
|
142
|
+
return streamToBuffer(res.Body);
|
|
143
|
+
}
|
|
144
|
+
async delete(key) {
|
|
145
|
+
const client = await this.getClient();
|
|
146
|
+
const s3 = await this.s3Mod();
|
|
147
|
+
const cmd = new s3.DeleteObjectCommand({ Bucket: this.bucket, Key: key });
|
|
148
|
+
await client.send(cmd);
|
|
149
|
+
}
|
|
150
|
+
async exists(key) {
|
|
151
|
+
const client = await this.getClient();
|
|
152
|
+
const s3 = await this.s3Mod();
|
|
153
|
+
try {
|
|
154
|
+
const cmd = new s3.HeadObjectCommand({ Bucket: this.bucket, Key: key });
|
|
155
|
+
await client.send(cmd);
|
|
156
|
+
return true;
|
|
157
|
+
} catch (err) {
|
|
158
|
+
if (err.name === "NotFound" || err.$metadata?.httpStatusCode === 404) return false;
|
|
159
|
+
throw err;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
async getInfo(key) {
|
|
163
|
+
const client = await this.getClient();
|
|
164
|
+
const s3 = await this.s3Mod();
|
|
165
|
+
const cmd = new s3.HeadObjectCommand({ Bucket: this.bucket, Key: key });
|
|
166
|
+
const res = await client.send(cmd);
|
|
167
|
+
return {
|
|
168
|
+
key,
|
|
169
|
+
size: res.ContentLength ?? 0,
|
|
170
|
+
contentType: res.ContentType,
|
|
171
|
+
lastModified: res.LastModified ?? /* @__PURE__ */ new Date(),
|
|
172
|
+
metadata: res.Metadata
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
async list(prefix) {
|
|
176
|
+
const client = await this.getClient();
|
|
177
|
+
const s3 = await this.s3Mod();
|
|
178
|
+
const cmd = new s3.ListObjectsV2Command({ Bucket: this.bucket, Prefix: prefix });
|
|
179
|
+
const res = await client.send(cmd);
|
|
180
|
+
return (res.Contents ?? []).map((item) => ({
|
|
181
|
+
key: item.Key,
|
|
182
|
+
size: item.Size ?? 0,
|
|
183
|
+
lastModified: item.LastModified ?? /* @__PURE__ */ new Date()
|
|
184
|
+
}));
|
|
185
|
+
}
|
|
186
|
+
// ---------------------------------------------------------------------------
|
|
187
|
+
// Presigned URLs
|
|
188
|
+
// ---------------------------------------------------------------------------
|
|
189
|
+
async getSignedUrl(key, expiresIn) {
|
|
190
|
+
const desc = await this.getPresignedDownload(key, expiresIn);
|
|
191
|
+
return desc.downloadUrl;
|
|
192
|
+
}
|
|
193
|
+
async getPresignedUpload(key, expiresIn, options) {
|
|
194
|
+
const client = await this.getClient();
|
|
195
|
+
const s3 = await this.s3Mod();
|
|
196
|
+
const { getSignedUrl } = await this.presignerMod();
|
|
197
|
+
const cmd = new s3.PutObjectCommand({
|
|
198
|
+
Bucket: this.bucket,
|
|
199
|
+
Key: key,
|
|
200
|
+
ContentType: options?.contentType,
|
|
201
|
+
Metadata: options?.metadata,
|
|
202
|
+
ACL: options?.acl === "public-read" ? "public-read" : void 0
|
|
203
|
+
});
|
|
204
|
+
const url = await getSignedUrl(client, cmd, { expiresIn });
|
|
205
|
+
return {
|
|
206
|
+
uploadUrl: url,
|
|
207
|
+
method: "PUT",
|
|
208
|
+
headers: options?.contentType ? { "content-type": options.contentType } : void 0,
|
|
209
|
+
expiresIn
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
async getPresignedDownload(key, expiresIn) {
|
|
213
|
+
const client = await this.getClient();
|
|
214
|
+
const s3 = await this.s3Mod();
|
|
215
|
+
const { getSignedUrl } = await this.presignerMod();
|
|
216
|
+
const cmd = new s3.GetObjectCommand({ Bucket: this.bucket, Key: key });
|
|
217
|
+
const url = await getSignedUrl(client, cmd, { expiresIn });
|
|
218
|
+
return { downloadUrl: url, expiresIn };
|
|
219
|
+
}
|
|
220
|
+
// ---------------------------------------------------------------------------
|
|
221
|
+
// Chunked / multipart upload
|
|
222
|
+
// ---------------------------------------------------------------------------
|
|
223
|
+
async initiateChunkedUpload(key, options) {
|
|
224
|
+
const client = await this.getClient();
|
|
225
|
+
const s3 = await this.s3Mod();
|
|
226
|
+
const cmd = new s3.CreateMultipartUploadCommand({
|
|
227
|
+
Bucket: this.bucket,
|
|
228
|
+
Key: key,
|
|
229
|
+
ContentType: options?.contentType,
|
|
230
|
+
Metadata: options?.metadata
|
|
231
|
+
});
|
|
232
|
+
const res = await client.send(cmd);
|
|
233
|
+
return res.UploadId;
|
|
234
|
+
}
|
|
235
|
+
async uploadChunk(uploadId, partNumber, data) {
|
|
236
|
+
const client = await this.getClient();
|
|
237
|
+
const s3 = await this.s3Mod();
|
|
238
|
+
const key = this._uploadKeys?.get(uploadId);
|
|
239
|
+
if (!key) {
|
|
240
|
+
throw new Error("S3StorageAdapter: key not found for uploadId. Call setUploadKey() before uploadChunk().");
|
|
241
|
+
}
|
|
242
|
+
const cmd = new s3.UploadPartCommand({
|
|
243
|
+
Bucket: this.bucket,
|
|
244
|
+
Key: key,
|
|
245
|
+
UploadId: uploadId,
|
|
246
|
+
PartNumber: partNumber,
|
|
247
|
+
Body: data
|
|
248
|
+
});
|
|
249
|
+
const res = await client.send(cmd);
|
|
250
|
+
return res.ETag;
|
|
251
|
+
}
|
|
252
|
+
async completeChunkedUpload(uploadId, parts) {
|
|
253
|
+
const client = await this.getClient();
|
|
254
|
+
const s3 = await this.s3Mod();
|
|
255
|
+
const key = this._uploadKeys?.get(uploadId);
|
|
256
|
+
if (!key) {
|
|
257
|
+
throw new Error("S3StorageAdapter: key not found for uploadId.");
|
|
258
|
+
}
|
|
259
|
+
const cmd = new s3.CompleteMultipartUploadCommand({
|
|
260
|
+
Bucket: this.bucket,
|
|
261
|
+
Key: key,
|
|
262
|
+
UploadId: uploadId,
|
|
263
|
+
MultipartUpload: {
|
|
264
|
+
Parts: parts.map((p) => ({ PartNumber: p.partNumber, ETag: p.eTag }))
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
await client.send(cmd);
|
|
268
|
+
this._uploadKeys?.delete(uploadId);
|
|
269
|
+
return key;
|
|
270
|
+
}
|
|
271
|
+
async abortChunkedUpload(uploadId) {
|
|
272
|
+
const client = await this.getClient();
|
|
273
|
+
const s3 = await this.s3Mod();
|
|
274
|
+
const key = this._uploadKeys?.get(uploadId);
|
|
275
|
+
if (!key) return;
|
|
276
|
+
const cmd = new s3.AbortMultipartUploadCommand({
|
|
277
|
+
Bucket: this.bucket,
|
|
278
|
+
Key: key,
|
|
279
|
+
UploadId: uploadId
|
|
280
|
+
});
|
|
281
|
+
await client.send(cmd);
|
|
282
|
+
this._uploadKeys?.delete(uploadId);
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Register the storage key for a multipart upload session. Must be called
|
|
286
|
+
* by the StorageServicePlugin after `initiateChunkedUpload()` returns so
|
|
287
|
+
* that subsequent `uploadChunk` / `completeChunkedUpload` calls can resolve
|
|
288
|
+
* the S3 key without it being part of the IStorageService contract signature.
|
|
289
|
+
*/
|
|
290
|
+
setUploadKey(uploadId, key) {
|
|
291
|
+
this._uploadKeys.set(uploadId, key);
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
});
|
|
296
|
+
|
|
20
297
|
// src/index.ts
|
|
21
298
|
var index_exports = {};
|
|
22
299
|
__export(index_exports, {
|
|
23
300
|
LocalStorageAdapter: () => LocalStorageAdapter,
|
|
24
301
|
S3StorageAdapter: () => S3StorageAdapter,
|
|
25
|
-
|
|
302
|
+
StorageMetadataStore: () => StorageMetadataStore,
|
|
303
|
+
StorageServicePlugin: () => StorageServicePlugin,
|
|
304
|
+
SwappableStorageService: () => SwappableStorageService,
|
|
305
|
+
SystemFile: () => SystemFile,
|
|
306
|
+
SystemUploadSession: () => SystemUploadSession,
|
|
307
|
+
registerStorageRoutes: () => registerStorageRoutes
|
|
26
308
|
});
|
|
27
309
|
module.exports = __toCommonJS(index_exports);
|
|
28
310
|
|
|
29
311
|
// src/local-storage-adapter.ts
|
|
30
312
|
var import_node_fs = require("fs");
|
|
31
313
|
var import_node_path = require("path");
|
|
314
|
+
var import_node_crypto = require("crypto");
|
|
32
315
|
var LocalStorageAdapter = class {
|
|
33
316
|
constructor(options) {
|
|
34
317
|
this.rootDir = options.rootDir;
|
|
318
|
+
this.partsDir = (0, import_node_path.join)(this.rootDir, ".parts");
|
|
319
|
+
this.baseUrl = options.baseUrl ?? "";
|
|
320
|
+
this.basePath = options.basePath ?? "/api/v1/storage";
|
|
321
|
+
this.signingSecret = options.signingSecret ?? (0, import_node_crypto.randomUUID)();
|
|
35
322
|
}
|
|
323
|
+
// ---------------------------------------------------------------------------
|
|
324
|
+
// Path helpers
|
|
325
|
+
// ---------------------------------------------------------------------------
|
|
36
326
|
resolvePath(key) {
|
|
327
|
+
if (key.includes("..")) {
|
|
328
|
+
throw new Error(`LocalStorageAdapter: path traversal not allowed (key="${key}")`);
|
|
329
|
+
}
|
|
37
330
|
return (0, import_node_path.join)(this.rootDir, key);
|
|
38
331
|
}
|
|
332
|
+
resolvePartPath(uploadId, partNumber) {
|
|
333
|
+
if (!/^[A-Za-z0-9_-]+$/.test(uploadId)) {
|
|
334
|
+
throw new Error(`LocalStorageAdapter: invalid uploadId "${uploadId}"`);
|
|
335
|
+
}
|
|
336
|
+
return (0, import_node_path.join)(this.partsDir, uploadId, String(partNumber).padStart(8, "0"));
|
|
337
|
+
}
|
|
338
|
+
// ---------------------------------------------------------------------------
|
|
339
|
+
// Basic file operations
|
|
340
|
+
// ---------------------------------------------------------------------------
|
|
39
341
|
async upload(key, data, _options) {
|
|
40
342
|
const filePath = this.resolvePath(key);
|
|
41
343
|
await import_node_fs.promises.mkdir((0, import_node_path.dirname)(filePath), { recursive: true });
|
|
42
344
|
if (data instanceof Buffer) {
|
|
43
345
|
await import_node_fs.promises.writeFile(filePath, data);
|
|
44
|
-
|
|
45
|
-
const chunks = [];
|
|
46
|
-
const reader = data.getReader();
|
|
47
|
-
let done = false;
|
|
48
|
-
while (!done) {
|
|
49
|
-
const result = await reader.read();
|
|
50
|
-
done = result.done;
|
|
51
|
-
if (result.value) chunks.push(result.value);
|
|
52
|
-
}
|
|
53
|
-
await import_node_fs.promises.writeFile(filePath, Buffer.concat(chunks));
|
|
346
|
+
return;
|
|
54
347
|
}
|
|
348
|
+
const chunks = [];
|
|
349
|
+
const reader = data.getReader();
|
|
350
|
+
let done = false;
|
|
351
|
+
while (!done) {
|
|
352
|
+
const result = await reader.read();
|
|
353
|
+
done = result.done;
|
|
354
|
+
if (result.value) chunks.push(result.value);
|
|
355
|
+
}
|
|
356
|
+
await import_node_fs.promises.writeFile(filePath, Buffer.concat(chunks));
|
|
55
357
|
}
|
|
56
358
|
async download(key) {
|
|
57
|
-
|
|
58
|
-
return import_node_fs.promises.readFile(filePath);
|
|
359
|
+
return import_node_fs.promises.readFile(this.resolvePath(key));
|
|
59
360
|
}
|
|
60
361
|
async delete(key) {
|
|
61
|
-
|
|
62
|
-
|
|
362
|
+
await import_node_fs.promises.unlink(this.resolvePath(key)).catch((err) => {
|
|
363
|
+
if (err && err.code === "ENOENT") return;
|
|
364
|
+
throw err;
|
|
365
|
+
});
|
|
63
366
|
}
|
|
64
367
|
async exists(key) {
|
|
65
368
|
try {
|
|
@@ -72,11 +375,7 @@ var LocalStorageAdapter = class {
|
|
|
72
375
|
async getInfo(key) {
|
|
73
376
|
const filePath = this.resolvePath(key);
|
|
74
377
|
const stat = await import_node_fs.promises.stat(filePath);
|
|
75
|
-
return {
|
|
76
|
-
key,
|
|
77
|
-
size: stat.size,
|
|
78
|
-
lastModified: stat.mtime
|
|
79
|
-
};
|
|
378
|
+
return { key, size: stat.size, lastModified: stat.mtime };
|
|
80
379
|
}
|
|
81
380
|
async list(prefix) {
|
|
82
381
|
const dirPath = this.resolvePath(prefix);
|
|
@@ -84,10 +383,10 @@ var LocalStorageAdapter = class {
|
|
|
84
383
|
const entries = await import_node_fs.promises.readdir(dirPath);
|
|
85
384
|
const results = [];
|
|
86
385
|
for (const entry of entries) {
|
|
386
|
+
if (entry.startsWith(".")) continue;
|
|
87
387
|
const fullKey = prefix ? `${prefix}/${entry}` : entry;
|
|
88
388
|
try {
|
|
89
|
-
|
|
90
|
-
results.push(info);
|
|
389
|
+
results.push(await this.getInfo(fullKey));
|
|
91
390
|
} catch {
|
|
92
391
|
}
|
|
93
392
|
}
|
|
@@ -96,74 +395,1052 @@ var LocalStorageAdapter = class {
|
|
|
96
395
|
return [];
|
|
97
396
|
}
|
|
98
397
|
}
|
|
398
|
+
// ---------------------------------------------------------------------------
|
|
399
|
+
// Presigned URL helpers
|
|
400
|
+
// ---------------------------------------------------------------------------
|
|
401
|
+
/**
|
|
402
|
+
* Sign an opaque token for the given payload.
|
|
403
|
+
* Format: base64url(JSON.stringify(payload)) + '.' + base64url(HMAC)
|
|
404
|
+
*/
|
|
405
|
+
signToken(payload) {
|
|
406
|
+
const b64 = Buffer.from(JSON.stringify(payload), "utf8").toString("base64url");
|
|
407
|
+
const sig = (0, import_node_crypto.createHmac)("sha256", this.signingSecret).update(b64).digest("base64url");
|
|
408
|
+
return `${b64}.${sig}`;
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Verify and decode a presigned token. Throws on invalid signature or
|
|
412
|
+
* expiration.
|
|
413
|
+
*/
|
|
414
|
+
verifyToken(token, expectedOp) {
|
|
415
|
+
const [b64, sig] = token.split(".");
|
|
416
|
+
if (!b64 || !sig) throw new Error("Invalid storage token format");
|
|
417
|
+
const expected = (0, import_node_crypto.createHmac)("sha256", this.signingSecret).update(b64).digest("base64url");
|
|
418
|
+
if (expected !== sig) throw new Error("Invalid storage token signature");
|
|
419
|
+
let payload;
|
|
420
|
+
try {
|
|
421
|
+
payload = JSON.parse(Buffer.from(b64, "base64url").toString("utf8"));
|
|
422
|
+
} catch {
|
|
423
|
+
throw new Error("Malformed storage token payload");
|
|
424
|
+
}
|
|
425
|
+
if (payload.op !== expectedOp) {
|
|
426
|
+
throw new Error(`Storage token op mismatch (expected="${expectedOp}", actual="${payload.op}")`);
|
|
427
|
+
}
|
|
428
|
+
if (Date.now() / 1e3 > payload.exp) {
|
|
429
|
+
throw new Error("Storage token expired");
|
|
430
|
+
}
|
|
431
|
+
return payload;
|
|
432
|
+
}
|
|
433
|
+
async getPresignedUpload(key, expiresIn, options) {
|
|
434
|
+
const exp = Math.floor(Date.now() / 1e3) + Math.max(1, expiresIn);
|
|
435
|
+
const token = this.signToken({ k: key, ct: options?.contentType, exp, op: "put" });
|
|
436
|
+
return {
|
|
437
|
+
uploadUrl: `${this.baseUrl}${this.basePath}/_local/raw/${token}`,
|
|
438
|
+
method: "PUT",
|
|
439
|
+
headers: options?.contentType ? { "content-type": options.contentType } : { "content-type": "application/octet-stream" },
|
|
440
|
+
expiresIn,
|
|
441
|
+
downloadUrl: `${this.baseUrl}${this.basePath}/_local/file/${encodeURIComponent(key)}`
|
|
442
|
+
};
|
|
443
|
+
}
|
|
444
|
+
async getPresignedDownload(key, expiresIn) {
|
|
445
|
+
const exp = Math.floor(Date.now() / 1e3) + Math.max(1, expiresIn);
|
|
446
|
+
const token = this.signToken({ k: key, exp, op: "get" });
|
|
447
|
+
return {
|
|
448
|
+
downloadUrl: `${this.baseUrl}${this.basePath}/_local/raw/${token}`,
|
|
449
|
+
expiresIn
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
async getSignedUrl(key, expiresIn) {
|
|
453
|
+
const desc = await this.getPresignedDownload(key, expiresIn);
|
|
454
|
+
return desc.downloadUrl;
|
|
455
|
+
}
|
|
456
|
+
// ---------------------------------------------------------------------------
|
|
457
|
+
// Chunked / multipart upload
|
|
458
|
+
// ---------------------------------------------------------------------------
|
|
459
|
+
async initiateChunkedUpload(key, options) {
|
|
460
|
+
const uploadId = (0, import_node_crypto.randomUUID)().replace(/-/g, "");
|
|
461
|
+
const dir = (0, import_node_path.join)(this.partsDir, uploadId);
|
|
462
|
+
await import_node_fs.promises.mkdir(dir, { recursive: true });
|
|
463
|
+
const meta = {
|
|
464
|
+
key,
|
|
465
|
+
contentType: options?.contentType,
|
|
466
|
+
metadata: options?.metadata,
|
|
467
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
468
|
+
};
|
|
469
|
+
await import_node_fs.promises.writeFile((0, import_node_path.join)(dir, "_meta.json"), JSON.stringify(meta), "utf8");
|
|
470
|
+
return uploadId;
|
|
471
|
+
}
|
|
472
|
+
async uploadChunk(uploadId, partNumber, data) {
|
|
473
|
+
if (!Number.isInteger(partNumber) || partNumber < 1) {
|
|
474
|
+
throw new Error(`uploadChunk: partNumber must be a positive integer (got ${partNumber})`);
|
|
475
|
+
}
|
|
476
|
+
const partPath = this.resolvePartPath(uploadId, partNumber);
|
|
477
|
+
await import_node_fs.promises.mkdir((0, import_node_path.dirname)(partPath), { recursive: true });
|
|
478
|
+
await import_node_fs.promises.writeFile(partPath, data);
|
|
479
|
+
const { createHash } = await import("crypto");
|
|
480
|
+
return createHash("md5").update(data).digest("hex");
|
|
481
|
+
}
|
|
482
|
+
async completeChunkedUpload(uploadId, parts) {
|
|
483
|
+
const dir = (0, import_node_path.join)(this.partsDir, uploadId);
|
|
484
|
+
let meta = {};
|
|
485
|
+
try {
|
|
486
|
+
meta = JSON.parse(await import_node_fs.promises.readFile((0, import_node_path.join)(dir, "_meta.json"), "utf8"));
|
|
487
|
+
} catch {
|
|
488
|
+
throw new Error(`Upload session "${uploadId}" not found`);
|
|
489
|
+
}
|
|
490
|
+
const targetKey = meta.key;
|
|
491
|
+
if (!targetKey) {
|
|
492
|
+
throw new Error(`Upload session "${uploadId}" missing target key`);
|
|
493
|
+
}
|
|
494
|
+
const sortedParts = [...parts].sort((a, b) => a.partNumber - b.partNumber);
|
|
495
|
+
const finalPath = this.resolvePath(targetKey);
|
|
496
|
+
await import_node_fs.promises.mkdir((0, import_node_path.dirname)(finalPath), { recursive: true });
|
|
497
|
+
const out = (0, import_node_fs.createWriteStream)(finalPath);
|
|
498
|
+
try {
|
|
499
|
+
for (const p of sortedParts) {
|
|
500
|
+
const partPath = this.resolvePartPath(uploadId, p.partNumber);
|
|
501
|
+
await new Promise((resolve, reject) => {
|
|
502
|
+
const inp = (0, import_node_fs.createReadStream)(partPath);
|
|
503
|
+
inp.on("error", reject);
|
|
504
|
+
inp.on("end", () => resolve());
|
|
505
|
+
inp.pipe(out, { end: false });
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
} finally {
|
|
509
|
+
await new Promise((resolve) => out.end(() => resolve()));
|
|
510
|
+
}
|
|
511
|
+
await import_node_fs.promises.rm(dir, { recursive: true, force: true });
|
|
512
|
+
return targetKey;
|
|
513
|
+
}
|
|
514
|
+
async abortChunkedUpload(uploadId) {
|
|
515
|
+
await import_node_fs.promises.rm((0, import_node_path.join)(this.partsDir, uploadId), { recursive: true, force: true });
|
|
516
|
+
}
|
|
99
517
|
};
|
|
100
518
|
|
|
101
519
|
// src/storage-service-plugin.ts
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
this.
|
|
520
|
+
init_s3_storage_adapter();
|
|
521
|
+
|
|
522
|
+
// src/metadata-store.ts
|
|
523
|
+
var StorageMetadataStore = class {
|
|
524
|
+
constructor(engine) {
|
|
525
|
+
this.engine = engine;
|
|
526
|
+
this.files = /* @__PURE__ */ new Map();
|
|
527
|
+
this.sessions = /* @__PURE__ */ new Map();
|
|
108
528
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
529
|
+
// ---------------------------------------------------------------------------
|
|
530
|
+
// Files
|
|
531
|
+
// ---------------------------------------------------------------------------
|
|
532
|
+
async createFile(rec) {
|
|
533
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
534
|
+
const full = { created_at: now, updated_at: now, ...rec };
|
|
535
|
+
this.files.set(full.id, full);
|
|
536
|
+
if (this.engine) {
|
|
537
|
+
try {
|
|
538
|
+
await this.engine.insert("sys_file", full);
|
|
539
|
+
} catch {
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
return full;
|
|
543
|
+
}
|
|
544
|
+
async getFile(id) {
|
|
545
|
+
if (this.engine) {
|
|
546
|
+
try {
|
|
547
|
+
const found = await this.engine.findOne("sys_file", { where: { id } });
|
|
548
|
+
if (found) return found;
|
|
549
|
+
} catch {
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
return this.files.get(id) ?? null;
|
|
553
|
+
}
|
|
554
|
+
async updateFile(id, patch) {
|
|
555
|
+
const existing = await this.getFile(id);
|
|
556
|
+
if (!existing) return null;
|
|
557
|
+
const merged = { ...existing, ...patch, id, updated_at: (/* @__PURE__ */ new Date()).toISOString() };
|
|
558
|
+
this.files.set(id, merged);
|
|
559
|
+
if (this.engine) {
|
|
560
|
+
try {
|
|
561
|
+
await this.engine.update("sys_file", merged, { where: { id } });
|
|
562
|
+
} catch {
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
return merged;
|
|
566
|
+
}
|
|
567
|
+
async deleteFile(id) {
|
|
568
|
+
this.files.delete(id);
|
|
569
|
+
if (this.engine) {
|
|
570
|
+
try {
|
|
571
|
+
await this.engine.delete("sys_file", { where: { id } });
|
|
572
|
+
} catch {
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
// ---------------------------------------------------------------------------
|
|
577
|
+
// Upload sessions
|
|
578
|
+
// ---------------------------------------------------------------------------
|
|
579
|
+
async createSession(rec) {
|
|
580
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
581
|
+
const full = {
|
|
582
|
+
uploaded_chunks: 0,
|
|
583
|
+
uploaded_size: 0,
|
|
584
|
+
parts: "[]",
|
|
585
|
+
started_at: now,
|
|
586
|
+
updated_at: now,
|
|
587
|
+
...rec
|
|
588
|
+
};
|
|
589
|
+
this.sessions.set(full.id, full);
|
|
590
|
+
if (this.engine) {
|
|
591
|
+
try {
|
|
592
|
+
await this.engine.insert("sys_upload_session", full);
|
|
593
|
+
} catch {
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
return full;
|
|
597
|
+
}
|
|
598
|
+
async getSession(id) {
|
|
599
|
+
if (this.engine) {
|
|
600
|
+
try {
|
|
601
|
+
const found = await this.engine.findOne("sys_upload_session", { where: { id } });
|
|
602
|
+
if (found) return found;
|
|
603
|
+
} catch {
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
return this.sessions.get(id) ?? null;
|
|
607
|
+
}
|
|
608
|
+
async updateSession(id, patch) {
|
|
609
|
+
const existing = await this.getSession(id);
|
|
610
|
+
if (!existing) return null;
|
|
611
|
+
const merged = {
|
|
612
|
+
...existing,
|
|
613
|
+
...patch,
|
|
614
|
+
id,
|
|
615
|
+
updated_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
616
|
+
};
|
|
617
|
+
this.sessions.set(id, merged);
|
|
618
|
+
if (this.engine) {
|
|
619
|
+
try {
|
|
620
|
+
await this.engine.update("sys_upload_session", merged, { where: { id } });
|
|
621
|
+
} catch {
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
return merged;
|
|
625
|
+
}
|
|
626
|
+
async deleteSession(id) {
|
|
627
|
+
this.sessions.delete(id);
|
|
628
|
+
if (this.engine) {
|
|
629
|
+
try {
|
|
630
|
+
await this.engine.delete("sys_upload_session", { where: { id } });
|
|
631
|
+
} catch {
|
|
632
|
+
}
|
|
115
633
|
}
|
|
116
|
-
const rootDir = this.options.local?.rootDir ?? "./storage";
|
|
117
|
-
const storage = new LocalStorageAdapter({ rootDir });
|
|
118
|
-
ctx.registerService("file-storage", storage);
|
|
119
|
-
ctx.logger.info(`StorageServicePlugin: registered local storage adapter (root: ${rootDir})`);
|
|
120
634
|
}
|
|
121
635
|
};
|
|
122
636
|
|
|
123
|
-
// src/
|
|
124
|
-
var
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
637
|
+
// src/storage-routes.ts
|
|
638
|
+
var import_node_crypto2 = require("crypto");
|
|
639
|
+
function registerStorageRoutes(httpServer, storage, store, opts = {}) {
|
|
640
|
+
const basePath = opts.basePath ?? "/api/v1/storage";
|
|
641
|
+
const presignedTtl = opts.presignedTtl ?? 3600;
|
|
642
|
+
const sessionTtl = opts.sessionTtl ?? 86400;
|
|
643
|
+
httpServer.post(`${basePath}/upload/presigned`, async (req, res) => {
|
|
644
|
+
try {
|
|
645
|
+
const { filename, mimeType, size, scope, bucket } = req.body ?? {};
|
|
646
|
+
if (!filename || !mimeType || size == null) {
|
|
647
|
+
res.status(400).json({ error: "filename, mimeType, and size are required" });
|
|
648
|
+
return;
|
|
649
|
+
}
|
|
650
|
+
const fileId = (0, import_node_crypto2.randomUUID)();
|
|
651
|
+
const key = buildKey(scope ?? "user", fileId, filename);
|
|
652
|
+
await store.createFile({
|
|
653
|
+
id: fileId,
|
|
654
|
+
key,
|
|
655
|
+
name: filename,
|
|
656
|
+
mime_type: mimeType,
|
|
657
|
+
size,
|
|
658
|
+
scope: scope ?? "user",
|
|
659
|
+
bucket,
|
|
660
|
+
acl: "private",
|
|
661
|
+
status: "pending"
|
|
662
|
+
});
|
|
663
|
+
let uploadUrl;
|
|
664
|
+
let method = "PUT";
|
|
665
|
+
let headers = { "content-type": mimeType };
|
|
666
|
+
let expiresIn = presignedTtl;
|
|
667
|
+
if (storage.getPresignedUpload) {
|
|
668
|
+
const desc = await storage.getPresignedUpload(key, presignedTtl, { contentType: mimeType });
|
|
669
|
+
uploadUrl = desc.uploadUrl;
|
|
670
|
+
method = desc.method;
|
|
671
|
+
if (desc.headers) headers = desc.headers;
|
|
672
|
+
expiresIn = desc.expiresIn;
|
|
673
|
+
} else {
|
|
674
|
+
uploadUrl = `${basePath}/_local/raw/${fileId}`;
|
|
675
|
+
}
|
|
676
|
+
res.json({
|
|
677
|
+
data: {
|
|
678
|
+
uploadUrl,
|
|
679
|
+
method,
|
|
680
|
+
headers,
|
|
681
|
+
fileId,
|
|
682
|
+
expiresIn,
|
|
683
|
+
downloadUrl: `${basePath}/files/${fileId}/url`
|
|
684
|
+
}
|
|
685
|
+
});
|
|
686
|
+
} catch (err) {
|
|
687
|
+
res.status(500).json({ error: err.message ?? "Internal error" });
|
|
688
|
+
}
|
|
689
|
+
});
|
|
690
|
+
httpServer.post(`${basePath}/upload/complete`, async (req, res) => {
|
|
691
|
+
try {
|
|
692
|
+
const { fileId, eTag } = req.body ?? {};
|
|
693
|
+
if (!fileId) {
|
|
694
|
+
res.status(400).json({ error: "fileId is required" });
|
|
695
|
+
return;
|
|
696
|
+
}
|
|
697
|
+
const file = await store.getFile(fileId);
|
|
698
|
+
if (!file) {
|
|
699
|
+
res.status(404).json({ error: "File not found" });
|
|
700
|
+
return;
|
|
701
|
+
}
|
|
702
|
+
const updated = await store.updateFile(fileId, {
|
|
703
|
+
status: "committed",
|
|
704
|
+
etag: eTag ?? void 0
|
|
705
|
+
});
|
|
706
|
+
res.json({
|
|
707
|
+
data: {
|
|
708
|
+
path: updated.key,
|
|
709
|
+
name: updated.name,
|
|
710
|
+
size: updated.size ?? 0,
|
|
711
|
+
mimeType: updated.mime_type ?? "application/octet-stream",
|
|
712
|
+
lastModified: updated.updated_at ?? (/* @__PURE__ */ new Date()).toISOString(),
|
|
713
|
+
created: updated.created_at ?? (/* @__PURE__ */ new Date()).toISOString(),
|
|
714
|
+
etag: updated.etag
|
|
715
|
+
}
|
|
716
|
+
});
|
|
717
|
+
} catch (err) {
|
|
718
|
+
res.status(500).json({ error: err.message ?? "Internal error" });
|
|
719
|
+
}
|
|
720
|
+
});
|
|
721
|
+
httpServer.post(`${basePath}/upload/chunked`, async (req, res) => {
|
|
722
|
+
try {
|
|
723
|
+
const { filename, mimeType, totalSize, chunkSize: reqChunkSize, scope, bucket, metadata } = req.body ?? {};
|
|
724
|
+
if (!filename || !mimeType || !totalSize) {
|
|
725
|
+
res.status(400).json({ error: "filename, mimeType, and totalSize are required" });
|
|
726
|
+
return;
|
|
727
|
+
}
|
|
728
|
+
const chunkSize = Math.max(reqChunkSize ?? 5242880, 5242880);
|
|
729
|
+
const totalChunks = Math.ceil(totalSize / chunkSize);
|
|
730
|
+
const fileId = (0, import_node_crypto2.randomUUID)();
|
|
731
|
+
const key = buildKey(scope ?? "user", fileId, filename);
|
|
732
|
+
await store.createFile({
|
|
733
|
+
id: fileId,
|
|
734
|
+
key,
|
|
735
|
+
name: filename,
|
|
736
|
+
mime_type: mimeType,
|
|
737
|
+
size: totalSize,
|
|
738
|
+
scope: scope ?? "user",
|
|
739
|
+
bucket,
|
|
740
|
+
acl: "private",
|
|
741
|
+
status: "pending",
|
|
742
|
+
metadata: metadata ? JSON.stringify(metadata) : void 0
|
|
743
|
+
});
|
|
744
|
+
let backendUploadId;
|
|
745
|
+
if (storage.initiateChunkedUpload) {
|
|
746
|
+
backendUploadId = await storage.initiateChunkedUpload(key, { contentType: mimeType, metadata });
|
|
747
|
+
if ("setUploadKey" in storage && typeof storage.setUploadKey === "function") {
|
|
748
|
+
storage.setUploadKey(backendUploadId, key);
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
const uploadId = backendUploadId ?? (0, import_node_crypto2.randomUUID)().replace(/-/g, "");
|
|
752
|
+
const resumeToken = (0, import_node_crypto2.randomUUID)();
|
|
753
|
+
const expiresAt = new Date(Date.now() + sessionTtl * 1e3).toISOString();
|
|
754
|
+
await store.createSession({
|
|
755
|
+
id: uploadId,
|
|
756
|
+
file_id: fileId,
|
|
757
|
+
key,
|
|
758
|
+
filename,
|
|
759
|
+
mime_type: mimeType,
|
|
760
|
+
total_size: totalSize,
|
|
761
|
+
chunk_size: chunkSize,
|
|
762
|
+
total_chunks: totalChunks,
|
|
763
|
+
resume_token: resumeToken,
|
|
764
|
+
backend_upload_id: backendUploadId,
|
|
765
|
+
scope: scope ?? "user",
|
|
766
|
+
bucket,
|
|
767
|
+
metadata: metadata ? JSON.stringify(metadata) : void 0,
|
|
768
|
+
status: "in_progress",
|
|
769
|
+
expires_at: expiresAt
|
|
770
|
+
});
|
|
771
|
+
res.json({
|
|
772
|
+
data: {
|
|
773
|
+
uploadId,
|
|
774
|
+
resumeToken,
|
|
775
|
+
fileId,
|
|
776
|
+
totalChunks,
|
|
777
|
+
chunkSize,
|
|
778
|
+
expiresAt
|
|
779
|
+
}
|
|
780
|
+
});
|
|
781
|
+
} catch (err) {
|
|
782
|
+
res.status(500).json({ error: err.message ?? "Internal error" });
|
|
783
|
+
}
|
|
784
|
+
});
|
|
785
|
+
httpServer.put(`${basePath}/upload/chunked/:uploadId/chunk/:chunkIndex`, async (req, res) => {
|
|
786
|
+
try {
|
|
787
|
+
const { uploadId, chunkIndex: chunkIndexStr } = req.params;
|
|
788
|
+
const chunkIndex = parseInt(chunkIndexStr, 10);
|
|
789
|
+
if (!uploadId || isNaN(chunkIndex)) {
|
|
790
|
+
res.status(400).json({ error: "uploadId and chunkIndex are required" });
|
|
791
|
+
return;
|
|
792
|
+
}
|
|
793
|
+
const session = await store.getSession(uploadId);
|
|
794
|
+
if (!session) {
|
|
795
|
+
res.status(404).json({ error: "Upload session not found" });
|
|
796
|
+
return;
|
|
797
|
+
}
|
|
798
|
+
const token = req.headers["x-resume-token"] ?? "";
|
|
799
|
+
if (session.resume_token && token !== session.resume_token) {
|
|
800
|
+
res.status(403).json({ error: "Invalid resume token" });
|
|
801
|
+
return;
|
|
802
|
+
}
|
|
803
|
+
let data;
|
|
804
|
+
if (req.rawBody) {
|
|
805
|
+
data = await req.rawBody();
|
|
806
|
+
} else if (Buffer.isBuffer(req.body)) {
|
|
807
|
+
data = req.body;
|
|
808
|
+
} else if (req.body instanceof ArrayBuffer) {
|
|
809
|
+
data = Buffer.from(req.body);
|
|
810
|
+
} else {
|
|
811
|
+
res.status(400).json({ error: "Binary body required" });
|
|
812
|
+
return;
|
|
813
|
+
}
|
|
814
|
+
let eTag = "";
|
|
815
|
+
if (storage.uploadChunk) {
|
|
816
|
+
eTag = await storage.uploadChunk(uploadId, chunkIndex + 1, data);
|
|
817
|
+
}
|
|
818
|
+
const currentParts = JSON.parse(session.parts ?? "[]");
|
|
819
|
+
currentParts.push({ chunkIndex, eTag });
|
|
820
|
+
const uploadedChunks = (session.uploaded_chunks ?? 0) + 1;
|
|
821
|
+
const uploadedSize = (session.uploaded_size ?? 0) + data.byteLength;
|
|
822
|
+
await store.updateSession(uploadId, {
|
|
823
|
+
uploaded_chunks: uploadedChunks,
|
|
824
|
+
uploaded_size: uploadedSize,
|
|
825
|
+
parts: JSON.stringify(currentParts)
|
|
826
|
+
});
|
|
827
|
+
res.json({
|
|
828
|
+
data: {
|
|
829
|
+
chunkIndex,
|
|
830
|
+
eTag,
|
|
831
|
+
bytesReceived: data.byteLength
|
|
832
|
+
}
|
|
833
|
+
});
|
|
834
|
+
} catch (err) {
|
|
835
|
+
res.status(500).json({ error: err.message ?? "Internal error" });
|
|
836
|
+
}
|
|
837
|
+
});
|
|
838
|
+
httpServer.post(`${basePath}/upload/chunked/:uploadId/complete`, async (req, res) => {
|
|
839
|
+
try {
|
|
840
|
+
const { uploadId } = req.params;
|
|
841
|
+
const session = await store.getSession(uploadId);
|
|
842
|
+
if (!session) {
|
|
843
|
+
res.status(404).json({ error: "Upload session not found" });
|
|
844
|
+
return;
|
|
845
|
+
}
|
|
846
|
+
await store.updateSession(uploadId, { status: "completing" });
|
|
847
|
+
const partsFromBody = req.body?.parts ?? [];
|
|
848
|
+
const partsForBackend = partsFromBody.map((p) => ({
|
|
849
|
+
partNumber: p.chunkIndex + 1,
|
|
850
|
+
eTag: p.eTag
|
|
851
|
+
}));
|
|
852
|
+
let finalKey = session.key;
|
|
853
|
+
if (storage.completeChunkedUpload) {
|
|
854
|
+
finalKey = await storage.completeChunkedUpload(uploadId, partsForBackend);
|
|
855
|
+
}
|
|
856
|
+
await store.updateFile(session.file_id, { status: "committed", key: finalKey });
|
|
857
|
+
await store.updateSession(uploadId, { status: "completed" });
|
|
858
|
+
res.json({
|
|
859
|
+
data: {
|
|
860
|
+
fileId: session.file_id,
|
|
861
|
+
key: finalKey,
|
|
862
|
+
size: session.total_size,
|
|
863
|
+
mimeType: session.mime_type ?? "application/octet-stream",
|
|
864
|
+
url: `${basePath}/files/${session.file_id}/url`
|
|
865
|
+
}
|
|
866
|
+
});
|
|
867
|
+
} catch (err) {
|
|
868
|
+
res.status(500).json({ error: err.message ?? "Internal error" });
|
|
869
|
+
}
|
|
870
|
+
});
|
|
871
|
+
httpServer.get(`${basePath}/upload/chunked/:uploadId/progress`, async (req, res) => {
|
|
872
|
+
try {
|
|
873
|
+
const { uploadId } = req.params;
|
|
874
|
+
const session = await store.getSession(uploadId);
|
|
875
|
+
if (!session) {
|
|
876
|
+
res.status(404).json({ error: "Upload session not found" });
|
|
877
|
+
return;
|
|
878
|
+
}
|
|
879
|
+
const uploadedChunks = session.uploaded_chunks ?? 0;
|
|
880
|
+
const uploadedSize = session.uploaded_size ?? 0;
|
|
881
|
+
const percentComplete = session.total_size > 0 ? Math.min(100, Math.round(uploadedSize / session.total_size * 100)) : 0;
|
|
882
|
+
res.json({
|
|
883
|
+
data: {
|
|
884
|
+
uploadId: session.id,
|
|
885
|
+
fileId: session.file_id,
|
|
886
|
+
filename: session.filename,
|
|
887
|
+
totalSize: session.total_size,
|
|
888
|
+
uploadedSize,
|
|
889
|
+
totalChunks: session.total_chunks,
|
|
890
|
+
uploadedChunks,
|
|
891
|
+
percentComplete,
|
|
892
|
+
status: session.status,
|
|
893
|
+
startedAt: session.started_at,
|
|
894
|
+
expiresAt: session.expires_at
|
|
895
|
+
}
|
|
896
|
+
});
|
|
897
|
+
} catch (err) {
|
|
898
|
+
res.status(500).json({ error: err.message ?? "Internal error" });
|
|
899
|
+
}
|
|
900
|
+
});
|
|
901
|
+
httpServer.get(`${basePath}/files/:fileId/url`, async (req, res) => {
|
|
902
|
+
try {
|
|
903
|
+
const { fileId } = req.params;
|
|
904
|
+
const file = await store.getFile(fileId);
|
|
905
|
+
if (!file || file.status !== "committed") {
|
|
906
|
+
res.status(404).json({ error: "File not found or not committed" });
|
|
907
|
+
return;
|
|
908
|
+
}
|
|
909
|
+
let url;
|
|
910
|
+
if (storage.getPresignedDownload) {
|
|
911
|
+
const desc = await storage.getPresignedDownload(file.key, presignedTtl);
|
|
912
|
+
url = desc.downloadUrl;
|
|
913
|
+
} else if (storage.getSignedUrl) {
|
|
914
|
+
url = await storage.getSignedUrl(file.key, presignedTtl);
|
|
915
|
+
} else {
|
|
916
|
+
url = `${basePath}/_local/file/${encodeURIComponent(file.key)}`;
|
|
917
|
+
}
|
|
918
|
+
res.json({ url });
|
|
919
|
+
} catch (err) {
|
|
920
|
+
res.status(500).json({ error: err.message ?? "Internal error" });
|
|
921
|
+
}
|
|
922
|
+
});
|
|
923
|
+
httpServer.get(`${basePath}/files/:fileId`, async (req, res) => {
|
|
924
|
+
try {
|
|
925
|
+
const { fileId } = req.params;
|
|
926
|
+
const file = await store.getFile(fileId);
|
|
927
|
+
if (!file || file.status !== "committed") {
|
|
928
|
+
res.status(404).json({ error: "File not found or not committed" });
|
|
929
|
+
return;
|
|
930
|
+
}
|
|
931
|
+
let url;
|
|
932
|
+
if (storage.getPresignedDownload) {
|
|
933
|
+
const desc = await storage.getPresignedDownload(file.key, presignedTtl);
|
|
934
|
+
url = desc.downloadUrl;
|
|
935
|
+
} else if (storage.getSignedUrl) {
|
|
936
|
+
url = await storage.getSignedUrl(file.key, presignedTtl);
|
|
937
|
+
} else {
|
|
938
|
+
url = `${basePath}/_local/file/${encodeURIComponent(file.key)}`;
|
|
939
|
+
}
|
|
940
|
+
res.status(302).header("Location", url).send("");
|
|
941
|
+
} catch (err) {
|
|
942
|
+
res.status(500).json({ error: err.message ?? "Internal error" });
|
|
943
|
+
}
|
|
944
|
+
});
|
|
945
|
+
httpServer.put(`${basePath}/_local/raw/:token`, async (req, res) => {
|
|
946
|
+
try {
|
|
947
|
+
const { token } = req.params;
|
|
948
|
+
const localAdapter = storage;
|
|
949
|
+
if (!localAdapter.verifyToken) {
|
|
950
|
+
res.status(501).json({ error: "Presigned raw upload not supported by this adapter" });
|
|
951
|
+
return;
|
|
952
|
+
}
|
|
953
|
+
const payload = localAdapter.verifyToken(token, "put");
|
|
954
|
+
let data;
|
|
955
|
+
if (req.rawBody) {
|
|
956
|
+
data = await req.rawBody();
|
|
957
|
+
} else if (Buffer.isBuffer(req.body)) {
|
|
958
|
+
data = req.body;
|
|
959
|
+
} else {
|
|
960
|
+
res.status(400).json({ error: "Binary body required" });
|
|
961
|
+
return;
|
|
962
|
+
}
|
|
963
|
+
await storage.upload(payload.k, data, { contentType: payload.ct });
|
|
964
|
+
res.json({ ok: true, key: payload.k });
|
|
965
|
+
} catch (err) {
|
|
966
|
+
const statusCode = err.message?.includes("expired") || err.message?.includes("signature") ? 403 : 500;
|
|
967
|
+
res.status(statusCode).json({ error: err.message ?? "Upload failed" });
|
|
968
|
+
}
|
|
969
|
+
});
|
|
970
|
+
httpServer.get(`${basePath}/_local/raw/:token`, async (req, res) => {
|
|
971
|
+
try {
|
|
972
|
+
const { token } = req.params;
|
|
973
|
+
const localAdapter = storage;
|
|
974
|
+
if (!localAdapter.verifyToken) {
|
|
975
|
+
res.status(501).json({ error: "Presigned download not supported by this adapter" });
|
|
976
|
+
return;
|
|
977
|
+
}
|
|
978
|
+
const payload = localAdapter.verifyToken(token, "get");
|
|
979
|
+
const data = await storage.download(payload.k);
|
|
980
|
+
res.header("content-type", payload.ct ?? "application/octet-stream");
|
|
981
|
+
res.header("content-length", String(data.byteLength));
|
|
982
|
+
res.send(data);
|
|
983
|
+
} catch (err) {
|
|
984
|
+
const statusCode = err.message?.includes("expired") || err.message?.includes("signature") ? 403 : 500;
|
|
985
|
+
res.status(statusCode).json({ error: err.message ?? "Download failed" });
|
|
986
|
+
}
|
|
987
|
+
});
|
|
988
|
+
}
|
|
989
|
+
function buildKey(scope, fileId, filename) {
|
|
990
|
+
const ext = filename.includes(".") ? "." + filename.split(".").pop() : "";
|
|
991
|
+
return `${scope}/${fileId}${ext}`;
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
// src/objects/system-file.object.ts
|
|
995
|
+
var import_data = require("@objectstack/spec/data");
|
|
996
|
+
var SystemFile = import_data.ObjectSchema.create({
|
|
997
|
+
name: "sys_file",
|
|
998
|
+
label: "System File",
|
|
999
|
+
pluralLabel: "System Files",
|
|
1000
|
+
icon: "file",
|
|
1001
|
+
description: "Storage service file metadata (fileId \u2194 key mapping)",
|
|
1002
|
+
titleFormat: "{name}",
|
|
1003
|
+
compactLayout: ["name", "mime_type", "size", "status", "created_at"],
|
|
1004
|
+
fields: {
|
|
1005
|
+
id: import_data.Field.text({
|
|
1006
|
+
label: "File ID",
|
|
1007
|
+
required: true,
|
|
1008
|
+
readonly: true
|
|
1009
|
+
}),
|
|
1010
|
+
key: import_data.Field.text({
|
|
1011
|
+
label: "Storage Key",
|
|
1012
|
+
required: true,
|
|
1013
|
+
searchable: true
|
|
1014
|
+
}),
|
|
1015
|
+
name: import_data.Field.text({
|
|
1016
|
+
label: "File Name",
|
|
1017
|
+
required: true,
|
|
1018
|
+
searchable: true
|
|
1019
|
+
}),
|
|
1020
|
+
mime_type: import_data.Field.text({
|
|
1021
|
+
label: "MIME Type"
|
|
1022
|
+
}),
|
|
1023
|
+
size: import_data.Field.number({
|
|
1024
|
+
label: "Size (bytes)"
|
|
1025
|
+
}),
|
|
1026
|
+
scope: import_data.Field.select({
|
|
1027
|
+
label: "Scope",
|
|
1028
|
+
options: [
|
|
1029
|
+
{ label: "User", value: "user" },
|
|
1030
|
+
{ label: "Tenant", value: "tenant" },
|
|
1031
|
+
{ label: "Public", value: "public" },
|
|
1032
|
+
{ label: "Private", value: "private" },
|
|
1033
|
+
{ label: "Temp", value: "temp" }
|
|
1034
|
+
]
|
|
1035
|
+
}),
|
|
1036
|
+
bucket: import_data.Field.text({
|
|
1037
|
+
label: "Bucket"
|
|
1038
|
+
}),
|
|
1039
|
+
acl: import_data.Field.select({
|
|
1040
|
+
label: "ACL",
|
|
1041
|
+
options: [
|
|
1042
|
+
{ label: "Private", value: "private" },
|
|
1043
|
+
{ label: "Public Read", value: "public_read" }
|
|
1044
|
+
]
|
|
1045
|
+
}),
|
|
1046
|
+
status: import_data.Field.select({
|
|
1047
|
+
label: "Status",
|
|
1048
|
+
required: true,
|
|
1049
|
+
options: [
|
|
1050
|
+
{ label: "Pending Upload", value: "pending" },
|
|
1051
|
+
{ label: "Committed", value: "committed" },
|
|
1052
|
+
{ label: "Deleted", value: "deleted" }
|
|
1053
|
+
]
|
|
1054
|
+
}),
|
|
1055
|
+
etag: import_data.Field.text({
|
|
1056
|
+
label: "ETag"
|
|
1057
|
+
}),
|
|
1058
|
+
owner_id: import_data.Field.text({
|
|
1059
|
+
label: "Owner ID"
|
|
1060
|
+
}),
|
|
1061
|
+
metadata: import_data.Field.text({
|
|
1062
|
+
label: "Metadata (JSON)"
|
|
1063
|
+
}),
|
|
1064
|
+
created_at: import_data.Field.datetime({
|
|
1065
|
+
label: "Created At"
|
|
1066
|
+
}),
|
|
1067
|
+
updated_at: import_data.Field.datetime({
|
|
1068
|
+
label: "Updated At"
|
|
1069
|
+
})
|
|
1070
|
+
}
|
|
1071
|
+
});
|
|
1072
|
+
|
|
1073
|
+
// src/objects/system-upload-session.object.ts
|
|
1074
|
+
var import_data2 = require("@objectstack/spec/data");
|
|
1075
|
+
var SystemUploadSession = import_data2.ObjectSchema.create({
|
|
1076
|
+
name: "sys_upload_session",
|
|
1077
|
+
label: "System Upload Session",
|
|
1078
|
+
pluralLabel: "System Upload Sessions",
|
|
1079
|
+
icon: "upload-cloud",
|
|
1080
|
+
description: "Resumable multipart upload sessions tracked by service-storage",
|
|
1081
|
+
titleFormat: "{filename}",
|
|
1082
|
+
compactLayout: ["filename", "status", "uploaded_chunks", "total_chunks", "expires_at"],
|
|
1083
|
+
fields: {
|
|
1084
|
+
id: import_data2.Field.text({
|
|
1085
|
+
label: "Upload Session ID",
|
|
1086
|
+
required: true,
|
|
1087
|
+
readonly: true
|
|
1088
|
+
}),
|
|
1089
|
+
file_id: import_data2.Field.text({
|
|
1090
|
+
label: "File ID",
|
|
1091
|
+
required: true
|
|
1092
|
+
}),
|
|
1093
|
+
key: import_data2.Field.text({
|
|
1094
|
+
label: "Storage Key",
|
|
1095
|
+
required: true
|
|
1096
|
+
}),
|
|
1097
|
+
filename: import_data2.Field.text({
|
|
1098
|
+
label: "Filename",
|
|
1099
|
+
required: true
|
|
1100
|
+
}),
|
|
1101
|
+
mime_type: import_data2.Field.text({
|
|
1102
|
+
label: "MIME Type"
|
|
1103
|
+
}),
|
|
1104
|
+
total_size: import_data2.Field.number({
|
|
1105
|
+
label: "Total Size (bytes)",
|
|
1106
|
+
required: true
|
|
1107
|
+
}),
|
|
1108
|
+
chunk_size: import_data2.Field.number({
|
|
1109
|
+
label: "Chunk Size (bytes)",
|
|
1110
|
+
required: true
|
|
1111
|
+
}),
|
|
1112
|
+
total_chunks: import_data2.Field.number({
|
|
1113
|
+
label: "Total Chunks",
|
|
1114
|
+
required: true
|
|
1115
|
+
}),
|
|
1116
|
+
uploaded_chunks: import_data2.Field.number({
|
|
1117
|
+
label: "Uploaded Chunks"
|
|
1118
|
+
}),
|
|
1119
|
+
uploaded_size: import_data2.Field.number({
|
|
1120
|
+
label: "Uploaded Size (bytes)"
|
|
1121
|
+
}),
|
|
1122
|
+
parts: import_data2.Field.text({
|
|
1123
|
+
label: "Uploaded Parts (JSON)"
|
|
1124
|
+
}),
|
|
1125
|
+
resume_token: import_data2.Field.text({
|
|
1126
|
+
label: "Resume Token"
|
|
1127
|
+
}),
|
|
1128
|
+
backend_upload_id: import_data2.Field.text({
|
|
1129
|
+
label: "Backend Upload ID"
|
|
1130
|
+
}),
|
|
1131
|
+
scope: import_data2.Field.text({
|
|
1132
|
+
label: "Scope"
|
|
1133
|
+
}),
|
|
1134
|
+
bucket: import_data2.Field.text({
|
|
1135
|
+
label: "Bucket"
|
|
1136
|
+
}),
|
|
1137
|
+
metadata: import_data2.Field.text({
|
|
1138
|
+
label: "Metadata (JSON)"
|
|
1139
|
+
}),
|
|
1140
|
+
status: import_data2.Field.select({
|
|
1141
|
+
label: "Status",
|
|
1142
|
+
required: true,
|
|
1143
|
+
options: [
|
|
1144
|
+
{ label: "In Progress", value: "in_progress" },
|
|
1145
|
+
{ label: "Completing", value: "completing" },
|
|
1146
|
+
{ label: "Completed", value: "completed" },
|
|
1147
|
+
{ label: "Failed", value: "failed" },
|
|
1148
|
+
{ label: "Expired", value: "expired" }
|
|
1149
|
+
]
|
|
1150
|
+
}),
|
|
1151
|
+
started_at: import_data2.Field.datetime({
|
|
1152
|
+
label: "Started At"
|
|
1153
|
+
}),
|
|
1154
|
+
expires_at: import_data2.Field.datetime({
|
|
1155
|
+
label: "Expires At"
|
|
1156
|
+
}),
|
|
1157
|
+
updated_at: import_data2.Field.datetime({
|
|
1158
|
+
label: "Updated At"
|
|
1159
|
+
})
|
|
1160
|
+
}
|
|
1161
|
+
});
|
|
1162
|
+
|
|
1163
|
+
// src/swappable-storage-service.ts
|
|
1164
|
+
var SwappableStorageService = class {
|
|
1165
|
+
constructor(initial, onSwap) {
|
|
1166
|
+
this.inner = initial;
|
|
1167
|
+
this.onSwap = onSwap;
|
|
1168
|
+
}
|
|
1169
|
+
/** Replace the inner adapter. */
|
|
1170
|
+
swap(next) {
|
|
1171
|
+
const previous = this.inner;
|
|
1172
|
+
this.inner = next;
|
|
1173
|
+
this.onSwap?.(previous, next);
|
|
1174
|
+
}
|
|
1175
|
+
/** Expose the active inner adapter — primarily for tests. */
|
|
1176
|
+
getInner() {
|
|
1177
|
+
return this.inner;
|
|
128
1178
|
}
|
|
129
|
-
|
|
130
|
-
|
|
1179
|
+
upload(key, data, options) {
|
|
1180
|
+
return this.inner.upload(key, data, options);
|
|
131
1181
|
}
|
|
132
|
-
|
|
133
|
-
|
|
1182
|
+
download(key) {
|
|
1183
|
+
return this.inner.download(key);
|
|
134
1184
|
}
|
|
135
|
-
|
|
136
|
-
|
|
1185
|
+
delete(key) {
|
|
1186
|
+
return this.inner.delete(key);
|
|
137
1187
|
}
|
|
138
|
-
|
|
139
|
-
|
|
1188
|
+
exists(key) {
|
|
1189
|
+
return this.inner.exists(key);
|
|
140
1190
|
}
|
|
141
|
-
|
|
142
|
-
|
|
1191
|
+
getInfo(key) {
|
|
1192
|
+
return this.inner.getInfo(key);
|
|
143
1193
|
}
|
|
144
|
-
|
|
145
|
-
|
|
1194
|
+
list(prefix) {
|
|
1195
|
+
if (typeof this.inner.list !== "function") {
|
|
1196
|
+
return Promise.reject(new Error("Active storage adapter does not support list()"));
|
|
1197
|
+
}
|
|
1198
|
+
return this.inner.list(prefix);
|
|
1199
|
+
}
|
|
1200
|
+
getSignedUrl(key, expiresIn) {
|
|
1201
|
+
if (typeof this.inner.getSignedUrl !== "function") {
|
|
1202
|
+
return Promise.reject(new Error("Active storage adapter does not support getSignedUrl()"));
|
|
1203
|
+
}
|
|
1204
|
+
return this.inner.getSignedUrl(key, expiresIn);
|
|
1205
|
+
}
|
|
1206
|
+
getPresignedUpload(key, expiresIn, options) {
|
|
1207
|
+
if (typeof this.inner.getPresignedUpload !== "function") {
|
|
1208
|
+
return Promise.reject(new Error("Active storage adapter does not support getPresignedUpload()"));
|
|
1209
|
+
}
|
|
1210
|
+
return this.inner.getPresignedUpload(key, expiresIn, options);
|
|
1211
|
+
}
|
|
1212
|
+
getPresignedDownload(key, expiresIn) {
|
|
1213
|
+
if (typeof this.inner.getPresignedDownload !== "function") {
|
|
1214
|
+
return Promise.reject(new Error("Active storage adapter does not support getPresignedDownload()"));
|
|
1215
|
+
}
|
|
1216
|
+
return this.inner.getPresignedDownload(key, expiresIn);
|
|
146
1217
|
}
|
|
147
|
-
|
|
148
|
-
|
|
1218
|
+
initiateChunkedUpload(key, options) {
|
|
1219
|
+
if (typeof this.inner.initiateChunkedUpload !== "function") {
|
|
1220
|
+
return Promise.reject(new Error("Active storage adapter does not support initiateChunkedUpload()"));
|
|
1221
|
+
}
|
|
1222
|
+
return this.inner.initiateChunkedUpload(key, options);
|
|
149
1223
|
}
|
|
150
|
-
|
|
151
|
-
|
|
1224
|
+
uploadChunk(uploadId, partNumber, data) {
|
|
1225
|
+
if (typeof this.inner.uploadChunk !== "function") {
|
|
1226
|
+
return Promise.reject(new Error("Active storage adapter does not support uploadChunk()"));
|
|
1227
|
+
}
|
|
1228
|
+
return this.inner.uploadChunk(uploadId, partNumber, data);
|
|
152
1229
|
}
|
|
153
|
-
|
|
154
|
-
|
|
1230
|
+
completeChunkedUpload(uploadId, parts) {
|
|
1231
|
+
if (typeof this.inner.completeChunkedUpload !== "function") {
|
|
1232
|
+
return Promise.reject(new Error("Active storage adapter does not support completeChunkedUpload()"));
|
|
1233
|
+
}
|
|
1234
|
+
return this.inner.completeChunkedUpload(uploadId, parts);
|
|
155
1235
|
}
|
|
156
|
-
|
|
157
|
-
|
|
1236
|
+
abortChunkedUpload(uploadId) {
|
|
1237
|
+
if (typeof this.inner.abortChunkedUpload !== "function") {
|
|
1238
|
+
return Promise.reject(new Error("Active storage adapter does not support abortChunkedUpload()"));
|
|
1239
|
+
}
|
|
1240
|
+
return this.inner.abortChunkedUpload(uploadId);
|
|
158
1241
|
}
|
|
159
|
-
|
|
160
|
-
|
|
1242
|
+
/**
|
|
1243
|
+
* Verify a presigned HMAC token (LocalStorageAdapter-specific).
|
|
1244
|
+
*
|
|
1245
|
+
* `IStorageService` does not declare this method, but `storage-routes`
|
|
1246
|
+
* type-narrows the active storage to `LocalStorageAdapter` to handle the
|
|
1247
|
+
* `/_local/raw/:token` PUT and GET endpoints. Without a passthrough on
|
|
1248
|
+
* the swappable wrapper, the route sees `verifyToken === undefined` and
|
|
1249
|
+
* returns 501 even though the underlying local adapter supports it.
|
|
1250
|
+
*/
|
|
1251
|
+
verifyToken(token, expectedOp) {
|
|
1252
|
+
const inner = this.inner;
|
|
1253
|
+
if (typeof inner.verifyToken !== "function") {
|
|
1254
|
+
throw new Error("Active storage adapter does not support verifyToken()");
|
|
1255
|
+
}
|
|
1256
|
+
return inner.verifyToken(token, expectedOp);
|
|
161
1257
|
}
|
|
162
1258
|
};
|
|
1259
|
+
|
|
1260
|
+
// src/storage-service-plugin.ts
|
|
1261
|
+
var StorageServicePlugin = class {
|
|
1262
|
+
constructor(options = {}) {
|
|
1263
|
+
this.name = "com.objectstack.service.storage";
|
|
1264
|
+
this.version = "1.0.0";
|
|
1265
|
+
this.type = "standard";
|
|
1266
|
+
this.storage = null;
|
|
1267
|
+
this.store = null;
|
|
1268
|
+
this.options = { adapter: "local", ...options };
|
|
1269
|
+
}
|
|
1270
|
+
/** Build a concrete adapter from a values map (settings-derived). */
|
|
1271
|
+
async buildAdapterFromValues(values) {
|
|
1272
|
+
const adapter = String(values.adapter ?? "local");
|
|
1273
|
+
if (adapter === "s3") {
|
|
1274
|
+
const bucket = values.s3_bucket;
|
|
1275
|
+
const region = values.s3_region;
|
|
1276
|
+
if (!bucket || !region) {
|
|
1277
|
+
throw new Error("StorageServicePlugin: S3 adapter requires s3_bucket and s3_region");
|
|
1278
|
+
}
|
|
1279
|
+
const opts = {
|
|
1280
|
+
bucket,
|
|
1281
|
+
region,
|
|
1282
|
+
endpoint: values.s3_endpoint || void 0,
|
|
1283
|
+
accessKeyId: values.s3_access_key_id || void 0,
|
|
1284
|
+
secretAccessKey: values.s3_secret_access_key || void 0,
|
|
1285
|
+
forcePathStyle: !!values.s3_force_path_style
|
|
1286
|
+
};
|
|
1287
|
+
return new S3StorageAdapter(opts);
|
|
1288
|
+
}
|
|
1289
|
+
const rootDir = values.local_root || "./storage";
|
|
1290
|
+
return new LocalStorageAdapter({
|
|
1291
|
+
basePath: this.options.basePath ?? "/api/v1/storage",
|
|
1292
|
+
...this.options.local ?? {},
|
|
1293
|
+
// settings value wins over any constructor-provided local.rootDir
|
|
1294
|
+
rootDir
|
|
1295
|
+
});
|
|
1296
|
+
}
|
|
1297
|
+
async init(ctx) {
|
|
1298
|
+
const adapter = this.options.adapter;
|
|
1299
|
+
let initial;
|
|
1300
|
+
if (adapter === "s3") {
|
|
1301
|
+
const { S3StorageAdapter: S3Ctor } = await Promise.resolve().then(() => (init_s3_storage_adapter(), s3_storage_adapter_exports));
|
|
1302
|
+
const s3Opts = this.options.s3;
|
|
1303
|
+
if (!s3Opts) {
|
|
1304
|
+
throw new Error('StorageServicePlugin: s3 options are required when adapter is "s3"');
|
|
1305
|
+
}
|
|
1306
|
+
initial = new S3Ctor(s3Opts);
|
|
1307
|
+
} else {
|
|
1308
|
+
const rootDir = this.options.local?.rootDir ?? "./storage";
|
|
1309
|
+
const basePath = this.options.basePath ?? "/api/v1/storage";
|
|
1310
|
+
initial = new LocalStorageAdapter({ rootDir, basePath, ...this.options.local });
|
|
1311
|
+
}
|
|
1312
|
+
this.storage = new SwappableStorageService(initial, (prev, next) => {
|
|
1313
|
+
const prevName = prev?.constructor?.name ?? "unknown";
|
|
1314
|
+
const nextName = next?.constructor?.name ?? "unknown";
|
|
1315
|
+
ctx.logger.warn(
|
|
1316
|
+
`StorageServicePlugin: storage adapter swapped (${prevName} \u2192 ${nextName}). Existing files were NOT migrated and may be unreachable through the new adapter.`
|
|
1317
|
+
);
|
|
1318
|
+
});
|
|
1319
|
+
ctx.registerService("file-storage", this.storage);
|
|
1320
|
+
ctx.logger.info(`StorageServicePlugin: registered ${adapter} storage adapter (swappable)`);
|
|
1321
|
+
try {
|
|
1322
|
+
ctx.getService("manifest").register({
|
|
1323
|
+
id: "com.objectstack.service.storage",
|
|
1324
|
+
name: "Storage Service",
|
|
1325
|
+
version: "1.0.0",
|
|
1326
|
+
type: "plugin",
|
|
1327
|
+
scope: "project",
|
|
1328
|
+
objects: [SystemFile, SystemUploadSession]
|
|
1329
|
+
});
|
|
1330
|
+
} catch {
|
|
1331
|
+
}
|
|
1332
|
+
}
|
|
1333
|
+
async start(ctx) {
|
|
1334
|
+
ctx.hook("kernel:ready", async () => {
|
|
1335
|
+
if (this.options.registerRoutes !== false) {
|
|
1336
|
+
let httpServer = null;
|
|
1337
|
+
try {
|
|
1338
|
+
httpServer = ctx.getService("http-server");
|
|
1339
|
+
} catch {
|
|
1340
|
+
}
|
|
1341
|
+
if (httpServer && this.storage) {
|
|
1342
|
+
let engine = null;
|
|
1343
|
+
try {
|
|
1344
|
+
engine = ctx.getService("objectql");
|
|
1345
|
+
} catch {
|
|
1346
|
+
}
|
|
1347
|
+
this.store = new StorageMetadataStore(engine);
|
|
1348
|
+
registerStorageRoutes(httpServer, this.storage, this.store, {
|
|
1349
|
+
basePath: this.options.basePath ?? "/api/v1/storage",
|
|
1350
|
+
presignedTtl: this.options.presignedTtl,
|
|
1351
|
+
sessionTtl: this.options.sessionTtl
|
|
1352
|
+
});
|
|
1353
|
+
ctx.logger.info(
|
|
1354
|
+
"StorageServicePlugin: REST routes registered at " + (this.options.basePath ?? "/api/v1/storage")
|
|
1355
|
+
);
|
|
1356
|
+
} else if (!httpServer) {
|
|
1357
|
+
ctx.logger.warn(
|
|
1358
|
+
'StorageServicePlugin: no HTTP server available \u2014 REST routes not registered. File storage is still accessible programmatically via kernel.getService("file-storage").'
|
|
1359
|
+
);
|
|
1360
|
+
}
|
|
1361
|
+
}
|
|
1362
|
+
if (this.options.bindToSettings === false) return;
|
|
1363
|
+
try {
|
|
1364
|
+
const settings = ctx.getService("settings");
|
|
1365
|
+
if (!settings || typeof settings.createClient !== "function") return;
|
|
1366
|
+
const applySettings = async () => {
|
|
1367
|
+
if (!this.storage) return;
|
|
1368
|
+
try {
|
|
1369
|
+
const payload = await settings.getNamespace("storage");
|
|
1370
|
+
const values = {};
|
|
1371
|
+
for (const [k, v] of Object.entries(payload.values)) {
|
|
1372
|
+
values[k] = v?.value;
|
|
1373
|
+
}
|
|
1374
|
+
const hasAny = Object.values(values).some((v) => v !== void 0 && v !== null && v !== "");
|
|
1375
|
+
if (!hasAny) return;
|
|
1376
|
+
const next = await this.buildAdapterFromValues(values);
|
|
1377
|
+
this.storage.swap(next);
|
|
1378
|
+
} catch (err) {
|
|
1379
|
+
ctx.logger.warn(
|
|
1380
|
+
"StorageServicePlugin: failed to apply storage settings: " + (err?.message ?? err)
|
|
1381
|
+
);
|
|
1382
|
+
}
|
|
1383
|
+
};
|
|
1384
|
+
await applySettings();
|
|
1385
|
+
if (typeof settings.subscribe === "function") {
|
|
1386
|
+
settings.subscribe("storage", () => {
|
|
1387
|
+
void applySettings();
|
|
1388
|
+
});
|
|
1389
|
+
ctx.logger.info("StorageServicePlugin: bound to settings:changed for namespace=storage");
|
|
1390
|
+
}
|
|
1391
|
+
if (typeof settings.registerAction === "function" && this.storage) {
|
|
1392
|
+
const proxy = this.storage;
|
|
1393
|
+
settings.registerAction("storage", "test", async ({ values }) => {
|
|
1394
|
+
const probeKey = `__objectstack_probe__/${Date.now()}-${Math.random().toString(36).slice(2, 10)}`;
|
|
1395
|
+
const probeBytes = Buffer.from(`probe@${(/* @__PURE__ */ new Date()).toISOString()}`, "utf-8");
|
|
1396
|
+
try {
|
|
1397
|
+
let target = proxy;
|
|
1398
|
+
if (values && Object.keys(values).length > 0) {
|
|
1399
|
+
try {
|
|
1400
|
+
target = await this.buildAdapterFromValues(values);
|
|
1401
|
+
} catch (err) {
|
|
1402
|
+
return { ok: false, severity: "error", message: err?.message ?? String(err) };
|
|
1403
|
+
}
|
|
1404
|
+
}
|
|
1405
|
+
await target.upload(probeKey, probeBytes, { contentType: "text/plain" });
|
|
1406
|
+
const got = await target.download(probeKey);
|
|
1407
|
+
if (!got || !Buffer.isBuffer(got) || got.toString("utf-8") !== probeBytes.toString("utf-8")) {
|
|
1408
|
+
return { ok: false, severity: "error", message: "Probe download did not match upload." };
|
|
1409
|
+
}
|
|
1410
|
+
await target.delete(probeKey);
|
|
1411
|
+
const adapter = String(values?.adapter ?? this.options.adapter ?? "local");
|
|
1412
|
+
return {
|
|
1413
|
+
ok: true,
|
|
1414
|
+
severity: "info",
|
|
1415
|
+
message: `Storage round-trip succeeded (adapter=${adapter}).`
|
|
1416
|
+
};
|
|
1417
|
+
} catch (err) {
|
|
1418
|
+
try {
|
|
1419
|
+
await proxy.delete(probeKey);
|
|
1420
|
+
} catch {
|
|
1421
|
+
}
|
|
1422
|
+
return { ok: false, severity: "error", message: err?.message ?? String(err) };
|
|
1423
|
+
}
|
|
1424
|
+
});
|
|
1425
|
+
ctx.logger.info("StorageServicePlugin: registered settings action storage/test");
|
|
1426
|
+
}
|
|
1427
|
+
} catch {
|
|
1428
|
+
}
|
|
1429
|
+
});
|
|
1430
|
+
}
|
|
1431
|
+
};
|
|
1432
|
+
|
|
1433
|
+
// src/index.ts
|
|
1434
|
+
init_s3_storage_adapter();
|
|
163
1435
|
// Annotate the CommonJS export names for ESM import in node:
|
|
164
1436
|
0 && (module.exports = {
|
|
165
1437
|
LocalStorageAdapter,
|
|
166
1438
|
S3StorageAdapter,
|
|
167
|
-
|
|
1439
|
+
StorageMetadataStore,
|
|
1440
|
+
StorageServicePlugin,
|
|
1441
|
+
SwappableStorageService,
|
|
1442
|
+
SystemFile,
|
|
1443
|
+
SystemUploadSession,
|
|
1444
|
+
registerStorageRoutes
|
|
168
1445
|
});
|
|
169
1446
|
//# sourceMappingURL=index.cjs.map
|