Skip to content

Commit

Permalink
Save ComicInfo.xml in CBZ Files (Optional) (#6102)
Browse files Browse the repository at this point in the history
* Add ComicInfo.xml generator and include it in chapters file

* The label has been updated to provide clarity for the user

* Fix code issues

* Remove wrong code
  • Loading branch information
apiweb authored and Sheepux committed Jan 2, 2024
1 parent 73d0e3d commit 6accfb2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/web/mjs/HakuNeko.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import BookmarkManager from './engine/BookmarkManager.mjs';
import ChaptermarkManager from './engine/ChaptermarkManager.mjs';
import Connectors from './engine/Connectors.mjs';
import DownloadManager from './engine/DownloadManager.mjs';
import ComicInfoGenerator from './engine/ComicInfoGenerator.mjs';
//import HistoryWorker from './engine/HistoryWorker.mjs'
import Request from './engine/Request.mjs';
import Settings from './engine/Settings.mjs';
Expand All @@ -29,6 +30,7 @@ export default class HakuNeko {
this._connectors = new Connectors(this._request);
this._storage = new Storage();
this._bookmarkManager = new BookmarkManager(this._settings, new BookmarkImporter());
this._comicInfoGenerator = new ComicInfoGenerator();
this._chaptermarkManager = new ChaptermarkManager(this._settings);
}

Expand Down Expand Up @@ -59,6 +61,10 @@ export default class HakuNeko {
return this._bookmarkManager;
}

get ComicInfoGenerator() {
return this._comicInfoGenerator;
}

get ChaptermarkManager() {
return this._chaptermarkManager;
}
Expand Down
26 changes: 26 additions & 0 deletions src/web/mjs/engine/ComicInfoGenerator.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export default class ComicInfoGenerator {
createComicInfoXML(series, title, pagesCount) {
series = this.escapeXML(series);
title = this.escapeXML(title);
return `<?xml version="1.0" encoding="utf-8"?>
<ComicInfo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Title>${title}</Title>
<Series>${series}</Series>
<PageCount>${pagesCount}</PageCount>
</ComicInfo>`;
}

escapeXML(str) {
const symbols = {
'<': '&lt;',
'>': '&gt;',
'&': '&amp;',
'\'': '&apos;',
'"': '&quot;'
};

return str.replace(/[<>&'"]/g, function (c) {
return symbols[c];
});
}
}
2 changes: 1 addition & 1 deletion src/web/mjs/engine/Settings.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -385,4 +385,4 @@ export default class Settings extends EventTarget {
return value;
}
}
}
}
10 changes: 7 additions & 3 deletions src/web/mjs/engine/Storage.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ export default class Storage {
}
if (Engine.Settings.chapterFormat.value === extensions.cbz) {
this._createDirectoryChain(this.path.dirname(output));
promise = this._saveChapterPagesCBZ(output, pageData)
promise = this._saveChapterPagesCBZ(output, pageData, chapter.manga.title, chapter.title)
.then(() => this._runPostChapterDownloadCommand(chapter, output));
}
if (Engine.Settings.chapterFormat.value === extensions.pdf) {
Expand Down Expand Up @@ -548,8 +548,12 @@ export default class Storage {
* Create and save pages to the given archive file.
* Callback will be executed after completion and provided with an array of errors (or an empty array when no errors occured).
*/
_saveChapterPagesCBZ(archive, pageData) {
_saveChapterPagesCBZ(archive, pageData, mangaName = '', chapterName = '') {
let zip = new JSZip();

let comicFile = Engine.ComicInfoGenerator.createComicInfoXML(mangaName, chapterName, pageData.length);
zip.file('ComicInfo.xml', comicFile);

pageData.forEach(page => {
zip.file(page.name, page.data);
});
Expand Down Expand Up @@ -893,4 +897,4 @@ export default class Storage {
});
});
}
}
}

0 comments on commit 6accfb2

Please sign in to comment.