From a8689ae26beaee6fb6e3ff2f16d5289b0bf9a2a0 Mon Sep 17 00:00:00 2001 From: GarcaMan Date: Wed, 24 Nov 2021 19:41:07 +0000 Subject: [PATCH 01/11] first commit --- cps/static/css/kthoom.css | 14 ++ cps/static/js/kthoom.js | 408 +++++++++++++++++++++++-------------- cps/templates/readcbr.html | 21 +- 3 files changed, 283 insertions(+), 160 deletions(-) diff --git a/cps/static/css/kthoom.css b/cps/static/css/kthoom.css index 9565cd30..2c733e52 100644 --- a/cps/static/css/kthoom.css +++ b/cps/static/css/kthoom.css @@ -149,6 +149,20 @@ body { word-wrap: break-word; } +#mainContent > canvas { + display: block; + margin-left: auto; + margin-right: auto; +} + +.long-strip > .mainImage { + margin-bottom: 4px; +} + +.long-strip > .mainImage:last-child { + margin-bottom: 0px !important; +} + #titlebar { min-height: 25px; height: auto; diff --git a/cps/static/js/kthoom.js b/cps/static/js/kthoom.js index 268fe9ec..92a34d5b 100644 --- a/cps/static/js/kthoom.js +++ b/cps/static/js/kthoom.js @@ -62,6 +62,7 @@ var currentImage = 0; var imageFiles = []; var imageFilenames = []; var totalImages = 0; +var prevScrollPosition = 0; var settings = { hflip: false, @@ -70,8 +71,8 @@ var settings = { fitMode: kthoom.Key.B, theme: "light", direction: 0, // 0 = Left to Right, 1 = Right to Left - nextPage: 0, // 0 = Reset to Top, 1 = Remember Position - scrollbar: 1 // 0 = Hide Scrollbar, 1 = Show Scrollbar + scrollbar: 1, // 0 = Hide Scrollbar, 1 = Show Scrollbar + pageDisplay: 0 // 0 = Single Page, 1 = Long Strip }; kthoom.saveSettings = function() { @@ -178,8 +179,7 @@ function initProgressClick() { }); } -function loadFromArrayBuffer(ab) { - var lastCompletion = 0; +function loadFromArrayBuffer(ab, _callback) { const collator = new Intl.Collator('en', { numeric: true, sensitivity: 'base' }); loadArchiveFormats(['rar', 'zip', 'tar'], function() { // Open the file as an archive @@ -208,9 +208,13 @@ function loadFromArrayBuffer(ab) { "" + "" ); + + drawCanvas(); + setImage(test.dataURI, null); + // display first page if we haven't yet if (imageFiles.length === currentImage + 1) { - updatePage(lastCompletion); + updatePage(); } } else { totalImages--; @@ -225,13 +229,6 @@ function loadFromArrayBuffer(ab) { } function scrollTocToActive() { - // Scroll to the thumbnail in the TOC on page change - $("#tocView").stop().animate({ - scrollTop: $("#tocView a.active").position().top - }, 200); -} - -function updatePage() { $(".page").text((currentImage + 1 ) + "/" + totalImages); // Mark the current page in the TOC @@ -243,22 +240,40 @@ function updatePage() { // Set it to active .addClass("active"); + // Scroll to the thumbnail in the TOC on page change + $("#tocView").stop().animate({ + scrollTop: $("#tocView a.active").position().top + }, 200); +} + +function updatePage() { scrollTocToActive(); + scrollCurrentImageIntoView(); updateProgress(); - - if (imageFiles[currentImage]) { - setImage(imageFiles[currentImage].dataURI); - } else { - setImage("loading"); - } - - $("body").toggleClass("dark-theme", settings.theme === "dark"); - $("#mainContent").toggleClass("disabled-scrollbar", settings.scrollbar === 0); + pageDisplayUpdate(); + setTheme(); kthoom.setSettings(); kthoom.saveSettings(); } +function setTheme() { + $("body").toggleClass("dark-theme", settings.theme === "dark"); + $("#mainContent").toggleClass("disabled-scrollbar", settings.scrollbar === 0); +} + +function pageDisplayUpdate() { + if(settings.pageDisplay === 0) { + $(".mainImage").addClass("hide"); + $(".mainImage").eq(currentImage).removeClass("hide"); + $("#mainContent").removeClass("long-strip"); + } else { + $(".mainImage").removeClass("hide"); + $("#mainContent").addClass("long-strip"); + scrollCurrentImageIntoView(); + } +} + function updateProgress(loadPercentage) { if (settings.direction === 0) { $("#progress .bar-read") @@ -290,100 +305,92 @@ function updateProgress(loadPercentage) { $("#progress .bar-read").css({ width: totalImages === 0 ? 0 : Math.round((currentImage + 1) / totalImages * 100) + "%"}); } -function setImage(url) { - var canvas = $("#mainImage")[0]; - var x = $("#mainImage")[0].getContext("2d"); +function setImage(url, _canvas) { + var canvas = _canvas || $(".mainImage").slice(-1)[0]; // Select the last item on the array if _canvas is null + var x = canvas.getContext("2d"); + $("#mainText").hide(); - if (url === "loading") { - updateScale(true); - canvas.width = innerWidth - 100; - canvas.height = 200; + if (url === "error") { x.fillStyle = "black"; x.textAlign = "center"; x.font = "24px sans-serif"; - x.strokeStyle = "black"; - x.fillText("Loading Page #" + (currentImage + 1), innerWidth / 2, 100); + x.strokeStyle = (settings.theme === "dark") ? "white" : "black"; + x.fillText("Unable to decompress image #" + (currentImage + 1), innerWidth / 2, 100); + + $(".mainImage").slice(-1).addClass("error"); } else { - if (url === "error") { - updateScale(true); - canvas.width = innerWidth - 100; - canvas.height = 200; - x.fillStyle = "black"; - x.textAlign = "center"; - x.font = "24px sans-serif"; - x.strokeStyle = "black"; - x.fillText("Unable to decompress image #" + (currentImage + 1), innerWidth / 2, 100); - } else { - if ($("body").css("scrollHeight") / innerHeight > 1) { - $("body").css("overflowY", "scroll"); - } - - var img = new Image(); - img.onerror = function() { - canvas.width = innerWidth - 100; - canvas.height = 300; - updateScale(true); - x.fillStyle = "black"; - x.font = "50px sans-serif"; - x.strokeStyle = "black"; - x.fillText("Page #" + (currentImage + 1) + " (" + - imageFiles[currentImage].filename + ")", innerWidth / 2, 100); - x.fillStyle = "black"; - x.fillText("Is corrupt or not an image", innerWidth / 2, 200); - - var xhr = new XMLHttpRequest(); - if (/(html|htm)$/.test(imageFiles[currentImage].filename)) { - xhr.open("GET", url, true); - xhr.onload = function() { - $("#mainText").css("display", ""); - $("#mainText").innerHTML(""); - }; - xhr.send(null); - } else if (!/(jpg|jpeg|png|gif|webp)$/.test(imageFiles[currentImage].filename) && imageFiles[currentImage].data.uncompressedSize < 10 * 1024) { - xhr.open("GET", url, true); - xhr.onload = function() { - $("#mainText").css("display", ""); - $("#mainText").innerText(xhr.responseText); - }; - xhr.send(null); - } - }; - img.onload = function() { - var h = img.height, - w = img.width, - sw = w, - sh = h; - settings.rotateTimes = (4 + settings.rotateTimes) % 4; - x.save(); - if (settings.rotateTimes % 2 === 1) { - sh = w; - sw = h; - } - canvas.height = sh; - canvas.width = sw; - x.translate(sw / 2, sh / 2); - x.rotate(Math.PI / 2 * settings.rotateTimes); - x.translate(-w / 2, -h / 2); - if (settings.vflip) { - x.scale(1, -1); - x.translate(0, -h); - } - if (settings.hflip) { - x.scale(-1, 1); - x.translate(-w, 0); - } - canvas.style.display = "none"; - scrollTo(0, 0); - x.drawImage(img, 0, 0); - - updateScale(false); - - canvas.style.display = ""; - $("body").css("overflowY", ""); - x.restore(); - }; - img.src = url; + if ($("body").css("scrollHeight") / innerHeight > 1) { + $("body").css("overflowY", "scroll"); } + + var img = new Image(); + img.onerror = function() { + canvas.width = innerWidth - 100; + canvas.height = 300; + x.fillStyle = "black"; + x.font = "50px sans-serif"; + x.strokeStyle = "black"; + x.fillText("Page #" + (currentImage + 1) + " (" + + imageFiles[currentImage].filename + ")", innerWidth / 2, 100); + x.fillStyle = "black"; + x.fillText("Is corrupt or not an image", innerWidth / 2, 200); + + var xhr = new XMLHttpRequest(); + if (/(html|htm)$/.test(imageFiles[currentImage].filename)) { + xhr.open("GET", url, true); + xhr.onload = function() { + $("#mainText").css("display", ""); + $("#mainText").innerHTML(""); + }; + xhr.send(null); + } else if (!/(jpg|jpeg|png|gif|webp)$/.test(imageFiles[currentImage].filename) && imageFiles[currentImage].data.uncompressedSize < 10 * 1024) { + xhr.open("GET", url, true); + xhr.onload = function() { + $("#mainText").css("display", ""); + $("#mainText").innerText(xhr.responseText); + }; + xhr.send(null); + } + }; + img.onload = function() { + var h = img.height, + w = img.width, + sw = w, + sh = h; + settings.rotateTimes = (4 + settings.rotateTimes) % 4; + x.save(); + if (settings.rotateTimes % 2 === 1) { + sh = w; + sw = h; + } + canvas.height = sh; + canvas.width = sw; + x.translate(sw / 2, sh / 2); + x.rotate(Math.PI / 2 * settings.rotateTimes); + x.translate(-w / 2, -h / 2); + if (settings.vflip) { + x.scale(1, -1); + x.translate(0, -h); + } + if (settings.hflip) { + x.scale(-1, 1); + x.translate(-w, 0); + } + canvas.style.display = "none"; + scrollTo(0, 0); + x.drawImage(img, 0, 0); + + canvas.style.display = ""; + $("body").css("overflowY", ""); + x.restore(); + }; + img.src = url; + } +} + +function reloadImages() { + for(i=0; imageFiles.length; i++) { + setImage(imageFiles[i].dataURI, $(".mainImage")[i]); } } @@ -410,9 +417,6 @@ function showPrevPage() { currentImage++; } else { updatePage(); - if (settings.nextPage === 0) { - $("#mainContent").scrollTop(0); - } } } @@ -423,36 +427,53 @@ function showNextPage() { currentImage--; } else { updatePage(); - if (settings.nextPage === 0) { - $("#mainContent").scrollTop(0); - } } } -function updateScale(clear) { - var mainImageStyle = getElem("mainImage").style; - mainImageStyle.width = ""; - mainImageStyle.height = ""; - mainImageStyle.maxWidth = ""; - mainImageStyle.maxHeight = ""; - var maxheight = innerHeight - 50; - - if (!clear) { - switch (settings.fitMode) { - case kthoom.Key.B: - mainImageStyle.maxWidth = "100%"; - mainImageStyle.maxHeight = maxheight + "px"; - break; - case kthoom.Key.H: - mainImageStyle.height = maxheight + "px"; - break; - case kthoom.Key.W: - mainImageStyle.width = "100%"; - break; - default: - break; - } +function scrollCurrentImageIntoView() { + if(settings.pageDisplay == 0) { + // This will scroll all the way up when Single Page is selected + $("#mainContent").scrollTop(0); + } else { + // This will scroll to the image when Long Strip is selected + $("#mainContent").stop().animate({ + scrollTop: $(".mainImage").eq(currentImage).offset().top + $("#mainContent").scrollTop() - $("#mainContent").offset().top + }, 200); } +} + +function updateScale() { + var canvasArray = $("#mainContent > canvas"); + var maxheight = innerHeight - 50; + + canvasArray.css("width", ""); + canvasArray.css("height", ""); + canvasArray.css("maxWidth", ""); + canvasArray.css("maxHeight", ""); + + if(settings.pageDisplay === 0) { + canvasArray.addClass("hide"); + pageDisplayUpdate(); + } + + switch (settings.fitMode) { + case kthoom.Key.B: + canvasArray.css("maxWidth", "100%"); + canvasArray.css("maxHeight", maxheight + "px"); + break; + case kthoom.Key.H: + canvasArray.css("maxHeight", maxheight + "px"); + break; + case kthoom.Key.W: + canvasArray.css("width", "100%"); + break; + default: + break; + } + + $("#mainContent > canvas.error").css("width", innerWidth - 100); + $("#mainContent > canvas.error").css("height", 200); + $("#mainContent").css({maxHeight: maxheight + 5}); kthoom.setSettings(); kthoom.saveSettings(); @@ -469,6 +490,20 @@ function keyHandler(evt) { if (hasModifier) break; showRightPage(); break; + case kthoom.Key.S: + if (hasModifier) break; + settings.pageDisplay = 0; + pageDisplayUpdate(); + kthoom.setSettings(); + kthoom.saveSettings(); + break; + case kthoom.Key.O: + if (hasModifier) break; + settings.pageDisplay = 1; + pageDisplayUpdate(); + kthoom.setSettings(); + kthoom.saveSettings(); + break; case kthoom.Key.L: if (hasModifier) break; settings.rotateTimes--; @@ -503,22 +538,22 @@ function keyHandler(evt) { case kthoom.Key.W: if (hasModifier) break; settings.fitMode = kthoom.Key.W; - updateScale(false); + updateScale(); break; case kthoom.Key.H: if (hasModifier) break; settings.fitMode = kthoom.Key.H; - updateScale(false); + updateScale(); break; case kthoom.Key.B: if (hasModifier) break; settings.fitMode = kthoom.Key.B; - updateScale(false); + updateScale(); break; case kthoom.Key.N: if (hasModifier) break; settings.fitMode = kthoom.Key.N; - updateScale(false); + updateScale(); break; case kthoom.Key.SPACE: if (evt.shiftKey) { @@ -537,6 +572,43 @@ function keyHandler(evt) { } } +function drawCanvas() { + var maxheight = innerHeight - 50; + var canvasElement = $(""); + var x = canvasElement[0].getContext("2d"); + canvasElement.addClass("mainImage"); + + switch (settings.fitMode) { + case kthoom.Key.B: + canvasElement.css("maxWidth", "100%"); + canvasElement.css("maxHeight", maxheight + "px"); + break; + case kthoom.Key.H: + canvasElement.css("maxHeight", maxheight + "px"); + break; + case kthoom.Key.W: + canvasElement.css("width", "100%"); + break; + default: + break; + } + + if(settings.pageDisplay === 0) { + canvasElement.addClass("hide"); + } + + //Fill with Placeholder text. setImage will override this + canvasElement.width = innerWidth - 100; + canvasElement.height = 200; + x.fillStyle = "black"; + x.textAlign = "center"; + x.font = "24px sans-serif"; + x.strokeStyle = (settings.theme === "dark") ? "white" : "black"; + x.fillText("Loading Page #" + (currentImage + 1), innerWidth / 2, 100); + + $("#mainContent").append(canvasElement); +} + function init(filename) { var request = new XMLHttpRequest(); request.open("GET", filename); @@ -548,16 +620,17 @@ function init(filename) { console.warn(request.statusText, request.responseText); } }); + kthoom.loadSettings(); + setTheme(); + updateScale(); request.send(); initProgressClick(); document.body.className += /AppleWebKit/.test(navigator.userAgent) ? " webkit" : ""; - kthoom.loadSettings(); - updateScale(true); $(document).keydown(keyHandler); $(window).resize(function() { - updateScale(false); + updateScale(); }); // Open TOC menu @@ -588,8 +661,14 @@ function init(filename) { value = /^\d+$/.test(value) ? parseInt(value) : value; settings[this.name] = value; + + if(["hflip", "vflip", "rotateTimes"].includes(this.name)) { + //reloadImages is a slow process when multiple images are involved. Only used when rotating/mirroring + reloadImages(); + } + updatePage(); - updateScale(false); + updateScale(); }); // Close modal @@ -601,9 +680,6 @@ function init(filename) { $("#thumbnails").on("click", "a", function() { currentImage = $(this).data("page") - 1; updatePage(); - if (settings.nextPage === 0) { - $("#mainContent").scrollTop(0); - } }); // Fullscreen mode @@ -633,7 +709,7 @@ function init(filename) { showRightPage(); }, }); - $("#mainImage").click(function(evt) { + $(".mainImage").click(function(evt) { // Firefox does not support offsetX/Y so we have to manually calculate // where the user clicked in the image. var mainContentWidth = $("#mainContent").width(); @@ -668,5 +744,37 @@ function init(filename) { showRightPage(); } }); + + //Scrolling up/down will update current image if a new image is into view (for Long Strip Display) + $("#mainContent").scroll(function(){ + var scroll = $("#mainContent").scrollTop(); + if(settings.pageDisplay === 0) { + // Don't trigger the scroll for Single Page + } else if(scroll > prevScrollPosition) { + //Scroll Down + if(currentImage + 1 < imageFiles.length) { + if(currentImageOffset(currentImage + 1) <= 1) { + currentImage++; + scrollTocToActive(); + updateProgress(); + } + } + } else { + //Scroll Up + if(currentImage - 1 > -1 ) { + if(currentImageOffset(currentImage - 1) >= 0) { + currentImage--; + scrollTocToActive(); + updateProgress(); + } + } + } + + // Update scroll position + prevScrollPosition = scroll; + }); } +function currentImageOffset(imageIndex) { + return $(".mainImage").eq(imageIndex).offset().top - $("#mainContent").position().top +} diff --git a/cps/templates/readcbr.html b/cps/templates/readcbr.html index 411e3fdd..df2e53dd 100644 --- a/cps/templates/readcbr.html +++ b/cps/templates/readcbr.html @@ -73,7 +73,6 @@
-
@@ -91,6 +90,8 @@ ← {{_('Previous Page')}} → {{_('Next Page')}} + S {{_('Single Page Display')}} + O {{_('Long Strip Display')}} B {{_('Scale to Best')}} W {{_('Scale to Width')}} H {{_('Scale to Height')}} @@ -118,6 +119,15 @@ + + {{_('Display')}}: + +
+ + +
+ + {{_('Scale')}}: @@ -157,15 +167,6 @@ - - - {{_('Next Page')}}: - -
- - -
- {{_('Scrollbar')}}: From 7fc04b353bdff5236d79037768e2d676afc9501f Mon Sep 17 00:00:00 2001 From: GarcaMan Date: Wed, 24 Nov 2021 20:22:10 +0000 Subject: [PATCH 02/11] Selecting Position will not scroll the current image up --- cps/static/js/kthoom.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cps/static/js/kthoom.js b/cps/static/js/kthoom.js index 92a34d5b..cfc9cf2d 100644 --- a/cps/static/js/kthoom.js +++ b/cps/static/js/kthoom.js @@ -665,6 +665,9 @@ function init(filename) { if(["hflip", "vflip", "rotateTimes"].includes(this.name)) { //reloadImages is a slow process when multiple images are involved. Only used when rotating/mirroring reloadImages(); + } else if(this.name === "direction") { + // Skips updatePage and updateScale so that the current image doesn't scroll up + return updateProgress(); } updatePage(); From 3f56f0dca75bb6b059289913cab61de740b4cc2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Rodr=C3=ADguez?= <68790233+GarckaMan@users.noreply.github.com> Date: Thu, 25 Nov 2021 01:30:20 -0300 Subject: [PATCH 03/11] Removed parameter that was wrongly added --- cps/static/js/kthoom.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cps/static/js/kthoom.js b/cps/static/js/kthoom.js index cfc9cf2d..6e309c86 100644 --- a/cps/static/js/kthoom.js +++ b/cps/static/js/kthoom.js @@ -179,7 +179,7 @@ function initProgressClick() { }); } -function loadFromArrayBuffer(ab, _callback) { +function loadFromArrayBuffer(ab) { const collator = new Intl.Collator('en', { numeric: true, sensitivity: 'base' }); loadArchiveFormats(['rar', 'zip', 'tar'], function() { // Open the file as an archive From 3ac08a8c0df1f4a70d6b3ca600a9a88fe06ae092 Mon Sep 17 00:00:00 2001 From: GarcaMan Date: Thu, 2 Dec 2021 18:20:14 +0000 Subject: [PATCH 04/11] After toggling fullscreen, focus on main container --- cps/static/js/kthoom.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cps/static/js/kthoom.js b/cps/static/js/kthoom.js index cfc9cf2d..b637fc93 100644 --- a/cps/static/js/kthoom.js +++ b/cps/static/js/kthoom.js @@ -666,7 +666,6 @@ function init(filename) { //reloadImages is a slow process when multiple images are involved. Only used when rotating/mirroring reloadImages(); } else if(this.name === "direction") { - // Skips updatePage and updateScale so that the current image doesn't scroll up return updateProgress(); } @@ -689,6 +688,8 @@ function init(filename) { if (typeof screenfull !== "undefined") { $("#fullscreen").click(function() { screenfull.toggle($("#container")[0]); + // Focus so you can use up/down keys immediately after fullscreen + $("#mainContent").focus(); }); if (screenfull.raw) { From 4eaa9413f9f2ad610dc1335f1f08ff3344cb74f4 Mon Sep 17 00:00:00 2001 From: Evan Peterson <77evan@gmail.com> Date: Mon, 10 Jan 2022 15:15:19 -0500 Subject: [PATCH 05/11] Kobo metadata return correct layout format for fixed layout --- cps/epub.py | 25 +++++++++++++++++++++++++ cps/kobo.py | 3 +++ 2 files changed, 28 insertions(+) diff --git a/cps/epub.py b/cps/epub.py index cbbdcbbd..aae6120b 100644 --- a/cps/epub.py +++ b/cps/epub.py @@ -21,6 +21,7 @@ import zipfile from lxml import etree from . import isoLanguages +from . import config from .helper import split_authors from .constants import BookMeta @@ -39,6 +40,30 @@ def extractCover(zipFile, coverFile, coverpath, tmp_file_name): image.close() return tmp_cover_name +def get_epub_layout(book, book_data): + ns = { + 'n': 'urn:oasis:names:tc:opendocument:xmlns:container', + 'pkg': 'http://www.idpf.org/2007/opf', + } + + file_path = os.path.normpath(os.path.join(config.config_calibre_dir, book.path, book_data.name + "." + book_data.format.lower())) + + epubZip = zipfile.ZipFile(file_path) + + txt = epubZip.read('META-INF/container.xml') + tree = etree.fromstring(txt) + cfname = tree.xpath('n:rootfiles/n:rootfile/@full-path', namespaces=ns)[0] + cf = epubZip.read(cfname) + tree = etree.fromstring(cf) + p = tree.xpath('/pkg:package/pkg:metadata', namespaces=ns)[0] + + layout = p.xpath('pkg:meta[@property="rendition:layout"]/text()', namespaces=ns) + + if len(layout) == 0: + return None + else: + return layout[0] + def get_epub_info(tmp_file_path, original_file_name, original_file_extension): ns = { diff --git a/cps/kobo.py b/cps/kobo.py index ef9a9476..0412ae17 100644 --- a/cps/kobo.py +++ b/cps/kobo.py @@ -49,6 +49,7 @@ import requests from . import config, logger, kobo_auth, db, calibre_db, helper, shelf as shelf_lib, ub, csrf, kobo_sync_status +from .epub import get_epub_layout from .constants import sqlalchemy_version2 from .helper import get_download_link from .services import SyncToken as SyncToken @@ -455,6 +456,8 @@ def get_metadata(book): continue for kobo_format in KOBO_FORMATS[book_data.format]: # log.debug('Id: %s, Format: %s' % (book.id, kobo_format)) + if get_epub_layout(book, book_data) == 'pre-paginated': + kobo_format = 'EPUB3FL' download_urls.append( { "Format": kobo_format, From bf12542df5cb36e2b266f66f92b31f1b609217a2 Mon Sep 17 00:00:00 2001 From: GarcaMan Date: Sun, 5 Jun 2022 19:56:34 +0000 Subject: [PATCH 06/11] Updated Rotate Left/Right shortcut funtions to update inmediatly Minor fixes --- cps/static/js/kthoom.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/cps/static/js/kthoom.js b/cps/static/js/kthoom.js index 1a3bcb89..7a9de8f4 100644 --- a/cps/static/js/kthoom.js +++ b/cps/static/js/kthoom.js @@ -388,8 +388,9 @@ function setImage(url, _canvas) { } } +// reloadImages is a slow process when multiple images are involved. Only used when rotating/mirroring function reloadImages() { - for(i=0; imageFiles.length; i++) { + for(i=0; i < imageFiles.length; i++) { setImage(imageFiles[i].dataURI, $(".mainImage")[i]); } } @@ -511,6 +512,7 @@ function keyHandler(evt) { settings.rotateTimes = 3; } updatePage(); + reloadImages(); break; case kthoom.Key.R: if (hasModifier) break; @@ -519,6 +521,7 @@ function keyHandler(evt) { settings.rotateTimes = 0; } updatePage(); + reloadImages(); break; case kthoom.Key.F: if (hasModifier) break; @@ -534,6 +537,7 @@ function keyHandler(evt) { settings.hflip = true; } updatePage(); + reloadImages(); break; case kthoom.Key.W: if (hasModifier) break; @@ -663,8 +667,7 @@ function init(filename) { settings[this.name] = value; if(["hflip", "vflip", "rotateTimes"].includes(this.name)) { - //reloadImages is a slow process when multiple images are involved. Only used when rotating/mirroring - reloadImages(); + reloadImages(); } else if(this.name === "direction") { return updateProgress(); } @@ -676,6 +679,7 @@ function init(filename) { // Close modal $(".closer, .overlay").click(function() { $(".md-show").removeClass("md-show"); + $("#mainContent").focus(); // focus back on the main container so you use up/down keys without having to click on it }); // TOC thumbnail pagination @@ -688,7 +692,7 @@ function init(filename) { if (typeof screenfull !== "undefined") { $("#fullscreen").click(function() { screenfull.toggle($("#container")[0]); - // Focus so you can use up/down keys immediately after fullscreen + // Focus on main container so you can use up/down keys immediately after fullscreen $("#mainContent").focus(); }); @@ -749,7 +753,7 @@ function init(filename) { } }); - //Scrolling up/down will update current image if a new image is into view (for Long Strip Display) + // Scrolling up/down will update current image if a new image is into view (for Long Strip Display) $("#mainContent").scroll(function(){ var scroll = $("#mainContent").scrollTop(); if(settings.pageDisplay === 0) { From 631496775eccf9d93c3e6266559c7dfce32a844b Mon Sep 17 00:00:00 2001 From: jvoisin Date: Sun, 25 Dec 2022 16:37:58 +0100 Subject: [PATCH 07/11] Minor code refactorisation of epub.py - Reduce the amount of nested indentation - Use proper functions instead of fragile manual parsing --- cps/epub.py | 87 +++++++++++++++++++++++++---------------------------- 1 file changed, 41 insertions(+), 46 deletions(-) diff --git a/cps/epub.py b/cps/epub.py index 00ce907b..8703d2a6 100644 --- a/cps/epub.py +++ b/cps/epub.py @@ -28,18 +28,18 @@ from .constants import BookMeta def _extract_cover(zip_file, cover_file, cover_path, tmp_file_name): if cover_file is None: return None - else: - cf = extension = None - zip_cover_path = os.path.join(cover_path, cover_file).replace('\\', '/') - prefix = os.path.splitext(tmp_file_name)[0] - tmp_cover_name = prefix + '.' + os.path.basename(zip_cover_path) - ext = os.path.splitext(tmp_cover_name) - if len(ext) > 1: - extension = ext[1].lower() - if extension in cover.COVER_EXTENSIONS: - cf = zip_file.read(zip_cover_path) - return cover.cover_processing(tmp_file_name, cf, extension) + cf = extension = None + zip_cover_path = os.path.join(cover_path, cover_file).replace('\\', '/') + + prefix = os.path.splitext(tmp_file_name)[0] + tmp_cover_name = prefix + '.' + os.path.basename(zip_cover_path) + ext = os.path.splitext(tmp_cover_name) + if len(ext) > 1: + extension = ext[1].lower() + if extension in cover.COVER_EXTENSIONS: + cf = zip_file.read(zip_cover_path) + return cover.cover_processing(tmp_file_name, cf, extension) def get_epub_info(tmp_file_path, original_file_name, original_file_extension): @@ -87,11 +87,7 @@ def get_epub_info(tmp_file_path, original_file_name, original_file_extension): epub_metadata['date'] = '' if epub_metadata['description'] == u'Unknown': - description = tree.xpath("//*[local-name() = 'description']/text()") - if len(description) > 0: - epub_metadata['description'] = description - else: - epub_metadata['description'] = "" + epub_metadata['description'] = tree.xpath("//*[local-name() = 'description']/text()") lang = epub_metadata['language'].split('-', 1)[0].lower() epub_metadata['language'] = isoLanguages.get_lang3(lang) @@ -131,41 +127,40 @@ def get_epub_info(tmp_file_path, original_file_name, original_file_extension): def parse_epub_cover(ns, tree, epub_zip, cover_path, tmp_file_path): cover_section = tree.xpath("/pkg:package/pkg:manifest/pkg:item[@id='cover-image']/@href", namespaces=ns) - cover_file = None - # if len(cover_section) > 0: for cs in cover_section: cover_file = _extract_cover(epub_zip, cs, cover_path, tmp_file_path) if cover_file: - break - if not cover_file: - meta_cover = tree.xpath("/pkg:package/pkg:metadata/pkg:meta[@name='cover']/@content", namespaces=ns) - if len(meta_cover) > 0: + return cover_file + + meta_cover = tree.xpath("/pkg:package/pkg:metadata/pkg:meta[@name='cover']/@content", namespaces=ns) + if len(meta_cover) > 0: + cover_section = tree.xpath( + "/pkg:package/pkg:manifest/pkg:item[@id='"+meta_cover[0]+"']/@href", namespaces=ns) + if not cover_section: cover_section = tree.xpath( - "/pkg:package/pkg:manifest/pkg:item[@id='"+meta_cover[0]+"']/@href", namespaces=ns) - if not cover_section: - cover_section = tree.xpath( - "/pkg:package/pkg:manifest/pkg:item[@properties='" + meta_cover[0] + "']/@href", namespaces=ns) + "/pkg:package/pkg:manifest/pkg:item[@properties='" + meta_cover[0] + "']/@href", namespaces=ns) + else: + cover_section = tree.xpath("/pkg:package/pkg:guide/pkg:reference/@href", namespaces=ns) + + cover_file = None + for cs in cover_section: + if cs.endswith('.xhtml') or s.endswith('.html'): + markup = epub_zip.read(os.path.join(cover_path, cs)) + markup_tree = etree.fromstring(markup) + # no matter xhtml or html with no namespace + img_src = markup_tree.xpath("//*[local-name() = 'img']/@src") + # Alternative image source + if not len(img_src): + img_src = markup_tree.xpath("//attribute::*[contains(local-name(), 'href')]") + if len(img_src): + # img_src maybe start with "../"" so fullpath join then relpath to cwd + filename = os.path.relpath(os.path.join(os.path.dirname(os.path.join(cover_path, cover_section[0])), + img_src[0])) + cover_file = _extract_cover(epub_zip, filename, "", tmp_file_path) else: - cover_section = tree.xpath("/pkg:package/pkg:guide/pkg:reference/@href", namespaces=ns) - for cs in cover_section: - filetype = cs.rsplit('.', 1)[-1] - if filetype == "xhtml" or filetype == "html": # if cover is (x)html format - markup = epub_zip.read(os.path.join(cover_path, cs)) - markup_tree = etree.fromstring(markup) - # no matter xhtml or html with no namespace - img_src = markup_tree.xpath("//*[local-name() = 'img']/@src") - # Alternative image source - if not len(img_src): - img_src = markup_tree.xpath("//attribute::*[contains(local-name(), 'href')]") - if len(img_src): - # img_src maybe start with "../"" so fullpath join then relpath to cwd - filename = os.path.relpath(os.path.join(os.path.dirname(os.path.join(cover_path, cover_section[0])), - img_src[0])) - cover_file = _extract_cover(epub_zip, filename, "", tmp_file_path) - else: - cover_file = _extract_cover(epub_zip, cs, cover_path, tmp_file_path) - if cover_file: break - return cover_file + cover_file = _extract_cover(epub_zip, cs, cover_path, tmp_file_path) + if cover_file: + return cover_file def parse_epub_series(ns, tree, epub_metadata): From 3f72c3fffe18f4977bbed66f4084bd0a4ef83660 Mon Sep 17 00:00:00 2001 From: _Fervor_ <87570146+UFervor@users.noreply.github.com> Date: Fri, 3 Feb 2023 23:31:49 +0800 Subject: [PATCH 08/11] Update web.py --- cps/web.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/cps/web.py b/cps/web.py index 3a575d9e..5fbf9c07 100644 --- a/cps/web.py +++ b/cps/web.py @@ -1159,7 +1159,14 @@ def serve_book(book_id, book_format, anyname): data = calibre_db.get_book_format(book_id, book_format.upper()) if not data: return "File not in Database" - log.info('Serving book: %s', data.name) + + range_header = request.headers.get('Range', None) + if not range_header: + log.info('Serving book: %s', data.name) + response = make_response(send_from_directory(os.path.join(config.config_calibre_dir, book.path), data.name + "." + book_format)) + response.headers['Accept-Ranges'] = 'bytes' + return response + if config.config_use_google_drive: try: headers = Headers() From 6da7d05c6c38ae00613fdee7ab0621ef5363ba42 Mon Sep 17 00:00:00 2001 From: _Fervor_ <87570146+UFervor@users.noreply.github.com> Date: Fri, 3 Feb 2023 23:34:55 +0800 Subject: [PATCH 09/11] Update readpdf.html --- cps/templates/readpdf.html | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cps/templates/readpdf.html b/cps/templates/readpdf.html index e2876c61..2eb9b73d 100644 --- a/cps/templates/readpdf.html +++ b/cps/templates/readpdf.html @@ -43,7 +43,9 @@ See https://github.com/adobe-type-tools/cmap-resources From 7ff4747f6395b9118b49a8281f14d348c39d75e4 Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Mon, 27 Feb 2023 13:09:18 +0100 Subject: [PATCH 11/11] Bugfix after merge --- cps/epub.py | 2 - cps/kobo.py | 28 +- test/Calibre-Web TestSummary_Linux.html | 490 ++++++++++-------------- 3 files changed, 212 insertions(+), 308 deletions(-) diff --git a/cps/epub.py b/cps/epub.py index 61a9d85c..c22bad7b 100644 --- a/cps/epub.py +++ b/cps/epub.py @@ -47,11 +47,9 @@ def get_epub_layout(book, book_data): 'n': 'urn:oasis:names:tc:opendocument:xmlns:container', 'pkg': 'http://www.idpf.org/2007/opf', } - file_path = os.path.normpath(os.path.join(config.config_calibre_dir, book.path, book_data.name + "." + book_data.format.lower())) epubZip = zipfile.ZipFile(file_path) - txt = epubZip.read('META-INF/container.xml') tree = etree.fromstring(txt) cfname = tree.xpath('n:rootfiles/n:rootfile/@full-path', namespaces=ns)[0] diff --git a/cps/kobo.py b/cps/kobo.py index 5cbc1844..de5d3235 100644 --- a/cps/kobo.py +++ b/cps/kobo.py @@ -21,6 +21,7 @@ import base64 import datetime import os import uuid +import zipfile from time import gmtime, strftime import json from urllib.parse import unquote @@ -460,18 +461,21 @@ def get_metadata(book): continue for kobo_format in KOBO_FORMATS[book_data.format]: # log.debug('Id: %s, Format: %s' % (book.id, kobo_format)) - if get_epub_layout(book, book_data) == 'pre-paginated': - kobo_format = 'EPUB3FL' - download_urls.append( - { - "Format": kobo_format, - "Size": book_data.uncompressed_size, - "Url": get_download_url_for_book(book, book_data.format), - # The Kobo forma accepts platforms: (Generic, Android) - "Platform": "Generic", - # "DrmType": "None", # Not required - } - ) + try: + if get_epub_layout(book, book_data) == 'pre-paginated': + kobo_format = 'EPUB3FL' + download_urls.append( + { + "Format": kobo_format, + "Size": book_data.uncompressed_size, + "Url": get_download_url_for_book(book, book_data.format), + # The Kobo forma accepts platforms: (Generic, Android) + "Platform": "Generic", + # "DrmType": "None", # Not required + } + ) + except (zipfile.BadZipfile, FileNotFoundError) as e: + log.error(e) book_uuid = book.uuid metadata = { diff --git a/test/Calibre-Web TestSummary_Linux.html b/test/Calibre-Web TestSummary_Linux.html index 0daf4ca3..e4d0ca4e 100644 --- a/test/Calibre-Web TestSummary_Linux.html +++ b/test/Calibre-Web TestSummary_Linux.html @@ -37,20 +37,20 @@
-

Start Time: 2023-02-21 20:53:24

+

Start Time: 2023-02-26 18:55:48

-

Stop Time: 2023-02-22 03:08:32

+

Stop Time: 2023-02-27 01:05:54

-

Duration: 5h 18 min

+

Duration: 5h 13 min

@@ -936,11 +936,11 @@ - + TestEditBooks 36 - 34 - 1 + 35 + 0 0 1 @@ -1228,34 +1228,11 @@ - +
TestEditBooks - test_upload_book_epub
- -
- FAIL -
- - - - + PASS @@ -1630,13 +1607,13 @@ AssertionError: 'book9' != 'book' - + TestLoadMetadata 1 - 0 - 0 1 0 + 0 + 0 Detail @@ -1644,31 +1621,11 @@ AssertionError: 'book9' != 'book' - +
TestLoadMetadata - test_load_metadata
- -
- ERROR -
- - - - + PASS @@ -1840,9 +1797,9 @@ IndexError: list index out of range
Traceback (most recent call last):
-  File "/home/ozzie/Development/calibre-web-test/test/test_edit_ebooks_gdrive.py", line 185, in test_edit_title
-    self.assertEqual('The camicdemo', books[1][8]['title'])
-IndexError: list index out of range
+ File "/home/ozzie/Development/calibre-web-test/test/test_edit_ebooks_gdrive.py", line 230, in test_edit_title + self.assertEqual(ele.text, u'Very long extra super turbo cool title without any issue of displaying including ö utf-8 characters') +AttributeError: 'bool' object has no attribute 'text'
@@ -2197,11 +2154,11 @@ IndexError: list index out of range - + TestKoboSync 11 - 10 - 1 + 11 + 0 0 0 @@ -2301,48 +2258,21 @@ IndexError: list index out of range - +
TestKoboSync - test_sync_upload
- -
- FAIL -
- - - - + PASS - + TestKoboSyncBig 6 - 6 0 + 6 0 0 @@ -2352,56 +2282,188 @@ First differing element 0: - +
TestKoboSyncBig - test_download_cover
- PASS + +
+ FAIL +
+ + + + - +
TestKoboSyncBig - test_kobo_sync_multi_user
- PASS + +
+ FAIL +
+ + + + - +
TestKoboSyncBig - test_kobo_sync_selected_shelves
- PASS + +
+ FAIL +
+ + + + - +
TestKoboSyncBig - test_sync_changed_book
- PASS + +
+ FAIL +
+ + + + - +
TestKoboSyncBig - test_sync_reading_state
- PASS + +
+ FAIL +
+ + + + - +
TestKoboSyncBig - test_sync_shelf
- PASS + +
+ FAIL +
+ + + + @@ -3270,11 +3332,11 @@ First differing element 0: - + TestReader 5 - 3 - 2 + 5 + 0 0 0 @@ -3284,31 +3346,11 @@ First differing element 0: - +
TestReader - test_comic_reader
- -
- FAIL -
- - - - + PASS @@ -3340,31 +3382,11 @@ AssertionError: False is not true - +
TestReader - test_txt_reader
- -
- FAIL -
- - - - + PASS @@ -3723,11 +3745,11 @@ AssertionError: False is not true : Encoding of textfile viewer is not respected - + TestThumbnails 8 - 3 - 4 + 7 + 0 0 1 @@ -3746,31 +3768,11 @@ AssertionError: False is not true : Encoding of textfile viewer is not respected - +
TestThumbnails - test_cache_of_deleted_book
- -
- FAIL -
- - - - + PASS @@ -3784,31 +3786,11 @@ AssertionError: 0 != 2 - +
TestThumbnails - test_cover_change_on_upload_new_cover
- -
- FAIL -
- - - - + PASS @@ -3822,31 +3804,11 @@ AssertionError: 0.02977373520222651 not greater than or equal to 0.03 - +
TestThumbnails - test_cover_on_upload_book
- -
- FAIL -
- - - - + PASS @@ -3860,31 +3822,11 @@ AssertionError: 222 != 220 - +
TestThumbnails - test_sideloaded_book
- -
- FAIL -
- - - - + PASS @@ -4003,11 +3945,11 @@ AssertionError: 0.0 not greater than or equal to 0.04 - + TestUploadEPubs 5 - 3 - 2 + 5 + 0 0 0 @@ -4017,60 +3959,20 @@ AssertionError: 0.0 not greater than or equal to 0.04 - +
TestUploadEPubs - test_upload_epub_cover
- -
- FAIL -
- - - - + PASS - +
TestUploadEPubs - test_upload_epub_cover_formats
- -
- FAIL -
- - - - + PASS @@ -4999,9 +4901,9 @@ AssertionError: 0.051106144230518215 != 0.0058 within 0.0001 delta (0.0453061442 Total 425 - 406 - 10 - 2 + 411 + 6 + 1 7   @@ -5450,7 +5352,7 @@ AssertionError: 0.051106144230518215 != 0.0058 within 0.0001 delta (0.0453061442