Merge pull request #283 from kiwix/remove_download_action

Remove kiwix-manage download action
This commit is contained in:
Kelson 2019-05-03 10:50:11 +02:00 committed by GitHub
commit 4cd74dca4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -21,18 +21,14 @@
#include <unistd.h> #include <unistd.h>
#endif #endif
#include <getopt.h> #include <getopt.h>
#include <kiwix/tools/pathTools.h>
#include <kiwix/tools/stringTools.h> #include <kiwix/tools/stringTools.h>
#include <kiwix/manager.h> #include <kiwix/manager.h>
#include <kiwix/downloader.h>
#include <cstdlib> #include <cstdlib>
#include <iostream> #include <iostream>
#include <time.h>
using namespace std; using namespace std;
enum supportedAction { NONE, ADD, SHOW, REMOVE, DOWNLOAD }; enum supportedAction { NONE, ADD, SHOW, REMOVE };
void show(kiwix::Library* library, const std::string& bookId) void show(kiwix::Library* library, const std::string& bookId)
{ {
@ -190,63 +186,6 @@ bool handle_remove(kiwix::Library* library, const std::string& libraryPath,
return(exitCode); return(exitCode);
} }
bool handle_download(kiwix::Library* library, const std::string& libraryPath,
int argc, char* argv[])
{
std::string bookId;
bool exitCode = false;
if (argc > 3) {
bookId = argv[3];
}
auto& book = library->getBookById(bookId);
auto did = book.getDownloadId();
kiwix::Downloader downloader;
kiwix::Download* download = nullptr;
if (!did.empty()) {
std::cout << "try resume " << did << std::endl;
try {
download = downloader.getDownload(did);
} catch(...) {}
}
if (nullptr == download || download->getStatus() == kiwix::Download::K_UNKNOWN) {
download = downloader.startDownload(book.getUrl());
book.setDownloadId(download->getDid());
}
int step = 60*5;
while (step--) {
download->updateStatus();
if (download->getStatus() == kiwix::Download::K_COMPLETE) {
auto followingId = download->getFollowedBy();
if (followingId.empty()) {
book.setPath(download->getPath());
book.setDownloadId("");
std::cout << "File downloaded to " << book.getPath() << std::endl;
break;
} else {
download = downloader.getDownload(followingId);
}
} else if (download->getStatus() == kiwix::Download::K_ACTIVE) {
std::cout << download->getDid() << " : "
<< kiwix::beautifyFileSize(download->getCompletedLength()) << "/"
<< kiwix::beautifyFileSize(download->getTotalLength())
<< " (" << kiwix::beautifyFileSize(download->getDownloadSpeed()) << "/s) "
<< " [" << kiwix::beautifyFileSize(download->getVerifiedLength()) << "]"
<< "[" << step << "] \n" << std::flush;
} else if (download->getStatus() == kiwix::Download::K_ERROR) {
std::cout << "File Error" << std::endl;
exitCode = true;
break;
}
struct timespec wait = {1, 0};
nanosleep(&wait, nullptr);
}
downloader.close();
return exitCode;
}
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
string libraryPath = ""; string libraryPath = "";
@ -264,8 +203,6 @@ int main(int argc, char** argv)
action = SHOW; action = SHOW;
else if (actionString == "remove" || actionString == "delete") else if (actionString == "remove" || actionString == "delete")
action = REMOVE; action = REMOVE;
else if (actionString == "download")
action = DOWNLOAD;
} }
/* Print usage)) if necessary */ /* Print usage)) if necessary */
@ -293,15 +230,12 @@ int main(int argc, char** argv)
case REMOVE: case REMOVE:
exitCode = handle_remove(&library, libraryPath, argc, argv); exitCode = handle_remove(&library, libraryPath, argc, argv);
break; break;
case DOWNLOAD:
exitCode = handle_download(&library, libraryPath, argc, argv);
break;
case NONE: case NONE:
break; break;
} }
/* Rewrite the library file */ /* Rewrite the library file */
if (action == REMOVE || action == ADD || action == DOWNLOAD) { if (action == REMOVE || action == ADD) {
library.writeToFile(libraryPath); library.writeToFile(libraryPath);
} }