mirror of
https://github.com/catdevnull/transicion-desordenada-diablo
synced 2024-11-14 18: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";
|
import { zData, type Distribution, zError, zDumpMetadata } from "common/schema";
|
||||||
|
|
||||||
export async function downloadFile(
|
export async function downloadFile(
|
||||||
|
@ -7,24 +6,49 @@ export async function downloadFile(
|
||||||
dist: Distribution,
|
dist: Distribution,
|
||||||
) {
|
) {
|
||||||
if (!dist.downloadURL) throw new Error("no downloadURL");
|
if (!dist.downloadURL) throw new Error("no downloadURL");
|
||||||
const outputS = streamSaver.createWriteStream(
|
const downloadURL = `${dataPath}/${datasetId}/${dist.identifier}/${
|
||||||
dist.downloadURL.slice(dist.downloadURL.lastIndexOf("/") + 1),
|
|
||||||
);
|
|
||||||
const res = await fetchGzipped(
|
|
||||||
`${dataPath}/${datasetId}/${dist.identifier}/${
|
|
||||||
dist.fileName || dist.identifier
|
dist.fileName || dist.identifier
|
||||||
}.gz`,
|
}.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);
|
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> {
|
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")) {
|
if (res.status === 404 && url.endsWith(".gz")) {
|
||||||
// probar cargando el archivo no comprimido
|
// probar cargando el archivo no comprimido
|
||||||
res = await fetch(url.slice(0, url.length - ".gz".length));
|
res = await fetch(url.slice(0, url.length - ".gz".length));
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
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;
|
let DecStream;
|
||||||
if ("DecompressionStream" in window) DecStream = window.DecompressionStream;
|
if ("DecompressionStream" in window) DecStream = window.DecompressionStream;
|
||||||
else {
|
else {
|
||||||
|
@ -38,6 +62,7 @@ async function fetchGzipped(url: string): Promise<Response> {
|
||||||
const resD = new Response(decompressedStream);
|
const resD = new Response(decompressedStream);
|
||||||
return resD;
|
return resD;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
let cachedGzippedJson = new Map<string, { date: Date; data: unknown }>();
|
let cachedGzippedJson = new Map<string, { date: Date; data: unknown }>();
|
||||||
|
|
||||||
async function loadGzippedJson(url: string): Promise<unknown> {
|
async function loadGzippedJson(url: string): Promise<unknown> {
|
||||||
|
|
Loading…
Reference in a new issue