From 3d3eac7cc72d9466464b59c66fb1b5162b789879 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Thu, 25 Dec 2014 12:55:06 -0200 Subject: Send an experimental file to the experimental album on Facebook And I had to create the Http header by hand because I couldn't figure out how to do using HttpMultiPart from Qt. not fun. Signed-off-by: Tomaz Canabrava Signed-off-by: Dirk Hohndel --- qt-ui/divelistview.cpp | 2 ++ qt-ui/socialnetworks.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ qt-ui/socialnetworks.h | 1 + 3 files changed, 47 insertions(+) diff --git a/qt-ui/divelistview.cpp b/qt-ui/divelistview.cpp index 83d78c4c2..cbbf35152 100644 --- a/qt-ui/divelistview.cpp +++ b/qt-ui/divelistview.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include "socialnetworks.h" #include "../qthelper.h" @@ -844,6 +845,7 @@ void DiveListView::contextMenuEvent(QContextMenuEvent *event) void DiveListView::publishFacebook() { FacebookManager *fb = FacebookManager::instance(); + fb->sendDive(1); } void DiveListView::shiftTimes() diff --git a/qt-ui/socialnetworks.cpp b/qt-ui/socialnetworks.cpp index 76a85566d..f9d5acf54 100644 --- a/qt-ui/socialnetworks.cpp +++ b/qt-ui/socialnetworks.cpp @@ -8,7 +8,9 @@ #include #include #include +#include #include +#include #include #include "pref.h" @@ -180,3 +182,45 @@ void FacebookManager::setDesiredAlbumName(const QString& a) { albumName = a; } + +/* + + +*/ + +void FacebookManager::sendDive(int divenr) +{ + + QUrl url("https://graph.facebook.com/v2.2/" + QString(prefs.facebook.album_id) + "/photos?" + + "&access_token=" + QString(prefs.facebook.access_token) + + "&source=image"); + + + QNetworkAccessManager *am = new QNetworkAccessManager(this); + QFile file("subsurfaceuploadtest.png"); + file.open(QIODevice::ReadOnly); + QNetworkRequest request(url); + + QString bound="margin"; + + //according to rfc 1867 we need to put this string here: + QByteArray data(QString("--" + bound + "\r\n").toLocal8Bit()); + data.append("Content-Disposition: form-data; name=\"action\"\r\n\r\n"); + data.append("https://graph.facebook.com/v2.2/\r\n"); + data.append("--" + bound + "\r\n"); //according to rfc 1867 + data.append("Content-Disposition: form-data; name=\"uploaded\"; filename=\"subsurfaceuploadtest.png\"\r\n"); //name of the input is "uploaded" in my form, next one is a file name. + data.append("Content-Type: image/jpeg\r\n\r\n"); //data type + data.append(file.readAll()); //let's read the file + data.append("\r\n"); + data.append("--" + bound + "--\r\n"); //closing boundary according to rfc 1867 + + request.setRawHeader(QString("Content-Type").toLocal8Bit(),QString("multipart/form-data; boundary=" + bound).toLocal8Bit()); + request.setRawHeader(QString("Content-Length").toLocal8Bit(), QString::number(data.length()).toLocal8Bit()); + QNetworkReply *reply = am->post(request,data); + + QEventLoop loop; + connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); + loop.exec(); + + qDebug() << reply->readAll(); +} diff --git a/qt-ui/socialnetworks.h b/qt-ui/socialnetworks.h index ed7804afa..8c474c906 100644 --- a/qt-ui/socialnetworks.h +++ b/qt-ui/socialnetworks.h @@ -22,6 +22,7 @@ public slots: void tryLogin(const QUrl& loginResponse); void logout(); void setDesiredAlbumName(const QString& albumName); + void sendDive(int divenr); private: explicit FacebookManager(QObject *parent = 0); -- cgit v1.2.3-70-g09d2