Better handling of renamed files in monitored directory

When a file in the monitored directory is renamed, it is better to
remove the old file from the library before adding the new one.
Doing it the other way around will not work correctly - adding the new file
will fail since a book with that UUID is already present in the library,
and then that book will be removed!
This commit is contained in:
Veloman Yunkan 2024-07-22 18:31:32 +04:00 committed by Kelson
parent 58e8f71dda
commit d0f7599cf6

View File

@ -878,6 +878,12 @@ void ContentManager::updateLibraryFromDir(QString monitorDir)
const auto kiwixLib = mp_library->getKiwixLibrary(); const auto kiwixLib = mp_library->getKiwixLibrary();
kiwix::Manager manager(kiwixLib); kiwix::Manager manager(kiwixLib);
bool needsRefresh = !removedZims.empty(); bool needsRefresh = !removedZims.empty();
for (auto bookPath : removedZims) {
try {
const auto book = kiwixLib->getBookByPath(bookPath.toStdString());
mp_library->removeBookFromLibraryById(QString::fromStdString(book.getId()));
} catch (...) {}
}
for (auto bookPath : addedZims) { for (auto bookPath : addedZims) {
if ( mp_library->isBeingDownloadedByUs(bookPath) ) { if ( mp_library->isBeingDownloadedByUs(bookPath) ) {
// qDebug() << "DBG: ContentManager::updateLibraryFromDir(): " // qDebug() << "DBG: ContentManager::updateLibraryFromDir(): "
@ -887,12 +893,6 @@ void ContentManager::updateLibraryFromDir(QString monitorDir)
needsRefresh |= manager.addBookFromPath(bookPath.toStdString()); needsRefresh |= manager.addBookFromPath(bookPath.toStdString());
} }
} }
for (auto bookPath : removedZims) {
try {
const auto book = kiwixLib->getBookByPath(bookPath.toStdString());
mp_library->removeBookFromLibraryById(QString::fromStdString(book.getId()));
} catch (...) {}
}
if (needsRefresh) { if (needsRefresh) {
emit(booksChanged()); emit(booksChanged());
setMonitorDirZims(monitorDir, newDirEntries); setMonitorDirZims(monitorDir, newDirEntries);