Skip to content

Commit

Permalink
fix: remove leftover chapters when fixing out-of-sync ones
Browse files Browse the repository at this point in the history
  • Loading branch information
oae committed Jan 28, 2023
1 parent 6100e6c commit 5713cd6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 32 deletions.
23 changes: 17 additions & 6 deletions src/server/queue/checkChapters.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Prisma } from '@prisma/client';
import { Job, Queue, Worker } from 'bullmq';
import path from 'path';
import { logger } from '../../utils/logging';
import { sanitizer } from '../../utils';
import { logger } from '../../utils/logging';
import { prisma } from '../db/client';
import { findMissingChapterFiles, getChaptersFromLocal } from '../utils/mangal';
import { downloadQueue } from './download';
Expand All @@ -13,10 +13,8 @@ const mangaWithLibraryAndMetadata = Prisma.validator<Prisma.MangaArgs>()({

export type MangaWithLibraryAndMetadata = Prisma.MangaGetPayload<typeof mangaWithLibraryAndMetadata>;

const checkChapters = async (manga: MangaWithLibraryAndMetadata) => {
logger.info(`Checking for new chapters: ${manga.title}`);
export const syncDbWithFiles = async (manga: MangaWithLibraryAndMetadata) => {
const mangaDir = path.resolve(manga.library.path, sanitizer(manga.title));
const missingChapterFiles = await findMissingChapterFiles(mangaDir, manga.source, manga.title);

const localChapters = await getChaptersFromLocal(mangaDir);
const dbChapters = await prisma.chapter.findMany({
Expand All @@ -26,11 +24,17 @@ const checkChapters = async (manga: MangaWithLibraryAndMetadata) => {
});

const dbOnlyChapters = dbChapters.filter(
(dbChapter) => localChapters.findIndex((localChapter) => localChapter.index === dbChapter.index) < 0,
(dbChapter) =>
localChapters.findIndex(
(localChapter) => localChapter.fileName === dbChapter.fileName && localChapter.index === dbChapter.index,
) < 0,
);

const missingDbChapters = localChapters.filter(
(localChapter) => dbChapters.findIndex((dbChapter) => dbChapter.index === localChapter.index) < 0,
(localChapter) =>
dbChapters.findIndex(
(dbChapter) => dbChapter.fileName === localChapter.fileName && dbChapter.index === localChapter.index,
) < 0,
);

await prisma.$transaction([
Expand All @@ -48,6 +52,13 @@ const checkChapters = async (manga: MangaWithLibraryAndMetadata) => {
})),
}),
]);
};

