1 /**
2 * \file downloadclient.cpp
3 * Client to download via http.
4 *
5 * \b Project: Kid3
6 * \author Urs Fleisch
7 * \date 12 Jun 2011
8 *
9 * Copyright (C) 2011-2018 Urs Fleisch
10 *
11 * This file is part of Kid3.
12 *
13 * Kid3 is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * Kid3 is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 */
26
27 #include "downloadclient.h"
28 #include <QRegularExpression>
29 #include "importconfig.h"
30
31 /**
32 * Constructor.
33 *
34 * @param netMgr network access manager
35 */
DownloadClient(QNetworkAccessManager * netMgr)36 DownloadClient::DownloadClient(QNetworkAccessManager* netMgr)
37 : HttpClient(netMgr), m_canceled(false)
38 {
39 connect(this, &HttpClient::bytesReceived,
40 this, &DownloadClient::requestFinished);
41 }
42
43 /**
44 * Send a download request.
45 *
46 * @param url URL of resource to download
47 */
startDownload(const QUrl & url)48 void DownloadClient::startDownload(const QUrl& url)
49 {
50 m_canceled = false;
51 m_url = url;
52 emit downloadStarted(m_url.toString());
53 emit progress(tr("Ready."), 0, 0);
54 sendRequest(m_url);
55 }
56
57 /**
58 * Cancel a download.
59 */
cancelDownload()60 void DownloadClient::cancelDownload()
61 {
62 m_canceled = true;
63 abort();
64 emit aborted();
65 }
66
67 /**
68 * Handle response when request is finished.
69 * downloadFinished() is emitted.
70 *
71 * @param data received data
72 */
requestFinished(const QByteArray & data)73 void DownloadClient::requestFinished(const QByteArray& data)
74 {
75 if (!m_canceled) {
76 emit downloadFinished(data, getContentType(), m_url.toString());
77 }
78 }
79
80 /**
81 * Get the URL of an image file.
82 * The input URL is transformed using the match picture URL table to
83 * get the URL of an image file.
84 *
85 * @param url URL from image drag
86 *
87 * @return URL of image file, empty if no image URL found.
88 */
getImageUrl(const QUrl & url)89 QUrl DownloadClient::getImageUrl(const QUrl& url)
90 {
91 QString urlStr = url.toString();
92 if (urlStr.endsWith(QLatin1String(".jpg"), Qt::CaseInsensitive) ||
93 urlStr.endsWith(QLatin1String(".jpeg"), Qt::CaseInsensitive) ||
94 urlStr.endsWith(QLatin1String(".png"), Qt::CaseInsensitive))
95 return url;
96
97 QUrl imgurl;
98 QList<QPair<QString, QString>> urlMap =
99 ImportConfig::instance().matchPictureUrlMap();
100 for (auto it = urlMap.constBegin(); it != urlMap.constEnd(); ++it) {
101 QRegularExpression re(it->first);
102 #if QT_VERSION >= 0x060000
103 auto match = re.match(
104 urlStr, 0, QRegularExpression::NormalMatch,
105 QRegularExpression::AnchorAtOffsetMatchOption);
106 #else
107 auto match = re.match(
108 urlStr, 0, QRegularExpression::NormalMatch,
109 QRegularExpression::AnchoredMatchOption);
110 #endif
111 if (match.hasMatch()) {
112 QString newUrl = urlStr;
113 newUrl.replace(re, it->second);
114 if (newUrl.indexOf(QLatin1String("%25")) != -1) {
115 // double URL encoded: first decode
116 newUrl = QUrl::fromPercentEncoding(newUrl.toUtf8());
117 }
118 if (newUrl.indexOf(QLatin1String("%2F")) != -1) {
119 // URL encoded: decode
120 newUrl = QUrl::fromPercentEncoding(newUrl.toUtf8());
121 }
122 imgurl.setUrl(newUrl);
123 break;
124 }
125 }
126 return imgurl;
127 }
128