Skip to content

Commit

Permalink
fix: download is not respecting the chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedroh1510 committed Dec 15, 2024
1 parent 9ccd7c0 commit 9ca253f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
32 changes: 29 additions & 3 deletions src/server/queue/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Job, Queue, Worker } from 'bullmq';
import { sanitizer } from '../../utils';
import { logger } from '../../utils/logging';
import { prisma } from '../db/client';
import { downloadChapter, getChapterFromLocal } from '../utils/mangal';
import { downloadChapter, getChapter, getChapterFromLocal } from '../utils/mangal';
import { integrationQueue } from './integration';
import { notificationQueue } from './notify';

Expand All @@ -17,6 +17,13 @@ export interface IDownloadWorkerData {
chapterIndex: number;
}

interface Download {
index: number;
size: number;
createdAt: Date;
fileName: string;
}

export const downloadWorker = new Worker(
'downloadQueue',
async (job: Job) => {
Expand All @@ -36,8 +43,27 @@ export const downloadWorker = new Worker(
});
return;
}
filePath = await downloadChapter(mangaInDb.title, mangaInDb.source, chapterIndex, mangaInDb.library.path);
const chapter = await getChapterFromLocal(filePath);
const download = async (index: number, up: boolean): Promise<Download> => {
const chapter = await getChapter(mangaInDb.title, mangaInDb.source, index, mangaInDb.library.path);
if (chapter.index === chapterIndex) {
filePath = await downloadChapter(mangaInDb.title, mangaInDb.source, index, mangaInDb.library.path);

return getChapterFromLocal(filePath);
}
if (up) {
return download(index + 1, true);
}
if (index === 0) {
throw new Error(`Not found chapter ${chapterIndex}`);
}
return download(index - 1, false);
};
let chapter;
try {
chapter = await download(chapterIndex, true);
} catch (error) {
chapter = await download(chapterIndex, false);
}

await prisma.chapter.deleteMany({
where: {
Expand Down
46 changes: 46 additions & 0 deletions src/server/utils/mangal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,52 @@ export const downloadChapter = async (
}
};

export interface ChapterMangal {
name: string;
url: string;
index: number;
id: number;
volume: number;
pages: null | number;
}
export const getChapter = async (
title: string,
source: string,
chapterIndex: number,
libraryPath: string,
): Promise<ChapterMangal> => {
try {
logger.info(`Get chapter #${chapterIndex} for ${title} from ${source}`);
const { stdout, stderr, escapedCommand } = await execa(
'mangal',
['inline', '--source', source, '--query', title, '--manga', 'exact', '--chapters', `${chapterIndex}`, '-j'],
{
cwd: libraryPath,
},
);

logger.info(`Get chapter with following command: ${escapedCommand}`);

if (stderr) {
logger.error(`Failed to get the chapter #${chapterIndex} for ${title}. Err:\n${stderr}`);
throw new Error(stderr);
} else {
logger.info(`Get chapter #${chapterIndex} for ${title}. Result:\n${stdout}`);
}
const response = JSON.parse(stdout.trim());
if (Array.isArray(response?.result) && response?.result.length > 0) {
const chapters = response?.result[0].mangal.chapters;
if (Array.isArray(chapters) && chapters.length > 0) {
return response?.result[0].mangal.chapters[0];
}
}
throw new Error('Failed to Get the chapter');
} catch (err) {
logger.error(`Failed to Get the chapter #${chapterIndex} for ${title}. Err:\n${err}`);
throw err;
}
};

export const getChapterIndexFromFile = (chapterFile: string) => {
const indexRegexp = /.*?\[(\d+)\].*/;
const match = indexRegexp.exec(path.basename(chapterFile));
Expand Down

0 comments on commit 9ca253f

Please sign in to comment.