@terreno/admin-backend 0.3.2 → 0.5.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/.claude/CLAUDE.local.md +208 -0
- package/.cursor/rules/00-root.mdc +342 -0
- package/.github/copilot-instructions.md +337 -0
- package/AGENTS.md +337 -0
- package/README.md +5 -0
- package/biome.jsonc +20 -0
- package/bunfig.toml +3 -0
- package/dist/adminApp.d.ts +96 -1
- package/dist/adminApp.js +375 -11
- package/dist/adminApp.test.d.ts +1 -0
- package/dist/adminApp.test.js +559 -0
- package/dist/documentStorageApp.d.ts +31 -0
- package/dist/documentStorageApp.js +399 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +3 -1
- package/package.json +31 -8
- package/src/adminApp.test.ts +311 -0
- package/src/adminApp.ts +330 -2
- package/src/documentStorageApp.ts +342 -0
- package/src/index.ts +7 -1
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
import {pipeline} from "node:stream/promises";
|
|
2
|
+
import {Storage} from "@google-cloud/storage";
|
|
3
|
+
import {APIError, asyncHandler, authenticateMiddleware, logger} from "@terreno/api";
|
|
4
|
+
import type express from "express";
|
|
5
|
+
import {DateTime} from "luxon";
|
|
6
|
+
import multer from "multer";
|
|
7
|
+
|
|
8
|
+
export interface DocumentStorageOptions {
|
|
9
|
+
bucketName: string;
|
|
10
|
+
folderPrefix?: string;
|
|
11
|
+
basePath?: string;
|
|
12
|
+
allowedMimeTypes?: string[];
|
|
13
|
+
maxFileSize?: number;
|
|
14
|
+
signedUrlExpiration?: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface DocumentFile {
|
|
18
|
+
name: string;
|
|
19
|
+
fullPath: string;
|
|
20
|
+
size: number;
|
|
21
|
+
contentType: string | undefined;
|
|
22
|
+
updated: string;
|
|
23
|
+
isFolder: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface DocumentListResponse {
|
|
27
|
+
files: DocumentFile[];
|
|
28
|
+
folders: string[];
|
|
29
|
+
prefix: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const DEFAULT_ALLOWED_MIME_TYPES = new Set([
|
|
33
|
+
"image/jpeg",
|
|
34
|
+
"image/png",
|
|
35
|
+
"image/gif",
|
|
36
|
+
"image/webp",
|
|
37
|
+
"image/svg+xml",
|
|
38
|
+
"application/pdf",
|
|
39
|
+
"text/plain",
|
|
40
|
+
"text/csv",
|
|
41
|
+
"application/json",
|
|
42
|
+
"application/msword",
|
|
43
|
+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
44
|
+
"application/vnd.ms-excel",
|
|
45
|
+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
46
|
+
]);
|
|
47
|
+
|
|
48
|
+
const DEFAULT_MAX_FILE_SIZE = 10 * 1024 * 1024; // 10MB
|
|
49
|
+
|
|
50
|
+
const isAdmin = (req: express.Request): boolean => {
|
|
51
|
+
const user = (req as any).user;
|
|
52
|
+
return user?.admin === true;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export class DocumentStorageApp {
|
|
56
|
+
private options: DocumentStorageOptions;
|
|
57
|
+
private storage: Storage;
|
|
58
|
+
|
|
59
|
+
constructor(options: DocumentStorageOptions) {
|
|
60
|
+
this.options = options;
|
|
61
|
+
this.storage = new Storage();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
private get bucket() {
|
|
65
|
+
const bucketName = this.options.bucketName || process.env.GCS_BUCKET;
|
|
66
|
+
if (!bucketName) {
|
|
67
|
+
throw new APIError({
|
|
68
|
+
detail: "Configure storage settings before accessing files.",
|
|
69
|
+
disableExternalErrorTracking: true,
|
|
70
|
+
status: 503,
|
|
71
|
+
title: "Storage not configured",
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
return this.storage.bucket(bucketName);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
private get prefix() {
|
|
78
|
+
return this.options.folderPrefix ?? "";
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
private get allowedMimeTypes(): Set<string> {
|
|
82
|
+
if (this.options.allowedMimeTypes) {
|
|
83
|
+
return new Set(this.options.allowedMimeTypes);
|
|
84
|
+
}
|
|
85
|
+
return DEFAULT_ALLOWED_MIME_TYPES;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
register(app: express.Application): void {
|
|
89
|
+
const basePath = this.options.basePath ?? "/documents";
|
|
90
|
+
const maxFileSize = this.options.maxFileSize ?? DEFAULT_MAX_FILE_SIZE;
|
|
91
|
+
|
|
92
|
+
const allowedMimeTypes = this.allowedMimeTypes;
|
|
93
|
+
|
|
94
|
+
const upload = multer({
|
|
95
|
+
fileFilter: (_req, file, cb) => {
|
|
96
|
+
if (allowedMimeTypes.has(file.mimetype)) {
|
|
97
|
+
cb(null, true);
|
|
98
|
+
} else {
|
|
99
|
+
cb(
|
|
100
|
+
new APIError({
|
|
101
|
+
detail: `File type ${file.mimetype} is not allowed`,
|
|
102
|
+
disableExternalErrorTracking: true,
|
|
103
|
+
status: 400,
|
|
104
|
+
title: "File type not allowed",
|
|
105
|
+
})
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
limits: {fileSize: maxFileSize},
|
|
110
|
+
storage: multer.memoryStorage(),
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
const adminGuard = [
|
|
114
|
+
authenticateMiddleware(),
|
|
115
|
+
(req: express.Request, _res: express.Response, next: express.NextFunction) => {
|
|
116
|
+
if (!isAdmin(req)) {
|
|
117
|
+
throw new APIError({status: 403, title: "Admin access required"});
|
|
118
|
+
}
|
|
119
|
+
next();
|
|
120
|
+
},
|
|
121
|
+
];
|
|
122
|
+
|
|
123
|
+
// GET basePath/ — List files and folders
|
|
124
|
+
app.get(
|
|
125
|
+
`${basePath}/`,
|
|
126
|
+
...adminGuard,
|
|
127
|
+
asyncHandler(async (req: express.Request, res: express.Response) => {
|
|
128
|
+
const queryPrefix = (req.query.prefix as string) ?? "";
|
|
129
|
+
const fullPrefix = `${this.prefix}${queryPrefix}`;
|
|
130
|
+
|
|
131
|
+
const [files, , apiResponse] = await this.bucket.getFiles({
|
|
132
|
+
delimiter: "/",
|
|
133
|
+
prefix: fullPrefix,
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
const documentFiles: DocumentFile[] = files
|
|
137
|
+
.filter((file) => file.name !== fullPrefix)
|
|
138
|
+
.map((file) => ({
|
|
139
|
+
contentType: file.metadata.contentType as string | undefined,
|
|
140
|
+
fullPath: file.name.slice(this.prefix.length),
|
|
141
|
+
isFolder: false,
|
|
142
|
+
name: file.name.split("/").filter(Boolean).pop() ?? file.name,
|
|
143
|
+
size: Number(file.metadata.size ?? 0),
|
|
144
|
+
updated: file.metadata.updated as string,
|
|
145
|
+
}));
|
|
146
|
+
|
|
147
|
+
const prefixes = ((apiResponse as any)?.prefixes as string[] | undefined) ?? [];
|
|
148
|
+
const folders = prefixes.map((p) => {
|
|
149
|
+
const relative = p.slice(this.prefix.length);
|
|
150
|
+
return relative;
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
const response: DocumentListResponse = {
|
|
154
|
+
files: documentFiles,
|
|
155
|
+
folders,
|
|
156
|
+
prefix: queryPrefix,
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
return res.json(response);
|
|
160
|
+
})
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
// POST basePath/ — Upload a file
|
|
164
|
+
app.post(
|
|
165
|
+
`${basePath}/`,
|
|
166
|
+
...adminGuard,
|
|
167
|
+
upload.single("file") as any,
|
|
168
|
+
asyncHandler(async (req: express.Request, res: express.Response) => {
|
|
169
|
+
const file = (req as any).file as Express.Multer.File | undefined;
|
|
170
|
+
if (!file) {
|
|
171
|
+
throw new APIError({status: 400, title: "No file provided"});
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const targetPrefix = (req.body.prefix as string) ?? "";
|
|
175
|
+
const sanitizedFilename = file.originalname.replace(/[^a-zA-Z0-9._-]/g, "_");
|
|
176
|
+
const gcsPath = `${this.prefix}${targetPrefix}${sanitizedFilename}`;
|
|
177
|
+
|
|
178
|
+
const gcsFile = this.bucket.file(gcsPath);
|
|
179
|
+
await gcsFile.save(file.buffer, {
|
|
180
|
+
contentType: file.mimetype,
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
const documentFile: DocumentFile = {
|
|
184
|
+
contentType: file.mimetype,
|
|
185
|
+
fullPath: `${targetPrefix}${sanitizedFilename}`,
|
|
186
|
+
isFolder: false,
|
|
187
|
+
name: sanitizedFilename,
|
|
188
|
+
size: file.buffer.length,
|
|
189
|
+
updated: DateTime.now().toISO(),
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
return res.json(documentFile);
|
|
193
|
+
})
|
|
194
|
+
);
|
|
195
|
+
|
|
196
|
+
// GET basePath/download/* — Stream file download
|
|
197
|
+
app.get(
|
|
198
|
+
`${basePath}/download/*filepath`,
|
|
199
|
+
...adminGuard,
|
|
200
|
+
asyncHandler(async (req: express.Request, res: express.Response) => {
|
|
201
|
+
const filePath = req.params.filepath as string;
|
|
202
|
+
if (!filePath) {
|
|
203
|
+
throw new APIError({status: 400, title: "File path is required"});
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const gcsPath = `${this.prefix}${filePath}`;
|
|
207
|
+
const gcsFile = this.bucket.file(gcsPath);
|
|
208
|
+
|
|
209
|
+
console.info("[documentStorage] download attempt", {filePath, gcsPath});
|
|
210
|
+
|
|
211
|
+
let metadata: Record<string, unknown>;
|
|
212
|
+
try {
|
|
213
|
+
const [meta] = await gcsFile.getMetadata();
|
|
214
|
+
metadata = meta as Record<string, unknown>;
|
|
215
|
+
console.info("[documentStorage] getMetadata success", {
|
|
216
|
+
contentType: (meta as any)?.contentType,
|
|
217
|
+
etag: (meta as any)?.etag,
|
|
218
|
+
size: (meta as any)?.size,
|
|
219
|
+
});
|
|
220
|
+
} catch (err: any) {
|
|
221
|
+
if (err?.code === 404) {
|
|
222
|
+
throw new APIError({
|
|
223
|
+
detail: filePath,
|
|
224
|
+
disableExternalErrorTracking: true,
|
|
225
|
+
status: 404,
|
|
226
|
+
title: "File not found",
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
console.error("[documentStorage] getMetadata error", {
|
|
230
|
+
code: err?.code,
|
|
231
|
+
errors: err?.errors,
|
|
232
|
+
message: err?.message,
|
|
233
|
+
response: err?.response,
|
|
234
|
+
stack: err?.stack,
|
|
235
|
+
status: err?.status,
|
|
236
|
+
});
|
|
237
|
+
logger.error("[documentStorage] getMetadata error", {
|
|
238
|
+
code: err?.code,
|
|
239
|
+
errors: err?.errors,
|
|
240
|
+
message: err?.message,
|
|
241
|
+
status: err?.status,
|
|
242
|
+
});
|
|
243
|
+
throw new APIError({
|
|
244
|
+
detail: err?.message ?? String(err),
|
|
245
|
+
status: 500,
|
|
246
|
+
title: "Failed to access file",
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const contentType =
|
|
251
|
+
(metadata.contentType as string | undefined) ?? "application/octet-stream";
|
|
252
|
+
const filename = filePath.split("/").filter(Boolean).pop() ?? "download";
|
|
253
|
+
|
|
254
|
+
res.setHeader("Content-Type", contentType);
|
|
255
|
+
res.setHeader("Content-Disposition", `attachment; filename="${filename}"`);
|
|
256
|
+
if (metadata.size) {
|
|
257
|
+
res.setHeader("Content-Length", String(metadata.size));
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
try {
|
|
261
|
+
await pipeline(gcsFile.createReadStream(), res);
|
|
262
|
+
} catch (err: any) {
|
|
263
|
+
console.error("[documentStorage] pipeline error", {
|
|
264
|
+
code: err?.code,
|
|
265
|
+
message: err?.message,
|
|
266
|
+
stack: err?.stack,
|
|
267
|
+
});
|
|
268
|
+
logger.error("[documentStorage] pipeline error", {
|
|
269
|
+
code: err?.code,
|
|
270
|
+
message: err?.message,
|
|
271
|
+
stack: err?.stack,
|
|
272
|
+
});
|
|
273
|
+
if (!res.headersSent) {
|
|
274
|
+
throw new APIError({
|
|
275
|
+
detail: err?.message ?? String(err),
|
|
276
|
+
status: 500,
|
|
277
|
+
title: "Failed to stream file",
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
})
|
|
282
|
+
);
|
|
283
|
+
|
|
284
|
+
// POST basePath/folder — Create a folder
|
|
285
|
+
app.post(
|
|
286
|
+
`${basePath}/folder`,
|
|
287
|
+
...adminGuard,
|
|
288
|
+
asyncHandler(async (req: express.Request, res: express.Response) => {
|
|
289
|
+
const {folderName, prefix} = req.body as {folderName?: string; prefix?: string};
|
|
290
|
+
if (!folderName) {
|
|
291
|
+
throw new APIError({status: 400, title: "Folder name is required"});
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
const sanitizedName = folderName.replace(/[^a-zA-Z0-9._-]/g, "_");
|
|
295
|
+
const targetPrefix = prefix ?? "";
|
|
296
|
+
const gcsPath = `${this.prefix}${targetPrefix}${sanitizedName}/`;
|
|
297
|
+
|
|
298
|
+
const gcsFile = this.bucket.file(gcsPath);
|
|
299
|
+
await gcsFile.save(Buffer.alloc(0), {contentType: "application/x-directory"});
|
|
300
|
+
|
|
301
|
+
return res.json({folder: `${targetPrefix}${sanitizedName}/`});
|
|
302
|
+
})
|
|
303
|
+
);
|
|
304
|
+
|
|
305
|
+
// DELETE basePath/folder/* — Delete a folder and all its contents
|
|
306
|
+
app.delete(
|
|
307
|
+
`${basePath}/folder/*folderpath`,
|
|
308
|
+
...adminGuard,
|
|
309
|
+
asyncHandler(async (req: express.Request, res: express.Response) => {
|
|
310
|
+
const folderPath = req.params.folderpath as string;
|
|
311
|
+
if (!folderPath) {
|
|
312
|
+
throw new APIError({status: 400, title: "Folder path is required"});
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
const gcsPrefix = `${this.prefix}${folderPath}`;
|
|
316
|
+
const [files] = await this.bucket.getFiles({prefix: gcsPrefix});
|
|
317
|
+
await Promise.all(files.map((f) => f.delete({ignoreNotFound: true})));
|
|
318
|
+
|
|
319
|
+
return res.json({success: true});
|
|
320
|
+
})
|
|
321
|
+
);
|
|
322
|
+
|
|
323
|
+
// DELETE basePath/* — Delete a file
|
|
324
|
+
app.delete(
|
|
325
|
+
`${basePath}/*filepath`,
|
|
326
|
+
...adminGuard,
|
|
327
|
+
asyncHandler(async (req: express.Request, res: express.Response) => {
|
|
328
|
+
const filePath = req.params.filepath as string;
|
|
329
|
+
if (!filePath) {
|
|
330
|
+
throw new APIError({status: 400, title: "File path is required"});
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
const gcsPath = `${this.prefix}${filePath}`;
|
|
334
|
+
const gcsFile = this.bucket.file(gcsPath);
|
|
335
|
+
|
|
336
|
+
await gcsFile.delete({ignoreNotFound: true});
|
|
337
|
+
|
|
338
|
+
return res.json({success: true});
|
|
339
|
+
})
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,2 +1,8 @@
|
|
|
1
|
-
export type {AdminModelConfig, AdminOptions} from "./adminApp";
|
|
1
|
+
export type {AdminModelConfig, AdminOptions, AdminScriptConfig} from "./adminApp";
|
|
2
2
|
export {AdminApp} from "./adminApp";
|
|
3
|
+
export type {
|
|
4
|
+
DocumentFile,
|
|
5
|
+
DocumentListResponse,
|
|
6
|
+
DocumentStorageOptions,
|
|
7
|
+
} from "./documentStorageApp";
|
|
8
|
+
export {DocumentStorageApp} from "./documentStorageApp";
|