|
| 1 | +import JSZip from "jszip"; |
| 2 | +import { getSolidDataset, getContainedResourceUrlAll, UrlString } from "@inrupt/solid-client"; |
| 3 | +import { decodeResourceNameFromUrl, ensureTrailingSlash } from "./copyUtils"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Downloads a single file |
| 7 | + */ |
| 8 | +export async function downloadFile( |
| 9 | + fileUrl: string, |
| 10 | + fileName: string, |
| 11 | + fetchFn: typeof fetch |
| 12 | +): Promise<void> { |
| 13 | + try { |
| 14 | + console.log("downloadFile: Starting fetch for", fileUrl); |
| 15 | + const response = await fetchFn(fileUrl); |
| 16 | + |
| 17 | + if (!response.ok) { |
| 18 | + const errorText = await response.text().catch(() => response.statusText); |
| 19 | + throw new Error(`Failed to fetch file: ${response.status} ${errorText}`); |
| 20 | + } |
| 21 | + |
| 22 | + const blob = await response.blob(); |
| 23 | + |
| 24 | + // Create a download link |
| 25 | + const url = URL.createObjectURL(blob); |
| 26 | + const link = document.createElement("a"); |
| 27 | + link.href = url; |
| 28 | + link.download = fileName; |
| 29 | + link.style.display = "none"; |
| 30 | + document.body.appendChild(link); |
| 31 | + |
| 32 | + // Trigger download immediately |
| 33 | + link.click(); |
| 34 | + |
| 35 | + // Clean up after a delay to ensure download starts |
| 36 | + setTimeout(() => { |
| 37 | + document.body.removeChild(link); |
| 38 | + URL.revokeObjectURL(url); |
| 39 | + }, 100); |
| 40 | + } catch (error) { |
| 41 | + console.error("Failed to download file:", error); |
| 42 | + throw new Error(`Failed to download file: ${error instanceof Error ? error.message : "Unknown error"}`); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Recursively collects all files in a folder |
| 48 | + */ |
| 49 | +async function collectFolderFiles( |
| 50 | + folderUrl: string, |
| 51 | + fetchFn: typeof fetch, |
| 52 | + zip: JSZip, |
| 53 | + basePath: string = "", |
| 54 | + visited: Set<string> = new Set() |
| 55 | +): Promise<void> { |
| 56 | + const normalizedUrl = ensureTrailingSlash(folderUrl); |
| 57 | + |
| 58 | + // Prevent infinite loops |
| 59 | + if (visited.has(normalizedUrl)) { |
| 60 | + return; |
| 61 | + } |
| 62 | + visited.add(normalizedUrl); |
| 63 | + |
| 64 | + try { |
| 65 | + const dataset = await getSolidDataset(normalizedUrl as UrlString, { fetch: fetchFn }); |
| 66 | + const containedResources = getContainedResourceUrlAll(dataset); |
| 67 | + |
| 68 | + for (const resourceUrl of containedResources) { |
| 69 | + if (resourceUrl.endsWith("/")) { |
| 70 | + // It's a folder - recurse |
| 71 | + const folderName = decodeResourceNameFromUrl(resourceUrl); |
| 72 | + const folderPath = basePath ? `${basePath}/${folderName}` : folderName; |
| 73 | + await collectFolderFiles(resourceUrl, fetchFn, zip, folderPath, visited); |
| 74 | + } else { |
| 75 | + // It's a file - add to zip |
| 76 | + try { |
| 77 | + const response = await fetchFn(resourceUrl); |
| 78 | + |
| 79 | + if (!response.ok) { |
| 80 | + console.warn(`Failed to fetch file ${resourceUrl}: ${response.status} ${response.statusText}`); |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + const blob = await response.blob(); |
| 85 | + const fileName = decodeResourceNameFromUrl(resourceUrl); |
| 86 | + const filePath = basePath ? `${basePath}/${fileName}` : fileName; |
| 87 | + |
| 88 | + zip.file(filePath, blob); |
| 89 | + } catch (error) { |
| 90 | + console.warn(`Failed to add file ${resourceUrl} to zip:`, error); |
| 91 | + |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } catch (error) { |
| 96 | + console.error(`Failed to access folder ${folderUrl}:`, error); |
| 97 | + throw error; |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * Downloads a folder as a ZIP file |
| 103 | + */ |
| 104 | +export async function downloadFolderAsZip( |
| 105 | + folderUrl: string, |
| 106 | + folderName: string, |
| 107 | + fetchFn: typeof fetch |
| 108 | +): Promise<void> { |
| 109 | + try { |
| 110 | + const zip = new JSZip(); |
| 111 | + |
| 112 | + // Collect all files recursively |
| 113 | + await collectFolderFiles(folderUrl, fetchFn, zip); |
| 114 | + |
| 115 | + // Generate the ZIP file |
| 116 | + const zipBlob = await zip.generateAsync({ type: "blob" }); |
| 117 | + |
| 118 | + // Create a download link |
| 119 | + const url = URL.createObjectURL(zipBlob); |
| 120 | + const link = document.createElement("a"); |
| 121 | + link.href = url; |
| 122 | + link.download = `${folderName}.zip`; |
| 123 | + link.style.display = "none"; |
| 124 | + document.body.appendChild(link); |
| 125 | + |
| 126 | + // Trigger download immediately |
| 127 | + link.click(); |
| 128 | + |
| 129 | + // Clean up after a delay to ensure download starts |
| 130 | + setTimeout(() => { |
| 131 | + document.body.removeChild(link); |
| 132 | + URL.revokeObjectURL(url); |
| 133 | + }, 100); |
| 134 | + } catch (error) { |
| 135 | + console.error("Failed to download folder as zip:", error); |
| 136 | + throw new Error(`Failed to download folder: ${error instanceof Error ? error.message : "Unknown error"}`); |
| 137 | + } |
| 138 | +} |
| 139 | + |
0 commit comments