TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
qsyncuploaddata.cpp
Go to the documentation of this file.
1 #include "qsyncuploaddata.h"
2 #include "qsyncdatainterface_p.h"
3 
4 #include <QFile>
5 
10 {
11 public:
14  {
15  }
16 
17  QString m_uploadTime;
18 };
19 
20 
21 
22 QSyncUploadData::QSyncUploadData(QNetworkAccessManager *networkManager, QObject *parent)
23  : QSyncDataInterface(TTK_CREATE_PRIVATE(QSyncUploadData), networkManager, parent)
24 {
26  d->m_manager = networkManager;
27 }
28 
29 void QSyncUploadData::uploadDataOperator(const QString &time, const QString &bucket, const QString &fileName, const QString &filePath)
30 {
32  d->m_uploadTime = time;
33 
34  const QString &method = "PUT";
35  const QString &url = TTK_SEPARATOR + fileName;
36  const QString &resource = TTK_SEPARATOR + bucket + url;
37  const QString &host = bucket + TTK_DOT + QSyncConfig::HOST;
38 
39  TTKStringMap headers;
40  headers.insert("Date", QSyncUtils::GMT());
41  headers.insert("Host", host);
42  headers.insert("Content-Type", "charset=utf-8");
43 
44  d->insertAuthorization(method, headers, resource);
45 
46  QNetworkRequest request;
47  request.setUrl("http://" + host + url);
48 
49  for(auto it = headers.constBegin(); it != headers.constEnd(); ++it)
50  {
51  request.setRawHeader(it.key().toUtf8(), it.value().toUtf8());
52  }
53 
54  QFile file(filePath);
55  file.open(QIODevice::ReadOnly);
56  QByteArray fileData = file.readAll();
57  file.close();
58 
59  QNetworkReply *reply = d->m_manager->put(request, fileData);
60  connect(reply, SIGNAL(finished()), SLOT(receiveDataFromServer()));
62 
63  if(parent()->metaObject()->indexOfSlot("uploadProgress(QString,qint64,qint64)") != -1)
64  {
65  connect(reply, SIGNAL(uploadProgress(qint64,qint64)), SLOT(uploadProgress(qint64,qint64)));
66  connect(this, SIGNAL(uploadProgressChanged(QString,qint64,qint64)), parent(), SLOT(uploadProgress(QString,qint64,qint64)));
67  }
68 }
69 
71 {
73  QNetworkReply *reply = TTKObjectCast(QNetworkReply*, sender());
74  if(reply)
75  {
76  if(reply->error() == QNetworkReply::NoError)
77  {
78  Q_EMIT uploadFileFinished(d->m_uploadTime);
79  }
80  else
81  {
83  }
84  reply->deleteLater();
85  }
86  else
87  {
89  }
90 }
91 
92 void QSyncUploadData::uploadProgress(qint64 bytesSent, qint64 bytesTotal)
93 {
95  Q_EMIT uploadProgressChanged(d->m_uploadTime, bytesSent, bytesTotal);
96 }
The class of the sync cloud upload data private.
static QString GMT()
Definition: qsyncutils.cpp:84
The class of the sync cloud data interface private.
void replyError(QNetworkReply::NetworkError error)
static QString HOST
Definition: qsyncconfig.h:32
void uploadFileFinished(const QString &time)
The class of the sync cloud data interface.
void uploadDataOperator(const QString &time, const QString &bucket, const QString &fileName, const QString &filePath)
#define TTK_DOT
Definition: ttkglobal.h:193
virtual void receiveDataFromServer() overridefinal
void uploadProgressChanged(const QString &time, qint64 bytesSent, qint64 bytesTotal)
#define TTK_NAN_STR
Definition: ttkglobal.h:201
The class of the sync cloud upload data.
QSyncUploadData(QNetworkAccessManager *networkManager, QObject *parent=nullptr)
#define TTK_SEPARATOR
Definition: ttkglobal.h:195
#define TTK_SLOT
Definition: ttkqtglobal.h:165
#define TTK_CREATE_PRIVATE(Class)
Definition: ttkprivate.h:24
QMap< QString, QString > TTKStringMap
Definition: ttkqtglobal.h:190
void uploadProgress(qint64 bytesSent, qint64 bytesTotal)
#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