diff options
| author | metamuffin <metamuffin@disroot.org> | 2022-10-27 08:01:55 +0200 | 
|---|---|---|
| committer | metamuffin <metamuffin@disroot.org> | 2022-10-27 08:01:55 +0200 | 
| commit | 25fca581c0fad99283709434cb9b8848ed603596 (patch) | |
| tree | 85928c285e2ecd8797598f44eb1d1f09273cdedb /client-web/source/sw | |
| parent | d5c98d16091de59e826e91e28cea65f403b39331 (diff) | |
| download | keks-meet-25fca581c0fad99283709434cb9b8848ed603596.tar keks-meet-25fca581c0fad99283709434cb9b8848ed603596.tar.bz2 keks-meet-25fca581c0fad99283709434cb9b8848ed603596.tar.zst | |
fallback download strat
Diffstat (limited to 'client-web/source/sw')
| -rw-r--r-- | client-web/source/sw/download_stream.ts | 49 | 
1 files changed, 26 insertions, 23 deletions
| diff --git a/client-web/source/sw/download_stream.ts b/client-web/source/sw/download_stream.ts index 2a8049d..1a2fec8 100644 --- a/client-web/source/sw/download_stream.ts +++ b/client-web/source/sw/download_stream.ts @@ -1,30 +1,33 @@ +import { log } from "../logger.ts"  import { SW } from "./init.ts" -// export function StreamDownload(size: number, filename?: string, progress?: (position: number) => void) { -//     let position = 0 -//     const buffer = new Uint8Array(size) -//     return { -//         close() { -//             const a = document.createElement("a") -//             a.href = URL.createObjectURL(new Blob([buffer], { type: "text/plain" })) -//             a.download = filename ?? "file" -//             a.click() -//         }, -//         write(chunk: Blob) { -//             const reader = new FileReader(); -//             reader.onload = function (event) { -//                 const arr = new Uint8Array(event.target!.result as ArrayBuffer); -//                 for (let i = 0; i < arr.length; i++, position++) { -//                     buffer[position] = arr[i] -//                 } -//                 if (progress) progress(position) -//             }; -//             reader.readAsArrayBuffer(chunk); -//         } -//     } -// } +function FallbackStreamDownload(size: number, filename?: string, progress?: (position: number) => void) { +    log({ scope: "*", warn: true }, "downloading to memory because serviceworker is not available") +    let position = 0 +    const buffer = new Uint8Array(size) +    return { +        close() { +            const a = document.createElement("a") +            a.href = URL.createObjectURL(new Blob([buffer], { type: "text/plain" })) +            a.download = filename ?? "file" +            a.click() +        }, +        write(chunk: Blob) { +            const reader = new FileReader(); +            reader.onload = function (event) { +                const arr = new Uint8Array(event.target!.result as ArrayBuffer); +                for (let i = 0; i < arr.length; i++, position++) { +                    buffer[position] = arr[i] +                } +                if (progress) progress(position) +            }; +            reader.readAsArrayBuffer(chunk); +        } +    } +}  export function StreamDownload(size: number, filename?: string, progress?: (position: number) => void) { +    if (!SW) FallbackStreamDownload(size, filename, progress)      let position = 0      const path = `/download/${encodeURIComponent(filename ?? "file")}` | 
