@tulip-systems/drive 0.8.1 → 0.8.3

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 (141) hide show
  1. package/dist/client.d.mts +5 -0
  2. package/dist/client.mjs +6 -0
  3. package/dist/components/content.d.mts +13 -0
  4. package/dist/components/content.mjs +17 -0
  5. package/dist/components/context.client.d.mts +13 -0
  6. package/dist/components/dnd.client.d.mts +16 -0
  7. package/dist/components/dnd.client.mjs +29 -0
  8. package/dist/components/grid-card.client.d.mts +46 -0
  9. package/dist/components/grid-card.client.mjs +173 -0
  10. package/dist/components/grid.client.d.mts +53 -0
  11. package/dist/components/grid.client.mjs +53 -0
  12. package/dist/components/navigation/breadcrumbs.client.d.mts +25 -0
  13. package/dist/components/navigation/breadcrumbs.client.mjs +51 -0
  14. package/dist/components/navigation/header.client.d.mts +27 -0
  15. package/dist/components/navigation/header.client.mjs +40 -0
  16. package/dist/components/navigation/toolbar.client.d.mts +23 -0
  17. package/dist/components/navigation/toolbar.client.mjs +34 -0
  18. package/dist/components/navigation/view-switcher.client.d.mts +9 -0
  19. package/dist/components/navigation/view-switcher.client.mjs +35 -0
  20. package/dist/components/selection.client.d.mts +27 -0
  21. package/dist/components/selection.client.mjs +36 -0
  22. package/dist/components/view.client.d.mts +25 -0
  23. package/dist/components/view.client.mjs +43 -0
  24. package/dist/config/filters.mjs +14 -0
  25. package/dist/config/types.mjs +60 -0
  26. package/dist/google/client.d.mts +8 -0
  27. package/dist/google/client.mjs +9 -0
  28. package/dist/google/server.d.mts +3 -0
  29. package/dist/google/server.mjs +4 -0
  30. package/dist/google.d.mts +6 -0
  31. package/dist/google.mjs +7 -0
  32. package/dist/index.d.mts +7 -0
  33. package/dist/index.mjs +7 -0
  34. package/dist/lib/constants.d.mts +11 -0
  35. package/dist/lib/constants.mjs +20 -0
  36. package/dist/lib/contracts.d.mts +100 -0
  37. package/dist/lib/dto.d.mts +117 -0
  38. package/dist/lib/dto.mjs +57 -0
  39. package/dist/lib/helpers.d.mts +17 -0
  40. package/dist/lib/helpers.mjs +36 -0
  41. package/dist/lib/helpers.server.d.mts +13 -0
  42. package/dist/lib/helpers.server.mjs +15 -0
  43. package/dist/lib/search-params.d.mts +8 -0
  44. package/dist/lib/search-params.mjs +7 -0
  45. package/dist/lib/validators.d.mts +157 -0
  46. package/dist/lib/validators.mjs +65 -0
  47. package/dist/local/client.d.mts +13 -0
  48. package/dist/local/client.mjs +13 -0
  49. package/dist/local/server.d.mts +4 -0
  50. package/dist/local/server.mjs +5 -0
  51. package/dist/local.d.mts +8 -0
  52. package/dist/local.mjs +9 -0
  53. package/dist/providers/google/components/command-file-update.d.mts +21 -0
  54. package/dist/providers/google/components/command-file-update.mjs +51 -0
  55. package/dist/providers/google/components/command-folder-create.d.mts +21 -0
  56. package/dist/providers/google/components/command-folder-create.mjs +58 -0
  57. package/dist/providers/google/components/command-folder-update.d.mts +21 -0
  58. package/dist/providers/google/components/command-folder-update.mjs +51 -0
  59. package/dist/providers/google/components/content.client.d.mts +9 -0
  60. package/dist/providers/google/components/content.client.mjs +10 -0
  61. package/dist/providers/google/components/navigation.client.d.mts +15 -0
  62. package/dist/providers/google/components/navigation.client.mjs +17 -0
  63. package/dist/providers/google/components/provider.client.d.mts +32 -0
  64. package/dist/providers/google/components/provider.client.mjs +42 -0
  65. package/dist/providers/google/components/view.client.d.mts +40 -0
  66. package/dist/providers/google/components/view.client.mjs +96 -0
  67. package/dist/providers/google/config/columns-data.d.mts +7 -0
  68. package/dist/providers/google/config/columns-data.mjs +69 -0
  69. package/dist/providers/google/config/filters.d.mts +15 -0
  70. package/dist/providers/google/config/filters.mjs +7 -0
  71. package/dist/providers/google/lib/constants.mjs +12 -0
  72. package/dist/providers/google/lib/dto.d.mts +38 -0
  73. package/dist/providers/google/lib/dto.mjs +65 -0
  74. package/dist/providers/google/lib/helpers.mjs +38 -0
  75. package/dist/providers/google/lib/router.server.d.mts +611 -0
  76. package/dist/providers/google/lib/router.server.mjs +39 -0
  77. package/dist/providers/google/lib/search-params.d.mts +14 -0
  78. package/dist/providers/google/lib/search-params.mjs +11 -0
  79. package/dist/providers/google/lib/service.server.d.mts +185 -0
  80. package/dist/providers/google/lib/service.server.mjs +610 -0
  81. package/dist/providers/google/lib/validators.d.mts +302 -0
  82. package/dist/providers/google/lib/validators.mjs +58 -0
  83. package/dist/providers/local/components/command-file-update.d.mts +17 -0
  84. package/dist/providers/local/components/command-file-update.mjs +47 -0
  85. package/dist/providers/local/components/command-file-upload.d.mts +10 -0
  86. package/dist/providers/local/components/command-file-upload.mjs +34 -0
  87. package/dist/providers/local/components/command-folder-create.d.mts +17 -0
  88. package/dist/providers/local/components/command-folder-create.mjs +54 -0
  89. package/dist/providers/local/components/command-folder-update.d.mts +17 -0
  90. package/dist/providers/local/components/command-folder-update.mjs +47 -0
  91. package/dist/providers/local/components/content.client.d.mts +6 -0
  92. package/dist/providers/local/components/content.client.mjs +7 -0
  93. package/dist/providers/local/components/navigation.client.d.mts +15 -0
  94. package/dist/providers/local/components/navigation.client.mjs +17 -0
  95. package/dist/providers/local/components/provider.client.d.mts +39 -0
  96. package/dist/providers/local/components/provider.client.mjs +60 -0
  97. package/dist/providers/local/components/upload-zone-context.client.d.mts +37 -0
  98. package/dist/providers/local/components/upload-zone-context.client.mjs +22 -0
  99. package/dist/providers/local/components/upload-zone.client.d.mts +29 -0
  100. package/dist/providers/local/components/upload-zone.client.mjs +146 -0
  101. package/dist/providers/local/components/view.client.d.mts +31 -0
  102. package/dist/providers/local/components/view.client.mjs +90 -0
  103. package/dist/providers/local/config/columns-data.d.mts +7 -0
  104. package/dist/providers/local/config/columns-data.mjs +69 -0
  105. package/dist/providers/local/config/filters.d.mts +25 -0
  106. package/dist/providers/local/config/filters.mjs +14 -0
  107. package/dist/providers/local/lib/constants.d.mts +11 -0
  108. package/dist/providers/local/lib/constants.mjs +28 -0
  109. package/dist/providers/local/lib/helpers.d.mts +44 -0
  110. package/dist/providers/local/lib/helpers.mjs +109 -0
  111. package/dist/providers/local/lib/route-handler.server.d.mts +33 -0
  112. package/dist/providers/local/lib/route-handler.server.mjs +113 -0
  113. package/dist/providers/local/lib/router.server.d.mts +41647 -0
  114. package/dist/providers/local/lib/router.server.mjs +51 -0
  115. package/dist/providers/local/lib/schema.d.mts +1112 -0
  116. package/dist/providers/local/lib/schema.mjs +70 -0
  117. package/dist/providers/local/lib/search-params.d.mts +13 -0
  118. package/dist/providers/local/lib/search-params.mjs +8 -0
  119. package/dist/providers/local/lib/service.server.d.mts +488 -0
  120. package/dist/providers/local/lib/service.server.mjs +667 -0
  121. package/dist/providers/local/lib/upload.client.d.mts +61 -0
  122. package/dist/providers/local/lib/upload.client.mjs +99 -0
  123. package/dist/providers/local/lib/validators.d.mts +453 -0
  124. package/dist/providers/local/lib/validators.mjs +95 -0
  125. package/dist/server.d.mts +2 -0
  126. package/dist/server.mjs +3 -0
  127. package/package.json +45 -46
  128. package/src/components/grid-card.client.tsx +17 -12
  129. package/src/components/selection.client.tsx +1 -2
  130. package/src/config/types.tsx +1 -1
  131. package/src/providers/google/components/command-file-update.tsx +2 -2
  132. package/src/providers/google/components/command-folder-create.tsx +2 -2
  133. package/src/providers/google/components/command-folder-update.tsx +2 -2
  134. package/src/providers/google/components/view.client.tsx +1 -7
  135. package/src/providers/google/lib/helpers.ts +0 -9
  136. package/src/providers/google/lib/service.server.ts +1 -3
  137. package/src/providers/local/components/command-file-update.tsx +2 -2
  138. package/src/providers/local/components/command-folder-create.tsx +2 -2
  139. package/src/providers/local/components/command-folder-update.tsx +2 -2
  140. package/src/providers/local/components/upload-zone.client.tsx +1 -1
  141. package/src/providers/local/lib/helpers.ts +0 -1
