1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
#include "UploadTask.h"
#include "EncodeTarget.h"
#include <QDebug>
#include <QFile>
#include <QFileInfo>
#include <QFtp>
UploadTask::UploadTask(QObject *parent) : Task(false, parent),
m_ftp(0),
m_sizeQuery(0),
m_currentFile(0)
{
}
UploadTask::~UploadTask()
{
disconnect(this, 0, 0, 0);
kill();
}
bool UploadTask::executeTask(Movie *movie)
{
Q_UNUSED(movie)
m_ftp = new QFtp(this);
connect(m_ftp, SIGNAL(dataTransferProgress(qint64,qint64)), this, SIGNAL(uploadProgress(qint64,qint64)));
connect(m_ftp, SIGNAL(dataTransferProgress(qint64,qint64)), this, SLOT(storeUploadProgress(qint64,qint64)));
connect(m_ftp, SIGNAL(commandFinished(int,bool)), this, SLOT(commandFinished(int,bool)));
connect(m_ftp, SIGNAL(rawCommandReply(int,QString)), this, SLOT(rawCommandReply(int,QString)));
m_currentFile = 0;
m_currentFileIndex = 0;
m_status.clear();
m_fileUpload = 0;
m_ftp->connectToHost("ec2-184-73-60-225.compute-1.amazonaws.com");
m_ftp->login("ftp_user", "8wu^x,1mWb!/o;xrp.:x37$Dr");
queueNext();
return true;
}
void UploadTask::queueNext()
{
if (m_currentFile) {
m_currentFile->close();
delete m_currentFile;
m_currentFile = 0;
}
m_status.clear();
if (m_currentFileIndex >= currentMovie()->mp4Locations().length()) {
if (m_ftp) {
disconnect(m_ftp, 0, 0, 0);
m_ftp->deleteLater();
m_ftp = 0;
}
setCompleted(true);
return;
}
m_currentFile = new QFile(currentMovie()->mp4Locations().at(m_currentFileIndex));
m_status = QString("%1 - Checking for already existing file...").arg(EncodeTarget::targets().at(m_currentFileIndex).name());
m_sizeQuery = m_ftp->rawCommand(QString("SIZE %1").arg(QFileInfo(m_currentFile->fileName()).fileName()));
}
void UploadTask::rawCommandReply(int replyCode, const QString &detail)
{
if (replyCode == 213 && m_ftp->currentId() == m_sizeQuery) {
bool success;
long long size = detail.toLongLong(&success);
if (success && m_currentFile->size() == size) {
++m_currentFileIndex;
queueNext();
} else
commandFinished(m_sizeQuery, true);
}
}
void UploadTask::cleanUp(bool result)
{
currentMovie()->setUploaded(result);
}
void UploadTask::commandFinished(int id, bool error)
{
if (error && id == m_sizeQuery) {
m_status = QString("%1 - Starting transfer...").arg(EncodeTarget::targets().at(m_currentFileIndex).name());
m_fileUpload = m_ftp->put(m_currentFile, QFileInfo(m_currentFile->fileName()).fileName(), QFtp::Binary);
}
else if (error)
terminate();
else if (id == m_fileUpload) {
++m_currentFileIndex;
queueNext();
}
}
bool UploadTask::canRunTask(const Movie *movie) const
{
return movie->hasEncoded() && !movie->hasUploaded();
}
void UploadTask::kill()
{
if (m_ftp) {
disconnect(m_ftp, 0, 0, 0);
m_ftp->deleteLater();
m_ftp = 0;
}
m_status.clear();
if (m_currentFile) {
m_currentFile->close();
delete m_currentFile;
m_currentFile = 0;
}
}
QString UploadTask::status() const
{
return m_status;
}
void UploadTask::storeUploadProgress(qint64 done, qint64 total)
{
m_status = QString("%4 - %1%: %2 of %3 transferred").arg(QString::number(100.0 * (double)done / (double)total, 'g', 2), QString::number(done), QString::number(total), EncodeTarget::targets().at(m_currentFileIndex).name());
}
|