/
upload.cpp
101 lines (62 loc) · 2.14 KB
/
upload.cpp
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
#include <upload.h>
#include <exttools.h>
//fortest
#include <accounts-qt/Accounts/Account>
Upload::Upload(QObject *parent)
: QObject(parent),
nam(new QNetworkAccessManager(this))
{
connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(parser(QNetworkReply*)));
}
Upload::~Upload()
{
}
void Upload::parser(QNetworkReply *reply) //parsing data due to request
{
QString response = reply->readAll();
QScriptValue sc;
QScriptEngine engine;
qDebug() << "test: "<<response;
sc = engine.evaluate("("+ QString(response)+ ")");
QString data;
qDebug() << "url: "<<sc.property("upload").property("links").property("original").toString();
data = sc.property("upload").property("links").property("original").toString();
emit resulturl(data);//return url only
reply->close();
return;
}
void Upload::progresscalc( qint64 sent, qint64 total)
{
double sent_p = sent;
double total_p = total;
double percentagedouble = (sent_p/total_p) * 100;
int percentage = percentagedouble;
emit resultprogress (percentage);
}
void Upload::upload_request(QString filename)
{
filename.remove(0,7);
QString key="c4f5cf8a8f1782d6388edb1b05cf8efa04ec64af4";
QString key_anon="0426cb779599180503fd02a0e134ff03";
QString secret="67560fc48f699bd5a518b35f120b44be";
QString method="HMAC-SHA1";
QString version ="1.0";
QUrl url("http://api.imgur.com/2/upload.json");
QByteArray postdata;
QFile img_file(filename);
if (!img_file.open(QIODevice::ReadOnly))
qDebug() << "can't read file" << endl;
QByteArray fileData = img_file.readAll().toBase64();
img_file.close();
//POST data
postdata.append(QString("key=").toUtf8());
postdata.append(QUrl::toPercentEncoding(key_anon));
postdata.append(QString("&image=").toUtf8());
postdata.append(QUrl::toPercentEncoding(fileData));
//END POST data
QNetworkReply *reply = nam->post(QNetworkRequest(url), postdata);
connect(reply, SIGNAL(uploadProgress(qint64,qint64)), this, SLOT(progresscalc(qint64,qint64)));
//testing the account thing
Tools::Tools t;
t.account_tokens();
}