mirror of
https://github.com/catdevnull/transicion-desordenada-diablo
synced 2024-11-14 10:21:38 +00:00
WIP: un intento de evitar streamsaver si es posible
no me genera mucha confianza este código pero era para prevenir usar streamsaver cuando no lo necesitamos con un hack en el servidor si está disponible
This commit is contained in:
parent
13dfb91a50
commit
8c251aae7f
1 changed files with 47 additions and 22 deletions
|
@ -1,4 +1,3 @@
|
|||
import streamSaver from "streamsaver";
|
||||
import { zData, type Distribution, zError, zDumpMetadata } from "common/schema";
|
||||
|
||||
export async function downloadFile(
|
||||
|
@ -7,36 +6,62 @@ export async function downloadFile(
|
|||
dist: Distribution,
|
||||
) {
|
||||
if (!dist.downloadURL) throw new Error("no downloadURL");
|
||||
const outputS = streamSaver.createWriteStream(
|
||||
dist.downloadURL.slice(dist.downloadURL.lastIndexOf("/") + 1),
|
||||
);
|
||||
const res = await fetchGzipped(
|
||||
`${dataPath}/${datasetId}/${dist.identifier}/${
|
||||
dist.fileName || dist.identifier
|
||||
}.gz`,
|
||||
);
|
||||
await res.body!.pipeTo(outputS);
|
||||
const downloadURL = `${dataPath}/${datasetId}/${dist.identifier}/${
|
||||
dist.fileName || dist.identifier
|
||||
}.gz`;
|
||||
|
||||
const localGzip = async () => {
|
||||
const streamSaver = await import("streamsaver");
|
||||
const outputS = streamSaver.createWriteStream(
|
||||
dist.downloadURL!.slice(dist.downloadURL!.lastIndexOf("/") + 1),
|
||||
);
|
||||
const res = await fetchGzipped(downloadURL);
|
||||
await res.body!.pipeTo(outputS);
|
||||
};
|
||||
|
||||
if (navigator.userAgent.indexOf("Firefox") != -1) {
|
||||
// firefox, por alguna razón inexplicable, no entiende el `content-encoding`
|
||||
// cuando se intenta descargar un archivo. lo ignora y guarda el archivo
|
||||
// comprimido. entonces tenemos que usar el hack de streamsaver. prefiero
|
||||
// usarlo solo en firefox porque anda medio mal o no anda en otros navegadores.
|
||||
await localGzip();
|
||||
} else {
|
||||
const infoRes = await fetch(downloadURL);
|
||||
await infoRes.body?.cancel();
|
||||
if (infoRes.headers.get("Content-Type") === "application/gzip-hack") {
|
||||
// server tiene el hack de gzip=true activado, genial
|
||||
location.href = `${downloadURL}?gzip=true`;
|
||||
} else {
|
||||
await localGzip();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchGzipped(url: string): Promise<Response> {
|
||||
let res = await fetch(url);
|
||||
let res = await fetch(`${url}?gzip=true`);
|
||||
if (res.status === 404 && url.endsWith(".gz")) {
|
||||
// probar cargando el archivo no comprimido
|
||||
res = await fetch(url.slice(0, url.length - ".gz".length));
|
||||
return res;
|
||||
}
|
||||
let DecStream;
|
||||
if ("DecompressionStream" in window) DecStream = window.DecompressionStream;
|
||||
else {
|
||||
const { makeDecompressionStream } = await import(
|
||||
"compression-streams-polyfill/ponyfill"
|
||||
);
|
||||
DecStream = makeDecompressionStream(TransformStream);
|
||||
if (res.headers.get("Content-Type") === "application/gzip-hack") {
|
||||
// server tiene el hack de gzip=true activado, genial
|
||||
return res;
|
||||
} else {
|
||||
// server no tiene el hack, usamos DecompressionStream
|
||||
let DecStream;
|
||||
if ("DecompressionStream" in window) DecStream = window.DecompressionStream;
|
||||
else {
|
||||
const { makeDecompressionStream } = await import(
|
||||
"compression-streams-polyfill/ponyfill"
|
||||
);
|
||||
DecStream = makeDecompressionStream(TransformStream);
|
||||
}
|
||||
const ds = new DecStream("gzip");
|
||||
const decompressedStream = res.body!.pipeThrough(ds);
|
||||
const resD = new Response(decompressedStream);
|
||||
return resD;
|
||||
}
|
||||
const ds = new DecStream("gzip");
|
||||
const decompressedStream = res.body!.pipeThrough(ds);
|
||||
const resD = new Response(decompressedStream);
|
||||
return resD;
|
||||
}
|
||||
let cachedGzippedJson = new Map<string, { date: Date; data: unknown }>();
|
||||
|
||||
|
|
Loading…
Reference in a new issue