From e612126d621aef048e0d8acf5c6fdfa33ee2a17e Mon Sep 17 00:00:00 2001 From: Alexandre_BRAVO Date: Mon, 6 Jan 2025 16:59:05 +0100 Subject: [PATCH] ajout traitement d'un fichier unique --- package-lock.json | 122 +++++++++++++++++++++++++- package.json | 1 + src/App.vue | 3 +- src/components/FileTable.vue | 1 - src/components/WebContentViewer.vue | 130 +++++++++++++++++----------- 5 files changed, 202 insertions(+), 55 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7d115c5..7696e87 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "@nextcloud/files": "^3.10.0", "@nextcloud/initial-state": "^2.2.0", "@nextcloud/vue": "^8.20.0", + "file-type": "^19.6.0", "i18next": "^24.0.2", "jszip": "^3.10.1", "vue": "^2.7.16", @@ -1819,6 +1820,12 @@ "license": "MIT", "peer": true }, + "node_modules/@sec-ant/readable-stream": { + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/@sec-ant/readable-stream/-/readable-stream-0.4.1.tgz", + "integrity": "sha512-831qok9r2t8AlxLko40y2ebgSDhenenCatLVeW/uBtnHPyhHOvG0C7TvfgecV+wHzIm5KUICgzmVpWS+IMEAeg==", + "license": "MIT" + }, "node_modules/@shikijs/core": { "version": "1.23.1", "resolved": "https://registry.npmjs.org/@shikijs/core/-/core-1.23.1.tgz", @@ -1882,6 +1889,12 @@ "dev": true, "license": "MIT" }, + "node_modules/@tokenizer/token": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@tokenizer/token/-/token-0.3.0.tgz", + "integrity": "sha512-OvjF+z51L3ov0OyAU0duzsYuvO01PH7x4t6DJx+guahgTnBHkhJdG7soQeTSFLWN3efnHyibZ4Z8l2EuWwJN3A==", + "license": "MIT" + }, "node_modules/@types/body-parser": { "version": "1.19.5", "resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.5.tgz", @@ -6712,6 +6725,24 @@ "node": "^10.12.0 || >=12.0.0" } }, + "node_modules/file-type": { + "version": "19.6.0", + "resolved": "https://registry.npmjs.org/file-type/-/file-type-19.6.0.tgz", + "integrity": "sha512-VZR5I7k5wkD0HgFnMsq5hOsSc710MJMu5Nc5QYsbe38NN5iPV/XTObYLc/cpttRTf6lX538+5uO1ZQRhYibiZQ==", + "license": "MIT", + "dependencies": { + "get-stream": "^9.0.1", + "strtok3": "^9.0.1", + "token-types": "^6.0.0", + "uint8array-extras": "^1.3.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sindresorhus/file-type?sponsor=1" + } + }, "node_modules/fill-range": { "version": "7.1.1", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz", @@ -7064,6 +7095,22 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/get-stream": { + "version": "9.0.1", + "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-9.0.1.tgz", + "integrity": "sha512-kVCxPF3vQM/N0B1PmoqVUqgHP+EeVjmZSQn+1oCRPxd2P21P2F19lIgbR3HBosbB1PUhOAoctJnfEn2GbN2eZA==", + "license": "MIT", + "dependencies": { + "@sec-ant/readable-stream": "^0.4.1", + "is-stream": "^4.0.1" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/get-symbol-description": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/get-symbol-description/-/get-symbol-description-1.0.2.tgz", @@ -7852,7 +7899,6 @@ "version": "1.2.1", "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", "integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==", - "dev": true, "funding": [ { "type": "github", @@ -7867,8 +7913,7 @@ "url": "https://feross.org/support" } ], - "license": "BSD-3-Clause", - "peer": true + "license": "BSD-3-Clause" }, "node_modules/ignore": { "version": "5.3.2", @@ -8555,6 +8600,18 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/is-stream": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-4.0.1.tgz", + "integrity": "sha512-Dnz92NInDqYckGEUJv689RbRiTSEHCQ7wOVeALbkOz999YpqT46yMRIGtSNl2iCL1waAZSx40+h59NV/EwzV/A==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/is-string": { "version": "1.0.7", "resolved": "https://registry.npmjs.org/is-string/-/is-string-1.0.7.tgz", @@ -11043,6 +11100,19 @@ "node": ">=0.12" } }, + "node_modules/peek-readable": { + "version": "5.3.1", + "resolved": "https://registry.npmjs.org/peek-readable/-/peek-readable-5.3.1.tgz", + "integrity": "sha512-GVlENSDW6KHaXcd9zkZltB7tCLosKB/4Hg0fqBJkAoBgYG2Tn1xtMgXtSUuMU9AK/gCm/tTdT8mgAeF4YNeeqw==", + "license": "MIT", + "engines": { + "node": ">=14.16" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/Borewit" + } + }, "node_modules/picocolors": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.1.tgz", @@ -13518,6 +13588,23 @@ "integrity": "sha512-J8bbNyKKXl5qYcR36TIO8W3mVGVHrmmxsd5PAItGkmyzwJvybiw2IVq5nqd0i4LSNSkB/sx9VHllbfFdr9k1JA==", "license": "MIT" }, + "node_modules/strtok3": { + "version": "9.1.1", + "resolved": "https://registry.npmjs.org/strtok3/-/strtok3-9.1.1.tgz", + "integrity": "sha512-FhwotcEqjr241ZbjFzjlIYg6c5/L/s4yBGWSMvJ9UoExiSqL+FnFA/CaeZx17WGaZMS/4SOZp8wH18jSS4R4lw==", + "license": "MIT", + "dependencies": { + "@tokenizer/token": "^0.3.0", + "peek-readable": "^5.3.1" + }, + "engines": { + "node": ">=16" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/Borewit" + } + }, "node_modules/style-loader": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/style-loader/-/style-loader-4.0.0.tgz", @@ -14245,6 +14332,23 @@ "node": ">=0.6" } }, + "node_modules/token-types": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/token-types/-/token-types-6.0.0.tgz", + "integrity": "sha512-lbDrTLVsHhOMljPscd0yitpozq7Ga2M5Cvez5AjGg8GASBjtt6iERCAJ93yommPmz62fb45oFIXHEZ3u9bfJEA==", + "license": "MIT", + "dependencies": { + "@tokenizer/token": "^0.3.0", + "ieee754": "^1.2.1" + }, + "engines": { + "node": ">=14.16" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/Borewit" + } + }, "node_modules/tree-dump": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/tree-dump/-/tree-dump-1.0.2.tgz", @@ -14587,6 +14691,18 @@ "license": "MIT", "peer": true }, + "node_modules/uint8array-extras": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/uint8array-extras/-/uint8array-extras-1.4.0.tgz", + "integrity": "sha512-ZPtzy0hu4cZjv3z5NW9gfKnNLjoz4y6uv4HlelAjDK7sY/xOkKZv9xK/WQpcsBB3jEybChz9DPC2U/+cusjJVQ==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/unbox-primitive": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/unbox-primitive/-/unbox-primitive-1.0.2.tgz", diff --git a/package.json b/package.json index 48e63da..b60054d 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "@nextcloud/files": "^3.10.0", "@nextcloud/initial-state": "^2.2.0", "@nextcloud/vue": "^8.20.0", + "file-type": "^19.6.0", "i18next": "^24.0.2", "jszip": "^3.10.1", "vue": "^2.7.16", diff --git a/src/App.vue b/src/App.vue index 23d7849..85519f5 100644 --- a/src/App.vue +++ b/src/App.vue @@ -38,9 +38,8 @@ export default { WebContentViewer }, data() { - console.log('feur') let zipUrl = document.getElementById('archiveInfos').getAttribute('dataarchiveurl'); - console.log(zipUrl) + //console.log(zipUrl) return { zipUrl, sharedFile: null, diff --git a/src/components/FileTable.vue b/src/components/FileTable.vue index e79b5d7..a10d141 100644 --- a/src/components/FileTable.vue +++ b/src/components/FileTable.vue @@ -407,7 +407,6 @@ export default { try { this.isTransfering = true; const file = this.file; - console.log(file) if (!file) return; if (file.isList) { diff --git a/src/components/WebContentViewer.vue b/src/components/WebContentViewer.vue index c266d41..71a1878 100644 --- a/src/components/WebContentViewer.vue +++ b/src/components/WebContentViewer.vue @@ -96,6 +96,7 @@ import ChevronRightIcon from 'vue-material-design-icons/ChevronRight.vue'; import ChevronDownIcon from 'vue-material-design-icons/ChevronDown.vue'; import Loading from 'vue-material-design-icons/Loading.vue'; import { ref } from 'vue'; +import {fileTypeFromBuffer} from 'file-type'; export default { name: 'WebContentViewer', @@ -186,67 +187,98 @@ export default { let response = await fetch(fullUrl); let responseJson = await response.json(); + const zipData = responseJson.parameters.data; - this.zipName = this.zipUrl.split('/').pop(); - const zip = await JSZip.loadAsync(zipData); - this.zipSize = zipData.size; + const first10Chars = zipData.substring(0,4); - const files = []; + // Check si le debut du fichier correspond a celui d'un zip + if(first10Chars === 'PK\x03\x04' || first10Chars === 'PK\x05\x06' || first10Chars === 'PK\x07\x08') { + this.zipName = this.zipUrl.split('/').pop(); + const zip = await JSZip.loadAsync(zipData); + this.zipSize = zipData.size; - zip.forEach((relativePath, file) => { - const pathParts = relativePath.split('/').filter(Boolean); - let currentLevel = files; + const files = []; - for (let i = 0; i < pathParts.length; i++) { - const partName = pathParts[i]; - const isDirectory = i < pathParts.length - 1 || file.dir; - let existing = currentLevel.find(f => f.name === partName && f.isDirectory === isDirectory); + zip.forEach((relativePath, file) => { + const pathParts = relativePath.split('/').filter(Boolean); + let currentLevel = files; - let promise; + for (let i = 0; i < pathParts.length; i++) { + const partName = pathParts[i]; + const isDirectory = i < pathParts.length - 1 || file.dir; + let existing = currentLevel.find(f => f.name === partName && f.isDirectory === isDirectory); - if (!isDirectory) { - promise = file.async("blob").then(content => { - existing.content = content; - }); - } + let promise; - if (!existing) { - existing = { - name: pathParts[i], - isDirectory, - size: isDirectory ? 0 : file._data.uncompressedSize, - content: isDirectory ? null : '', // Initialiser 'content' pour les fichiers - children: isDirectory ? [] : null, - depth: pathParts.length, // Profondeur du fichier dans l'arborescence - //remove the name of the file from the path - parentPath: i > 0 ? pathParts[i - 1] : '', - unzip: promise - }; - currentLevel.push(existing); - } + if (!isDirectory) { + promise = file.async("blob").then(content => { + existing.content = content; + }); + } - if (isDirectory) { - currentLevel = existing.children; - } - } - }); + if (!existing) { + existing = { + name: pathParts[i], + isDirectory, + size: isDirectory ? 0 : file._data.uncompressedSize, + content: isDirectory ? null : '', // Initialiser 'content' pour les fichiers + children: isDirectory ? [] : null, + depth: pathParts.length, // Profondeur du fichier dans l'arborescence + //remove the name of the file from the path + parentPath: i > 0 ? pathParts[i - 1] : '', + unzip: promise + }; + currentLevel.push(existing); + } - // Attendre que tous les contenus de fichier soient extraits - this.zipContent = files; - - // Initialiser folderMap - const initializeFolderMap = (files, parentPath = '') => { - files.forEach(file => { - const fullPath = parentPath ? `${parentPath}/${file.name}` : file.name; - this.$set(this.folderMap, fullPath, false); - if (file.isDirectory && file.children) { - initializeFolderMap(file.children, fullPath); + if (isDirectory) { + currentLevel = existing.children; + } } }); - }; - initializeFolderMap(this.zipContent); - console.log('Contenu du ZIP chargé avec succès'); + // Attendre que tous les contenus de fichier soient extraits + this.zipContent = files; + + // Initialiser folderMap + const initializeFolderMap = (files, parentPath = '') => { + files.forEach(file => { + const fullPath = parentPath ? `${parentPath}/${file.name}` : file.name; + this.$set(this.folderMap, fullPath, false); + if (file.isDirectory && file.children) { + initializeFolderMap(file.children, fullPath); + } + }); + }; + + initializeFolderMap(this.zipContent); + console.log('Contenu du ZIP chargé avec succès'); + } + else{ + const uint8Array = new Uint8Array(zipData.length); + for (let i = 0; i