Library maintains "correct" paths of books being downloaded

When a book is downloaded via a metalink URL the fake path (with a
".beingdownloadedbykiwix" suffix appended to it) recorded for it in
the library could not in general be used to obtain the real name (and,
hence, path) of the ZIM file. Now the book path is "corrected" as soon
as the file name becomes available.
This commit is contained in:
Veloman Yunkan 2024-05-24 16:23:03 +04:00 committed by Kelson
parent b5e3c30d46
commit 77ee3a2936
3 changed files with 20 additions and 2 deletions

View File

@ -564,9 +564,11 @@ void ContentManager::updateDownload(QString bookId, const DownloadInfo& download
{
const auto downloadState = m_downloads.value(bookId);
if ( downloadState ) {
const auto downloadPath = downloadInfo["path"].toString();
if ( downloadInfo["status"].toString() == "completed" ) {
downloadCompleted(bookId, downloadInfo["path"].toString());
downloadCompleted(bookId, downloadPath);
} else {
mp_library->updateBookBeingDownloaded(bookId, downloadPath);
downloadState->update(downloadInfo);
managerModel->updateDownload(bookId);
}

View File

@ -116,7 +116,10 @@ void Library::addBookBeingDownloaded(const kiwix::Book& book, QString downloadDi
const QString downloadUrl = QString::fromStdString(book.getUrl());
// XXX: This works if the URL is a direct link to a ZIM file
// XXX: rather than to a torrent or a metalink file
// XXX: rather than to a torrent or a metalink file. In those cases
// XXX: the file name of the download will be discovered after the
// XXX: the metalink or torrent file is downloaded. Then the real
// XXX: file name of a book must be set via updateBookBeingDownloaded();
const QString fileName = downloadUrl.split('/').back();
const QString path = QDir(downloadDir).absoluteFilePath(fileName);
@ -127,6 +130,18 @@ void Library::addBookBeingDownloaded(const kiwix::Book& book, QString downloadDi
addBookToLibrary(bookCopy);
}
void Library::updateBookBeingDownloaded(const QString& bookId, const QString& bookPath)
{
const kiwix::Book& book = getBookById(bookId);
const auto bookPseudoPath = pseudoPathOfAFileBeingDownloaded(bookPath.toStdString());
if ( bookPseudoPath != book.getPath() ) {
kiwix::Book bookCopy(book);
bookCopy.setPath(bookPseudoPath);
mp_library->addOrUpdateBook(bookCopy);
save();
}
}
bool Library::isBeingDownloadedByUs(QString path) const
{
const auto fakePath = pseudoPathOfAFileBeingDownloaded(path.toStdString());

View File

@ -39,6 +39,7 @@ public:
void addBookToLibrary(kiwix::Book& book);
void addBookBeingDownloaded(const kiwix::Book& book, QString downloadDir);
bool isBeingDownloadedByUs(QString path) const;
void updateBookBeingDownloaded(const QString& bookId, const QString& bookPath);
void removeBookFromLibraryById(const QString& id);
void addBookmark(kiwix::Bookmark& bookmark);
void removeBookmark(const QString& zimId, const QString& url);