Commit cafbf857e65c9ffbd03e1d5a238ca91d601071cb

Authored by Josh Klontz
1 parent 48df2ef8

a more robust br-download implementation that supports local files

Showing 1 changed file with 35 additions and 15 deletions
app/br-download/br-download.cpp
@@ -50,44 +50,64 @@ static bool json = false; @@ -50,44 +50,64 @@ static bool json = false;
50 static bool permissive = false; 50 static bool permissive = false;
51 static bool url_provided = false; 51 static bool url_provided = false;
52 52
53 -static bool processReply(QNetworkReply* reply) 53 +static void process(QString url, QNetworkAccessManager &nam)
54 { 54 {
55 - while (!reply->isFinished())  
56 - QCoreApplication::processEvents();  
57 - const QByteArray data = reply->readAll();  
58 - reply->deleteLater(); 55 + if (url.isEmpty())
  56 + return;
  57 +
  58 + if (url.startsWith("file://"))
  59 + url = url.mid(7);
  60 +
  61 + QIODevice *device = NULL;
  62 + if (QFileInfo(url).exists()) {
  63 + device = new QFile(url);
  64 + device->open(QIODevice::ReadOnly);
  65 + } else {
  66 + QNetworkReply *reply = nam.get(QNetworkRequest(url));
  67 + while (!reply->isFinished())
  68 + QCoreApplication::processEvents();
  69 +
  70 + if (reply->error() != QNetworkReply::NoError) {
  71 + qDebug() << reply->errorString();
  72 + reply->deleteLater();
  73 + } else {
  74 + device = reply;
  75 + }
  76 + }
  77 +
  78 + if (!device)
  79 + return;
  80 +
  81 + const QByteArray data = device->readAll();
  82 + device->deleteLater();
59 83
60 if (!permissive && imdecode(Mat(1, data.size(), CV_8UC1, (void*)data.data()), IMREAD_ANYDEPTH | IMREAD_ANYCOLOR).empty()) 84 if (!permissive && imdecode(Mat(1, data.size(), CV_8UC1, (void*)data.data()), IMREAD_ANYDEPTH | IMREAD_ANYCOLOR).empty())
61 - return false; 85 + return;
62 86
63 const QByteArray hash = QCryptographicHash::hash(data, QCryptographicHash::Md5); 87 const QByteArray hash = QCryptographicHash::hash(data, QCryptographicHash::Md5);
64 br_append_utemplate_contents(stdout, reinterpret_cast<const unsigned char*>(hash.data()), reinterpret_cast<const unsigned char*>(hash.data()), 3, data.size(), reinterpret_cast<const unsigned char*>(data.data())); 88 br_append_utemplate_contents(stdout, reinterpret_cast<const unsigned char*>(hash.data()), reinterpret_cast<const unsigned char*>(hash.data()), 3, data.size(), reinterpret_cast<const unsigned char*>(data.data()));
65 - return true;  
66 } 89 }
67 90
68 int main(int argc, char *argv[]) 91 int main(int argc, char *argv[])
69 { 92 {
70 QCoreApplication application(argc, argv); 93 QCoreApplication application(argc, argv);
71 -  
72 - QNetworkAccessManager networkAccessManager; 94 + QNetworkAccessManager nam;
73 95
74 for (int i=1; i<argc; i++) { 96 for (int i=1; i<argc; i++) {
75 if (!strcmp(argv[i], "-help" )) { help(); exit(EXIT_SUCCESS); } 97 if (!strcmp(argv[i], "-help" )) { help(); exit(EXIT_SUCCESS); }
76 else if (!strcmp(argv[i], "-json" )) json = true; 98 else if (!strcmp(argv[i], "-json" )) json = true;
77 else if (!strcmp(argv[i], "-permissive")) permissive = true; 99 else if (!strcmp(argv[i], "-permissive")) permissive = true;
78 - else { url_provided = processReply(networkAccessManager.get(QNetworkRequest(QUrl(argv[i])))); } 100 + else { url_provided = true; process(argv[i], nam); }
79 } 101 }
80 102
81 if (!url_provided) { 103 if (!url_provided) {
82 QFile file; 104 QFile file;
83 file.open(stdin, QFile::ReadOnly); 105 file.open(stdin, QFile::ReadOnly);
84 -  
85 while (!file.atEnd()) { 106 while (!file.atEnd()) {
86 const QByteArray line = file.readLine(); 107 const QByteArray line = file.readLine();
87 - const QString url = json ? QJsonDocument::fromJson(line).object().value("URL").toString()  
88 - : QString::fromLocal8Bit(line);  
89 - if (!url.isEmpty())  
90 - processReply(networkAccessManager.get(QNetworkRequest(url))); 108 + process(json ? QJsonDocument::fromJson(line).object().value("URL").toString()
  109 + : QString::fromLocal8Bit(line),
  110 + nam);
91 } 111 }
92 } 112 }
93 113