File indexing completed on 2024-05-19 04:56:01

0001 /**
0002  * \file downloadclient.cpp
0003  * Client to download via http.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 12 Jun 2011
0008  *
0009  * Copyright (C) 2011-2018  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #include "downloadclient.h"
0028 #include <QRegularExpression>
0029 #include "importconfig.h"
0030 
0031 /**
0032  * Constructor.
0033  *
0034  * @param netMgr network access manager
0035  */
0036 DownloadClient::DownloadClient(QNetworkAccessManager* netMgr)
0037   : HttpClient(netMgr), m_canceled(false)
0038 {
0039   connect(this, &HttpClient::bytesReceived,
0040           this, &DownloadClient::requestFinished);
0041 }
0042 
0043 /**
0044  * Send a download request.
0045  *
0046  * @param url URL of resource to download
0047  */
0048 void DownloadClient::startDownload(const QUrl& url)
0049 {
0050   m_canceled = false;
0051   m_url = url;
0052   emit downloadStarted(m_url.toString());
0053   emit progress(tr("Ready."), 0, 0);
0054   sendRequest(m_url);
0055 }
0056 
0057 /**
0058  * Cancel a download.
0059  */
0060 void DownloadClient::cancelDownload()
0061 {
0062   m_canceled = true;
0063   abort();
0064   emit aborted();
0065 }
0066 
0067 /**
0068  * Handle response when request is finished.
0069  * downloadFinished() is emitted.
0070  *
0071  * @param data received data
0072  */
0073 void DownloadClient::requestFinished(const QByteArray& data)
0074 {
0075   if (!m_canceled) {
0076     emit downloadFinished(data, getContentType(), m_url.toString());
0077   }
0078 }
0079 
0080 /**
0081  * Get the URL of an image file.
0082  * The input URL is transformed using the match picture URL table to
0083  * get the URL of an image file.
0084  *
0085  * @param url URL from image drag
0086  *
0087  * @return URL of image file, empty if no image URL found.
0088  */
0089 QUrl DownloadClient::getImageUrl(const QUrl& url)
0090 {
0091   QString urlStr = url.toString();
0092   if (urlStr.endsWith(QLatin1String(".jpg"), Qt::CaseInsensitive) ||
0093       urlStr.endsWith(QLatin1String(".jpeg"), Qt::CaseInsensitive) ||
0094       urlStr.endsWith(QLatin1String(".webp"), Qt::CaseInsensitive) ||
0095       urlStr.endsWith(QLatin1String(".png"), Qt::CaseInsensitive))
0096     return url;
0097 
0098   QUrl imgurl;
0099   QList<QPair<QString, QString>> urlMap =
0100       ImportConfig::instance().matchPictureUrlMap();
0101   for (auto it = urlMap.constBegin(); it != urlMap.constEnd(); ++it) {
0102     QRegularExpression re(it->first);
0103 #if QT_VERSION >= 0x060000
0104     auto match = re.match(
0105           urlStr, 0, QRegularExpression::NormalMatch,
0106           QRegularExpression::AnchorAtOffsetMatchOption);
0107 #else
0108     auto match = re.match(
0109           urlStr, 0, QRegularExpression::NormalMatch,
0110           QRegularExpression::AnchoredMatchOption);
0111 #endif
0112     if (match.hasMatch()) {
0113       QString newUrl = urlStr;
0114       newUrl.replace(re, it->second);
0115       if (newUrl.indexOf(QLatin1String("%25")) != -1) {
0116         // double URL encoded: first decode
0117         newUrl = QUrl::fromPercentEncoding(newUrl.toUtf8());
0118       }
0119       if (newUrl.indexOf(QLatin1String("%2F")) != -1) {
0120         // URL encoded: decode
0121         newUrl = QUrl::fromPercentEncoding(newUrl.toUtf8());
0122       }
0123       imgurl.setUrl(newUrl);
0124       break;
0125     }
0126   }
0127   return imgurl;
0128 }