Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: download is not respecting the chapter #166

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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