|
|
|
@ -4,7 +4,6 @@ const FavoriteModel = require('../models/favorite.js');
|
|
|
|
|
const ChapterModel = require('../models/readChapter.js');
|
|
|
|
|
const fs = require('fs');
|
|
|
|
|
const path = require('path');
|
|
|
|
|
const { isNullOrUndefined } = require('util');
|
|
|
|
|
|
|
|
|
|
exports.home = async (req,res)=>
|
|
|
|
|
{
|
|
|
|
@ -45,7 +44,6 @@ exports.search = async (req, res)=>
|
|
|
|
|
const scanlatorB = b.scanlator.toLowerCase();
|
|
|
|
|
return scanlatorA.localeCompare(scanlatorB);
|
|
|
|
|
})
|
|
|
|
|
console.log(data[1].Results)
|
|
|
|
|
res.render('searchResults.ejs', {data})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -118,6 +116,7 @@ exports.manga = async (req, res)=>
|
|
|
|
|
{
|
|
|
|
|
manga.List[i]['completely'] = chaptersRead[j].completely
|
|
|
|
|
manga.List[i]['Read'] = true;
|
|
|
|
|
manga.List[i]['_id'] = chaptersRead[j]._id;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -127,8 +126,13 @@ exports.manga = async (req, res)=>
|
|
|
|
|
const sortedChapters = chaptersRead.sort((a, b) => a.chapterNum - b.chapterNum);
|
|
|
|
|
const sortedAndFilteredChapters = sortedChapters.filter(item=> item.lastImageRead && !item.completely);
|
|
|
|
|
const lastChapterWithReadImages=sortedAndFilteredChapters.shift();
|
|
|
|
|
const chapterNumToContinue = getChapterNumToContinue(lastCompletelyRead, lastChapterWithReadImages);
|
|
|
|
|
manga.List[chapterNumToContinue-1]['continue'] = true;
|
|
|
|
|
let chapterNumToContinue = getChapterNumToContinue(lastCompletelyRead, lastChapterWithReadImages, manga.List);
|
|
|
|
|
if(!manga.List.some(item=> item.num==chapterNumToContinue)) chapterNumToContinue = Math.trunc(chapterNumToContinue);
|
|
|
|
|
let index = manga.List.indexOf(manga.List.filter(item=>
|
|
|
|
|
{
|
|
|
|
|
return item.num == chapterNumToContinue
|
|
|
|
|
})[0]);
|
|
|
|
|
manga.List[index]['continue'] = true;
|
|
|
|
|
}
|
|
|
|
|
catch (error)
|
|
|
|
|
{
|
|
|
|
@ -136,15 +140,19 @@ exports.manga = async (req, res)=>
|
|
|
|
|
}
|
|
|
|
|
res.render('mangaPage.ejs', {data:{...manga, scanlator}});
|
|
|
|
|
}
|
|
|
|
|
function getChapterNumToContinue(lastCompletelyRead, lastChapterWithReadImages)
|
|
|
|
|
function getChapterNumToContinue(lastCompletelyRead, lastChapterWithReadImages, List)
|
|
|
|
|
{
|
|
|
|
|
const firstIndex = List.indexOf(List.filter(item=>item.num == lastCompletelyRead.chapterNum)[0]);
|
|
|
|
|
if(lastCompletelyRead)
|
|
|
|
|
{
|
|
|
|
|
if(lastCompletelyRead.chapterNum < lastChapterWithReadImages.chapterNum)
|
|
|
|
|
{
|
|
|
|
|
return lastChapterWithReadImages.chapterNum;
|
|
|
|
|
}
|
|
|
|
|
return lastCompletelyRead.chapterNum+1;
|
|
|
|
|
const chNum = lastCompletelyRead.chapterNum+1;
|
|
|
|
|
const secondIndex = List.indexOf(List.filter(item=>item.num == chNum)[0]);
|
|
|
|
|
if(secondIndex - firstIndex > 1) return List[firstIndex + 1].num;
|
|
|
|
|
return chNum
|
|
|
|
|
}
|
|
|
|
|
return lastChapterWithReadImages.chapterNum;
|
|
|
|
|
}
|
|
|
|
@ -161,7 +169,7 @@ exports.chapter = async (req, res)=>
|
|
|
|
|
const latestChap = fs.readdirSync(`./public/Saved/${scanlator}/${title}`).length
|
|
|
|
|
const List = imgs.map(filename => {return `./Saved/${scanlator}/${title}/CH_${chapter}/${filename}`});
|
|
|
|
|
const mangaLink = await new Chapter(scanlator, link, title, chapter).getMangaLink();
|
|
|
|
|
return res.render('chapterPage.ejs', {data:{title,latestChap,scanlator, chapterNum:parseInt(chapter), mangaLink, List}});
|
|
|
|
|
return res.render('chapterPage.ejs', {data:{title,latestChap,scanlator, chapterNum:parseFloat(chapter), mangaLink, List}});
|
|
|
|
|
}
|
|
|
|
|
//If it isn't make sure there is no bad params being passed
|
|
|
|
|
const scanlatorExists = await new Modules(scanlator).exists();
|
|
|
|
@ -181,7 +189,7 @@ exports.chapter = async (req, res)=>
|
|
|
|
|
{
|
|
|
|
|
return res.redirect('/')
|
|
|
|
|
}
|
|
|
|
|
res.render('chapterPage.ejs', {data:{...manga, scanlator, chapterNum:parseInt(chapter)}});
|
|
|
|
|
res.render('chapterPage.ejs', {data:{...manga, scanlator, chapterNum:parseFloat(chapter)}});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
exports.favorites = async (req, res)=>
|
|
|
|
@ -313,7 +321,7 @@ exports.chapterNavInfo = async (req, res)=>
|
|
|
|
|
if(!scanlatorExists) return res.sendStatus(404);
|
|
|
|
|
const scanlatorHasTitle = await new Modules(scanlator).titleExists(title);
|
|
|
|
|
if(!scanlatorHasTitle) return res.sendStatus(404);
|
|
|
|
|
let chapter = parseInt(req.params.chapter);
|
|
|
|
|
let chapter = parseFloat(req.params.chapter);
|
|
|
|
|
let manga;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
@ -429,6 +437,8 @@ exports.errorPage = (req, res)=>
|
|
|
|
|
|
|
|
|
|
exports.chapterRead = async (req, res)=>
|
|
|
|
|
{
|
|
|
|
|
const cookieStr = req.headers.cookie;
|
|
|
|
|
if(!cookieStr) return res.sendStatus(404);
|
|
|
|
|
const {scanlator, title, chapter, link} = req.params;
|
|
|
|
|
const scanlatorExists = await new Modules(scanlator).exists();
|
|
|
|
|
if(!scanlatorExists) return res.sendStatus(404);
|
|
|
|
@ -439,13 +449,21 @@ exports.chapterRead = async (req, res)=>
|
|
|
|
|
if(chapter > latestChap || chapter < 0) return res.sendStatus(404);
|
|
|
|
|
const chap = new ChapterModel();
|
|
|
|
|
let chapterNum = chapter;
|
|
|
|
|
let chapterLink = List.filter(item=>item.num == chapterNum)[0].link;
|
|
|
|
|
let chapFromList = List.filter(item=>item.num == chapterNum)[0];
|
|
|
|
|
let chapterLink = chapFromList.link;
|
|
|
|
|
let chapterDate = chapFromList.date;
|
|
|
|
|
if(!chapterLink) return res.sendStatus(404);
|
|
|
|
|
const _chapter = await new Chapter(scanlator, chapterLink,title, chapter).get();
|
|
|
|
|
const _chapter = await new Chapter(scanlator, chapterLink,title, chapterNum).get();
|
|
|
|
|
let lastImageRead = _chapter.List[_chapter.List.length-1];
|
|
|
|
|
chap.lastImageRead = lastImageRead;
|
|
|
|
|
const chaps = await ChapterModel.find({scanlator, title, link, chapterNum});
|
|
|
|
|
if(chaps.length>0) return updateChapter(chaps[0],res, lastImageRead, true)
|
|
|
|
|
if(chaps.length>0) updateChapter(chaps[0], lastImageRead, true);
|
|
|
|
|
_chapter['num'] = _chapter.chNum;
|
|
|
|
|
_chapter['date'] = chapterDate;
|
|
|
|
|
_chapter['Read'] = true;
|
|
|
|
|
_chapter['_id'] = chap._id;
|
|
|
|
|
_chapter['completely'] = true;
|
|
|
|
|
res.render('chapterButton.ejs', {chapter:_chapter, scanlator, title, link, shortCut:false});
|
|
|
|
|
chap.scanlator = scanlator;
|
|
|
|
|
chap.title = title;
|
|
|
|
|
chap.link = link;
|
|
|
|
@ -453,12 +471,11 @@ exports.chapterRead = async (req, res)=>
|
|
|
|
|
chap.lastImageRead = lastImageRead;
|
|
|
|
|
chap.completely = true;
|
|
|
|
|
chap.save()
|
|
|
|
|
res.sendStatus(200);
|
|
|
|
|
}
|
|
|
|
|
async function updateChapter(chap, res, lastImageRead, completely=false)
|
|
|
|
|
async function updateChapter(chap,lastImageRead, completely=false,res)
|
|
|
|
|
{
|
|
|
|
|
await ChapterModel.findOneAndUpdate(chap._id,{completely, lastImageRead:lastImageRead}, {new:true});
|
|
|
|
|
res.sendStatus(200);
|
|
|
|
|
if(res) return res.sendStatus(200)
|
|
|
|
|
}
|
|
|
|
|
exports.chapterIncompleteRead = async (req,res)=>
|
|
|
|
|
{
|
|
|
|
@ -569,26 +586,7 @@ exports.continue = async (req, res)=>
|
|
|
|
|
|
|
|
|
|
return [...results, ...uniqueContinuationChapters, ...chaptersToStart];
|
|
|
|
|
}
|
|
|
|
|
async function getNextChapter (chapter)
|
|
|
|
|
{
|
|
|
|
|
const {scanlator, link, title, chapterNum, _id } = chapter;
|
|
|
|
|
const manga = await new Manga(scanlator, link, title).get();
|
|
|
|
|
if((chapterNum+1)>manga.latestChap) return false;
|
|
|
|
|
const chapLink = manga.List.filter(item=>item.num == (chapterNum+1))[0].link
|
|
|
|
|
const nextChapter = await new Chapter(scanlator, chapLink, title, (chapterNum+1)).get();
|
|
|
|
|
let index = nextChapter.List.indexOf(chapter.lastImageRead)
|
|
|
|
|
const imgReadOutOfTotal = (index !=-1 ? index:0)+'/'+(nextChapter.List.length-1)
|
|
|
|
|
return {
|
|
|
|
|
...nextChapter,
|
|
|
|
|
_id,
|
|
|
|
|
mangaCoverImg:manga.img,
|
|
|
|
|
chapterNum:nextChapter.chNum,
|
|
|
|
|
lastImageRead:nextChapter.List[0],
|
|
|
|
|
completely:false,
|
|
|
|
|
imgReadOutOfTotal,
|
|
|
|
|
byPass:true
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const chaptersToContinue = await getChaptersToContinue();
|
|
|
|
|
chaptersToContinue.sort((a, b) => {
|
|
|
|
@ -614,11 +612,49 @@ exports.continue = async (req, res)=>
|
|
|
|
|
}
|
|
|
|
|
res.render('chaptersToContinue.ejs', {data:chaptersToContinue});
|
|
|
|
|
}
|
|
|
|
|
async function getNextChapter (chapter)
|
|
|
|
|
{
|
|
|
|
|
const {scanlator, link, title, _id } = chapter;
|
|
|
|
|
let chapterNum = chapter.chapterNum;
|
|
|
|
|
const manga = await new Manga(scanlator, link, title).get();
|
|
|
|
|
if((chapterNum+1)>manga.latestChap) return false;
|
|
|
|
|
if(!manga.List.some(item=>item.num == (chapterNum+1))) chapterNum = Math.trunc(chapterNum);
|
|
|
|
|
const chapterFromList = manga.List.filter(item=>item.num == (chapterNum+1))[0]
|
|
|
|
|
const {date, num} = chapterFromList;
|
|
|
|
|
const nextChapter = await new Chapter(scanlator, chapterFromList.link, title, (chapterNum+1)).get();
|
|
|
|
|
let index = chapter.lastImageRead?nextChapter.List.indexOf(chapter.lastImageRead):-1;
|
|
|
|
|
const imgReadOutOfTotal = (index !=-1 ? index:0)+'/'+(nextChapter.List.length-1)
|
|
|
|
|
return {
|
|
|
|
|
...nextChapter,
|
|
|
|
|
num,
|
|
|
|
|
date,
|
|
|
|
|
_id,
|
|
|
|
|
mangaCoverImg:manga.img,
|
|
|
|
|
chapterNum:nextChapter.chNum,
|
|
|
|
|
lastImageRead:nextChapter.List[0],
|
|
|
|
|
completely:false,
|
|
|
|
|
imgReadOutOfTotal,
|
|
|
|
|
byPass:true
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
exports.chapterToContinue = async (req, res)=>
|
|
|
|
|
{
|
|
|
|
|
const {scanlator, mangaLink, title} = req.params;
|
|
|
|
|
const chaptersRead = await ChapterModel.find({scanlator, title}).then(res=>{return res});
|
|
|
|
|
let sorted = chaptersRead.sort((a,b)=>{return b.chapterNum-a.chapterNum});
|
|
|
|
|
const newChapter = await getNextChapter({scanlator, link:mangaLink, title,chapterNum:parseFloat(sorted[0].chapterNum), _id:null});
|
|
|
|
|
res.render('chapterButton.ejs',{chapter:newChapter, scanlator, title,shortCut:true})
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
exports.removeChapter = async (req, res)=>
|
|
|
|
|
{
|
|
|
|
|
if(!isValidID(req.params.id)) return res.sendStatus(404)
|
|
|
|
|
const chapter = await ChapterModel.findOneAndDelete({_id:req.params.id});
|
|
|
|
|
res.render('empty.ejs');
|
|
|
|
|
const cookieStr = req.headers.cookie;
|
|
|
|
|
if(!cookieStr) return res.sendStatus(404);
|
|
|
|
|
if(!isValidID(req.params.id)) return res.sendStatus(404);
|
|
|
|
|
const {link, scanlator, title, chapterNum} = await ChapterModel.findOneAndDelete({_id:req.params.id}).then(deleted=>{return deleted});
|
|
|
|
|
const {List} = await new Manga(scanlator, link, title).get();
|
|
|
|
|
const chapter = List.filter(item=>item.num == chapterNum)[0];
|
|
|
|
|
res.render('chapterButton.ejs', {chapter, scanlator, title, shortCut:false});
|
|
|
|
|
}
|
|
|
|
|
exports.dashboard = async (req, res)=>
|
|
|
|
|
{
|
|
|
|
|