const checkChapters = async (manga: MangaWithLibraryAndMetadata) => {
logger.info(`Checking for new chapters: ${manga.title}`);
const mangaDir = path.resolve(manga.library.path, sanitizer(manga.title));
const missingChapterFiles = await findMissingChapterFiles(mangaDir, manga.source, manga.title);
await syncDbWithFiles(manga);

if (missingChapterFiles.length === 0) {
logger.info(`There are no missing chapter files for ${manga.title}`);
Expand Down
10 changes: 6 additions & 4 deletions src/server/queue/checkOutOfSyncChapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from 'path';
import { sanitizer } from '../../utils';
import { prisma } from '../db/client';
import { getOutOfSyncChapters } from '../utils/mangal';
import { syncDbWithFiles } from './checkChapters';

export interface ICheckOutOfSyncChaptersWorkerData {
mangaId: number;
Expand All @@ -15,17 +16,18 @@ export const checkOutOfSyncChaptersWorker = new Worker(
const { mangaId }: ICheckOutOfSyncChaptersWorkerData = job.data;
try {
const mangaInDb = await prisma.manga.findUniqueOrThrow({
include: { library: true, chapters: true },
include: { library: true, chapters: true, metadata: true },
where: { id: mangaId },
});
await syncDbWithFiles(mangaInDb);
const mangaDir = path.resolve(mangaInDb.library.path, sanitizer(mangaInDb.title));
const outOfSyncChapters = await getOutOfSyncChapters(mangaDir, mangaInDb.source, mangaInDb.title);
const toBeRemovedChapters = await getOutOfSyncChapters(mangaDir, mangaInDb.source, mangaInDb.title);

await prisma.outOfSyncChapter.deleteMany({ where: { mangaId: mangaInDb.id } });
await prisma.outOfSyncChapter.createMany({
data: outOfSyncChapters
data: toBeRemovedChapters
.map((outOfSyncChapterFile) => {
const chapter = mangaInDb.chapters.find((c) => c.fileName === outOfSyncChapterFile);
const chapter = mangaInDb.chapters.find((c) => c.fileName === outOfSyncChapterFile.fileName);
if (!chapter) {
return undefined;
}
Expand Down
22 changes: 5 additions & 17 deletions src/server/queue/fixOutOfSyncChapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Job, Queue, Worker } from 'bullmq';
import path from 'path';
import { sanitizer } from '../../utils';
import { prisma } from '../db/client';
import { downloadChapter, getChapterFromLocal, removeChapter } from '../utils/mangal';
import { integrationQueue } from './integration';
import { removeChapter } from '../utils/mangal';
import { schedule } from './checkChapters';

export interface IFixOutOfSyncChaptersWorkerData {
mangaId: number;
Expand All @@ -15,7 +15,7 @@ export const fixOutOfSyncChaptersWorker = new Worker(
const { mangaId }: IFixOutOfSyncChaptersWorkerData = job.data;
try {
const mangaInDb = await prisma.manga.findUniqueOrThrow({
include: { library: true, outOfSyncChapters: true, chapters: true },
include: { library: true, outOfSyncChapters: true, chapters: true, metadata: true },
where: { id: mangaId },
});
const mangaPath = path.resolve(mangaInDb.library.path, sanitizer(mangaInDb.title));
Expand All @@ -26,25 +26,13 @@ export const fixOutOfSyncChaptersWorker = new Worker(
if (!chapter) {
return;
}
const filePath = await downloadChapter(
mangaInDb.title,
mangaInDb.source,
chapter.index,
mangaInDb.library.path,
);
await removeChapter(mangaPath, chapter.fileName);
await prisma.outOfSyncChapter.delete({ where: { id: outOfSyncChapter.id } });
await prisma.chapter.delete({ where: { id: outOfSyncChapter.id } });
const newChapter = await getChapterFromLocal(filePath);
await prisma.chapter.create({
data: {
...newChapter,
mangaId,
},
});
}),
);
await integrationQueue.add('run_integrations', null);

await schedule(mangaInDb, true);
await job.updateProgress(100);
} catch (err) {
await job.log(`${err}`);
Expand Down
13 changes: 8 additions & 5 deletions src/server/utils/mangal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,14 @@ export const removeChapter = async (mangaDir: string, chapterFileName: string) =
};

export const getOutOfSyncChapters = async (mangaDir: string, source: string, title: string) => {
const localChapterNames = (await fs.readdir(mangaDir)).filter(shouldIncludeFile);
const localChapters = await getChaptersFromLocal(mangaDir);
const remoteChapters = await getChaptersFromRemote(source, title);
const remoteChapterNames = remoteChapters.map(
(r) => `[${String(r.index + 1).padStart(4, '0')}]_${sanitizer(r.name)}.cbz`,
);
const remoteChaptersWithIndex = remoteChapters.map((r) => ({
fileName: `[${String(r.index + 1).padStart(4, '0')}]_${sanitizer(r.name)}.cbz`,
index: r.index + 1,
}));

return localChapterNames.filter((l) => !remoteChapterNames.includes(l));
return localChapters.filter(
(l) => !remoteChaptersWithIndex.find((r) => l.index + 1 === r.index && l.fileName === r.fileName),
);
};

0 comments on commit 5713cd6

Please sign in to comment.