TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
qsyncdownloaddata.cpp
Go to the documentation of this file.
1 #include "qsyncdownloaddata.h"
2 #include "qsyncdatainterface_p.h"
3 
4 #include <QFile>
5 #include <QDateTime>
6 
11 {
12 public:
15  {
16  }
17 
18  QString m_downloadTime;
19  QString m_downloadPath;
20 };
21 
22 
23 
24 QSyncDownloadData::QSyncDownloadData(QNetworkAccessManager *networkManager, QObject *parent)
25  : QSyncDataInterface(TTK_CREATE_PRIVATE(QSyncDownloadData), networkManager, parent)
26 {
28  d->m_manager = networkManager;
29 }
30 
31 void QSyncDownloadData::downloadDataOperator(const QString &time, const QString &bucket, const QString &fileName, const QString &filePath)
32 {
34  d->m_downloadTime = time;
35  d->m_downloadPath = filePath;
36 
37  const QString &method = "GET";
38  const QString &url = TTK_SEPARATOR + fileName;
39  const QString &resource = TTK_SEPARATOR + bucket + url;
40  const QString &host = bucket + TTK_DOT + QSyncConfig::HOST;
41 
42  TTKStringMap headers;
43  headers.insert("Date", QSyncUtils::GMT());
44  headers.insert("Host", host);
45  headers.insert("Content-Type", "charset=utf-8");
46 
47  d->insertAuthorization(method, headers, resource);
48 
49  QNetworkRequest request;
50  request.setUrl("http://" + host + url);
51 
52  for(auto it = headers.constBegin(); it != headers.constEnd(); ++it)
53  {
54  request.setRawHeader(it.key().toUtf8(), it.value().toUtf8());
55  }
56 
57  QNetworkReply *reply = d->m_manager->get(request);
58  connect(reply, SIGNAL(finished()), SLOT(receiveDataFromServer()));
60 
61  if(parent()->metaObject()->indexOfSlot("downloadProgress(QString,qint64,qint64)") != -1)
62  {
63  connect(reply, SIGNAL(downloadProgress(qint64,qint64)), SLOT(downloadProgress(qint64,qint64)));
64  connect(this, SIGNAL(downloadProgressChanged(QString,qint64,qint64)), parent(), SLOT(downloadProgress(QString,qint64,qint64)));
65  }
66 }
67 
68 QString QSyncDownloadData::downloadUrl(const QString &bucket, const QString &fileName)
69 {
70  return downloadUrl(bucket, fileName, "application/x-www-form-urlencoded");
71 }
72 
73 QString QSyncDownloadData::downloadUrl(const QString &bucket, const QString &fileName, const QString &contentType)
74 {
75  const qint64 deadline = QDateTime::currentDateTimeUtc().addSecs(60 * 30).toMSecsSinceEpoch() / 1000;
76 
77  const QString &encodeKey = pathEncode(fileName);
78  const QString &method = "GET";
79  const QString &resource = TTK_SEPARATOR + bucket + TTK_SEPARATOR + fileName;
80  const QString &host = bucket + TTK_DOT + QSyncConfig::HOST;
81 
82  TTKStringMap headers;
83  headers.insert("Date", QString::number(deadline));
84  headers.insert("Content-Type", contentType);
85  headers.insert("Host", host);
86 
87  const QString &signature = QSyncUtils::authorizationCode(QSyncConfig::KEY, method, headers, resource);
88  return QString("http://%1/%2?OSSAccessKeyId=%3&Expires=%4&Signature=%5").arg(host, encodeKey, QSyncConfig::NAME).arg(deadline).arg(signature);
89 }
90 
92 {
94  QNetworkReply *reply = TTKObjectCast(QNetworkReply*, sender());
95  if(reply)
96  {
97  if(reply->error() == QNetworkReply::NoError)
98  {
99  QFile file(d->m_downloadPath);
100  if(file.open(QIODevice::WriteOnly))
101  {
102  file.write(reply->readAll());
103  file.close();
104  }
105  Q_EMIT downloadFileFinished(d->m_downloadTime);
106  }
107  else
108  {
110  }
111  reply->deleteLater();
112  }
113  else
114  {
116  }
117 }
118 
119 void QSyncDownloadData::downloadProgress(qint64 bytesSent, qint64 bytesTotal)
120 {
122  Q_EMIT downloadProgressChanged(d->m_downloadTime, bytesSent, bytesTotal);
123 }
static QString GMT()
Definition: qsyncutils.cpp:84
The class of the sync cloud data interface private.
QString pathEncode(const QString &data) const
void replyError(QNetworkReply::NetworkError error)
The class of the sync cloud download data private.
static QString HOST
Definition: qsyncconfig.h:32
void downloadProgressChanged(const QString &time, qint64 bytesSent, qint64 bytesTotal)
void downloadProgress(qint64 bytesSent, qint64 bytesTotal)
static QByteArray KEY
Definition: qsyncconfig.h:34
The class of the sync cloud data interface.
#define TTK_DOT
Definition: ttkglobal.h:193
void downloadFileFinished(const QString &time)
The class of the sync cloud download data.
QString downloadUrl(const QString &bucket, const QString &fileName)
#define TTK_NAN_STR
Definition: ttkglobal.h:201
#define TTK_SEPARATOR
Definition: ttkglobal.h:195
#define TTK_SLOT
Definition: ttkqtglobal.h:165
#define TTK_CREATE_PRIVATE(Class)
Definition: ttkprivate.h:24
virtual void receiveDataFromServer() overridefinal
static QString authorizationCode(const QString &key, const QString &method, const TTKStringMap &headers, const QString &resource)
Definition: qsyncutils.cpp:54
void downloadDataOperator(const QString &time, const QString &bucket, const QString &fileName, const QString &filePath)
static QString NAME
Definition: qsyncconfig.h:33
QSyncDownloadData(QNetworkAccessManager *networkManager, QObject *parent=nullptr)
QMap< QString, QString > TTKStringMap
Definition: ttkqtglobal.h:190
#define QtNetworkErrorConnect(p, q, f, s)
Network error connect.
Definition: ttkqtcompat.h:59
#define TTK_D(Class)
Definition: ttkprivate.h:41
#define TTKObjectCast(x, y)
Definition: ttkqtglobal.h:60