Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix download #288

Merged
merged 2 commits into from
Jun 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 11 additions & 26 deletions src/lib/components/Line.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import type { File, Directory } from '$lib/api';
import { formatDate } from '$lib/date';
import { getDoneStatus } from '$lib/todo-file';
import { GH_PAGES_BASE_URL } from '$lib/const';

export let data: File | Directory;
// export let customUrl: string | undefined = undefined;
Expand All @@ -18,29 +19,12 @@
* This function is especially created for '/libri/'.
*/
function isUsingExternalBase(data: File | Directory) {
if (data.url.startsWith('https://csunibo.github.io')) {
if (data.url.startsWith(GH_PAGES_BASE_URL)) {
return false;
}
return true;
}

let isSpinning = false;
async function downloadFile() {
isSpinning = true;
const url = data.url;
const response = await fetch(url);
const blob = await response.blob();
const urlObject = window.URL.createObjectURL(new Blob([blob]));
const a = document.createElement('a');
a.href = urlObject;
a.download = data.name;
document.body.appendChild(a);
a.click();
a.remove();
isSpinning = false;
URL.revokeObjectURL(urlObject);
}

$: isDone = getDoneStatus(data.url);

function splitDate(date: string) {
Expand Down Expand Up @@ -101,15 +85,16 @@
{#if isFile}
{isFile && data.size != '0 B' ? data.size : '-'}
{#if data.size != '0 B'}
<button class="flex text-lg ml-3" on:click={downloadFile}>
<span
class="text-accent text-3xl {isSpinning
? 'icon-[eos-icons--loading]'
: 'icon-[solar--download-square-bold]'}"
></span>
</button>
<a
class="flex text-lg ml-3"
href={GH_PAGES_BASE_URL + base + '/' + data.name}
download
target="_blank"
>
<span class="text-accent text-3xl icon-[solar--download-square-bold]"></span>
</a>
{:else}
<button disabled class="flex text-lg ml-3" on:click={downloadFile}>
<button disabled class="flex text-lg ml-3">
<span class="text-neutral text-3xl icon-[solar--download-square-bold]"></span>
</button>
{/if}
Expand Down
Loading