webtransfer/src/components/WebContentViewer.vue

166 lines
6.0 KiB
Vue
Raw Normal View History

2024-11-19 16:11:23 +01:00
<template>
<div class="flex flex-col h-full w-full border">
2024-11-19 18:17:54 +01:00
<!-- ... -->
2024-11-19 16:11:23 +01:00
<div class="overflow-y-auto">
<div v-for="(file, index) in sortedFiles" :key="file.fullPath" class="flex flex-col">
<div
class="flex items-center pl-4 cursor-pointer"
@click="toggleFolder(file)"
v-if="file.isDirectory"
2024-11-19 18:17:54 +01:00
draggable="true"
@dragstart="onDragStart(file, $event)"
2024-11-19 16:11:23 +01:00
>
<div class="w-5/6 flex items-center px-4 py-2 truncate">
<span class="mr-2">{{ folderMap[file.fullPath] ? '-' : '+' }}</span>
{{ file.fullPath }}
</div>
<div class="w-1/6 px-4 py-2">-</div>
</div>
<div
class="flex items-center pl-4"
v-else
2024-11-19 18:17:54 +01:00
draggable="true"
@dragstart="onDragStart(file, $event)"
2024-11-19 16:11:23 +01:00
>
<div class="w-5/6 flex items-center px-4 py-2 truncate">
{{ file.fullPath }}
</div>
<div class="w-1/6 px-4 py-2">
{{ formatFileSize(file.size) }}
</div>
</div>
</div>
</div>
</div>
</template>
<script>
import JSZip from 'jszip';
export default {
name: 'WebContentViewer',
data() {
return {
zipContent: [],
2024-11-19 18:17:54 +01:00
pathTable: [],
2024-11-19 16:11:23 +01:00
folderMap: {}, // Map to track folder open/close state
};
},
props: {
zipUrl: {
type: String,
required: true,
},
},
computed: {
sortedFiles() {
const flattenAndSort = (files, parentPath = '') => {
const flatList = [];
files.forEach(file => {
const fullPath = parentPath ? `${parentPath}/${file.name}` : file.name;
// Toujours ajouter le dossier parent
flatList.push({
...file,
fullPath,
parentPath,
});
// Ajouter les enfants uniquement si le dossier est ouvert
if (file.isDirectory && this.folderMap[fullPath] && file.children) {
flatList.push(...flattenAndSort(file.children, fullPath));
}
});
return flatList.sort((a, b) => a.fullPath.localeCompare(b.fullPath));
};
return flattenAndSort(this.zipContent);
},
},
async mounted() {
await this.loadZipContent();
2024-11-19 16:27:48 +01:00
const webTransferDiv = document.getElementById('archiveInfos');
if (webTransferDiv) {
this.archiveUrl = webTransferDiv.dataset.archiveUrl;
this.token = webTransferDiv.dataset.token;
} else {
console.error('Pas d\'informations pour recuperer l\'archive');
}
2024-11-19 16:11:23 +01:00
},
methods: {
async loadZipContent() {
try {
const response = await fetch(this.zipUrl);
const zipData = await response.blob();
const zip = await JSZip.loadAsync(zipData);
const files = [];
zip.forEach((relativePath, file) => {
const pathParts = relativePath.split('/').filter(Boolean);
let currentLevel = 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);
if (!existing) {
existing = {
name: partName,
isDirectory,
size: isDirectory ? 0 : file._data.uncompressedSize,
children: isDirectory ? [] : null,
2024-11-19 18:17:54 +01:00
file: file,
2024-11-19 16:11:23 +01:00
};
currentLevel.push(existing);
}
if (isDirectory) {
currentLevel = existing.children;
}
}
});
this.zipContent = files;
// Initialize folderMap
const initializeFolderMap = (files, parentPath = '') => {
files.forEach(file => {
const fullPath = parentPath ? `${parentPath}/${file.name}` : file.name;
2024-11-19 16:17:10 +01:00
this.$set(this.folderMap, fullPath, false);
2024-11-19 16:11:23 +01:00
if (file.isDirectory && file.children) {
initializeFolderMap(file.children, fullPath);
}
});
};
initializeFolderMap(this.zipContent);
} catch (error) {
console.error('Erreur lors du chargement du contenu du ZIP :', error);
}
},
formatFileSize(size) {
if (size < 1024) return `${size} B`;
if (size < 1024 * 1024) return `${(size / 1024).toFixed(2)} KB`;
if (size < 1024 * 1024 * 1024) return `${(size / 1024 / 1024).toFixed(2)} MB`;
return `${(size / 1024 / 1024 / 1024).toFixed(2)} GB`;
},
toggleFolder(file) {
if (!file.isDirectory) return;
const currentState = this.folderMap[file.fullPath];
this.$set(this.folderMap, file.fullPath, !currentState);
},
2024-11-19 18:17:54 +01:00
onDragStart(file, event) {
// Sauvegarder l'objet du fichier dans l'événement
event.dataTransfer.setData('file', JSON.stringify(file.file));
},
2024-11-19 16:11:23 +01:00
},
};
</script>
<style scoped>
/* Ajoutez ici des styles si nécessaire */
</style>