Cleanup and zip handling
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import type { Volume } from '$lib/upload';
|
||||
import type { Volume } from '$lib/types';
|
||||
import Dexie, { type Table } from 'dexie';
|
||||
|
||||
export interface Catalog {
|
||||
id?: number;
|
||||
id: string;
|
||||
manga: Volume[];
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ export class CatalogDexie extends Dexie {
|
||||
constructor() {
|
||||
super('mokuro');
|
||||
this.version(1).stores({
|
||||
catalog: '++id, manga'
|
||||
catalog: 'id, manga'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import type { Volume } from "$lib/upload";
|
||||
import type { Volume } from "$lib/types";
|
||||
import { writable } from "svelte/store";
|
||||
import { db } from "$lib/catalog/db";
|
||||
import { liveQuery } from "dexie";
|
||||
|
||||
export const currentManga = writable<Volume[] | undefined>(undefined);
|
||||
export const currentVolume = writable<Volume | undefined>(undefined);
|
||||
export const catalog = liveQuery(() => db.catalog.toArray());
|
||||
@@ -1,9 +0,0 @@
|
||||
import { writable } from "svelte/store";
|
||||
import { db } from "$lib/catalog/db";
|
||||
import type { Volume } from "$lib/upload";
|
||||
import { liveQuery } from "dexie";
|
||||
|
||||
|
||||
|
||||
|
||||
export const catalog = liveQuery(() => db.catalog.toArray());
|
||||
@@ -1,5 +1,5 @@
|
||||
<script lang="ts">
|
||||
import { catalog } from '$lib/catalog/test';
|
||||
import { catalog } from '$lib/catalog';
|
||||
import CatalogItem from './CatalogItem.svelte';
|
||||
</script>
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<script lang="ts">
|
||||
import { currentManga } from '$lib/catalog';
|
||||
import type { Volume } from '$lib/upload';
|
||||
import type { Volume } from '$lib/types';
|
||||
export let manga: Volume[];
|
||||
const { volumeName, files, mokuroData } = manga[0];
|
||||
|
||||
|
||||
@@ -1,18 +1,5 @@
|
||||
<script lang="ts">
|
||||
type Block = {
|
||||
box: number[];
|
||||
vertical: boolean;
|
||||
fontSize: number;
|
||||
lines: string[];
|
||||
};
|
||||
|
||||
type Page = {
|
||||
version?: string;
|
||||
img_width: number;
|
||||
img_height: number;
|
||||
blocks: Block[];
|
||||
imgPath: string;
|
||||
};
|
||||
import type { Page } from '$lib/types';
|
||||
|
||||
export let page: Page;
|
||||
|
||||
@@ -28,7 +15,7 @@
|
||||
|
||||
$: textBoxes = page.blocks.map((block) => {
|
||||
const { img_height, img_width } = page;
|
||||
const { box, fontSize, lines, vertical } = block;
|
||||
const { box, font_size, lines, vertical } = block;
|
||||
|
||||
let [_xmin, _ymin, _xmax, _ymax] = box;
|
||||
|
||||
@@ -46,7 +33,7 @@
|
||||
top: `${ymin}px`,
|
||||
width: `${width}px`,
|
||||
height: `${height}px`,
|
||||
fontSize: `${fontSize}px`,
|
||||
fontSize: `${font_size}px`,
|
||||
writingMode: vertical ? 'vertical-rl' : 'horizontal-tb',
|
||||
lines
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<script lang="ts">
|
||||
import { page } from '$app/stores';
|
||||
import { currentVolume } from '$lib/catalog';
|
||||
import type { Volume } from '$lib/upload';
|
||||
import type { Volume } from '$lib/types';
|
||||
|
||||
export let volume: Volume;
|
||||
const { volumeName, files } = volume;
|
||||
|
||||
29
src/lib/types/index.ts
Normal file
29
src/lib/types/index.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
export type Block = {
|
||||
box: number[];
|
||||
vertical: boolean;
|
||||
font_size: number;
|
||||
lines: string[];
|
||||
};
|
||||
|
||||
export type Page = {
|
||||
version: string;
|
||||
img_width: number;
|
||||
img_height: number;
|
||||
blocks: Block[];
|
||||
imgPath: string;
|
||||
};
|
||||
|
||||
export type MokuroData = {
|
||||
version: string;
|
||||
title: string;
|
||||
title_uuid: string;
|
||||
volume: string;
|
||||
volume_uuid: string;
|
||||
pages: Page[];
|
||||
}
|
||||
|
||||
export type Volume = {
|
||||
mokuroData: MokuroData;
|
||||
volumeName: string;
|
||||
files: Record<string, File>;
|
||||
}
|
||||
@@ -1,19 +1,29 @@
|
||||
import { db } from "$lib/catalog/db";
|
||||
import type { Volume } from "$lib/types";
|
||||
import { showSnackbar } from "$lib/util/snackbar";
|
||||
import { requestPersistentStorage } from "$lib/util/upload";
|
||||
import { BlobReader, ZipReader } from "@zip.js/zip.js";
|
||||
|
||||
export type Volume = {
|
||||
mokuroData: any;
|
||||
volumeName: string;
|
||||
archiveFile?: File | undefined;
|
||||
files?: Record<string, File>;
|
||||
}
|
||||
import { BlobReader, ZipReader, BlobWriter, getMimeType } from "@zip.js/zip.js";
|
||||
|
||||
export async function unzipManga(file: File) {
|
||||
const zipFileReader = new BlobReader(file);
|
||||
const zipReader = new ZipReader(zipFileReader);
|
||||
|
||||
return await zipReader.getEntries()
|
||||
const entries = await zipReader.getEntries()
|
||||
const unzippedFiles: Record<string, File> = {};
|
||||
|
||||
|
||||
for (const entry of entries) {
|
||||
const mime = getMimeType(entry.filename);
|
||||
if (mime === 'image/jpeg' || mime === 'image/png') {
|
||||
const blob = await entry.getData?.(new BlobWriter(mime))
|
||||
if (blob) {
|
||||
const file = new File([blob], entry.filename, { type: mime })
|
||||
unzippedFiles[entry.filename] = file
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return unzippedFiles;
|
||||
}
|
||||
|
||||
function getDetails(file: File) {
|
||||
@@ -26,11 +36,13 @@ function getDetails(file: File) {
|
||||
}
|
||||
|
||||
|
||||
export function processVolumes(volumes: Volume[]) {
|
||||
volumes.map(({ mokuroData, volumeName, archiveFile, files }) => {
|
||||
console.log(mokuroData, volumeName, archiveFile, files);
|
||||
})
|
||||
}
|
||||
// export async function processVolumes(volumes: Volume[]) {
|
||||
// for (const { mokuroData, volumeName, archiveFile, files } of volumes) {
|
||||
// const { title_uuid } = mokuroData
|
||||
// }
|
||||
|
||||
|
||||
// }
|
||||
|
||||
export async function processFiles(fileList: FileList) {
|
||||
const files = [...fileList]
|
||||
@@ -71,10 +83,13 @@ export async function processFiles(fileList: FileList) {
|
||||
}
|
||||
|
||||
if (zipTypes.includes(ext)) {
|
||||
const unzippedFiles = await unzipManga(file)
|
||||
|
||||
volumes[filename] = {
|
||||
...volumes[filename],
|
||||
archiveFile: file
|
||||
files: unzippedFiles
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@@ -82,8 +97,25 @@ export async function processFiles(fileList: FileList) {
|
||||
const vols = Object.values(volumes);
|
||||
|
||||
if (vols.length > 0) {
|
||||
await requestPersistentStorage();
|
||||
await processVolumes(vols)
|
||||
await db.catalog.put({ manga: vols })
|
||||
const valid = vols.map((vol) => {
|
||||
const { files, mokuroData, volumeName } = vol
|
||||
if (!mokuroData || !volumeName) {
|
||||
showSnackbar('Missing .mokuro file')
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!files) {
|
||||
showSnackbar('Missing image files')
|
||||
return false;
|
||||
}
|
||||
|
||||
return true
|
||||
})
|
||||
|
||||
if (!valid.includes(false)) {
|
||||
await requestPersistentStorage();
|
||||
await db.catalog.put({ id: vols[0].mokuroData.title_uuid, manga: vols })
|
||||
showSnackbar('Catalog updated successfully')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
<script lang="ts">
|
||||
import { goto } from '$app/navigation';
|
||||
import { db } from '$lib/catalog/db';
|
||||
import { catalog } from '$lib/catalog/test';
|
||||
import Button from '$lib/components/Button.svelte';
|
||||
import Catalog from '$lib/components/Catalog.svelte';
|
||||
import Modal from '$lib/components/Modal.svelte';
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
<script lang="ts">
|
||||
import { db } from '$lib/catalog/db';
|
||||
import Button from '$lib/components/Button.svelte';
|
||||
import Catalog from '$lib/components/Catalog.svelte';
|
||||
import FileUpload from '$lib/components/FileUpload.svelte';
|
||||
import { processFiles } from '$lib/upload';
|
||||
|
||||
Reference in New Issue
Block a user