Clean up drop uploading
This commit is contained in:
@@ -3,7 +3,8 @@
|
|||||||
import FileUpload from './FileUpload.svelte';
|
import FileUpload from './FileUpload.svelte';
|
||||||
import { processFiles } from '$lib/upload';
|
import { processFiles } from '$lib/upload';
|
||||||
import { onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
import { formatBytes, scanFiles } from '$lib/util/upload';
|
import { scanFiles } from '$lib/upload';
|
||||||
|
import { formatBytes } from '$lib/util/upload';
|
||||||
|
|
||||||
export let open = false;
|
export let open = false;
|
||||||
|
|
||||||
@@ -49,11 +50,10 @@
|
|||||||
if (event?.dataTransfer?.items) {
|
if (event?.dataTransfer?.items) {
|
||||||
for (const item of [...event.dataTransfer.items]) {
|
for (const item of [...event.dataTransfer.items]) {
|
||||||
const entry = item.webkitGetAsEntry();
|
const entry = item.webkitGetAsEntry();
|
||||||
if (entry && entry.isDirectory) {
|
if (item.kind === 'file' && entry) {
|
||||||
|
if (entry.isDirectory) {
|
||||||
await scanFiles(entry, filePromises);
|
await scanFiles(entry, filePromises);
|
||||||
}
|
} else {
|
||||||
|
|
||||||
if (item.kind === 'file') {
|
|
||||||
const file = item.getAsFile();
|
const file = item.getAsFile();
|
||||||
if (file) {
|
if (file) {
|
||||||
draggedFiles.push(file);
|
draggedFiles.push(file);
|
||||||
@@ -61,6 +61,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (filePromises && filePromises.length > 0) {
|
if (filePromises && filePromises.length > 0) {
|
||||||
const files = await Promise.all(filePromises);
|
const files = await Promise.all(filePromises);
|
||||||
|
|||||||
@@ -35,16 +35,47 @@ function getDetails(file: File) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function getFile(fileEntry: FileSystemFileEntry) {
|
||||||
|
try {
|
||||||
|
return new Promise<File>((resolve, reject) => fileEntry.file(resolve, reject));
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function scanFiles(item: FileSystemEntry, files: Promise<File | undefined>[]) {
|
||||||
|
if (item.isDirectory) {
|
||||||
|
const directoryReader = (item as FileSystemDirectoryEntry).createReader();
|
||||||
|
await new Promise<void>((resolve) => {
|
||||||
|
directoryReader.readEntries(async (entries) => {
|
||||||
|
for (const entry of entries) {
|
||||||
|
if (entry.isFile) {
|
||||||
|
files.push(getFile(entry as FileSystemFileEntry))
|
||||||
|
} else {
|
||||||
|
await scanFiles(entry, files);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
resolve()
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function processFiles(files: File[]) {
|
export async function processFiles(files: File[]) {
|
||||||
const zipTypes = ['zip', 'cbz']
|
const zipTypes = ['zip', 'cbz']
|
||||||
const volumes: Record<string, Volume> = {};
|
const volumes: Record<string, Volume> = {};
|
||||||
|
const mangas: string[] = [];
|
||||||
|
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
const { ext, filename } = getDetails(file)
|
const { ext, filename } = getDetails(file)
|
||||||
const { type, webkitRelativePath } = file
|
const { type, webkitRelativePath } = file
|
||||||
|
|
||||||
if (ext === 'mokuro') {
|
if (ext === 'mokuro') {
|
||||||
const mokuroData = JSON.parse(await file.text())
|
const mokuroData: Volume['mokuroData'] = JSON.parse(await file.text())
|
||||||
|
|
||||||
|
if (!mangas.includes(mokuroData.title_uuid)) {
|
||||||
|
mangas.push(mokuroData.title_uuid)
|
||||||
|
}
|
||||||
|
|
||||||
volumes[filename] = {
|
volumes[filename] = {
|
||||||
...volumes[filename],
|
...volumes[filename],
|
||||||
@@ -108,18 +139,20 @@ export async function processFiles(files: File[]) {
|
|||||||
if (!valid.includes(false)) {
|
if (!valid.includes(false)) {
|
||||||
await requestPersistentStorage();
|
await requestPersistentStorage();
|
||||||
|
|
||||||
const key = vols[0].mokuroData.title_uuid;
|
for (const key of mangas) {
|
||||||
const existingCatalog = await db.catalog.get(key);
|
const existingCatalog = await db.catalog.get(key);
|
||||||
|
|
||||||
if (existingCatalog) {
|
|
||||||
const filtered = vols.filter((vol) => {
|
const filtered = vols.filter((vol) => {
|
||||||
return !existingCatalog.manga.some(manga => {
|
return !existingCatalog?.manga.some(manga => {
|
||||||
return manga.mokuroData.volume_uuid === vol.mokuroData.volume_uuid
|
return manga.mokuroData.volume_uuid === vol.mokuroData.volume_uuid
|
||||||
|
}) && key === vol.mokuroData.title_uuid
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
if (existingCatalog) {
|
||||||
await db.catalog.update(key, { manga: [...existingCatalog.manga, ...filtered] })
|
await db.catalog.update(key, { manga: [...existingCatalog.manga, ...filtered] })
|
||||||
} else {
|
} else {
|
||||||
await db.catalog.put({ id: key, manga: vols })
|
await db.catalog.put({ id: key, manga: filtered })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
showSnackbar('Catalog updated successfully')
|
showSnackbar('Catalog updated successfully')
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
export async function requestPersistentStorage() {
|
export async function requestPersistentStorage() {
|
||||||
if (navigator.storage && navigator.storage.persist) {
|
if (navigator.storage && navigator.storage.persist) {
|
||||||
const isPersisted = await navigator.storage.persist();
|
await navigator.storage.persist();
|
||||||
console.log(`Persisted storage granted: ${isPersisted}`);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -16,29 +15,3 @@ export function formatBytes(bytes: number, decimals = 2) {
|
|||||||
|
|
||||||
return `${parseFloat((bytes / k ** i).toFixed(dm))} ${sizes[i]}`;
|
return `${parseFloat((bytes / k ** i).toFixed(dm))} ${sizes[i]}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getFile(fileEntry: FileSystemFileEntry) {
|
|
||||||
try {
|
|
||||||
return new Promise<File>((resolve, reject) => fileEntry.file(resolve, reject));
|
|
||||||
} catch (err) {
|
|
||||||
console.log(err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function scanFiles(item: FileSystemEntry, files: Promise<File | undefined>[]) {
|
|
||||||
if (item.isDirectory) {
|
|
||||||
const directoryReader = (item as FileSystemDirectoryEntry).createReader();
|
|
||||||
await new Promise<void>((resolve) => {
|
|
||||||
directoryReader.readEntries((entries) => {
|
|
||||||
entries.forEach((entry) => {
|
|
||||||
if (entry.isFile) {
|
|
||||||
files.push(getFile(entry as FileSystemFileEntry))
|
|
||||||
resolve()
|
|
||||||
}
|
|
||||||
scanFiles(entry, files);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user