@@ -0,0 +1,667 @@
1
+ import { deviceSizes } from "./constants.mjs";
2
+ import { inferLocalDriveNodeSubtype, isLocalDriveFile, isLocalDriveFolder, toLocalDriveNode } from "./helpers.mjs";
3
+ import { nodePresignedUrls, nodeVariants, nodes } from "./schema.mjs";
4
+ import { getLocalFileURLSchemaDefaults } from "./validators.mjs";
5
+ import { storageAssets } from "@tulip-systems/core/storage";
6
+ import { and, asc, desc, eq, getTableColumns, inArray, isNotNull, isNull, sql } from "drizzle-orm";
7
+ import { ServerError } from "@tulip-systems/core/router/server";
8
+ import { after } from "next/server";
9
+ import stream from "node:stream/consumers";
10
+ import { convertOrderByToQueryParams, convertSearchToQueryParams, createTableQueryResponse } from "@tulip-systems/core/data-tables/server";
11
+ import { addSeconds } from "date-fns";
12
+
13
+ //#region src/providers/local/lib/service.server.ts
14
+ /**
15
+ * Drive Service
16
+ */
17
+ var LocalDrive = class {
18
+ #db;
19
+ storage;
20
+ constructor({ db, storage }) {
21
+ this.#db = db;
22
+ this.storage = storage;
23
+ }
24
+ /**
25
+ * Loads and validates the parent folder for write operations.
26
+ *
27
+ * The returned parent is guaranteed to exist, be a folder, belong to the
28
+ * same namespace, and not be archived. Root-level operations return `null`.
29
+ *
30
+ * @param input - Parent identifier and target namespace.
31
+ * @returns The validated parent node or `null` for root operations.
32
+ */
33
+ async #getWritableParent(input) {
34
+ if (!input.parentId) return null;
35
+ const [parent] = await this.#db.select().from(nodes).where(eq(nodes.id, input.parentId)).limit(1);
36
+ if (!parent) throw new ServerError("BAD_REQUEST", { message: "Parent not found" });
37
+ if (!isLocalDriveFolder(parent)) throw new ServerError("BAD_REQUEST", { message: "Parent is not a folder" });
38
+ if (parent.namespace !== input.namespace) throw new ServerError("BAD_REQUEST", { message: "Parent is not in the same namespace" });
39
+ if (parent.archivedAt) throw new ServerError("BAD_REQUEST", { message: "Parent is archived" });
40
+ return parent;
41
+ }
42
+ /**
43
+ * Collects the storage asset ids associated with a node subtree.
44
+ *
45
+ * This includes both the main file assets attached to file nodes and any
46
+ * generated variant assets stored in `node_variants`.
47
+ *
48
+ * @param nodeIds - Node ids that belong to the subtree.
49
+ * @param subtree - Lightweight subtree rows returned by `getNodeChildren`.
50
+ * @returns A de-duplicated list of storage asset ids.
51
+ */
52
+ async #getSubtreeAssetIds(nodeIds, subtree) {
53
+ const fileAssetIds = [...new Set(subtree.filter((node) => node.type === "file").map((node) => node.assetId).filter((assetId) => assetId != null))];
54
+ const variantAssets = await this.#db.select({ assetId: nodeVariants.assetId }).from(nodeVariants).where(inArray(nodeVariants.nodeId, nodeIds));
55
+ return [...new Set([...fileAssetIds, ...variantAssets.map((variant) => variant.assetId)])];
56
+ }
57
+ /**
58
+ * Retrieves the node row for a given id.
59
+ *
60
+ * This is a thin lookup helper that returns the raw database result for the
61
+ * matching node. It does not load children, variants, or perform namespace
62
+ * validation.
63
+ *
64
+ * @param id - The node identifier.
65
+ * @returns A promise resolving to the matching node row as a single-item array,
66
+ * or an empty array when no node exists.
67
+ */
68
+ async getNodeById(id) {
69
+ const [node] = await this.#db.select().from(nodes).where(eq(nodes.id, id));
70
+ return node ? toLocalDriveNode(node) : null;
71
+ }
72
+ /**
73
+ * Retrieves a node together with its direct children for a namespace.
74
+ *
75
+ * This is used by the drive UI when opening a node detail view that expects
76
+ * the immediate child nodes in the same payload.
77
+ *
78
+ * @param input - The target node id and namespace.
79
+ * @returns The node with its children when found, otherwise `null`.
80
+ */
81
+ async getNodeWithChildren(input) {
82
+ const node = await this.getNodeById(input.id);
83
+ if (!node || node.namespace !== input.namespace) return null;
84
+ const children = await this.getNodesByParentId({ filters: {
85
+ namespace: input.namespace,
86
+ parentId: node.id,
87
+ hidden: false,
88
+ isArchived: false
89
+ } });
90
+ return {
91
+ ...node,
92
+ children
93
+ };
94
+ }
95
+ /**
96
+ * Lists nodes within a folder scope using the current table-query filters.
97
+ *
98
+ * Behavior:
99
+ * - Scopes results to a namespace
100
+ * - Resolves either root nodes (`parentId = null`) or a specific parent folder
101
+ * - Applies optional filters such as ids, types, hidden state, and archive state
102
+ * - Applies search and sorting through the shared table-query utilities
103
+ *
104
+ * @param input - Table query input containing drive filters, search, and sorting.
105
+ * @returns A promise resolving to the matching child nodes.
106
+ */
107
+ async getNodesByParentId({ filters, ...query }) {
108
+ const orderBy = convertOrderByToQueryParams(query, nodes, asc(nodes.createdAt));
109
+ const search = convertSearchToQueryParams(query, [nodes.name]);
110
+ const archivedFilter = filters.isArchived === true ? isNotNull(nodes.archivedAt) : filters.isArchived === false ? isNull(nodes.archivedAt) : void 0;
111
+ const parentFilter = filters.parentId ? eq(nodes.parentId, filters.parentId) : isNull(nodes.parentId);
112
+ return (await this.#db.select().from(nodes).where(and(filters.nodeIds != null ? inArray(nodes.id, filters.nodeIds) : void 0, filters.types != null ? inArray(nodes.type, filters.types) : void 0, archivedFilter, filters.hidden != null ? eq(nodes.hidden, filters.hidden) : void 0, parentFilter, eq(nodes.namespace, filters.namespace), search)).orderBy(orderBy)).map(toLocalDriveNode);
113
+ }
114
+ /**
115
+ * Lists tree-scoped nodes with pagination, search, and richer local filters.
116
+ *
117
+ * This query is intended for admin table views where additional local fields
118
+ * such as subtype and contentType are part of the filtering contract.
119
+ *
120
+ * @param input - Table query input with tree filters.
121
+ * @returns A paginated table-query response.
122
+ */
123
+ async listTree({ filters, ...query }) {
124
+ const orderBy = convertOrderByToQueryParams(query, nodes, desc(nodes.createdAt));
125
+ const search = convertSearchToQueryParams(query, [nodes.name]);
126
+ const archivedFilter = filters.isArchived === true ? isNotNull(nodes.archivedAt) : filters.isArchived === false ? isNull(nodes.archivedAt) : void 0;
127
+ const parentFilter = filters.parentId ? eq(nodes.parentId, filters.parentId) : isNull(nodes.parentId);
128
+ const limit = query.limit;
129
+ const offset = query.cursor * limit;
130
+ const where = and(filters.nodeIds != null ? inArray(nodes.id, filters.nodeIds) : void 0, filters.types != null ? inArray(nodes.type, filters.types) : void 0, filters.subtypes != null ? inArray(nodes.subtype, filters.subtypes) : void 0, filters.contentTypes != null ? inArray(nodes.contentType, filters.contentTypes) : void 0, archivedFilter, filters.hidden != null ? eq(nodes.hidden, filters.hidden) : void 0, parentFilter, eq(nodes.namespace, filters.namespace), search);
131
+ const [data, total] = await this.#db.transaction(async (tx) => {
132
+ return [await tx.select({
133
+ ...getTableColumns(nodes),
134
+ asset: storageAssets
135
+ }).from(nodes).where(where).leftJoin(storageAssets, eq(nodes.assetId, storageAssets.id)).orderBy(orderBy).limit(limit).offset(offset), await tx.$count(nodes, where)];
136
+ });
137
+ return createTableQueryResponse({
138
+ data: data.map(({ asset, ...node }) => ({
139
+ ...toLocalDriveNode(node),
140
+ asset
141
+ })),
142
+ input: query,
143
+ total
144
+ });
145
+ }
146
+ /**
147
+ * Lists flat nodes with pagination, search, and richer local filters.
148
+ *
149
+ * Unlike `listTree`, this query ignores parent scoping and is intended for
150
+ * flat file-manager style views.
151
+ *
152
+ * @param input - Table query input with flat filters.
153
+ * @returns A paginated table-query response.
154
+ */
155
+ async listFlat({ filters, ...query }) {
156
+ const orderBy = convertOrderByToQueryParams(query, nodes, desc(nodes.createdAt));
157
+ const search = convertSearchToQueryParams(query, [nodes.name]);
158
+ const archivedFilter = filters.isArchived === true ? isNotNull(nodes.archivedAt) : filters.isArchived === false ? isNull(nodes.archivedAt) : void 0;
159
+ const limit = query.limit;
160
+ const offset = query.cursor * limit;
161
+ const where = and(filters.nodeIds != null ? inArray(nodes.id, filters.nodeIds) : void 0, filters.types != null ? inArray(nodes.type, filters.types) : void 0, filters.subtypes != null ? inArray(nodes.subtype, filters.subtypes) : void 0, filters.contentTypes != null ? inArray(nodes.contentType, filters.contentTypes) : void 0, archivedFilter, filters.hidden != null ? eq(nodes.hidden, filters.hidden) : void 0, filters.namespace ? eq(nodes.namespace, filters.namespace) : void 0, search);
162
+ const [data, total] = await this.#db.transaction(async (tx) => {
163
+ return [await tx.select({
164
+ ...getTableColumns(nodes),
165
+ asset: storageAssets
166
+ }).from(nodes).where(where).leftJoin(storageAssets, eq(nodes.assetId, storageAssets.id)).orderBy(orderBy).limit(limit).offset(offset), await tx.$count(nodes, where)];
167
+ });
168
+ return createTableQueryResponse({
169
+ data: data.map(({ asset, ...node }) => ({
170
+ ...toLocalDriveNode(node),
171
+ asset
172
+ })),
173
+ input: query,
174
+ total
175
+ });
176
+ }
177
+ /**
178
+ * Resolves the ancestor chain for a folder or file node.
179
+ *
180
+ * The returned list starts with the root-most ancestor and ends with the
181
+ * requested node. This is primarily used for breadcrumb navigation.
182
+ *
183
+ * @param input - The node id and namespace to resolve.
184
+ * @returns A promise resolving to the ordered ancestor chain.
185
+ */
186
+ async getFolderParents(input) {
187
+ if (!input.id) return [];
188
+ const startNodeQuery = this.#db.select({
189
+ id: nodes.id,
190
+ name: nodes.name,
191
+ parentId: nodes.parentId,
192
+ depth: sql`0`.as("depth")
193
+ }).from(nodes).where(and(eq(nodes.id, input.id), eq(nodes.namespace, input.namespace)));
194
+ const alias = "parent_nodes";
195
+ const parentNodesQueryAlias = startNodeQuery.as(alias);
196
+ const recursiveQueryName = sql.raw(`"${alias}"`);
197
+ const recursiveQuery = startNodeQuery.unionAll(this.#db.select({
198
+ id: nodes.id,
199
+ name: nodes.name,
200
+ parentId: nodes.parentId,
201
+ depth: sql`${parentNodesQueryAlias.depth} + 1`
202
+ }).from(nodes).innerJoin(recursiveQueryName, eq(nodes.id, parentNodesQueryAlias.parentId)));
203
+ return (await this.#db.execute(sql`WITH RECURSIVE ${recursiveQueryName} AS ${recursiveQuery} SELECT * FROM ${recursiveQueryName}`)).rows?.toSorted((a, b) => Number(b.depth) - Number(a.depth)) ?? [];
204
+ }
205
+ /**
206
+ * Retrieves the structural subtree for one or more root node ids.
207
+ *
208
+ * The result includes the provided start nodes and all descendant nodes,
209
+ * returned as a lightweight shape containing only the fields needed for
210
+ * recursive drive operations such as delete, archive, and validation.
211
+ *
212
+ * Depth starts at `0` for the provided root nodes and increases for each
213
+ * descendant level. Duplicate paths are collapsed by keeping the minimum depth.
214
+ *
215
+ * @param ids - Root node ids to traverse from.
216
+ * @returns A promise resolving to the subtree rows for the provided nodes.
217
+ */
218
+ async getNodeChildren(ids) {
219
+ if (ids.length === 0) return [];
220
+ const startNodeQuery = this.#db.select({
221
+ id: nodes.id,
222
+ assetId: nodes.assetId,
223
+ type: nodes.type,
224
+ parentId: nodes.parentId,
225
+ depth: sql`0`.as("depth")
226
+ }).from(nodes).where(inArray(nodes.id, ids));
227
+ const alias = "child_nodes";
228
+ const childNodesQueryAlias = startNodeQuery.as(alias);
229
+ const recursiveQueryName = sql.raw(`"${alias}"`);
230
+ const recursiveQuery = startNodeQuery.unionAll(this.#db.select({
231
+ id: nodes.id,
232
+ assetId: nodes.assetId,
233
+ type: nodes.type,
234
+ parentId: nodes.parentId,
235
+ depth: sql`${childNodesQueryAlias.depth} + 1`
236
+ }).from(nodes).innerJoin(recursiveQueryName, eq(nodes.parentId, childNodesQueryAlias.id)));
237
+ return (await this.#db.execute(sql`WITH RECURSIVE ${recursiveQueryName} AS ${recursiveQuery}
238
+ SELECT
239
+ ${childNodesQueryAlias.id} as id,
240
+ ${childNodesQueryAlias.assetId} as "assetId",
241
+ ${childNodesQueryAlias.type} as type,
242
+ ${childNodesQueryAlias.parentId} as "parentId",
243
+ MIN(${childNodesQueryAlias.depth})::int as depth
244
+ FROM ${recursiveQueryName}
245
+ GROUP BY
246
+ ${childNodesQueryAlias.id},
247
+ ${childNodesQueryAlias.assetId},
248
+ ${childNodesQueryAlias.type},
249
+ ${childNodesQueryAlias.parentId}
250
+ `)).rows ?? [];
251
+ }
252
+ /**
253
+ * Resolves an access URL for a file node and the requested variant.
254
+ *
255
+ * Behavior:
256
+ * - Rejects folder nodes because only file nodes can resolve a file URL
257
+ * - Reuses a cached URL when an unexpired entry exists for the requested
258
+ * variant and disposition
259
+ * - Resolves the requested preview variant asset when available
260
+ * - Falls back to the main file asset when the requested variant does not exist
261
+ * - Delegates final URL generation to the storage service
262
+ * - Stores the generated URL in the local cache table for future reuse
263
+ *
264
+ * @param node - The file node to resolve.
265
+ * @param options - URL options such as variant and content disposition.
266
+ * @returns A promise resolving to a temporary file URL.
267
+ */
268
+ async getURL(node, options = getLocalFileURLSchemaDefaults) {
269
+ if (!isLocalDriveFile(node)) throw new ServerError("BAD_REQUEST", { message: "Node is not a file" });
270
+ const [presignedUrl] = await this.#db.select({
271
+ url: nodePresignedUrls.url,
272
+ expiresAt: nodePresignedUrls.expiresAt
273
+ }).from(nodePresignedUrls).where(and(eq(nodePresignedUrls.nodeId, node.id), eq(nodePresignedUrls.variant, options.variant), eq(nodePresignedUrls.disposition, options.disposition)));
274
+ if (presignedUrl && presignedUrl.expiresAt > /* @__PURE__ */ new Date()) return presignedUrl.url;
275
+ const expiresIn = 3600 * 24;
276
+ const [variantRecord] = options.variant === "main" ? [] : await this.#db.select({
277
+ id: nodeVariants.id,
278
+ assetId: nodeVariants.assetId,
279
+ variant: nodeVariants.variant
280
+ }).from(nodeVariants).where(and(eq(nodeVariants.nodeId, node.id), eq(nodeVariants.variant, options.variant)));
281
+ const variant = variantRecord ? options.variant : "main";
282
+ const assetId = variantRecord?.assetId ?? node.assetId;
283
+ if (!assetId) throw new ServerError("NOT_FOUND", { message: "Storage asset not found" });
284
+ console.info(`Generating new signed url for file: ${node.id} with variant: ${variant} and disposition: ${options.disposition}`);
285
+ const url = await this.storage.getObjectURL(assetId, {
286
+ disposition: `${options.disposition}; filename="${node.name}"`,
287
+ expiresIn
288
+ });
289
+ after(async () => {
290
+ await this.#db.insert(nodePresignedUrls).values({
291
+ nodeId: node.id,
292
+ url,
293
+ variant,
294
+ variantId: variantRecord?.id,
295
+ disposition: options.disposition,
296
+ expiresAt: addSeconds(/* @__PURE__ */ new Date(), expiresIn)
297
+ }).onConflictDoUpdate({
298
+ target: [
299
+ nodePresignedUrls.nodeId,
300
+ nodePresignedUrls.variant,
301
+ nodePresignedUrls.disposition
302
+ ],
303
+ set: {
304
+ url,
305
+ expiresAt: addSeconds(/* @__PURE__ */ new Date(), expiresIn)
306
+ }
307
+ });
308
+ });
309
+ return url;
310
+ }
311
+ /**
312
+ * Moves a node to a different parent folder.
313
+ *
314
+ * The target parent is validated before the move is persisted. Moving a node
315
+ * into itself or into one of its descendants is rejected to keep the tree
316
+ * structure valid.
317
+ *
318
+ * @param input - The node id and the target parent id.
319
+ * @returns A promise resolving to the updated node.
320
+ * @throws {ServerError} If the move is invalid.
321
+ */
322
+ async moveNode(input) {
323
+ const [node] = await this.#db.select().from(nodes).where(eq(nodes.id, input.id)).limit(1);
324
+ if (!node) throw new ServerError("BAD_REQUEST", { message: "Node not found" });
325
+ if (node.readonly) throw new ServerError("BAD_REQUEST", { message: "Deze node is alleen leesbaar en kan niet worden gewijzigd" });
326
+ if (input.parentId === node.id) throw new ServerError("BAD_REQUEST", { message: "Node cannot be moved into itself" });
327
+ if (input.parentId !== void 0) await this.#getWritableParent({
328
+ parentId: input.parentId,
329
+ namespace: node.namespace
330
+ });
331
+ if (input.parentId) {
332
+ if ((await this.getNodeChildren([input.id])).some((child) => child.id === input.parentId)) throw new ServerError("BAD_REQUEST", { message: "Node cannot be moved into one of its descendants" });
333
+ }
334
+ if (node.parentId === input.parentId) return toLocalDriveNode(node);
335
+ const [result] = await this.#db.update(nodes).set({ parentId: input.parentId }).where(eq(nodes.id, input.id)).returning();
336
+ if (!result) throw new ServerError("INTERNAL_SERVER_ERROR", { message: "Node kon niet worden gewijzigd" });
337
+ return toLocalDriveNode(result);
338
+ }
339
+ /**
340
+ * Uploads a file through the storage service and creates the matching node.
341
+ *
342
+ * Behavior:
343
+ * - Validates the parent folder when `parentId` is provided
344
+ * - Uploads the file bytes through the storage service
345
+ * - Persists the created storage asset id on the node
346
+ * - Cleans up the uploaded asset when node creation fails
347
+ *
348
+ * @param input - File metadata and body.
349
+ * @returns A promise resolving to the created file node.
350
+ * @throws {ServerError} If validation or persistence fails.
351
+ */
352
+ async uploadFile(input) {
353
+ await this.#getWritableParent({
354
+ parentId: input.parentId,
355
+ namespace: input.namespace
356
+ });
357
+ const asset = await this.storage.upload({
358
+ name: input.name,
359
+ body: input.body,
360
+ contentType: input.contentType,
361
+ size: input.size,
362
+ visibility: "private"
363
+ });
364
+ try {
365
+ const [result] = await this.#db.insert(nodes).values({
366
+ type: "file",
367
+ name: input.name,
368
+ namespace: input.namespace,
369
+ parentId: input.parentId,
370
+ size: asset.size ?? input.size,
371
+ contentType: asset.contentType ?? input.contentType,
372
+ hidden: input.hidden ?? false,
373
+ readonly: input.readonly ?? false,
374
+ subtype: inferLocalDriveNodeSubtype(input),
375
+ assetId: asset.id
376
+ }).returning();
377
+ if (!result) throw new ServerError("INTERNAL_SERVER_ERROR", { message: "Oep! Er is iets fout gegaan" });
378
+ return toLocalDriveNode(result);
379
+ } catch (error) {
380
+ await this.storage.purgeAsset(asset.id).catch(() => void 0);
381
+ throw error;
382
+ }
383
+ }
384
+ /**
385
+ * Creates a direct upload intent and the matching file node.
386
+ *
387
+ * Behavior:
388
+ * - Validates the parent folder when `parentId` is provided
389
+ * - Requests a presigned upload from the storage service
390
+ * - Creates the file node linked to the pending storage asset
391
+ * - Purges the pending asset when node creation fails
392
+ *
393
+ * @param input - File metadata used to create the upload intent.
394
+ * @returns The created asset intent, presigned URL, and file node.
395
+ * @throws {ServerError} If validation or node creation fails.
396
+ */
397
+ async presignUpload(input) {
398
+ await this.#getWritableParent(input);
399
+ const { presignedUrl, ...asset } = await this.storage.presignUpload({
400
+ uploadId: input.uploadId,
401
+ name: input.name,
402
+ contentType: input.contentType,
403
+ size: input.size,
404
+ visibility: input.visibility
405
+ });
406
+ try {
407
+ const [node] = await this.#db.insert(nodes).values({
408
+ type: "file",
409
+ name: input.name,
410
+ namespace: input.namespace,
411
+ parentId: input.parentId,
412
+ size: asset.size ?? input.size,
413
+ contentType: asset.contentType ?? input.contentType,
414
+ hidden: input.hidden ?? false,
415
+ readonly: input.readonly ?? false,
416
+ assetId: asset.id,
417
+ subtype: inferLocalDriveNodeSubtype(input)
418
+ }).returning();
419
+ if (!node) throw new ServerError("INTERNAL_SERVER_ERROR", { message: "Oep! Er is iets fout gegaan" });
420
+ return {
421
+ uploadId: asset.uploadId,
422
+ presignedUrl,
423
+ node: toLocalDriveNode(node),
424
+ asset
425
+ };
426
+ } catch (error) {
427
+ await this.storage.purgeAsset(asset.id).catch(() => void 0);
428
+ throw error;
429
+ }
430
+ }
431
+ /**
432
+ * Finalizes a presigned upload and synchronizes the node metadata.
433
+ *
434
+ * Behavior:
435
+ * - Confirms the pending asset through the storage service
436
+ * - Finds the matching node by `assetId`
437
+ * - Synchronizes final asset metadata onto the node
438
+ * - Schedules preview generation for image files
439
+ *
440
+ * @param uploadId - The storage upload id returned by the presign step.
441
+ * @returns A promise resolving to the finalized file node.
442
+ * @throws {ServerError} If the asset or node cannot be resolved.
443
+ */
444
+ async confirmUpload(uploadId) {
445
+ const asset = await this.storage.confirmUpload(uploadId);
446
+ const [node] = await this.#db.select().from(nodes).where(eq(nodes.assetId, asset.id)).limit(1);
447
+ if (!node) throw new ServerError("NOT_FOUND", { message: "File not found" });
448
+ const [result] = await this.#db.update(nodes).set({
449
+ size: asset.size ?? node.size,
450
+ contentType: asset.contentType ?? node.contentType
451
+ }).where(eq(nodes.assetId, asset.id)).returning();
452
+ if (!result) throw new ServerError("NOT_FOUND", { message: "File not found" });
453
+ /**
454
+ * Generate the preview version of the file
455
+ */
456
+ after(async () => {
457
+ await this.generatePreviews({ id: result.id });
458
+ });
459
+ return toLocalDriveNode(result);
460
+ }
461
+ /**
462
+ * Generates preview assets for an image node.
463
+ *
464
+ * The original file is loaded from storage, resized to the configured device
465
+ * widths, uploaded as separate storage assets, and linked through
466
+ * `node_variants`. Existing preview variants are skipped.
467
+ *
468
+ * @param input - The target file node identifier.
469
+ * @returns A promise that resolves once preview generation completes.
470
+ * @throws {ServerError} If the source node or asset cannot be resolved.
471
+ */
472
+ async generatePreviews(input) {
473
+ const [node] = await this.#db.select().from(nodes).where(eq(nodes.id, input.id)).limit(1);
474
+ if (!node) throw new ServerError("NOT_FOUND", { message: "Node not found" });
475
+ if (!node.assetId) throw new ServerError("BAD_REQUEST", { message: "Node has no storage asset" });
476
+ /**
477
+ * Get the main version of the file
478
+ */
479
+ const response = await this.storage.getObject(node.assetId);
480
+ const contentType = response.contentType;
481
+ if (!response.body) throw new ServerError("INTERNAL_SERVER_ERROR", { message: "Oeps! Er is iets fout gegaan" });
482
+ /**
483
+ * Transform the main version of the file to a buffer
484
+ */
485
+ const buffer = await stream.buffer(response.body);
486
+ /**
487
+ * Generate the preview versions for images
488
+ */
489
+ if (contentType?.startsWith("image/")) {
490
+ const sharp = await import("sharp");
491
+ const sourceAsset = await this.storage.getAssetById(node.assetId);
492
+ if (!sourceAsset) throw new ServerError("NOT_FOUND", { message: "Storage asset not found" });
493
+ const existingVariants = await this.#db.select({ variant: nodeVariants.variant }).from(nodeVariants).where(eq(nodeVariants.nodeId, input.id));
494
+ const existingVariantNames = new Set(existingVariants.map((variant) => variant.variant));
495
+ await Promise.allSettled(deviceSizes.flatMap(async (width) => {
496
+ const variant = `preview-${width}`;
497
+ if (existingVariantNames.has(variant)) return;
498
+ const preview = await sharp.default(buffer).resize({ width }).webp().toBuffer();
499
+ return this.#db.transaction(async (tx) => {
500
+ const asset = await this.storage.upload({
501
+ body: preview,
502
+ name: `${input.id}-preview-${width}.webp`,
503
+ contentType: "image/webp",
504
+ size: preview.byteLength,
505
+ visibility: sourceAsset.visibility
506
+ });
507
+ try {
508
+ await tx.insert(nodeVariants).values({
509
+ nodeId: input.id,
510
+ assetId: asset.id,
511
+ variant,
512
+ width
513
+ });
514
+ } catch (error) {
515
+ await this.storage.purgeAsset(asset.id).catch(() => void 0);
516
+ throw error;
517
+ }
518
+ });
519
+ }));
520
+ }
521
+ }
522
+ /**
523
+ * Creates a folder node in the drive tree.
524
+ *
525
+ * The parent folder is validated before the folder is inserted. Folders do
526
+ * not own a storage asset and are stored purely as drive metadata.
527
+ *
528
+ * @param input - Folder metadata.
529
+ * @returns A promise resolving to the created folder node.
530
+ * @throws {ServerError} If validation or insertion fails.
531
+ */
532
+ async createFolder(input) {
533
+ await this.#getWritableParent({
534
+ parentId: input.parentId,
535
+ namespace: input.namespace
536
+ });
537
+ /**
538
+ * Create the folder
539
+ */
540
+ const [result] = await this.#db.insert(nodes).values({
541
+ type: "folder",
542
+ name: input.name,
543
+ namespace: input.namespace,
544
+ parentId: input.parentId,
545
+ hidden: input.hidden ?? false,
546
+ readonly: input.readonly ?? false
547
+ }).returning();
548
+ if (!result) throw new ServerError("INTERNAL_SERVER_ERROR", { message: "Folder kon niet worden aangemaakt" });
549
+ return toLocalDriveNode(result);
550
+ }
551
+ /**
552
+ * Updates mutable node metadata.
553
+ *
554
+ * Supported updates currently include renaming, reparenting, visibility flags,
555
+ * and archive state. Structural updates are validated before being persisted.
556
+ *
557
+ * @param id - The target node id.
558
+ * @param data - Partial node fields to update.
559
+ * @returns A promise resolving to the updated node.
560
+ * @throws {ServerError} If the node does not exist or the update is invalid.
561
+ */
562
+ async updateNode(id, data) {
563
+ const [node] = await this.#db.select().from(nodes).where(eq(nodes.id, id)).limit(1);
564
+ if (!node) throw new ServerError("NOT_FOUND", { message: "Node not found" });
565
+ if (data.parentId !== void 0) await this.#getWritableParent({
566
+ parentId: data.parentId,
567
+ namespace: node.namespace
568
+ });
569
+ const [result] = await this.#db.update(nodes).set({
570
+ name: data.name,
571
+ parentId: data.parentId,
572
+ hidden: data.hidden,
573
+ readonly: data.readonly,
574
+ archivedAt: data.archivedAt
575
+ }).where(eq(nodes.id, id)).returning();
576
+ if (!result) throw new ServerError("INTERNAL_SERVER_ERROR", { message: "Node kon niet worden gewijzigd" });
577
+ return toLocalDriveNode(result);
578
+ }
579
+ /**
580
+ * Sets the readonly state for multiple nodes.
581
+ *
582
+ * This method is intended for bulk lock and unlock operations in the drive UI.
583
+ *
584
+ * @param ids - The node ids to update.
585
+ * @param readonly - The readonly state to apply.
586
+ * @returns A promise resolving to the updated nodes.
587
+ * @throws {ServerError} If no ids are provided or no matching nodes are found.
588
+ */
589
+ async setReadonly(ids, readonly) {
590
+ if (ids.length === 0) throw new ServerError("BAD_REQUEST", { message: "No node ids provided" });
591
+ const result = await this.#db.update(nodes).set({ readonly }).where(inArray(nodes.id, [...new Set(ids)])).returning();
592
+ if (result.length === 0) throw new ServerError("BAD_REQUEST", { message: "No matching nodes found" });
593
+ return result.map(toLocalDriveNode);
594
+ }
595
+ /**
596
+ * Archives nodes by setting their archive timestamp.
597
+ *
598
+ * This is a drive-level archive state and does not delete the underlying
599
+ * storage assets.
600
+ *
601
+ * @param ids - The node ids to archive.
602
+ * @returns A promise resolving to the archived nodes.
603
+ * @throws {ServerError} If no ids are provided or no matching nodes are found.
604
+ */
605
+ async archiveNodes(ids) {
606
+ if (ids.length === 0) throw new ServerError("BAD_REQUEST", { message: "No node ids provided" });
607
+ const result = await this.#db.update(nodes).set({ archivedAt: /* @__PURE__ */ new Date() }).where(and(inArray(nodes.id, [...new Set(ids)]), isNull(nodes.archivedAt))).returning();
608
+ if (result.length === 0) throw new ServerError("BAD_REQUEST", { message: "No matching nodes found" });
609
+ return result.map(toLocalDriveNode);
610
+ }
611
+ /**
612
+ * Restores archived nodes by clearing their archive timestamp.
613
+ *
614
+ * @param ids - The node ids to restore.
615
+ * @returns A promise resolving to the restored nodes.
616
+ * @throws {ServerError} If no ids are provided or no matching nodes are found.
617
+ */
618
+ async restoreNodes(ids) {
619
+ if (ids.length === 0) throw new ServerError("BAD_REQUEST", { message: "No node ids provided" });
620
+ const result = await this.#db.update(nodes).set({ archivedAt: null }).where(and(inArray(nodes.id, [...new Set(ids)]), isNotNull(nodes.archivedAt))).returning();
621
+ if (result.length === 0) throw new ServerError("BAD_REQUEST", { message: "No matching nodes found" });
622
+ return result.map(toLocalDriveNode);
623
+ }
624
+ /**
625
+ * Permanently deletes a node subtree and all linked assets.
626
+ *
627
+ * The subtree includes the provided node and all descendants. Associated file
628
+ * assets and generated preview assets are purged from storage after the nodes
629
+ * are removed from the drive catalog.
630
+ *
631
+ * @param id - The root node id to delete.
632
+ * @throws {ServerError} If the node does not exist.
633
+ */
634
+ async deleteNode(id) {
635
+ const subtree = await this.getNodeChildren([id]);
636
+ if (subtree.length === 0) throw new ServerError("NOT_FOUND", { message: "Node not found" });
637
+ const nodeIds = [...new Set(subtree.map((node) => node.id))];
638
+ const assetIds = await this.#getSubtreeAssetIds(nodeIds, subtree);
639
+ await this.#db.delete(nodes).where(inArray(nodes.id, nodeIds));
640
+ if (assetIds.length > 0) await this.storage.purgeAssets(assetIds);
641
+ }
642
+ /**
643
+ * Permanently deletes multiple node subtrees and their linked assets.
644
+ *
645
+ * Each provided id is treated as a subtree root. All descendant nodes are
646
+ * removed together with their main file assets and generated preview assets.
647
+ *
648
+ * @param ids - Root node ids to delete.
649
+ * @returns A promise that resolves when deletion completes.
650
+ * @throws {ServerError} If no ids are provided or no matching nodes are found.
651
+ */
652
+ async deleteNodes(ids) {
653
+ if (ids.length === 0) throw new ServerError("BAD_REQUEST", { message: "No node ids provided" });
654
+ const subtree = await this.getNodeChildren(ids);
655
+ if (subtree.length === 0) throw new ServerError("BAD_REQUEST", { message: "No matching nodes found" });
656
+ const nodeIds = [...new Set(subtree.map((node) => node.id))];
657
+ const assetIds = await this.#getSubtreeAssetIds(nodeIds, subtree);
658
+ /**
659
+ * Delete files and folders in a database
660
+ */
661
+ await this.#db.delete(nodes).where(inArray(nodes.id, nodeIds));
662
+ if (assetIds.length > 0) await this.storage.purgeAssets(assetIds);
663
+ }
664
+ };
665
+
666
+ //#endregion
667
+ export { LocalDrive };