Cleanup and zip handling

This commit is contained in:
ZXY101
2023-08-11 12:35:17 +02:00
parent 416b832f63
commit 1e943ea74e
11 changed files with 92 additions and 53 deletions

View File

@@ -1,8 +1,8 @@
import type { Volume } from '$lib/upload'; import type { Volume } from '$lib/types';
import Dexie, { type Table } from 'dexie'; import Dexie, { type Table } from 'dexie';
export interface Catalog { export interface Catalog {
id?: number; id: string;
manga: Volume[]; manga: Volume[];
} }
@@ -12,7 +12,7 @@ export class CatalogDexie extends Dexie {
constructor() { constructor() {
super('mokuro'); super('mokuro');
this.version(1).stores({ this.version(1).stores({
catalog: '++id, manga' catalog: 'id, manga'
}); });
} }
} }

View File

@@ -1,5 +1,8 @@
import type { Volume } from "$lib/upload"; import type { Volume } from "$lib/types";
import { writable } from "svelte/store"; import { writable } from "svelte/store";
import { db } from "$lib/catalog/db";
import { liveQuery } from "dexie";
export const currentManga = writable<Volume[] | undefined>(undefined); export const currentManga = writable<Volume[] | undefined>(undefined);
export const currentVolume = writable<Volume | undefined>(undefined); export const currentVolume = writable<Volume | undefined>(undefined);
export const catalog = liveQuery(() => db.catalog.toArray());

View File

@@ -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());

View File

@@ -1,5 +1,5 @@
<script lang="ts"> <script lang="ts">
import { catalog } from '$lib/catalog/test'; import { catalog } from '$lib/catalog';
import CatalogItem from './CatalogItem.svelte'; import CatalogItem from './CatalogItem.svelte';
</script> </script>

View File

@@ -1,6 +1,6 @@
<script lang="ts"> <script lang="ts">
import { currentManga } from '$lib/catalog'; import { currentManga } from '$lib/catalog';
import type { Volume } from '$lib/upload'; import type { Volume } from '$lib/types';
export let manga: Volume[]; export let manga: Volume[];
const { volumeName, files, mokuroData } = manga[0]; const { volumeName, files, mokuroData } = manga[0];

View File

@@ -1,18 +1,5 @@
<script lang="ts"> <script lang="ts">
type Block = { import type { Page } from '$lib/types';
box: number[];
vertical: boolean;
fontSize: number;
lines: string[];
};
type Page = {
version?: string;
img_width: number;
img_height: number;
blocks: Block[];
imgPath: string;
};
export let page: Page; export let page: Page;
@@ -28,7 +15,7 @@
$: textBoxes = page.blocks.map((block) => { $: textBoxes = page.blocks.map((block) => {
const { img_height, img_width } = page; 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; let [_xmin, _ymin, _xmax, _ymax] = box;
@@ -46,7 +33,7 @@
top: `${ymin}px`, top: `${ymin}px`,
width: `${width}px`, width: `${width}px`,
height: `${height}px`, height: `${height}px`,
fontSize: `${fontSize}px`, fontSize: `${font_size}px`,
writingMode: vertical ? 'vertical-rl' : 'horizontal-tb', writingMode: vertical ? 'vertical-rl' : 'horizontal-tb',
lines lines
}; };

View File

@@ -1,7 +1,7 @@
<script lang="ts"> <script lang="ts">
import { page } from '$app/stores'; import { page } from '$app/stores';
import { currentVolume } from '$lib/catalog'; import { currentVolume } from '$lib/catalog';
import type { Volume } from '$lib/upload'; import type { Volume } from '$lib/types';
export let volume: Volume; export let volume: Volume;
const { volumeName, files } = volume; const { volumeName, files } = volume;

29
src/lib/types/index.ts Normal file
View 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>;
}

View File

@@ -1,19 +1,29 @@
import { db } from "$lib/catalog/db"; 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 { requestPersistentStorage } from "$lib/util/upload";
import { BlobReader, ZipReader } from "@zip.js/zip.js"; import { BlobReader, ZipReader, BlobWriter, getMimeType } from "@zip.js/zip.js";
export type Volume = {
mokuroData: any;
volumeName: string;
archiveFile?: File | undefined;
files?: Record<string, File>;
}
export async function unzipManga(file: File) { export async function unzipManga(file: File) {
const zipFileReader = new BlobReader(file); const zipFileReader = new BlobReader(file);
const zipReader = new ZipReader(zipFileReader); 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) { function getDetails(file: File) {
@@ -26,11 +36,13 @@ function getDetails(file: File) {
} }
export function processVolumes(volumes: Volume[]) { // export async function processVolumes(volumes: Volume[]) {
volumes.map(({ mokuroData, volumeName, archiveFile, files }) => { // for (const { mokuroData, volumeName, archiveFile, files } of volumes) {
console.log(mokuroData, volumeName, archiveFile, files); // const { title_uuid } = mokuroData
}) // }
}
// }
export async function processFiles(fileList: FileList) { export async function processFiles(fileList: FileList) {
const files = [...fileList] const files = [...fileList]
@@ -71,10 +83,13 @@ export async function processFiles(fileList: FileList) {
} }
if (zipTypes.includes(ext)) { if (zipTypes.includes(ext)) {
const unzippedFiles = await unzipManga(file)
volumes[filename] = { volumes[filename] = {
...volumes[filename], ...volumes[filename],
archiveFile: file files: unzippedFiles
} }
continue; continue;
} }
} }
@@ -82,8 +97,25 @@ export async function processFiles(fileList: FileList) {
const vols = Object.values(volumes); const vols = Object.values(volumes);
if (vols.length > 0) { if (vols.length > 0) {
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 requestPersistentStorage();
await processVolumes(vols) await db.catalog.put({ id: vols[0].mokuroData.title_uuid, manga: vols })
await db.catalog.put({ manga: vols }) showSnackbar('Catalog updated successfully')
}
} }
} }

View File

@@ -1,7 +1,6 @@
<script lang="ts"> <script lang="ts">
import { goto } from '$app/navigation'; import { goto } from '$app/navigation';
import { db } from '$lib/catalog/db'; import { db } from '$lib/catalog/db';
import { catalog } from '$lib/catalog/test';
import Button from '$lib/components/Button.svelte'; import Button from '$lib/components/Button.svelte';
import Catalog from '$lib/components/Catalog.svelte'; import Catalog from '$lib/components/Catalog.svelte';
import Modal from '$lib/components/Modal.svelte'; import Modal from '$lib/components/Modal.svelte';

View File

@@ -1,6 +1,4 @@
<script lang="ts"> <script lang="ts">
import { db } from '$lib/catalog/db';
import Button from '$lib/components/Button.svelte';
import Catalog from '$lib/components/Catalog.svelte'; import Catalog from '$lib/components/Catalog.svelte';
import FileUpload from '$lib/components/FileUpload.svelte'; import FileUpload from '$lib/components/FileUpload.svelte';
import { processFiles } from '$lib/upload'; import { processFiles } from '$lib/upload';