Support downloading files from zip files in filesystem

This commit is contained in:
2024-02-16 18:02:51 +01:00
parent a4c5b97cdf
commit 68ef84b1e7
5 changed files with 131 additions and 59 deletions

View File

@@ -1,7 +1,7 @@
<script>
import { createEventDispatcher } from "svelte";
import { formatDataVolume, formatDate } from "../../util/Formatting.svelte"
import ZipItem from "./ZipItem.svelte";
import ZipItem from "../../file_viewer/viewers/ZipItem.svelte";
import IconBlock from "../../file_viewer/viewers/IconBlock.svelte";
import TextBlock from "../../file_viewer/viewers/TextBlock.svelte";
import { fs_node_icon, fs_path_url } from "../FilesystemUtil";
@@ -18,10 +18,17 @@ let zip = {
}
let uncomp_size = 0
let comp_ratio = 0
let archive_type = ""
export const update = async () => {
dispatch("loading", true)
if (state.base.file_type === "application/zip") {
archive_type = "zip"
} else if (state.base.file_type === "application/x-7z-compressed") {
archive_type = "7z"
}
try {
let resp = await fetch(fs_path_url(state.base.path)+"?zip_info")
@@ -32,6 +39,13 @@ export const update = async () => {
zip = await resp.json()
// Check if the zip has the property which allows separate files to be
// downloaded. If so then we set the download URL for each file
if (zip.properties && zip.properties.includes("read_individual_files")) {
// Set the download URL for each file in the zip
recursive_set_url(fs_path_url(state.base.path)+"?zip_file=", zip)
}
uncomp_size = recursive_size(zip)
comp_ratio = (uncomp_size / state.base.file_size)
} catch (err) {
@@ -43,6 +57,16 @@ export const update = async () => {
status = "finished"
}
const recursive_set_url = (parent_path, file) => {
file.download_url = parent_path
if (file.children) {
Object.entries(file.children).forEach(child => {
recursive_set_url(file.download_url + "/" + child[0], child[1])
});
}
}
const recursive_size = (file) => {
let size = file.size
@@ -64,8 +88,14 @@ const recursive_size = (file) => {
<h1>{state.base.name}</h1>
<IconBlock icon_href={fs_node_icon(state.base, 256, 256)}>
{#if archive_type === "7z"}
This is a 7-zip archive. You will need
<a href="https://www.7-zip.org/">7-zip</a> or compatible software to
extract it<br/>
{/if}
Compressed size: {formatDataVolume(state.base.file_size, 3)}<br/>
Uncompressed size: {formatDataVolume(uncomp_size, 3)} (Ratio: {comp_ratio.toFixed(2)}x)<br/>
Uncompressed size: {formatDataVolume(zip.size, 3)} (Ratio: {comp_ratio.toFixed(2)}x)<br/>
Uploaded on: {formatDate(state.base.created, true, true, true)}
<br/>
<button class="button_highlight" on:click={() => {dispatch("download")}}>