diff --git a/src/pages/gtfs_archivo/Admin.svelte b/src/pages/gtfs_archivo/Admin.svelte index 0c09d11..40109c7 100644 --- a/src/pages/gtfs_archivo/Admin.svelte +++ b/src/pages/gtfs_archivo/Admin.svelte @@ -1,11 +1,12 @@ @@ -136,7 +135,13 @@ {offset + index + 1} - {app.archivo} + + {#if !loading} + onDownload(app)}>{app.archivo} + {:else} + {app.archivo} + {/if} + {app.created} {app.status} {app.vigente ? '✅':'🚫'} diff --git a/src/services/gtfs_archivo.js b/src/services/gtfs_archivo.js index 6d9f034..b3d5a82 100644 --- a/src/services/gtfs_archivo.js +++ b/src/services/gtfs_archivo.js @@ -1,6 +1,6 @@ import { base, getToken } from './_config' -export function getGtfsBase(){ +export function getGtfsBase() { return (base) } @@ -23,22 +23,22 @@ export async function getGtfsArchivoId(id) { export async function createGtfsArchivo(data) { const form = new FormData() - const directorioUpload= '/uploads' - form.append('ruta_archivo', directorioUpload) - form.append('status', 'PENDIENTE') - for (let key in data) { + const directorioUpload = '/uploads' + form.append('ruta_archivo', directorioUpload) + form.append('status', 'PENDIENTE') + for (let key in data) { if (key === 'archivo') { - const file = data[key] + const file = data[key] form.append('binario', file) - form.append(key, file.name) + form.append(key, file.name) } else { form.append(key, data[key]) } } - + const res = await fetch(`${base}/gtfs-archivo/`, { method: 'POST', - body: form, + body: form, headers: { "Authorization": `Bearer ${getToken()}` } }) if (!res.ok) throw await res.json() @@ -62,4 +62,26 @@ export async function deleteGtfsArchivo(id) { }) if (!res.ok) throw await res.json() return res.text() +} + + +export async function downloadGtfsArchivo(id) { + const res = await fetch(`${base}/gtfs-archivo/download/?id=${id}`, { + headers: { "Authorization": `Bearer ${getToken()}` } + }) + if (!res.ok) throw await res.text() + + const filename = res.headers.get('content-disposition').split('filename=')[1].replace(/\"/g,''); + const blob = await res.blob() + const url = URL.createObjectURL(blob); + + // Crear un enlace para simular la descarga + let enlaceDescarga = document.createElement('a'); + enlaceDescarga.href = url; + enlaceDescarga.download = filename; // Establece el nombre del archivo + enlaceDescarga.click(); + + // Liberar la URL del Blob una vez que se haya completado la descarga + URL.revokeObjectURL(url); + enlaceDescarga.remove() } \ No newline at end of file