@tulip-systems/drive 0.7.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.
Files changed (70) hide show
  1. package/LICENSE +662 -0
  2. package/package.json +113 -0
  3. package/src/components/content.tsx +13 -0
  4. package/src/components/context.client.tsx +12 -0
  5. package/src/components/dnd.client.tsx +47 -0
  6. package/src/components/grid-card.client.tsx +252 -0
  7. package/src/components/grid.client.tsx +96 -0
  8. package/src/components/navigation/breadcrumbs.client.tsx +125 -0
  9. package/src/components/navigation/header.client.tsx +45 -0
  10. package/src/components/navigation/toolbar.client.tsx +35 -0
  11. package/src/components/navigation/view-switcher.client.tsx +32 -0
  12. package/src/components/selection.client.tsx +48 -0
  13. package/src/components/view.client.tsx +67 -0
  14. package/src/config/filters.ts +14 -0
  15. package/src/config/types.tsx +90 -0
  16. package/src/entry.client.ts +7 -0
  17. package/src/entry.server.ts +4 -0
  18. package/src/entry.ts +10 -0
  19. package/src/lib/constants.ts +19 -0
  20. package/src/lib/contracts.ts +121 -0
  21. package/src/lib/dto.ts +83 -0
  22. package/src/lib/helpers.server.ts +14 -0
  23. package/src/lib/helpers.ts +32 -0
  24. package/src/lib/search-params.ts +5 -0
  25. package/src/lib/validators.ts +89 -0
  26. package/src/providers/google/components/command-file-update.tsx +100 -0
  27. package/src/providers/google/components/command-folder-create.tsx +104 -0
  28. package/src/providers/google/components/command-folder-update.tsx +100 -0
  29. package/src/providers/google/components/content.client.tsx +6 -0
  30. package/src/providers/google/components/navigation.client.tsx +21 -0
  31. package/src/providers/google/components/provider.client.tsx +60 -0
  32. package/src/providers/google/components/view.client.tsx +158 -0
  33. package/src/providers/google/config/columns-data.tsx +81 -0
  34. package/src/providers/google/config/filters.ts +3 -0
  35. package/src/providers/google/entry.client.ts +10 -0
  36. package/src/providers/google/entry.server.ts +5 -0
  37. package/src/providers/google/entry.ts +12 -0
  38. package/src/providers/google/lib/constants.ts +10 -0
  39. package/src/providers/google/lib/dto.ts +104 -0
  40. package/src/providers/google/lib/helpers.ts +37 -0
  41. package/src/providers/google/lib/router.server.ts +62 -0
  42. package/src/providers/google/lib/schema.ts +9 -0
  43. package/src/providers/google/lib/search-params.ts +7 -0
  44. package/src/providers/google/lib/service.server.ts +792 -0
  45. package/src/providers/google/lib/validators.ts +148 -0
  46. package/src/providers/local/components/command-file-update.tsx +93 -0
  47. package/src/providers/local/components/command-file-upload.tsx +29 -0
  48. package/src/providers/local/components/command-folder-create.tsx +100 -0
  49. package/src/providers/local/components/command-folder-update.tsx +93 -0
  50. package/src/providers/local/components/content.client.tsx +3 -0
  51. package/src/providers/local/components/navigation.client.tsx +23 -0
  52. package/src/providers/local/components/provider.client.tsx +90 -0
  53. package/src/providers/local/components/upload-zone-context.client.tsx +43 -0
  54. package/src/providers/local/components/upload-zone.client.tsx +182 -0
  55. package/src/providers/local/components/view.client.tsx +145 -0
  56. package/src/providers/local/config/columns-data.tsx +81 -0
  57. package/src/providers/local/config/filters.ts +14 -0
  58. package/src/providers/local/entry.client.ts +18 -0
  59. package/src/providers/local/entry.server.ts +7 -0
  60. package/src/providers/local/entry.ts +14 -0
  61. package/src/providers/local/lib/constants.ts +23 -0
  62. package/src/providers/local/lib/helpers.ts +105 -0
  63. package/src/providers/local/lib/route-handler.server.ts +153 -0
  64. package/src/providers/local/lib/router.server.ts +137 -0
  65. package/src/providers/local/lib/schema.ts +104 -0
  66. package/src/providers/local/lib/search-params.ts +4 -0
  67. package/src/providers/local/lib/service.server.ts +1116 -0
  68. package/src/providers/local/lib/upload.client.ts +177 -0
  69. package/src/providers/local/lib/validators.ts +154 -0
  70. package/src/styles.css +1 -0
