@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
@@ -0,0 +1,169 @@
1
+ import JSZip from 'jszip';
2
+ /**
3
+ * Extracts the content of a zip file as a stream (for large files)
4
+ * @param zipBuffer - The content of the zip file as Buffer
5
+ * @returns A generator yielding tuples containing the name and content of each file in the zip file
6
+ */
7
+ export async function* extractZipContentStream(zipBuffer) {
8
+ const zip = new JSZip();
9
+ const zipContent = await zip.loadAsync(zipBuffer);
10
+ for (const [name, zipObject] of Object.entries(zipContent.files)) {
11
+ if (!zipObject.dir) {
12
+ // Skip directories
13
+ const content = await zipObject.async('nodebuffer');
14
+ // Try to decode as UTF-8, fallback to Buffer for binary files
15
+ try {
16
+ const textContent = content.toString('utf-8');
17
+ // Check if it's valid UTF-8 by trying to encode it back
18
+ Buffer.from(textContent, 'utf-8');
19
+ yield [name, textContent];
20
+ }
21
+ catch {
22
+ // If UTF-8 decoding fails, return as Buffer
23
+ yield [name, content];
24
+ }
25
+ }
26
+ }
27
+ }
28
+ /**
29
+ * Converts a zip file to a dictionary containing all files and their contents
30
+ * @param zipBuffer - The content of the zip file as Buffer
31
+ * @returns A dictionary containing the content of the zip file
32
+ */
33
+ export async function zipToDict(zipBuffer) {
34
+ const output = {};
35
+ for await (const [name, content] of extractZipContentStream(zipBuffer)) {
36
+ output[name] = content;
37
+ }
38
+ return output;
39
+ }
40
+ /**
41
+ * Detects the root file for 3D Tiles tilesets
42
+ * Looks for tileset.json or similar entry point files
43
+ * @param files - List of file paths in the archive
44
+ * @returns The path to the root file, or undefined if not found
45
+ */
46
+ export function detectTilesetRootFile(files) {
47
+ // Priority order for 3D Tiles root files
48
+ const rootFilePatterns = [
49
+ /^tileset\.json$/i,
50
+ /\/tileset\.json$/i,
51
+ /^[^/]+\/tileset\.json$/i, // One level deep
52
+ /tileset\.json$/i // Any tileset.json as fallback
53
+ ];
54
+ for (const pattern of rootFilePatterns) {
55
+ const match = files.find(f => pattern.test(f));
56
+ if (match)
57
+ return match;
58
+ }
59
+ return undefined;
60
+ }
61
+ /**
62
+ * Normalizes file paths from ZIP archives
63
+ * Removes leading directory if all files share the same root folder
64
+ * @param files - Original file paths from the archive
65
+ * @returns Normalized file paths (original -> normalized)
66
+ */
67
+ export function normalizeArchivePaths(files) {
68
+ const pathMap = new Map();
69
+ // Check if all files share a common root directory
70
+ const firstParts = files.map(f => f.split('/')[0]);
71
+ const commonRoot = firstParts.every(p => p === firstParts[0]) && firstParts[0] !== '' ? firstParts[0] : null;
72
+ for (const file of files) {
73
+ if (commonRoot && file.startsWith(commonRoot + '/')) {
74
+ // Remove the common root prefix
75
+ pathMap.set(file, file.substring(commonRoot.length + 1));
76
+ }
77
+ else {
78
+ pathMap.set(file, file);
79
+ }
80
+ }
81
+ return pathMap;
82
+ }
83
+ /**
84
+ * Extracts a ZIP archive and stores each file individually using the storage service.
85
+ *
86
+ * This function:
87
+ * 1. Extracts all files from the ZIP
88
+ * 2. Normalizes paths (removes common root directory if present)
89
+ * 3. Stores each file using the storage service with a unique base path
90
+ * 4. Returns the root file path and file count
91
+ *
92
+ * Files are uploaded in parallel batches for performance.
93
+ *
94
+ * @param zipBuffer - The ZIP file content as a Buffer
95
+ * @param storage - The storage service to use for saving files
96
+ * @param basePath - Base path/folder for storing extracted files (e.g., 'tilesets/1234567890')
97
+ * @returns ExtractedArchiveResult with root file and file count
98
+ *
99
+ * @example
100
+ * ```typescript
101
+ * const result = await extractAndStoreArchive(zipBuffer, storage, 'tilesets/1234567890')
102
+ * // result.root_file = 'tileset.json'
103
+ * // result.file_count = 42
104
+ *
105
+ * // Files are stored at:
106
+ * // tilesets/1234567890/tileset.json
107
+ * // tilesets/1234567890/tiles/tile_0.b3dm
108
+ * // etc.
109
+ * ```
110
+ */
111
+ export async function extractAndStoreArchive(zipBuffer, storage, basePath) {
112
+ const zip = new JSZip();
113
+ const zipContent = await zip.loadAsync(zipBuffer);
114
+ // Get all file paths (excluding directories)
115
+ const filePaths = Object.entries(zipContent.files)
116
+ .filter(([_, zipObject]) => !zipObject.dir)
117
+ .map(([name]) => name);
118
+ // Normalize paths (remove common root if present)
119
+ const normalizedPaths = normalizeArchivePaths(filePaths);
120
+ // Detect root file before normalization, then get normalized path
121
+ const rootFileOriginal = detectTilesetRootFile(filePaths);
122
+ const rootFileNormalized = rootFileOriginal ? normalizedPaths.get(rootFileOriginal) : undefined;
123
+ // Extract and store files in parallel (batched to avoid overwhelming storage)
124
+ const PARALLEL_UPLOADS = 10;
125
+ const entries = Array.from(normalizedPaths.entries());
126
+ const totalFiles = entries.length;
127
+ let uploadedCount = 0;
128
+ const uploadedPaths = [];
129
+ console.log(`[ZipUtils] Extracting ${totalFiles} files to ${basePath}`);
130
+ try {
131
+ const totalBatches = Math.ceil(totalFiles / PARALLEL_UPLOADS);
132
+ // Log progress every 10% or at least every 10 batches
133
+ const logInterval = Math.max(1, Math.floor(totalBatches / 10));
134
+ for (let i = 0; i < entries.length; i += PARALLEL_UPLOADS) {
135
+ const batch = entries.slice(i, i + PARALLEL_UPLOADS);
136
+ const batchNum = Math.floor(i / PARALLEL_UPLOADS) + 1;
137
+ await Promise.all(batch.map(async ([originalPath, normalizedPath]) => {
138
+ const zipObject = zipContent.files[originalPath];
139
+ const content = await zipObject.async('nodebuffer');
140
+ // Build storage path: basePath/normalizedPath
141
+ const storagePath = `${basePath}/${normalizedPath}`;
142
+ // Save the file using saveWithPath which preserves the exact path
143
+ await storage.saveWithPath(content, storagePath);
144
+ uploadedPaths.push(storagePath);
145
+ }));
146
+ uploadedCount += batch.length;
147
+ // Log progress periodically (every ~10%) or on last batch
148
+ if (batchNum % logInterval === 0 || batchNum === totalBatches) {
149
+ const percent = Math.round((uploadedCount / totalFiles) * 100);
150
+ console.log(`[ZipUtils] Progress: ${percent}% (${uploadedCount}/${totalFiles} files)`);
151
+ }
152
+ }
153
+ }
154
+ catch (error) {
155
+ // Clean up any files that were already uploaded before the error
156
+ if (uploadedPaths.length > 0) {
157
+ console.log(`[ZipUtils] Error during extraction, cleaning up ${uploadedPaths.length} uploaded files...`);
158
+ await storage.deleteBatch(uploadedPaths).catch(cleanupErr => {
159
+ console.error(`[ZipUtils] Failed to clean up files after error:`, cleanupErr);
160
+ });
161
+ }
162
+ throw error;
163
+ }
164
+ return {
165
+ root_file: rootFileNormalized,
166
+ file_count: uploadedCount
167
+ };
168
+ }
169
+ //# sourceMappingURL=zip_utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zip_utils.js","sourceRoot":"","sources":["../../src/utils/zip_utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAazB;;;;GAIG;AACH,MAAM,CAAC,KAAK,SAAS,CAAC,CAAC,uBAAuB,CAAC,SAAiB;IAC5D,MAAM,GAAG,GAAG,IAAI,KAAK,EAAE,CAAA;IACvB,MAAM,UAAU,GAAG,MAAM,GAAG,CAAC,SAAS,CAAC,SAAS,CAAC,CAAA;IAEjD,KAAK,MAAM,CAAC,IAAI,EAAE,SAAS,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE,CAAC;QAC/D,IAAI,CAAC,SAAS,CAAC,GAAG,EAAE,CAAC;YACjB,mBAAmB;YACnB,MAAM,OAAO,GAAG,MAAM,SAAS,CAAC,KAAK,CAAC,YAAY,CAAC,CAAA;YAEnD,8DAA8D;YAC9D,IAAI,CAAC;gBACD,MAAM,WAAW,GAAG,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAA;gBAC7C,wDAAwD;gBACxD,MAAM,CAAC,IAAI,CAAC,WAAW,EAAE,OAAO,CAAC,CAAA;gBACjC,MAAM,CAAC,IAAI,EAAE,WAAW,CAAC,CAAA;YAC7B,CAAC;YAAC,MAAM,CAAC;gBACL,4CAA4C;gBAC5C,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,CAAA;YACzB,CAAC;QACL,CAAC;IACL,CAAC;AACL,CAAC;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AAE,WAAW,CAAC,CAAA;gBAChD,aAAa,CAAC,IAAI,CAAC,WAAW,CAAC,CAAA;YACnC,CAAC,CAAC,CACL,CAAA;YAED,aAAa,IAAI,KAAK,CAAC,MAAM,CAAA;YAE7B,0DAA0D;YAC1D,IAAI,QAAQ,GAAG,WAAW,KAAK,CAAC,IAAI,QAAQ,KAAK,YAAY,EAAE,CAAC;gBAC5D,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,aAAa,GAAG,UAAU,CAAC,GAAG,GAAG,CAAC,CAAA;gBAC9D,OAAO,CAAC,GAAG,CAAC,wBAAwB,OAAO,MAAM,aAAa,IAAI,UAAU,SAAS,CAAC,CAAA;YAC1F,CAAC;QACL,CAAC;IACL,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACb,iEAAiE;QACjE,IAAI,aAAa,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAC3B,OAAO,CAAC,GAAG,CAAC,mDAAmD,aAAa,CAAC,MAAM,oBAAoB,CAAC,CAAA;YACxG,MAAM,OAAO,CAAC,WAAW,CAAC,aAAa,CAAC,CAAC,KAAK,CAAC,UAAU,CAAC,EAAE;gBACxD,OAAO,CAAC,KAAK,CAAC,kDAAkD,EAAE,UAAU,CAAC,CAAA;YACjF,CAAC,CAAC,CAAA;QACN,CAAC;QACD,MAAM,KAAK,CAAA;IACf,CAAC;IAED,OAAO;QACH,SAAS,EAAE,kBAAkB;QAC7B,UAAU,EAAE,aAAa;KAC5B,CAAA;AACL,CAAC"}
package/package.json ADDED
@@ -0,0 +1,72 @@
1
+ {
2
+ "name": "@cepseudo/assets",
3
+ "version": "1.0.0",
4
+ "description": "Asset management for Digital Twin framework (AssetsManager, TilesetManager, MapManager)",
5
+ "license": "MIT",
6
+ "author": "Axel Hoffmann",
7
+ "type": "module",
8
+ "main": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "import": "./dist/index.js",
13
+ "types": "./dist/index.d.ts"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist/",
18
+ "README.md",
19
+ "LICENSE"
20
+ ],
21
+ "scripts": {
22
+ "build": "tsc",
23
+ "dev": "tsc --watch",
24
+ "clean": "rimraf dist tsconfig.tsbuildinfo",
25
+ "test": "node -r ./bin/set-test-env.cjs --import ts-node-maintained/register/esm --enable-source-maps bin/test.ts",
26
+ "lint": "eslint src tests --no-error-on-unmatched-pattern",
27
+ "lint:fix": "eslint src tests --fix --no-error-on-unmatched-pattern"
28
+ },
29
+ "engines": {
30
+ "node": ">=20.0.0"
31
+ },
32
+ "dependencies": {
33
+ "@cepseudo/shared": "workspace:*",
34
+ "@cepseudo/database": "workspace:*",
35
+ "@cepseudo/storage": "workspace:*",
36
+ "@cepseudo/auth": "workspace:*",
37
+ "jszip": "^3.10.1"
38
+ },
39
+ "peerDependencies": {
40
+ "bullmq": ">=5.0.0"
41
+ },
42
+ "peerDependenciesMeta": {
43
+ "bullmq": {
44
+ "optional": true
45
+ }
46
+ },
47
+ "devDependencies": {
48
+ "@japa/assert": "^4.1.0",
49
+ "@japa/runner": "^4.3.0",
50
+ "ts-node-maintained": "^10.9.5"
51
+ },
52
+ "repository": {
53
+ "type": "git",
54
+ "url": "git+https://github.com/CePseudoBE/digitaltwin.git",
55
+ "directory": "packages/assets"
56
+ },
57
+ "keywords": [
58
+ "digitaltwin",
59
+ "assets",
60
+ "upload",
61
+ "tileset",
62
+ "3d",
63
+ "presigned-url"
64
+ ],
65
+ "bugs": {
66
+ "url": "https://github.com/CePseudoBE/digitaltwin/issues"
67
+ },
68
+ "homepage": "https://github.com/CePseudoBE/digitaltwin#readme",
69
+ "publishConfig": {
70
+ "access": "public"
71
+ }
72
+ }