diff --git a/src/lib/components/UploadModal.svelte b/src/lib/components/UploadModal.svelte index dc35784..16ca43e 100644 --- a/src/lib/components/UploadModal.svelte +++ b/src/lib/components/UploadModal.svelte @@ -5,7 +5,6 @@ import { onMount } from 'svelte'; import { scanFiles } from '$lib/upload'; import { formatBytes } from '$lib/util/upload'; - import { catalog } from '$lib/catalog'; export let open = false; diff --git a/src/lib/upload/index.ts b/src/lib/upload/index.ts index ad72fb7..9ffa4e2 100644 --- a/src/lib/upload/index.ts +++ b/src/lib/upload/index.ts @@ -30,17 +30,31 @@ export async function unzipManga(file: File) { } function getDetails(file: File) { - const [filename, ext] = file.name.split('.'); + const { webkitRelativePath, name } = file + const [filename, ext] = name.split('.'); + let path = filename + + if (webkitRelativePath) { + path = webkitRelativePath.split('.')[0] + } return { filename, - ext + ext, + path }; } async function getFile(fileEntry: FileSystemFileEntry) { try { - return new Promise((resolve, reject) => fileEntry.file(resolve, reject)); + return new Promise((resolve, reject) => fileEntry.file((file) => { + if (!file.webkitRelativePath) { + Object.defineProperty(file, 'webkitRelativePath', { + value: fileEntry.fullPath.substring(1) + }) + } + resolve(file) + }, reject)); } catch (err) { console.log(err); } @@ -69,8 +83,7 @@ export async function processFiles(files: File[]) { const mangas: string[] = []; for (const file of files) { - const { ext, filename } = getDetails(file); - const { type, webkitRelativePath } = file; + const { ext, filename, path } = getDetails(file); if (ext === 'mokuro') { const mokuroData: Volume['mokuroData'] = JSON.parse(await file.text()); @@ -79,20 +92,33 @@ export async function processFiles(files: File[]) { mangas.push(mokuroData.title_uuid); } - volumes[filename] = { - ...volumes[filename], + + volumes[path] = { + ...volumes[path], mokuroData, volumeName: filename }; continue; } + } + + + for (const file of files) { + const { ext, path } = getDetails(file); + const { type, webkitRelativePath } = file; const mimeType = type || getMimeType(file.name); if (imageTypes.includes(mimeType)) { if (webkitRelativePath) { const imageName = webkitRelativePath.split('/').at(-1); - const vol = webkitRelativePath.split('/').at(-2); + let vol = '' + + Object.keys(volumes).forEach((key) => { + if (webkitRelativePath.startsWith(key)) { + vol = key + } + }) if (vol && imageName) { volumes[vol] = { @@ -110,8 +136,8 @@ export async function processFiles(files: File[]) { if (zipTypes.includes(ext)) { const unzippedFiles = await unzipManga(file); - volumes[filename] = { - ...volumes[filename], + volumes[path] = { + ...volumes[path], files: unzippedFiles }; @@ -124,6 +150,7 @@ export async function processFiles(files: File[]) { if (vols.length > 0) { const valid = vols.map((vol) => { const { files, mokuroData, volumeName } = vol; + if (!mokuroData || !volumeName) { showSnackbar('Missing .mokuro file'); return false;