Skip to content

Commit

Permalink
change: correct pagination; fix: download socket closed prematurely
Browse files Browse the repository at this point in the history
  • Loading branch information
khanhas committed Feb 6, 2021
1 parent b56fc0b commit c81b07e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 40 deletions.
23 changes: 13 additions & 10 deletions backend/download.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { writeFile, copyFile, existsSync, mkdirSync, createWriteStream } = require("fs");
const { writeFileSync, copyFileSync, existsSync, mkdirSync, createWriteStream, constants } = require("fs");
const { join: pathJoin, extname, basename } = require("path");
const { v4 } = require("uuid");
const { domain, fetchOptions, additionalBookLocation } = require("./common");
Expand All @@ -25,6 +25,8 @@ module.exports = function (args, socket) {
const nameMatch = disposition.match(/filename="(.+)"/);
if (nameMatch) {
let name = nameMatch[1].replace(" (z-lib.org)", "");
// Convert multi-byte chars to their true representation
name = Buffer.from(name, "binary").toString("utf8");

fileExt = extname(name);
fileName = basename(name, fileExt);
Expand All @@ -47,7 +49,7 @@ module.exports = function (args, socket) {

response.body.on("data", (chunk) => {
fileStream.write(chunk);
socket.write("PROG:" + Math.floor(fileStream.bytesWritten / fileLength * 100).toString() + "\n");
socket.write("PROG:" + Math.floor(fileStream.bytesWritten / fileLength * 95).toString() + "\n");
});

response.body.on("error", (error) => {
Expand All @@ -56,13 +58,10 @@ module.exports = function (args, socket) {
});

response.body.on('end', () => {
socket.write("PROG:100\n");
socket.end();

console.log("DOWNLOAD DONE");
socket.write("DOWNLOAD DONE\n");

if (fileExt == ".epub" || fileExt == ".pdf") {
writeFile(xochitlFolder + uuid + ".metadata", JSON.stringify({
writeFileSync(xochitlFolder + uuid + ".metadata", JSON.stringify({
"deleted": false,
"lastModified": "1",
"lastOpenedPage": 0,
Expand All @@ -74,17 +73,21 @@ module.exports = function (args, socket) {
"type": "DocumentType",
"version": 1,
"visibleName": fileName
}), () => { });
}));

copyFile(tempFilePath, pathJoin(xochitlFolder, uuid + fileExt), () => { console.log("COPIED XOCHITL") });
copyFileSync(tempFilePath, pathJoin(xochitlFolder, uuid + fileExt), constants.COPYFILE_FICLONE);
socket.write("COPIED XOCHITL\n");
}

if (additionalBookLocation) {
if (!existsSync(additionalBookLocation)) {
mkdirSync(additionalBookLocation, { recursive: true });
}
copyFile(tempFilePath, pathJoin(additionalBookLocation, fileName + fileExt), () => { console.log("COPIED LOCAL") });
copyFileSync(tempFilePath, pathJoin(additionalBookLocation, fileName + fileExt), constants.COPYFILE_FICLONE);
socket.write("COPIED LOCAL\n");
}
socket.write("PROG:100\n");
socket.end();
});
})
.catch(err => {
Expand Down
5 changes: 1 addition & 4 deletions backend/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,7 @@ module.exports = function (args, socket) {
let totalItems = $(".totalCounter")
if (totalItems) {
totalItems = parseInt(totalItems.text().replace(/[()+]/g, ""));
if (totalItems > 50) {
const totalPages = Math.ceil(totalItems / 50);
socket.write("TOTAL:" + totalPages.toString() + "\n");
}
socket.write("TOTAL:" + totalItems.toString() + "\n");
}

const books = $(".resItemBoxBooks").get().map(ele => {
Expand Down
5 changes: 2 additions & 3 deletions qml/Main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ Rectangle {
flow: GridView.TopToBottom
clip: true
snapMode: GridView.SnapToRow
// pixelAligned: true
flickDeceleration: 0
onMovementEnded: currentIndex = indexAt(contentX, 0)

Expand Down Expand Up @@ -247,7 +246,7 @@ Rectangle {
anchors.leftMargin: screenMargin
anchors.bottomMargin: screenMargin + 10
Repeater {
model: store.totalPages
model: store.pages
Rectangle {
width: 100; height: 60
Rectangle {
Expand All @@ -259,7 +258,7 @@ Rectangle {
color: "white"
}
Text {
text: (index + 1) * 50
text: modelData
anchors.centerIn: bg
anchors.verticalCenterOffset: 2
horizontalAlignment: Text.AlignHCenter
Expand Down
51 changes: 31 additions & 20 deletions store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,18 @@ Store::Store() : rootView(rootObject()), context(rootContext())
if (stat.startsWith("TOTAL:"))
{
// Hacky trick to force redrawing Repeater
_totalPages = 0;
emit totalPagesChanged();
_totalPages = stat.mid(6).trimmed().toInt();
emit totalPagesChanged();
QStringList pagesList;
setProperty("pages", QVariant::fromValue(pagesList));

int totalItems = stat.mid(6).trimmed().toInt();
if (totalItems < 50)
return;

for (int i = 50; i < totalItems; i += 50)
pagesList.push_back(QString::number(i));

pagesList.push_back(QString::number(totalItems));
setProperty("pages", QVariant::fromValue(pagesList));
}
});
connect(worker, &Worker::updateProgress, this, [this](int prog) {
Expand Down Expand Up @@ -294,18 +302,25 @@ bool Store::setConfig()

void Store::download(Book* book)
{
book->worker = new Worker({"DOWN", book->_dlUrl});
connect(book->worker, &Worker::updateProgress, book, &Book::updateProgress);
connect(book->worker, &Worker::updateStatus, book, [book](QString stat) {
qDebug() << "LOG: " << stat;
if (stat.startsWith("ERR:"))
{
book->setProperty("status", QVariant(stat.trimmed()));
book->worker->terminate();
delete book->worker;
}
});
connect(book->worker, &Worker::socketClosed, book, [book]() { delete book->worker; });
if (book->worker == nullptr) {
book->worker = new Worker({"DOWN", book->_dlUrl});
connect(book->worker, &Worker::updateProgress, book, &Book::updateProgress);
connect(book->worker, &Worker::updateStatus, book, [book](QString stat) {
qDebug() << "LOG: " << stat;
if (stat.startsWith("ERR:"))
{
book->setProperty("status", QVariant(stat.trimmed()));
book->worker->terminate();
delete book->worker;
}
});
connect(book->worker, &Worker::socketClosed, book, [book]() {
if (infoThread != nullptr && !infoThread->isRunning())
{
infoThread->work();
}
});
}

book->setParent(nullptr);
_downloadList.prepend(book);
Expand Down Expand Up @@ -379,10 +394,6 @@ void Book::updateProgress(int prog)
if (prog == 100)
{
setProperty("status", QVariant("Downloaded"));
if (infoThread != nullptr && !infoThread->isRunning())
{
infoThread->work();
}
return;
}
setProperty("status", QVariant(QString::number(prog) + "%"));
Expand Down
6 changes: 3 additions & 3 deletions store.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class Store : public QQuickView
Q_PROPERTY(QString order MEMBER _order)
Q_PROPERTY(QString query MEMBER _query)
Q_PROPERTY(QString accountStatus MEMBER _accountStatus NOTIFY accountStatusChanged)
Q_PROPERTY(int totalPages MEMBER _totalPages NOTIFY totalPagesChanged)
Q_PROPERTY(QStringList pages MEMBER _pages NOTIFY pagesChanged)
Q_PROPERTY(int currentPage MEMBER _currentPage NOTIFY currentPageChanged)

Store();
Expand All @@ -89,7 +89,7 @@ public slots:
void downloadListChanged();
void isBusyChanged();
void accountStatusChanged();
void totalPagesChanged();
void pagesChanged();
void currentPageChanged();

private:
Expand All @@ -111,7 +111,7 @@ public slots:
QString _accountStatus;

bool _cookieAvailable;
int _totalPages = 0;
QStringList _pages = { "10", "20", "30"};
int _currentPage = 0;
};

Expand Down

0 comments on commit c81b07e

Please sign in to comment.