-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix image previews in encrypted drives
- Loading branch information
Showing
4 changed files
with
76 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,40 @@ | ||
<script lang="ts"> | ||
import { Img } from "flowbite-svelte"; | ||
import { getContext } from "svelte"; | ||
import { Img, Spinner } from "flowbite-svelte"; | ||
import { joinPath } from "../../../../../blossom-drive-client/FileTree/methods"; | ||
import { servers } from "../../../../../services/servers"; | ||
import { getContext } from "svelte"; | ||
import { EncryptedDrive } from "../../../../../blossom-drive-client/EncryptedDrive"; | ||
import type Drive from "../../../../../blossom-drive-client/Drive"; | ||
import { getLocalFileURL } from "../../../../../services/downloads"; | ||
export let src: string; | ||
export let alt: string; | ||
let loading = false; | ||
let resolved = src; | ||
$: { | ||
const drive = getContext<Drive | EncryptedDrive>("drive"); | ||
const drive = getContext<Drive>("drive"); | ||
if (drive) { | ||
if (src.startsWith("./") || src.startsWith("/")) { | ||
loading = true; | ||
try { | ||
const subPath = getContext<string>("path"); | ||
if (drive instanceof EncryptedDrive) { | ||
resolved = ""; | ||
} else { | ||
const fullPath = src.startsWith("/") ? src : joinPath(subPath, src.replace(/^\.\//, "")); | ||
resolved = drive.getFileURL(fullPath, $servers); | ||
} | ||
const fullPath = src.startsWith("/") ? src : joinPath(subPath, src.replace(/^\.\//, "")); | ||
getLocalFileURL(drive, fullPath, $servers) | ||
.then((url) => (resolved = url)) | ||
.finally(() => (loading = false)); | ||
} catch (e) { | ||
resolved = ""; | ||
loading = false; | ||
} | ||
} | ||
} | ||
} | ||
</script> | ||
|
||
<Img src={resolved || undefined} {alt} class="max-h-96" /> | ||
{#if loading} | ||
<Spinner /> | ||
{:else} | ||
<Img src={resolved || undefined} {alt} class="max-h-96" /> | ||
{/if} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,52 @@ | ||
import { writable } from "svelte/store"; | ||
import type { MultiDownload } from "../helpers/multi-download"; | ||
import type Drive from "../blossom-drive-client/Drive"; | ||
import { EncryptedDrive } from "../blossom-drive-client/EncryptedDrive"; | ||
import type { Path } from "../blossom-drive-client/FileTree/methods"; | ||
|
||
export const drawerOpen = writable(false); | ||
export const downloads = writable<MultiDownload[]>([]); | ||
|
||
export function addDownload(download: MultiDownload) { | ||
downloads.update((arr) => [...arr, download]); | ||
} | ||
|
||
const cache = new Map<string, File>(); | ||
export async function getOrDownloadFile(drive: Drive, path: Path, additionalServers: string[]) { | ||
const file = drive.getFile(path); | ||
|
||
let download = cache.get(file.sha256); | ||
if (!download) { | ||
const d = await drive.downloadFile(file.path, additionalServers); | ||
if (!d) throw new Error("Failed to download file"); | ||
cache.set(file.sha256, d); | ||
download = d; | ||
} | ||
|
||
return download; | ||
} | ||
|
||
const URLCache = new Map<string, string>(); | ||
export async function getLocalFileURL(drive: Drive, path: Path, additionalServers: string[]) { | ||
const file = drive.getFile(path); | ||
|
||
if (drive instanceof EncryptedDrive) { | ||
let url = URLCache.get(file.sha256); | ||
if (!url) { | ||
let download = await getOrDownloadFile(drive, path, additionalServers); | ||
url = URL.createObjectURL(download); | ||
URLCache.set(file.sha256, url); | ||
} | ||
return url; | ||
} else { | ||
return drive.getFileURL(file.path, additionalServers); | ||
} | ||
} | ||
|
||
export function clearCache() { | ||
for (const [_, url] of URLCache) { | ||
URL.revokeObjectURL(url); | ||
} | ||
URLCache.clear(); | ||
cache.clear(); | ||
} |