1
1
import { Prisma } from '@prisma/client' ;
2
2
import { Job , Queue , Worker } from 'bullmq' ;
3
3
import path from 'path' ;
4
- import { logger } from '../../utils/logging' ;
5
4
import { sanitizer } from '../../utils' ;
5
+ import { logger } from '../../utils/logging' ;
6
6
import { prisma } from '../db/client' ;
7
7
import { findMissingChapterFiles , getChaptersFromLocal } from '../utils/mangal' ;
8
8
import { downloadQueue } from './download' ;
@@ -13,10 +13,8 @@ const mangaWithLibraryAndMetadata = Prisma.validator<Prisma.MangaArgs>()({
13
13
14
14
export type MangaWithLibraryAndMetadata = Prisma . MangaGetPayload < typeof mangaWithLibraryAndMetadata > ;
15
15
16
- const checkChapters = async ( manga : MangaWithLibraryAndMetadata ) => {
17
- logger . info ( `Checking for new chapters: ${ manga . title } ` ) ;
16
+ export const syncDbWithFiles = async ( manga : MangaWithLibraryAndMetadata ) => {
18
17
const mangaDir = path . resolve ( manga . library . path , sanitizer ( manga . title ) ) ;
19
- const missingChapterFiles = await findMissingChapterFiles ( mangaDir , manga . source , manga . title ) ;
20
18
21
19
const localChapters = await getChaptersFromLocal ( mangaDir ) ;
22
20
const dbChapters = await prisma . chapter . findMany ( {
@@ -26,11 +24,17 @@ const checkChapters = async (manga: MangaWithLibraryAndMetadata) => {
26
24
} ) ;
27
25
28
26
const dbOnlyChapters = dbChapters . filter (
29
- ( dbChapter ) => localChapters . findIndex ( ( localChapter ) => localChapter . index === dbChapter . index ) < 0 ,
27
+ ( dbChapter ) =>
28
+ localChapters . findIndex (
29
+ ( localChapter ) => localChapter . fileName === dbChapter . fileName && localChapter . index === dbChapter . index ,
30
+ ) < 0 ,
30
31
) ;
31
32
32
33
const missingDbChapters = localChapters . filter (
33
- ( localChapter ) => dbChapters . findIndex ( ( dbChapter ) => dbChapter . index === localChapter . index ) < 0 ,
34
+ ( localChapter ) =>
35
+ dbChapters . findIndex (
36
+ ( dbChapter ) => dbChapter . fileName === localChapter . fileName && dbChapter . index === localChapter . index ,
37
+ ) < 0 ,
34
38
) ;
35
39
36
40
await prisma . $transaction ( [
@@ -48,6 +52,13 @@ const checkChapters = async (manga: MangaWithLibraryAndMetadata) => {
48
52
} ) ) ,
49
53
} ) ,
50
54
] ) ;
55
+ } ;
56
+
57
+ const checkChapters = async ( manga : MangaWithLibraryAndMetadata ) => {
58
+ logger . info ( `Checking for new chapters: ${ manga . title } ` ) ;
59
+ const mangaDir = path . resolve ( manga . library . path , sanitizer ( manga . title ) ) ;
60
+ const missingChapterFiles = await findMissingChapterFiles ( mangaDir , manga . source , manga . title ) ;
61
+ await syncDbWithFiles ( manga ) ;
51
62
52
63
if ( missingChapterFiles . length === 0 ) {
53
64
logger . info ( `There are no missing chapter files for ${ manga . title } ` ) ;
0 commit comments