feat: accept files and folders when uploading books
The picker could only choose folders: webkitdirectory switches the OS dialog into folder mode rather than filtering, so one input cannot offer both. Adds a drop zone with two browse buttons behind two inputs. Dropping a folder never worked either — dataTransfer.files does not descend into directories, so it arrived as one typeless entry and was rejected. Walks dataTransfer.items instead. Also collects skipped files into one expandable line rather than a toast each, which a folder of covers and notes made unusable, and shows the file count and total size before uploading.
This commit is contained in:
@@ -4,17 +4,14 @@
|
||||
import * as Field from '$lib/components/ui/field/index.js';
|
||||
import { Button } from '$lib/components/ui/button';
|
||||
import * as NativeSelect from '$lib/components/ui/native-select/index.js';
|
||||
import {
|
||||
displaySize,
|
||||
FileDropZone,
|
||||
type FileDropZoneProps
|
||||
} from '$lib/components/ui/file-drop-zone';
|
||||
import { displaySize, type FileRejectedReason } from '$lib/components/ui/file-drop-zone';
|
||||
import BookDropZone from './book-drop-zone.svelte';
|
||||
import { X } from '@lucide/svelte';
|
||||
import { toast } from 'svelte-sonner';
|
||||
|
||||
import { Switch } from '$lib/components/ui/switch/index';
|
||||
|
||||
import { tick } from 'svelte';
|
||||
import { tick, untrack } from 'svelte';
|
||||
import { Spinner } from '$lib/components/ui/spinner/index';
|
||||
import { getLibraryState } from '$lib/state/library.svelte';
|
||||
import { uploadBooks } from '$lib/api';
|
||||
@@ -29,14 +26,20 @@
|
||||
uploadBooks.fields.library_id.set(libraryState.activeLibrary!.id);
|
||||
});
|
||||
|
||||
let files = $derived(uploadBooks.fields.files.value() ?? []);
|
||||
// Narrowed once here rather than asserted at each use in the markup.
|
||||
let files = $derived(
|
||||
(uploadBooks.fields.files.value() ?? []).filter((file): file is File => Boolean(file))
|
||||
);
|
||||
let totalSize = $derived(files.reduce((sum, file) => sum + file.size, 0));
|
||||
|
||||
let autoUploadOnDrop = $state(true);
|
||||
let navigateOnUpload = $state(true);
|
||||
let formEl = $state<HTMLFormElement>();
|
||||
|
||||
const onUpload: FileDropZoneProps['onUpload'] = async (uploadedFiles) => {
|
||||
// Rename files to use webkitRelativePath so directory structure is preserved through form submission
|
||||
const onUpload = async (uploadedFiles: File[]) => {
|
||||
// Rename files to use webkitRelativePath so directory structure is preserved
|
||||
// through form submission. Dropped folders already arrive named that way,
|
||||
// since the drop zone builds the path while walking them.
|
||||
const renamedFiles = uploadedFiles.map(
|
||||
(f) => new File([f], f.webkitRelativePath || f.name, { type: f.type })
|
||||
);
|
||||
@@ -47,10 +50,29 @@
|
||||
}
|
||||
};
|
||||
|
||||
const onFileRejected: FileDropZoneProps['onFileRejected'] = async ({ reason, file }) => {
|
||||
toast.error(`${file.name} failed to upload!`, { description: reason });
|
||||
/**
|
||||
* Collected rather than raised one at a time. A folder of a few hundred books
|
||||
* carries covers and notes alongside them, and a toast per rejected file
|
||||
* buries the screen.
|
||||
*/
|
||||
let rejected = $state<{ name: string; reason: FileRejectedReason }[]>([]);
|
||||
let showRejected = $state(false);
|
||||
|
||||
const onFileRejected = ({ reason, file }: { reason: FileRejectedReason; file: File }) => {
|
||||
rejected = [...rejected, { name: file.webkitRelativePath || file.name, reason }];
|
||||
};
|
||||
|
||||
// Start each visit clean — a skipped-files list left over from last time reads
|
||||
// as though it applies to what was just chosen.
|
||||
$effect(() => {
|
||||
if (open) {
|
||||
untrack(() => {
|
||||
rejected = [];
|
||||
showRejected = false;
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
function navigateToBooks(books: PaginatedResponse<Book>) {
|
||||
open = false;
|
||||
let libraryId = books.items[0].library_id;
|
||||
@@ -59,7 +81,9 @@
|
||||
goto(resolve('/(root)/(library)/book/[bookId]', { bookId: String(books.items[0].id) }));
|
||||
} else {
|
||||
// The path is resolved; the query string is what the rule cannot see past.
|
||||
const view = resolve('/(root)/(library)/library/[libraryId]/view', { libraryId: String(libraryId) });
|
||||
const view = resolve('/(root)/(library)/library/[libraryId]/view', {
|
||||
libraryId: String(libraryId)
|
||||
});
|
||||
// eslint-disable-next-line svelte/no-navigation-without-resolve
|
||||
goto(`${view}?orderBy=created_at&sortOrder=desc`);
|
||||
}
|
||||
@@ -81,7 +105,7 @@
|
||||
</Dialog.Header>
|
||||
|
||||
<form
|
||||
{...uploadBooks.enhance(async ({ submit, form }) => {
|
||||
{...uploadBooks.enhance(async ({ submit }) => {
|
||||
try {
|
||||
await submit();
|
||||
|
||||
@@ -99,6 +123,7 @@
|
||||
|
||||
// Reset the files field
|
||||
uploadBooks.fields.files.set([]);
|
||||
rejected = [];
|
||||
toast.success('Books successfully uploaded!');
|
||||
|
||||
if (navigateOnUpload) {
|
||||
@@ -116,31 +141,40 @@
|
||||
<!-- Library select field -->
|
||||
<Field.Label for="library_id">Select Library</Field.Label>
|
||||
<NativeSelect.Root {...uploadBooks.fields.library_id.as('select')} class="w-36">
|
||||
{#each libraryState.libraries as library}
|
||||
{#each libraryState.libraries as library (library.id)}
|
||||
<NativeSelect.Option value={library.id}>
|
||||
{library.name}
|
||||
</NativeSelect.Option>
|
||||
{/each}
|
||||
</NativeSelect.Root>
|
||||
|
||||
<FileDropZone
|
||||
<BookDropZone
|
||||
{onUpload}
|
||||
{onFileRejected}
|
||||
directory={true}
|
||||
accept=".pdf,.epub,.mobi,application/pdf,application/epub+zip,application/x-mobipocket-ebook"
|
||||
sublabel="Only PDF, EPUB, and MOBI files supported"
|
||||
/>
|
||||
<input class="hidden" {...uploadBooks.fields.files.as('file multiple')} />
|
||||
|
||||
{#if files.length > 0}
|
||||
<div class="flex items-baseline justify-between border-b pb-1 text-sm">
|
||||
<span><strong class="tabular-nums">{files.length}</strong> ready to upload</span>
|
||||
<span class="font-mono text-xs text-muted-foreground tabular-nums">
|
||||
{displaySize(totalSize)}
|
||||
</span>
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<div class="flex max-h-[300px] flex-col gap-2 overflow-y-auto">
|
||||
{#each files as file, idx}
|
||||
{#each files as file, idx (file.name)}
|
||||
<div class="flex place-items-center justify-between gap-2">
|
||||
<div class="flex flex-col">
|
||||
<span>{file.name}</span>
|
||||
<div class="flex min-w-0 flex-col">
|
||||
<span class="truncate" title={file.name}>{file.name}</span>
|
||||
<span class="text-xs text-muted-foreground">{displaySize(file.size)}</span>
|
||||
</div>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="icon"
|
||||
class="shrink-0"
|
||||
onclick={() => {
|
||||
uploadBooks.fields.files.set([
|
||||
...Array.from(files).slice(0, idx),
|
||||
@@ -149,11 +183,36 @@
|
||||
}}
|
||||
>
|
||||
<X />
|
||||
<span class="sr-only">Remove {file.name}</span>
|
||||
</Button>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
|
||||
{#if rejected.length > 0}
|
||||
<div class="rounded-md border border-star/50 bg-star/10 p-2 text-sm">
|
||||
<div class="flex items-center justify-between gap-2">
|
||||
<span>
|
||||
{rejected.length}
|
||||
{rejected.length === 1 ? 'file was' : 'files were'} skipped
|
||||
</span>
|
||||
<Button variant="ghost" size="sm" onclick={() => (showRejected = !showRejected)}>
|
||||
{showRejected ? 'Hide' : 'Show'}
|
||||
</Button>
|
||||
</div>
|
||||
{#if showRejected}
|
||||
<ul class="mt-2 flex max-h-32 flex-col gap-1 overflow-y-auto">
|
||||
{#each rejected as entry (entry.name)}
|
||||
<li class="flex justify-between gap-2 text-xs text-muted-foreground">
|
||||
<span class="truncate" title={entry.name}>{entry.name}</span>
|
||||
<span class="shrink-0">{entry.reason}</span>
|
||||
</li>
|
||||
{/each}
|
||||
</ul>
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<div class="flex flex-col gap-2">
|
||||
<div class="flex items-center gap-2">
|
||||
<Switch bind:checked={autoUploadOnDrop} />
|
||||
|
||||
Reference in New Issue
Block a user