@@ -0,0 +1,1116 @@
1
+ import stream from "node:stream/consumers";
2
+ import type { TDatabaseSchema } from "@tulip-systems/core/config";
3
+ import type { TableQueryResponse } from "@tulip-systems/core/data-tables/server";
4
+ import {
5
+ convertOrderByToQueryParams,
6
+ convertSearchToQueryParams,
7
+ createTableQueryResponse,
8
+ } from "@tulip-systems/core/data-tables/server";
9
+ import type { Database } from "@tulip-systems/core/database/server";
10
+ import { ServerError } from "@tulip-systems/core/router/server";
11
+ import { type ObjectBodyInput, storageAssets } from "@tulip-systems/core/storage";
12
+ import type { Storage } from "@tulip-systems/core/storage/server";
13
+ import { addSeconds } from "date-fns";
14
+ import {
15
+ and,
16
+ asc,
17
+ desc,
18
+ eq,
19
+ getTableColumns,
20
+ inArray,
21
+ isNotNull,
22
+ isNull,
23
+ type SQL,
24
+ sql,
25
+ } from "drizzle-orm";
26
+ import { after } from "next/server";
27
+ import type { QueryResult } from "pg";
28
+ import type {
29
+ DriveArchive,
30
+ DriveDirectUpload,
31
+ DriveNodeMutations,
32
+ DrivePresignedUpload,
33
+ DrivePreviewGenerator,
34
+ DriveReader,
35
+ DriveReadonly,
36
+ } from "../../../lib/contracts";
37
+ import { deviceSizes } from "./constants";
38
+ import {
39
+ inferLocalDriveNodeSubtype,
40
+ isLocalDriveFile,
41
+ isLocalDriveFolder,
42
+ toLocalDriveNode,
43
+ } from "./helpers";
44
+ import { nodePresignedUrls, nodes, nodeVariants } from "./schema";
45
+ import {
46
+ type CreateLocalDriveFolderSchema,
47
+ type GetLocalDriveNodesByParentIdInput,
48
+ type GetLocalFileURLSchema,
49
+ getLocalFileURLSchemaDefaults,
50
+ type ListLocalDriveFlatSchema,
51
+ type ListLocalDriveTreeSchema,
52
+ type LocalDriveFileNode,
53
+ type LocalDriveNode,
54
+ type LocalDriveNodeChild,
55
+ type LocalDriveNodeWithAsset,
56
+ type LocalDriveNodeWithChildren,
57
+ type LocalNode,
58
+ type PresignLocalDriveFileInput,
59
+ type UpdateLocalDriveNodeInput,
60
+ type UploadLocalDriveFileSchema,
61
+ } from "./validators";
62
+
63
+ /**
64
+ * Drive Service Config
65
+ */
66
+ export type LocalDriveConfig<TSchema extends TDatabaseSchema> = {
67
+ db: Database<TSchema>;
68
+ storage: Storage<TSchema>;
69
+ };
70
+
71
+ /**
72
+ * Drive Service
73
+ */
74
+ export class LocalDrive<TSchema extends TDatabaseSchema>
75
+ implements
76
+ DriveReader<LocalDriveNode>,
77
+ DriveNodeMutations<LocalDriveNode>,
78
+ DriveDirectUpload<LocalDriveNode, ObjectBodyInput>,
79
+ DrivePresignedUpload<LocalDriveFileNode>,
80
+ DrivePreviewGenerator,
81
+ DriveReadonly<LocalDriveNode>,
82
+ DriveArchive<LocalDriveNode>
83
+ {
84
+ #db: Database<TSchema>;
85
+ storage: Storage<TSchema>;
86
+
87
+ constructor({ db, storage }: LocalDriveConfig<TSchema>) {
88
+ this.#db = db;
89
+ this.storage = storage;
90
+ }
91
+
92
+ /**
93
+ * Loads and validates the parent folder for write operations.
94
+ *
95
+ * The returned parent is guaranteed to exist, be a folder, belong to the
96
+ * same namespace, and not be archived. Root-level operations return `null`.
97
+ *
98
+ * @param input - Parent identifier and target namespace.
99
+ * @returns The validated parent node or `null` for root operations.
100
+ */
101
+ async #getWritableParent(input: { parentId?: string | null; namespace: string }) {
102
+ if (!input.parentId) return null;
103
+
104
+ const [parent] = await this.#db
105
+ .select()
106
+ .from(nodes)
107
+ .where(eq(nodes.id, input.parentId))
108
+ .limit(1);
109
+
110
+ if (!parent) {
111
+ throw new ServerError("BAD_REQUEST", { message: "Parent not found" });
112
+ }
113
+
114
+ if (!isLocalDriveFolder(parent)) {
115
+ throw new ServerError("BAD_REQUEST", { message: "Parent is not a folder" });
116
+ }
117
+
118
+ if (parent.namespace !== input.namespace) {
119
+ throw new ServerError("BAD_REQUEST", {
120
+ message: "Parent is not in the same namespace",
121
+ });
122
+ }
123
+
124
+ if (parent.archivedAt) {
125
+ throw new ServerError("BAD_REQUEST", {
126
+ message: "Parent is archived",
127
+ });
128
+ }
129
+
130
+ return parent;
131
+ }
132
+
133
+ /**
134
+ * Collects the storage asset ids associated with a node subtree.
135
+ *
136
+ * This includes both the main file assets attached to file nodes and any
137
+ * generated variant assets stored in `node_variants`.
138
+ *
139
+ * @param nodeIds - Node ids that belong to the subtree.
140
+ * @param subtree - Lightweight subtree rows returned by `getNodeChildren`.
141
+ * @returns A de-duplicated list of storage asset ids.
142
+ */
143
+ async #getSubtreeAssetIds(
144
+ nodeIds: string[],
145
+ subtree: Array<Pick<LocalNode, "id" | "assetId" | "type" | "parentId"> & { depth: number }>,
146
+ ) {
147
+ const fileAssetIds = [
148
+ ...new Set(
149
+ subtree
150
+ .filter((node) => node.type === "file")
151
+ .map((node) => node.assetId)
152
+ .filter((assetId): assetId is string => assetId != null),
153
+ ),
154
+ ];
155
+
156
+ const variantAssets = await this.#db
157
+ .select({ assetId: nodeVariants.assetId })
158
+ .from(nodeVariants)
159
+ .where(inArray(nodeVariants.nodeId, nodeIds));
160
+
161
+ return [...new Set([...fileAssetIds, ...variantAssets.map((variant) => variant.assetId)])];
162
+ }
163
+
164
+ /**
165
+ * Retrieves the node row for a given id.
166
+ *
167
+ * This is a thin lookup helper that returns the raw database result for the
168
+ * matching node. It does not load children, variants, or perform namespace
169
+ * validation.
170
+ *
171
+ * @param id - The node identifier.
172
+ * @returns A promise resolving to the matching node row as a single-item array,
173
+ * or an empty array when no node exists.
174
+ */
175
+ async getNodeById(id: string): Promise<LocalDriveNode | null> {
176
+ const [node] = await this.#db.select().from(nodes).where(eq(nodes.id, id));
177
+ return node ? toLocalDriveNode(node) : null;
178
+ }
179
+
180
+ /**
181
+ * Retrieves a node together with its direct children for a namespace.
182
+ *
183
+ * This is used by the drive UI when opening a node detail view that expects
184
+ * the immediate child nodes in the same payload.
185
+ *
186
+ * @param input - The target node id and namespace.
187
+ * @returns The node with its children when found, otherwise `null`.
188
+ */
189
+ async getNodeWithChildren(input: {
190
+ id: string;
191
+ namespace: string;
192
+ }): Promise<LocalDriveNodeWithChildren | null> {
193
+ const node = await this.getNodeById(input.id);
194
+ if (!node || node.namespace !== input.namespace) return null;
195
+
196
+ const children = await this.getNodesByParentId({
197
+ filters: {
198
+ namespace: input.namespace,
199
+ parentId: node.id,
200
+ hidden: false,
201
+ isArchived: false,
202
+ },
203
+ });
204
+
205
+ return { ...node, children };
206
+ }
207
+
208
+ /**
209
+ * Lists nodes within a folder scope using the current table-query filters.
210
+ *
211
+ * Behavior:
212
+ * - Scopes results to a namespace
213
+ * - Resolves either root nodes (`parentId = null`) or a specific parent folder
214
+ * - Applies optional filters such as ids, types, hidden state, and archive state
215
+ * - Applies search and sorting through the shared table-query utilities
216
+ *
217
+ * @param input - Table query input containing drive filters, search, and sorting.
218
+ * @returns A promise resolving to the matching child nodes.
219
+ */
220
+ async getNodesByParentId({
221
+ filters,
222
+ ...query
223
+ }: GetLocalDriveNodesByParentIdInput): Promise<LocalDriveNode[]> {
224
+ const orderBy = convertOrderByToQueryParams(query, nodes, asc(nodes.createdAt));
225
+ const search = convertSearchToQueryParams(query, [nodes.name]);
226
+ const archivedFilter =
227
+ filters.isArchived === true
228
+ ? isNotNull(nodes.archivedAt)
229
+ : filters.isArchived === false
230
+ ? isNull(nodes.archivedAt)
231
+ : undefined;
232
+ const parentFilter = filters.parentId
233
+ ? eq(nodes.parentId, filters.parentId)
234
+ : isNull(nodes.parentId);
235
+
236
+ const items = await this.#db
237
+ .select()
238
+ .from(nodes)
239
+ .where(
240
+ and(
241
+ filters.nodeIds != null ? inArray(nodes.id, filters.nodeIds) : undefined,
242
+ filters.types != null ? inArray(nodes.type, filters.types) : undefined,
243
+ archivedFilter,
244
+ filters.hidden != null ? eq(nodes.hidden, filters.hidden) : undefined,
245
+ parentFilter,
246
+ eq(nodes.namespace, filters.namespace),
247
+ search,
248
+ ),
249
+ )
250
+ .orderBy(orderBy as SQL);
251
+
252
+ return items.map(toLocalDriveNode);
253
+ }
254
+
255
+ /**
256
+ * Lists tree-scoped nodes with pagination, search, and richer local filters.
257
+ *
258
+ * This query is intended for admin table views where additional local fields
259
+ * such as subtype and contentType are part of the filtering contract.
260
+ *
261
+ * @param input - Table query input with tree filters.
262
+ * @returns A paginated table-query response.
263
+ */
264
+ async listTree({
265
+ filters,
266
+ ...query
267
+ }: ListLocalDriveTreeSchema): Promise<TableQueryResponse<LocalDriveNodeWithAsset>> {
268
+ const orderBy = convertOrderByToQueryParams(query, nodes, desc(nodes.createdAt));
269
+ const search = convertSearchToQueryParams(query, [nodes.name]);
270
+
271
+ const archivedFilter =
272
+ filters.isArchived === true
273
+ ? isNotNull(nodes.archivedAt)
274
+ : filters.isArchived === false
275
+ ? isNull(nodes.archivedAt)
276
+ : undefined;
277
+ const parentFilter = filters.parentId
278
+ ? eq(nodes.parentId, filters.parentId)
279
+ : isNull(nodes.parentId);
280
+
281
+ const limit = query.limit;
282
+ const cursor = query.cursor;
283
+ const offset = cursor * limit;
284
+
285
+ const where = and(
286
+ filters.nodeIds != null ? inArray(nodes.id, filters.nodeIds) : undefined,
287
+ filters.types != null ? inArray(nodes.type, filters.types) : undefined,
288
+ filters.subtypes != null ? inArray(nodes.subtype, filters.subtypes) : undefined,
289
+ filters.contentTypes != null ? inArray(nodes.contentType, filters.contentTypes) : undefined,
290
+ archivedFilter,
291
+ filters.hidden != null ? eq(nodes.hidden, filters.hidden) : undefined,
292
+ parentFilter,
293
+ eq(nodes.namespace, filters.namespace),
294
+ search,
295
+ );
296
+
297
+ const [data, total] = await this.#db.transaction(async (tx) => {
298
+ const data = await tx
299
+ .select({
300
+ ...getTableColumns(nodes),
301
+ asset: storageAssets,
302
+ })
303
+ .from(nodes)
304
+ .where(where)
305
+ .leftJoin(storageAssets, eq(nodes.assetId, storageAssets.id))
306
+ .orderBy(orderBy as SQL)
307
+ .limit(limit)
308
+ .offset(offset);
309
+
310
+ const total = await tx.$count(nodes, where);
311
+ return [data, total] as const;
312
+ });
313
+
314
+ return createTableQueryResponse({
315
+ data: data.map(({ asset, ...node }) => ({ ...toLocalDriveNode(node), asset })),
316
+ input: query,
317
+ total,
318
+ });
319
+ }
320
+
321
+ /**
322
+ * Lists flat nodes with pagination, search, and richer local filters.
323
+ *
324
+ * Unlike `listTree`, this query ignores parent scoping and is intended for
325
+ * flat file-manager style views.
326
+ *
327
+ * @param input - Table query input with flat filters.
328
+ * @returns A paginated table-query response.
329
+ */
330
+ async listFlat({
331
+ filters,
332
+ ...query
333
+ }: ListLocalDriveFlatSchema): Promise<TableQueryResponse<LocalDriveNodeWithAsset>> {
334
+ const orderBy = convertOrderByToQueryParams(query, nodes, desc(nodes.createdAt));
335
+ const search = convertSearchToQueryParams(query, [nodes.name]);
336
+ const archivedFilter =
337
+ filters.isArchived === true
338
+ ? isNotNull(nodes.archivedAt)
339
+ : filters.isArchived === false
340
+ ? isNull(nodes.archivedAt)
341
+ : undefined;
342
+ const limit = query.limit;
343
+ const cursor = query.cursor;
344
+ const offset = cursor * limit;
345
+
346
+ const where = and(
347
+ filters.nodeIds != null ? inArray(nodes.id, filters.nodeIds) : undefined,
348
+ filters.types != null ? inArray(nodes.type, filters.types) : undefined,
349
+ filters.subtypes != null ? inArray(nodes.subtype, filters.subtypes) : undefined,
350
+ filters.contentTypes != null ? inArray(nodes.contentType, filters.contentTypes) : undefined,
351
+ archivedFilter,
352
+ filters.hidden != null ? eq(nodes.hidden, filters.hidden) : undefined,
353
+ filters.namespace ? eq(nodes.namespace, filters.namespace) : undefined,
354
+ search,
355
+ );
356
+
357
+ const [data, total] = await this.#db.transaction(async (tx) => {
358
+ const data = await tx
359
+ .select({
360
+ ...getTableColumns(nodes),
361
+ asset: storageAssets,
362
+ })
363
+ .from(nodes)
364
+ .where(where)
365
+ .leftJoin(storageAssets, eq(nodes.assetId, storageAssets.id))
366
+ .orderBy(orderBy as SQL)
367
+ .limit(limit)
368
+ .offset(offset);
369
+
370
+ const total = await tx.$count(nodes, where);
371
+ return [data, total] as const;
372
+ });
373
+
374
+ return createTableQueryResponse({
375
+ data: data.map(({ asset, ...node }) => ({ ...toLocalDriveNode(node), asset })),
376
+ input: query,
377
+ total,
378
+ });
379
+ }
380
+
381
+ /**
382
+ * Resolves the ancestor chain for a folder or file node.
383
+ *
384
+ * The returned list starts with the root-most ancestor and ends with the
385
+ * requested node. This is primarily used for breadcrumb navigation.
386
+ *
387
+ * @param input - The node id and namespace to resolve.
388
+ * @returns A promise resolving to the ordered ancestor chain.
389
+ */
390
+ async getFolderParents(input: { id: string | null; namespace: string }) {
391
+ if (!input.id) return [];
392
+
393
+ const startNodeQuery = this.#db
394
+ .select({
395
+ id: nodes.id,
396
+ name: nodes.name,
397
+ parentId: nodes.parentId,
398
+ depth: sql<number>`0`.as("depth"),
399
+ })
400
+ .from(nodes)
401
+ .where(and(eq(nodes.id, input.id), eq(nodes.namespace, input.namespace)));
402
+
403
+ const alias = "parent_nodes";
404
+ const parentNodesQueryAlias = startNodeQuery.as(alias);
405
+ const recursiveQueryName = sql.raw(`"${alias}"`);
406
+
407
+ const recursiveQuery = startNodeQuery.unionAll(
408
+ this.#db
409
+ .select({
410
+ id: nodes.id,
411
+ name: nodes.name,
412
+ parentId: nodes.parentId,
413
+ depth: sql<number>`${parentNodesQueryAlias.depth} + 1`,
414
+ })
415
+ .from(nodes)
416
+ .innerJoin(recursiveQueryName, eq(nodes.id, parentNodesQueryAlias.parentId)),
417
+ );
418
+
419
+ const result = (await this.#db.execute(
420
+ sql`WITH RECURSIVE ${recursiveQueryName} AS ${recursiveQuery} SELECT * FROM ${recursiveQueryName}`,
421
+ )) as QueryResult<Pick<LocalNode, "id" | "name" | "parentId"> & { depth: number }>;
422
+
423
+ return result.rows?.toSorted((a, b) => Number(b.depth) - Number(a.depth)) ?? [];
424
+ }
425
+
426
+ /**
427
+ * Retrieves the structural subtree for one or more root node ids.
428
+ *
429
+ * The result includes the provided start nodes and all descendant nodes,
430
+ * returned as a lightweight shape containing only the fields needed for
431
+ * recursive drive operations such as delete, archive, and validation.
432
+ *
433
+ * Depth starts at `0` for the provided root nodes and increases for each
434
+ * descendant level. Duplicate paths are collapsed by keeping the minimum depth.
435
+ *
436
+ * @param ids - Root node ids to traverse from.
437
+ * @returns A promise resolving to the subtree rows for the provided nodes.
438
+ */
439
+ async getNodeChildren(ids: string[]) {
440
+ if (ids.length === 0) return [];
441
+
442
+ const startNodeQuery = this.#db
443
+ .select({
444
+ id: nodes.id,
445
+ assetId: nodes.assetId,
446
+ type: nodes.type,
447
+ parentId: nodes.parentId,
448
+ depth: sql<number>`0`.as("depth"),
449
+ })
450
+ .from(nodes)
451
+ .where(inArray(nodes.id, ids));
452
+
453
+ const alias = "child_nodes";
454
+ const childNodesQueryAlias = startNodeQuery.as(alias);
455
+ const recursiveQueryName = sql.raw(`"${alias}"`);
456
+
457
+ const recursiveQuery = startNodeQuery.unionAll(
458
+ this.#db
459
+ .select({
460
+ id: nodes.id,
461
+ assetId: nodes.assetId,
462
+ type: nodes.type,
463
+ parentId: nodes.parentId,
464
+ depth: sql<number>`${childNodesQueryAlias.depth} + 1`,
465
+ })
466
+ .from(nodes)
467
+ .innerJoin(recursiveQueryName, eq(nodes.parentId, childNodesQueryAlias.id)),
468
+ );
469
+
470
+ const result = (await this.#db.execute(
471
+ sql`WITH RECURSIVE ${recursiveQueryName} AS ${recursiveQuery}
472
+ SELECT
473
+ ${childNodesQueryAlias.id} as id,
474
+ ${childNodesQueryAlias.assetId} as "assetId",
475
+ ${childNodesQueryAlias.type} as type,
476
+ ${childNodesQueryAlias.parentId} as "parentId",
477
+ MIN(${childNodesQueryAlias.depth})::int as depth
478
+ FROM ${recursiveQueryName}
479
+ GROUP BY
480
+ ${childNodesQueryAlias.id},
481
+ ${childNodesQueryAlias.assetId},
482
+ ${childNodesQueryAlias.type},
483
+ ${childNodesQueryAlias.parentId}
484
+ `,
485
+ )) as QueryResult<Pick<LocalNode, "id" | "assetId" | "type" | "parentId"> & { depth: number }>;
486
+
487
+ return (result.rows ?? []) as LocalDriveNodeChild[];
488
+ }
489
+
490
+ /**
491
+ * Resolves an access URL for a file node and the requested variant.
492
+ *
493
+ * Behavior:
494
+ * - Rejects folder nodes because only file nodes can resolve a file URL
495
+ * - Reuses a cached URL when an unexpired entry exists for the requested
496
+ * variant and disposition
497
+ * - Resolves the requested preview variant asset when available
498
+ * - Falls back to the main file asset when the requested variant does not exist
499
+ * - Delegates final URL generation to the storage service
500
+ * - Stores the generated URL in the local cache table for future reuse
501
+ *
502
+ * @param node - The file node to resolve.
503
+ * @param options - URL options such as variant and content disposition.
504
+ * @returns A promise resolving to a temporary file URL.
505
+ */
506
+ async getURL(node: LocalNode, options: GetLocalFileURLSchema = getLocalFileURLSchemaDefaults) {
507
+ if (!isLocalDriveFile(node)) {
508
+ throw new ServerError("BAD_REQUEST", { message: "Node is not a file" });
509
+ }
510
+
511
+ const [presignedUrl] = await this.#db
512
+ .select({ url: nodePresignedUrls.url, expiresAt: nodePresignedUrls.expiresAt })
513
+ .from(nodePresignedUrls)
514
+ .where(
515
+ and(
516
+ eq(nodePresignedUrls.nodeId, node.id),
517
+ eq(nodePresignedUrls.variant, options.variant),
518
+ eq(nodePresignedUrls.disposition, options.disposition),
519
+ ),
520
+ );
521
+
522
+ if (presignedUrl && presignedUrl.expiresAt > new Date()) return presignedUrl.url;
523
+
524
+ const expiresIn = 3600 * 24; // 24 hours
525
+
526
+ const [variantRecord] =
527
+ options.variant === "main"
528
+ ? []
529
+ : await this.#db
530
+ .select({
531
+ id: nodeVariants.id,
532
+ assetId: nodeVariants.assetId,
533
+ variant: nodeVariants.variant,
534
+ })
535
+ .from(nodeVariants)
536
+ .where(
537
+ and(eq(nodeVariants.nodeId, node.id), eq(nodeVariants.variant, options.variant)),
538
+ );
539
+
540
+ // If the requested variant does not exist, fallback to main
541
+ const variant = variantRecord ? options.variant : "main";
542
+ const assetId = variantRecord?.assetId ?? node.assetId;
543
+
544
+ if (!assetId) {
545
+ throw new ServerError("NOT_FOUND", { message: "Storage asset not found" });
546
+ }
547
+
548
+ console.info(
549
+ `Generating new signed url for file: ${node.id} with variant: ${variant} and disposition: ${options.disposition}`,
550
+ );
551
+
552
+ // Generate the presigned url that expires in 24 hours
553
+ const url = await this.storage.getObjectURL(assetId, {
554
+ disposition: `${options.disposition}; filename="${node.name}"`,
555
+ expiresIn,
556
+ });
557
+
558
+ // Add the presigned url to the database
559
+ after(async () => {
560
+ await this.#db
561
+ .insert(nodePresignedUrls)
562
+ .values({
563
+ nodeId: node.id,
564
+ url,
565
+ variant,
566
+ variantId: variantRecord?.id,
567
+ disposition: options.disposition,
568
+ expiresAt: addSeconds(new Date(), expiresIn),
569
+ })
570
+ .onConflictDoUpdate({
571
+ target: [
572
+ nodePresignedUrls.nodeId,
573
+ nodePresignedUrls.variant,
574
+ nodePresignedUrls.disposition,
575
+ ],
576
+ set: { url, expiresAt: addSeconds(new Date(), expiresIn) },
577
+ });
578
+ });
579
+
580
+ return url;
581
+ }
582
+
583
+ /**
584
+ * Moves a node to a different parent folder.
585
+ *
586
+ * The target parent is validated before the move is persisted. Moving a node
587
+ * into itself or into one of its descendants is rejected to keep the tree
588
+ * structure valid.
589
+ *
590
+ * @param input - The node id and the target parent id.
591
+ * @returns A promise resolving to the updated node.
592
+ * @throws {ServerError} If the move is invalid.
593
+ */
594
+ async moveNode(input: { id: string; parentId: string | null }) {
595
+ const [node] = await this.#db.select().from(nodes).where(eq(nodes.id, input.id)).limit(1);
596
+
597
+ if (!node) {
598
+ throw new ServerError("BAD_REQUEST", { message: "Node not found" });
599
+ }
600
+
601
+ if (node.readonly) {
602
+ throw new ServerError("BAD_REQUEST", {
603
+ message: "Deze node is alleen leesbaar en kan niet worden gewijzigd",
604
+ });
605
+ }
606
+
607
+ if (input.parentId === node.id) {
608
+ throw new ServerError("BAD_REQUEST", { message: "Node cannot be moved into itself" });
609
+ }
610
+
611
+ if (input.parentId !== undefined) {
612
+ await this.#getWritableParent({ parentId: input.parentId, namespace: node.namespace });
613
+ }
614
+
615
+ if (input.parentId) {
616
+ const subtree = await this.getNodeChildren([input.id]);
617
+
618
+ if (subtree.some((child) => child.id === input.parentId)) {
619
+ throw new ServerError("BAD_REQUEST", {
620
+ message: "Node cannot be moved into one of its descendants",
621
+ });
622
+ }
623
+ }
624
+
625
+ if (node.parentId === input.parentId) {
626
+ return toLocalDriveNode(node);
627
+ }
628
+
629
+ const [result] = await this.#db
630
+ .update(nodes)
631
+ .set({ parentId: input.parentId })
632
+ .where(eq(nodes.id, input.id))
633
+ .returning();
634
+
635
+ if (!result) {
636
+ throw new ServerError("INTERNAL_SERVER_ERROR", {
637
+ message: "Node kon niet worden gewijzigd",
638
+ });
639
+ }
640
+
641
+ return toLocalDriveNode(result);
642
+ }
643
+
644
+ /**
645
+ * Uploads a file through the storage service and creates the matching node.
646
+ *
647
+ * Behavior:
648
+ * - Validates the parent folder when `parentId` is provided
649
+ * - Uploads the file bytes through the storage service
650
+ * - Persists the created storage asset id on the node
651
+ * - Cleans up the uploaded asset when node creation fails
652
+ *
653
+ * @param input - File metadata and body.
654
+ * @returns A promise resolving to the created file node.
655
+ * @throws {ServerError} If validation or persistence fails.
656
+ */
657
+ async uploadFile(input: UploadLocalDriveFileSchema & { body: ObjectBodyInput }) {
658
+ await this.#getWritableParent({ parentId: input.parentId, namespace: input.namespace });
659
+
660
+ const asset = await this.storage.upload({
661
+ name: input.name,
662
+ body: input.body,
663
+ contentType: input.contentType,
664
+ size: input.size,
665
+ visibility: "private",
666
+ });
667
+
668
+ try {
669
+ const [result] = await this.#db
670
+ .insert(nodes)
671
+ .values({
672
+ type: "file",
673
+ name: input.name,
674
+ namespace: input.namespace,
675
+ parentId: input.parentId,
676
+ size: asset.size ?? input.size,
677
+ contentType: asset.contentType ?? input.contentType,
678
+ hidden: input.hidden ?? false,
679
+ readonly: input.readonly ?? false,
680
+ subtype: inferLocalDriveNodeSubtype(input),
681
+ assetId: asset.id,
682
+ })
683
+ .returning();
684
+
685
+ if (!result) {
686
+ throw new ServerError("INTERNAL_SERVER_ERROR", {
687
+ message: "Oep! Er is iets fout gegaan",
688
+ });
689
+ }
690
+
691
+ return toLocalDriveNode(result);
692
+ } catch (error) {
693
+ await this.storage.purgeAsset(asset.id).catch(() => undefined);
694
+ throw error;
695
+ }
696
+ }
697
+
698
+ /**
699
+ * Creates a direct upload intent and the matching file node.
700
+ *
701
+ * Behavior:
702
+ * - Validates the parent folder when `parentId` is provided
703
+ * - Requests a presigned upload from the storage service
704
+ * - Creates the file node linked to the pending storage asset
705
+ * - Purges the pending asset when node creation fails
706
+ *
707
+ * @param input - File metadata used to create the upload intent.
708
+ * @returns The created asset intent, presigned URL, and file node.
709
+ * @throws {ServerError} If validation or node creation fails.
710
+ */
711
+ async presignUpload(input: PresignLocalDriveFileInput) {
712
+ await this.#getWritableParent(input);
713
+
714
+ // Generate the presigned url
715
+ const { presignedUrl, ...asset } = await this.storage.presignUpload({
716
+ uploadId: input.uploadId,
717
+ name: input.name,
718
+ contentType: input.contentType,
719
+ size: input.size,
720
+ visibility: input.visibility,
721
+ });
722
+
723
+ try {
724
+ const [node] = await this.#db
725
+ .insert(nodes)
726
+ .values({
727
+ type: "file",
728
+ name: input.name,
729
+ namespace: input.namespace,
730
+ parentId: input.parentId,
731
+ size: asset.size ?? input.size,
732
+ contentType: asset.contentType ?? input.contentType,
733
+ hidden: input.hidden ?? false,
734
+ readonly: input.readonly ?? false,
735
+ assetId: asset.id,
736
+ subtype: inferLocalDriveNodeSubtype(input),
737
+ })
738
+ .returning();
739
+
740
+ if (!node) {
741
+ throw new ServerError("INTERNAL_SERVER_ERROR", {
742
+ message: "Oep! Er is iets fout gegaan",
743
+ });
744
+ }
745
+
746
+ return {
747
+ uploadId: asset.uploadId,
748
+ presignedUrl,
749
+ node: toLocalDriveNode(node) as LocalDriveFileNode,
750
+ asset,
751
+ };
752
+ } catch (error) {
753
+ await this.storage.purgeAsset(asset.id).catch(() => undefined);
754
+ throw error;
755
+ }
756
+ }
757
+
758
+ /**
759
+ * Finalizes a presigned upload and synchronizes the node metadata.
760
+ *
761
+ * Behavior:
762
+ * - Confirms the pending asset through the storage service
763
+ * - Finds the matching node by `assetId`
764
+ * - Synchronizes final asset metadata onto the node
765
+ * - Schedules preview generation for image files
766
+ *
767
+ * @param uploadId - The storage upload id returned by the presign step.
768
+ * @returns A promise resolving to the finalized file node.
769
+ * @throws {ServerError} If the asset or node cannot be resolved.
770
+ */
771
+ async confirmUpload(uploadId: string) {
772
+ const asset = await this.storage.confirmUpload(uploadId);
773
+
774
+ const [node] = await this.#db.select().from(nodes).where(eq(nodes.assetId, asset.id)).limit(1);
775
+
776
+ if (!node) {
777
+ throw new ServerError("NOT_FOUND", { message: "File not found" });
778
+ }
779
+
780
+ const [result] = await this.#db
781
+ .update(nodes)
782
+ .set({
783
+ size: asset.size ?? node.size,
784
+ contentType: asset.contentType ?? node.contentType,
785
+ })
786
+ .where(eq(nodes.assetId, asset.id))
787
+ .returning();
788
+
789
+ if (!result) {
790
+ throw new ServerError("NOT_FOUND", { message: "File not found" });
791
+ }
792
+
793
+ /**
794
+ * Generate the preview version of the file
795
+ */
796
+ after(async () => {
797
+ await this.generatePreviews({ id: result.id });
798
+ });
799
+
800
+ return toLocalDriveNode(result) as LocalDriveFileNode;
801
+ }
802
+
803
+ /**
804
+ * Generates preview assets for an image node.
805
+ *
806
+ * The original file is loaded from storage, resized to the configured device
807
+ * widths, uploaded as separate storage assets, and linked through
808
+ * `node_variants`. Existing preview variants are skipped.
809
+ *
810
+ * @param input - The target file node identifier.
811
+ * @returns A promise that resolves once preview generation completes.
812
+ * @throws {ServerError} If the source node or asset cannot be resolved.
813
+ */
814
+ async generatePreviews(input: { id: string }) {
815
+ const [node] = await this.#db.select().from(nodes).where(eq(nodes.id, input.id)).limit(1);
816
+
817
+ if (!node) {
818
+ throw new ServerError("NOT_FOUND", { message: "Node not found" });
819
+ }
820
+
821
+ if (!node.assetId) {
822
+ throw new ServerError("BAD_REQUEST", { message: "Node has no storage asset" });
823
+ }
824
+
825
+ /**
826
+ * Get the main version of the file
827
+ */
828
+ const response = await this.storage.getObject(node.assetId);
829
+
830
+ const contentType = response.contentType;
831
+ if (!response.body) {
832
+ throw new ServerError("INTERNAL_SERVER_ERROR", {
833
+ message: "Oeps! Er is iets fout gegaan",
834
+ });
835
+ }
836
+
837
+ /**
838
+ * Transform the main version of the file to a buffer
839
+ */
840
+ const buffer = await stream.buffer(response.body);
841
+
842
+ /**
843
+ * Generate the preview versions for images
844
+ */
845
+ if (contentType?.startsWith("image/")) {
846
+ const sharp = await import("sharp");
847
+ const sourceAsset = await this.storage.getAssetById(node.assetId);
848
+
849
+ if (!sourceAsset) {
850
+ throw new ServerError("NOT_FOUND", { message: "Storage asset not found" });
851
+ }
852
+
853
+ const existingVariants = await this.#db
854
+ .select({ variant: nodeVariants.variant })
855
+ .from(nodeVariants)
856
+ .where(eq(nodeVariants.nodeId, input.id));
857
+
858
+ const existingVariantNames = new Set(existingVariants.map((variant) => variant.variant));
859
+
860
+ // Generate the preview versions
861
+ await Promise.allSettled(
862
+ deviceSizes.flatMap(async (width) => {
863
+ const variant = `preview-${width}` as const;
864
+ if (existingVariantNames.has(variant)) return;
865
+
866
+ // Generate the preview
867
+ const preview = await sharp.default(buffer).resize({ width }).webp().toBuffer();
868
+
869
+ // Upload the preview and add the variant to the database
870
+ return this.#db.transaction(async (tx) => {
871
+ const asset = await this.storage.upload({
872
+ body: preview,
873
+ name: `${input.id}-preview-${width}.webp`,
874
+ contentType: "image/webp",
875
+ size: preview.byteLength,
876
+ visibility: sourceAsset.visibility,
877
+ });
878
+
879
+ try {
880
+ await tx.insert(nodeVariants).values({
881
+ nodeId: input.id,
882
+ assetId: asset.id,
883
+ variant,
884
+ width,
885
+ });
886
+ } catch (error) {
887
+ await this.storage.purgeAsset(asset.id).catch(() => undefined);
888
+ throw error;
889
+ }
890
+ });
891
+ }),
892
+ );
893
+ }
894
+ }
895
+
896
+ /**
897
+ * Creates a folder node in the drive tree.
898
+ *
899
+ * The parent folder is validated before the folder is inserted. Folders do
900
+ * not own a storage asset and are stored purely as drive metadata.
901
+ *
902
+ * @param input - Folder metadata.
903
+ * @returns A promise resolving to the created folder node.
904
+ * @throws {ServerError} If validation or insertion fails.
905
+ */
906
+ async createFolder(input: CreateLocalDriveFolderSchema) {
907
+ await this.#getWritableParent({ parentId: input.parentId, namespace: input.namespace });
908
+
909
+ /**
910
+ * Create the folder
911
+ */
912
+ const [result] = await this.#db
913
+ .insert(nodes)
914
+ .values({
915
+ type: "folder",
916
+ name: input.name,
917
+ namespace: input.namespace,
918
+ parentId: input.parentId,
919
+ hidden: input.hidden ?? false,
920
+ readonly: input.readonly ?? false,
921
+ })
922
+ .returning();
923
+
924
+ if (!result) {
925
+ throw new ServerError("INTERNAL_SERVER_ERROR", {
926
+ message: "Folder kon niet worden aangemaakt",
927
+ });
928
+ }
929
+
930
+ return toLocalDriveNode(result);
931
+ }
932
+
933
+ /**
934
+ * Updates mutable node metadata.
935
+ *
936
+ * Supported updates currently include renaming, reparenting, visibility flags,
937
+ * and archive state. Structural updates are validated before being persisted.
938
+ *
939
+ * @param id - The target node id.
940
+ * @param data - Partial node fields to update.
941
+ * @returns A promise resolving to the updated node.
942
+ * @throws {ServerError} If the node does not exist or the update is invalid.
943
+ */
944
+ async updateNode(id: string, data: UpdateLocalDriveNodeInput) {
945
+ const [node] = await this.#db.select().from(nodes).where(eq(nodes.id, id)).limit(1);
946
+
947
+ if (!node) {
948
+ throw new ServerError("NOT_FOUND", { message: "Node not found" });
949
+ }
950
+
951
+ if (data.parentId !== undefined) {
952
+ await this.#getWritableParent({ parentId: data.parentId, namespace: node.namespace });
953
+ }
954
+
955
+ const [result] = await this.#db
956
+ .update(nodes)
957
+ .set({
958
+ name: data.name,
959
+ parentId: data.parentId,
960
+ hidden: data.hidden,
961
+ readonly: data.readonly,
962
+ archivedAt: data.archivedAt,
963
+ })
964
+ .where(eq(nodes.id, id))
965
+ .returning();
966
+
967
+ if (!result) {
968
+ throw new ServerError("INTERNAL_SERVER_ERROR", {
969
+ message: "Node kon niet worden gewijzigd",
970
+ });
971
+ }
972
+
973
+ return toLocalDriveNode(result);
974
+ }
975
+
976
+ /**
977
+ * Sets the readonly state for multiple nodes.
978
+ *
979
+ * This method is intended for bulk lock and unlock operations in the drive UI.
980
+ *
981
+ * @param ids - The node ids to update.
982
+ * @param readonly - The readonly state to apply.
983
+ * @returns A promise resolving to the updated nodes.
984
+ * @throws {ServerError} If no ids are provided or no matching nodes are found.
985
+ */
986
+ async setReadonly(ids: string[], readonly: boolean) {
987
+ if (ids.length === 0) {
988
+ throw new ServerError("BAD_REQUEST", { message: "No node ids provided" });
989
+ }
990
+
991
+ const result = await this.#db
992
+ .update(nodes)
993
+ .set({ readonly })
994
+ .where(inArray(nodes.id, [...new Set(ids)]))
995
+ .returning();
996
+
997
+ if (result.length === 0) {
998
+ throw new ServerError("BAD_REQUEST", { message: "No matching nodes found" });
999
+ }
1000
+
1001
+ return result.map(toLocalDriveNode);
1002
+ }
1003
+
1004
+ /**
1005
+ * Archives nodes by setting their archive timestamp.
1006
+ *
1007
+ * This is a drive-level archive state and does not delete the underlying
1008
+ * storage assets.
1009
+ *
1010
+ * @param ids - The node ids to archive.
1011
+ * @returns A promise resolving to the archived nodes.
1012
+ * @throws {ServerError} If no ids are provided or no matching nodes are found.
1013
+ */
1014
+ async archiveNodes(ids: string[]) {
1015
+ if (ids.length === 0) {
1016
+ throw new ServerError("BAD_REQUEST", { message: "No node ids provided" });
1017
+ }
1018
+
1019
+ const result = await this.#db
1020
+ .update(nodes)
1021
+ .set({ archivedAt: new Date() })
1022
+ .where(and(inArray(nodes.id, [...new Set(ids)]), isNull(nodes.archivedAt)))
1023
+ .returning();
1024
+
1025
+ if (result.length === 0) {
1026
+ throw new ServerError("BAD_REQUEST", { message: "No matching nodes found" });
1027
+ }
1028
+
1029
+ return result.map(toLocalDriveNode);
1030
+ }
1031
+
1032
+ /**
1033
+ * Restores archived nodes by clearing their archive timestamp.
1034
+ *
1035
+ * @param ids - The node ids to restore.
1036
+ * @returns A promise resolving to the restored nodes.
1037
+ * @throws {ServerError} If no ids are provided or no matching nodes are found.
1038
+ */
1039
+ async restoreNodes(ids: string[]) {
1040
+ if (ids.length === 0) {
1041
+ throw new ServerError("BAD_REQUEST", { message: "No node ids provided" });
1042
+ }
1043
+
1044
+ const result = await this.#db
1045
+ .update(nodes)
1046
+ .set({ archivedAt: null })
1047
+ .where(and(inArray(nodes.id, [...new Set(ids)]), isNotNull(nodes.archivedAt)))
1048
+ .returning();
1049
+
1050
+ if (result.length === 0) {
1051
+ throw new ServerError("BAD_REQUEST", { message: "No matching nodes found" });
1052
+ }
1053
+
1054
+ return result.map(toLocalDriveNode);
1055
+ }
1056
+
1057
+ /**
1058
+ * Permanently deletes a node subtree and all linked assets.
1059
+ *
1060
+ * The subtree includes the provided node and all descendants. Associated file
1061
+ * assets and generated preview assets are purged from storage after the nodes
1062
+ * are removed from the drive catalog.
1063
+ *
1064
+ * @param id - The root node id to delete.
1065
+ * @throws {ServerError} If the node does not exist.
1066
+ */
1067
+ async deleteNode(id: string) {
1068
+ const subtree = await this.getNodeChildren([id]); // includes root + descendants
1069
+ if (subtree.length === 0) {
1070
+ throw new ServerError("NOT_FOUND", { message: "Node not found" });
1071
+ }
1072
+
1073
+ const nodeIds = [...new Set(subtree.map((node) => node.id))];
1074
+ const assetIds = await this.#getSubtreeAssetIds(nodeIds, subtree);
1075
+
1076
+ // then DB cleanup
1077
+ await this.#db.delete(nodes).where(inArray(nodes.id, nodeIds));
1078
+
1079
+ if (assetIds.length > 0) {
1080
+ await this.storage.purgeAssets(assetIds);
1081
+ }
1082
+ }
1083
+
1084
+ /**
1085
+ * Permanently deletes multiple node subtrees and their linked assets.
1086
+ *
1087
+ * Each provided id is treated as a subtree root. All descendant nodes are
1088
+ * removed together with their main file assets and generated preview assets.
1089
+ *
1090
+ * @param ids - Root node ids to delete.
1091
+ * @returns A promise that resolves when deletion completes.
1092
+ * @throws {ServerError} If no ids are provided or no matching nodes are found.
1093
+ */
1094
+ async deleteNodes(ids: string[]) {
1095
+ if (ids.length === 0) {
1096
+ throw new ServerError("BAD_REQUEST", { message: "No node ids provided" });
1097
+ }
1098
+
1099
+ const subtree = await this.getNodeChildren(ids);
1100
+ if (subtree.length === 0) {
1101
+ throw new ServerError("BAD_REQUEST", { message: "No matching nodes found" });
1102
+ }
1103
+
1104
+ const nodeIds = [...new Set(subtree.map((node) => node.id))];
1105
+ const assetIds = await this.#getSubtreeAssetIds(nodeIds, subtree);
1106
+
1107
+ /**
1108
+ * Delete files and folders in a database
1109
+ */
1110
+ await this.#db.delete(nodes).where(inArray(nodes.id, nodeIds));
1111
+
1112
+ if (assetIds.length > 0) {
1113
+ await this.storage.purgeAssets(assetIds);
1114
+ }
1115
+ }
1116
+ }