From c8209c5799f9e6562077be28863ba0c8c9a88b77 Mon Sep 17 00:00:00 2001 From: Salem Yaslem Date: Sat, 28 Sep 2024 02:43:18 +0300 Subject: [PATCH] add(#8): add download button for individual files --- app.vue | 28 +++++++++++++++++++++++----- composables/files-manager.ts | 22 ++++++++++++++++++++-- composables/worker/7zip-manager.ts | 4 ++-- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/app.vue b/app.vue index ce50a57..e50a53a 100644 --- a/app.vue +++ b/app.vue @@ -115,9 +115,11 @@ function stepUp(path: string) { @@ -162,8 +164,23 @@ function stepUp(path: string) { - + + + + + + + + + +

{{ file.name }}

@@ -190,7 +207,8 @@ function stepUp(path: string) { Extract and Explore compressed files online and securely.

- mdi-shield nothing leave + mdi-shield nothing + leave your browser

diff --git a/composables/files-manager.ts b/composables/files-manager.ts index 60102f2..37176b1 100644 --- a/composables/files-manager.ts +++ b/composables/files-manager.ts @@ -5,6 +5,7 @@ import * as Comlink from "comlink"; // @ts-expect-error typescript can't find it when query it with ?worker import SevenZipWorker from "./worker/7zip-manager?worker"; import { SevenZipManager, iFile } from "./worker/7zip-manager"; +import mime from 'mime'; export const videoExtensions = ['mp4', 'avi', 'mov', 'mkv']; export const imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp']; @@ -74,8 +75,25 @@ export class FilesManager { return await this.remoteSevenZipManager.generateBlobUrl(JSON.stringify(this.getFile(path)) as any); } - async getFileContent(path: string) { + async getFileContent(path: string, encoding: "utf8" | "binary" = "utf8") { if (!this.remoteSevenZipManager) return; - return await this.remoteSevenZipManager.getFileContent(JSON.stringify(this.getFile(path)) as any); + return await this.remoteSevenZipManager.getFileContent(JSON.stringify(this.getFile(path)) as any, encoding); + } + + async downloadFile(path: string) { + if (!this.remoteSevenZipManager) return; + + const file = this.getFile(path); + const fileContent = await this.getFileContent(path, "binary"); + if (!file) return; + + const blob = new Blob([fileContent as any], { type: mime.getType(file.extension!) || "application/octet-stream" }); + const url = URL.createObjectURL(blob); + + const a = document.createElement("a"); + a.href = url; + a.download = file.name; + a.click(); + } } \ No newline at end of file diff --git a/composables/worker/7zip-manager.ts b/composables/worker/7zip-manager.ts index 579de02..40f366c 100644 --- a/composables/worker/7zip-manager.ts +++ b/composables/worker/7zip-manager.ts @@ -163,7 +163,7 @@ export class SevenZipManager { } // get content from buffer (Experimental) - async getFileContent(file: iFile) { + async getFileContent(file: iFile, encoding: "utf8" | "binary" = "utf8") { if (!this.sevenZip) return; file = typeof file === "string" ? JSON.parse(file) : file; @@ -172,7 +172,7 @@ export class SevenZipManager { this.sevenZip.FS.chmod(file.path, 0o777); // get file buffer - const buffer = this.sevenZip.FS.readFile(file.path, { encoding: "utf8" }); + const buffer = this.sevenZip.FS.readFile(file.path, { encoding: encoding as any }); // remove the file after extract local blob url this.sevenZip.FS.unlink(file.path);