Skip to content

Commit

Permalink
file level download
Browse files Browse the repository at this point in the history
  • Loading branch information
juansensio committed Aug 7, 2024
1 parent aa9776c commit 943153b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 34 deletions.
85 changes: 51 additions & 34 deletions ui/src/components/FileExplorer.svelte
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<script>
import { browser } from "$app/environment";
import Folder from "svelte-material-icons/Folder.svelte";
import ArrowLeft from "svelte-material-icons/ArrowLeft.svelte";
import File from "svelte-material-icons/File.svelte";
import { id_token } from "$stores/auth";
import { PUBLIC_EOTDL_API } from "$env/static/public";
import { onMount } from "svelte";
export let data;
export let retrieveFiles;
export let version;
export let datasetId;
let createWriteStream;
let files = null;
Expand All @@ -18,6 +21,14 @@
let currentPath = [];
let onDetails = false;
let details = {};
let currentFileName = null;
onMount(async () => {
if (browser) {
// only works in browser
const streamsaver = await import("streamsaver");
createWriteStream = streamsaver.createWriteStream;
}
});
const sizeFormat = (bytes) => {
const size = bytes;
Expand Down Expand Up @@ -96,13 +107,13 @@
const goToDetails = (file, filename) => {
onDetails = true;
details = {
checksum: file.checksum,
version: file.version,
size: sizeFormat(file.size),
};
currentPath = [...currentPath, filename];
currentFileName = file.filename;
};
const getCurrentPath = (intoFolder) => {
Expand All @@ -112,38 +123,41 @@
currentPath = [];
}
};
// const download = async (fileName) => {
// // seems to work, but not sure if it will with large datasets (need to test)
// fetch(`${PUBLIC_EOTDL_API}/datasets/${id}/download/${fileName}`, {
// method: "GET",
// headers: {
// Authorization: `Bearer ${$id_token}`,
// },
// })
// .then((res) => {
// if (!res.ok) return res.json();
// const fileStream = createWriteStream(fileName);
// const writer = fileStream.getWriter();
// if (res.body.pipeTo) {
// writer.releaseLock();
// return res.body.pipeTo(fileStream);
// }
// const reader = res.body.getReader();
// const pump = () =>
// reader
// .read()
// .then(({ value, done }) =>
// done
// ? writer.close()
// : writer.write(value).then(pump)
// );
// data.dataset.downloads = data.dataset.downloads + 1;
// return pump();
// })
// .then((res) => {
// alert(res.detail);
// });
// };
const download = async () => {
// seems to work, but not sure if it will with large datasets (need to test)
fetch(
`${PUBLIC_EOTDL_API}/datasets/${datasetId}/download/${currentFileName}`,
{
method: "GET",
headers: {
Authorization: `Bearer ${$id_token}`,
},
},
)
.then((res) => {
if (!res.ok) return res.json();
const fileStream = createWriteStream(currentFileName);
const writer = fileStream.getWriter();
if (res.body.pipeTo) {
writer.releaseLock();
return res.body.pipeTo(fileStream);
}
const reader = res.body.getReader();
const pump = () =>
reader
.read()
.then(({ value, done }) =>
done
? writer.close()
: writer.write(value).then(pump),
);
return pump();
})
.then((res) => {
alert(res.detail);
});
};
</script>

{#if !loading}
Expand Down Expand Up @@ -222,6 +236,9 @@
<td class="pl-1">{details[detail]}</td>
</tr>
{/each}
<button class="btn" on:click={() => download(details)}
>download</button
>
{/if}
</table>
</div>
Expand Down
1 change: 1 addition & 0 deletions ui/src/routes/datasets/[name]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
data={dataset}
{version}
retrieveFiles={retrieveDatasetFiles}
datasetId={dataset.id}
/>
</div>
{:else if dataset.items?.features?.length > 0}
Expand Down

0 comments on commit 943153b

Please sign in to comment.