@cepseudo/assets 1.0.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 (43) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +204 -0
  3. package/dist/assets_manager.d.ts +643 -0
  4. package/dist/assets_manager.d.ts.map +1 -0
  5. package/dist/assets_manager.js +1217 -0
  6. package/dist/assets_manager.js.map +1 -0
  7. package/dist/assets_openapi.d.ts +9 -0
  8. package/dist/assets_openapi.d.ts.map +1 -0
  9. package/dist/assets_openapi.js +391 -0
  10. package/dist/assets_openapi.js.map +1 -0
  11. package/dist/async_upload.d.ts +20 -0
  12. package/dist/async_upload.d.ts.map +1 -0
  13. package/dist/async_upload.js +10 -0
  14. package/dist/async_upload.js.map +1 -0
  15. package/dist/index.d.ts +18 -0
  16. package/dist/index.d.ts.map +1 -0
  17. package/dist/index.js +17 -0
  18. package/dist/index.js.map +1 -0
  19. package/dist/map_manager.d.ts +61 -0
  20. package/dist/map_manager.d.ts.map +1 -0
  21. package/dist/map_manager.js +217 -0
  22. package/dist/map_manager.js.map +1 -0
  23. package/dist/presigned_upload_service.d.ts +52 -0
  24. package/dist/presigned_upload_service.d.ts.map +1 -0
  25. package/dist/presigned_upload_service.js +152 -0
  26. package/dist/presigned_upload_service.js.map +1 -0
  27. package/dist/tileset_manager.d.ts +128 -0
  28. package/dist/tileset_manager.d.ts.map +1 -0
  29. package/dist/tileset_manager.js +705 -0
  30. package/dist/tileset_manager.js.map +1 -0
  31. package/dist/upload_processor.d.ts +42 -0
  32. package/dist/upload_processor.d.ts.map +1 -0
  33. package/dist/upload_processor.js +138 -0
  34. package/dist/upload_processor.js.map +1 -0
  35. package/dist/upload_reconciler.d.ts +44 -0
  36. package/dist/upload_reconciler.d.ts.map +1 -0
  37. package/dist/upload_reconciler.js +140 -0
  38. package/dist/upload_reconciler.js.map +1 -0
  39. package/dist/utils/zip_utils.d.ts +66 -0
  40. package/dist/utils/zip_utils.d.ts.map +1 -0
  41. package/dist/utils/zip_utils.js +169 -0
  42. package/dist/utils/zip_utils.js.map +1 -0
  43. package/package.json +72 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Axel Hoffmann
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,204 @@
1
+ # @cepseudo/assets
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@cepseudo/assets)](https://www.npmjs.com/package/@cepseudo/assets)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](./LICENSE)
5
+
6
+ Asset lifecycle management for the Digital Twin framework -- upload, metadata, download, and deletion of files with user ownership and access control.
7
+
8
+ ## Installation
9
+
10
+ ```bash
11
+ pnpm add @cepseudo/assets
12
+ ```
13
+
14
+ This package depends on sibling workspace packages that must also be installed:
15
+
16
+ ```
17
+ @cepseudo/shared
18
+ @cepseudo/database
19
+ @cepseudo/storage
20
+ @cepseudo/auth
21
+ ```
22
+
23
+ ## Managers
24
+
25
+ Three abstract manager classes cover different asset use cases. All extend `AssetsManager` and expose HTTP endpoints automatically when registered with the engine.
26
+
27
+ | Manager | Purpose | Storage | Upload method |
28
+ |---|---|---|---|
29
+ | `AssetsManager` | Generic file assets (images, documents, 3D models) | S3 + PostgreSQL metadata | Multipart or presigned URL |
30
+ | `TilesetManager` | 3D tilesets (Cesium 3D Tiles) | ZIP uploaded, extracted to individual files on S3 | Multipart (async via BullMQ) or presigned URL |
31
+ | `MapManager` | Map layers (GeoJSON, small payloads) | JSON stored directly in PostgreSQL | JSON body (no file upload) |
32
+
33
+ Each manager creates its own database table based on the `name` field in its configuration. Endpoints are mounted at the configured `endpoint` path.
34
+
35
+ ## Upload Flows
36
+
37
+ ### Classic Multipart Upload
38
+
39
+ Standard `multipart/form-data` upload through the backend. Suitable for small to medium files.
40
+
41
+ ```
42
+ Client --POST multipart--> Backend --putObject--> S3
43
+ |
44
+ +--> Save metadata to PostgreSQL
45
+ ```
46
+
47
+ ### Presigned URL Upload
48
+
49
+ For large files (3D assets, tilesets). The file goes directly from the client to S3, bypassing the backend and API gateway entirely.
50
+
51
+ ```
52
+ 1. Client --POST /assets/upload-request--> Backend
53
+ (fileName, fileSize, contentType)
54
+
55
+ 2. Backend validates auth, creates DB record (status: pending),
56
+ generates presigned PUT URL, returns { fileId, uploadUrl, expiresAt }
57
+
58
+ 3. Client --PUT file--> S3 (using presigned URL)
59
+
60
+ 4. Client --POST /assets/confirm/{fileId}--> Backend
61
+
62
+ 5. Backend does HEAD on S3 to verify file exists --> status: completed
63
+ ```
64
+
65
+ The `UploadReconciler` runs on a configurable interval (default: 5 minutes) to handle edge cases:
66
+
67
+ - Pending upload + file exists on S3 --> mark `completed`
68
+ - Pending upload + presigned URL expired + no file --> mark `expired`
69
+
70
+ ### Async Tileset Processing
71
+
72
+ `TilesetManager` implements `AsyncUploadable`. When a ZIP archive is uploaded, extraction happens asynchronously via a BullMQ queue. The `UploadProcessor` worker picks up the job, extracts the ZIP, and stores individual files on S3.
73
+
74
+ ## Usage
75
+
76
+ ### Creating a Custom AssetsManager
77
+
78
+ ```typescript
79
+ import { AssetsManager } from '@cepseudo/assets'
80
+ import type { AssetsManagerConfiguration } from '@cepseudo/shared'
81
+
82
+ class PhotoManager extends AssetsManager {
83
+ getConfiguration(): AssetsManagerConfiguration {
84
+ return {
85
+ name: 'photos',
86
+ description: 'Manage photo assets',
87
+ contentType: 'image/jpeg',
88
+ endpoint: 'api/photos',
89
+ extension: '.jpg'
90
+ }
91
+ }
92
+ }
93
+
94
+ const manager = new PhotoManager()
95
+ manager.setDependencies(databaseAdapter, storageService)
96
+ ```
97
+
98
+ This creates a `photos` table in the database and exposes endpoints at `/api/photos`.
99
+
100
+ ### Creating a TilesetManager
101
+
102
+ ```typescript
103
+ import { TilesetManager } from '@cepseudo/assets'
104
+ import type { AssetsManagerConfiguration } from '@cepseudo/shared'
105
+
106
+ class BuildingTilesets extends TilesetManager {
107
+ getConfiguration(): AssetsManagerConfiguration {
108
+ return {
109
+ name: 'building-tilesets',
110
+ description: '3D building tilesets',
111
+ contentType: 'application/octet-stream',
112
+ endpoint: 'api/tilesets/buildings',
113
+ extension: '.zip'
114
+ }
115
+ }
116
+ }
117
+ ```
118
+
119
+ ### Presigned Upload Flow
120
+
121
+ ```typescript
122
+ import { PresignedUploadService } from '@cepseudo/assets'
123
+
124
+ // The PresignedUploadService is created automatically by AssetsManager.
125
+ // Client-side usage:
126
+
127
+ // 1. Request a presigned URL
128
+ const response = await fetch('/api/photos/upload-request', {
129
+ method: 'POST',
130
+ headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
131
+ body: JSON.stringify({
132
+ fileName: 'building.glb',
133
+ fileSize: 52428800,
134
+ contentType: 'model/gltf-binary'
135
+ })
136
+ })
137
+ const { fileId, uploadUrl, expiresAt } = await response.json()
138
+
139
+ // 2. Upload directly to S3
140
+ await fetch(uploadUrl, {
141
+ method: 'PUT',
142
+ headers: { 'Content-Type': 'model/gltf-binary' },
143
+ body: fileBlob
144
+ })
145
+
146
+ // 3. Confirm the upload
147
+ await fetch(`/api/photos/confirm/${fileId}`, {
148
+ method: 'POST',
149
+ headers: { Authorization: `Bearer ${token}` }
150
+ })
151
+ ```
152
+
153
+ ### Setting Up the UploadReconciler
154
+
155
+ ```typescript
156
+ import { UploadReconciler } from '@cepseudo/assets'
157
+
158
+ const reconciler = new UploadReconciler(databaseAdapter, storageService, {
159
+ intervalMs: 5 * 60 * 1000 // 5 minutes (default)
160
+ })
161
+
162
+ // Register tables to monitor
163
+ reconciler.registerTables(['photos', 'building-tilesets'])
164
+
165
+ // Start the reconciliation loop
166
+ reconciler.start()
167
+
168
+ // Stop on shutdown
169
+ reconciler.stop()
170
+ ```
171
+
172
+ ### Generating OpenAPI Specs
173
+
174
+ ```typescript
175
+ import { generateAssetsOpenAPISpec } from '@cepseudo/assets'
176
+
177
+ const spec = generateAssetsOpenAPISpec(manager.getConfiguration())
178
+ ```
179
+
180
+ ## Peer Dependencies
181
+
182
+ | Package | Required | Purpose |
183
+ |---|---|---|
184
+ | `bullmq` (>=5.0.0) | Optional | Async tileset upload processing via `UploadProcessor`. Only needed if you use `TilesetManager` with queue-based extraction. |
185
+
186
+ If `bullmq` is not installed, `AssetsManager` and `MapManager` work without it. `TilesetManager` falls back to synchronous extraction.
187
+
188
+ ## API Endpoints
189
+
190
+ Each manager automatically exposes the following endpoints (prefixed by the configured `endpoint`):
191
+
192
+ | Method | Path | Description |
193
+ |---|---|---|
194
+ | `GET` | `/` | List assets (paginated) |
195
+ | `GET` | `/:id` | Get asset by ID |
196
+ | `POST` | `/` | Upload asset (multipart) |
197
+ | `PUT` | `/:id` | Update asset metadata |
198
+ | `DELETE` | `/:id` | Delete asset and file |
199
+ | `POST` | `/upload-request` | Request presigned upload URL |
200
+ | `POST` | `/confirm/:id` | Confirm presigned upload |
201
+
202
+ ## License
203
+
204
+ MIT