ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

onedriveuploadrequest.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "backends/cloud/onedrive/onedriveuploadrequest.h"
00024 #include "backends/cloud/onedrive/onedrivestorage.h"
00025 #include "backends/cloud/iso8601.h"
00026 #include "backends/cloud/storage.h"
00027 #include "backends/networking/curl/connectionmanager.h"
00028 #include "backends/networking/curl/curljsonrequest.h"
00029 #include "backends/networking/curl/networkreadstream.h"
00030 #include "common/json.h"
00031 #include "onedrivetokenrefresher.h"
00032 
00033 namespace Cloud {
00034 namespace OneDrive {
00035 
00036 #define ONEDRIVE_API_SPECIAL_APPROOT_UPLOAD "https://api.onedrive.com/v1.0/drive/special/approot:/%s:/upload.createSession"
00037 #define ONEDRIVE_API_SPECIAL_APPROOT_CONTENT "https://api.onedrive.com/v1.0/drive/special/approot:/%s:/content"
00038 
00039 OneDriveUploadRequest::OneDriveUploadRequest(OneDriveStorage *storage, Common::String path, Common::SeekableReadStream *contents, Storage::UploadCallback callback, Networking::ErrorCallback ecb):
00040     Networking::Request(nullptr, ecb), _storage(storage), _savePath(path), _contentsStream(contents), _uploadCallback(callback),
00041     _workingRequest(nullptr), _ignoreCallback(false) {
00042     start();
00043 }
00044 
00045 OneDriveUploadRequest::~OneDriveUploadRequest() {
00046     _ignoreCallback = true;
00047     if (_workingRequest)
00048         _workingRequest->finish();
00049     delete _contentsStream;
00050     delete _uploadCallback;
00051 }
00052 
00053 void OneDriveUploadRequest::start() {
00054     _ignoreCallback = true;
00055     if (_workingRequest)
00056         _workingRequest->finish();
00057     if (_contentsStream == nullptr) {
00058         warning("OneDriveUploadRequest: cannot restart because no stream given");
00059         finishError(Networking::ErrorResponse(this, false, true, "No stream given", -1));
00060         return;
00061     }
00062     if (!_contentsStream->seek(0)) {
00063         warning("OneDriveUploadRequest: cannot restart because stream couldn't seek(0)");
00064         finishError(Networking::ErrorResponse(this, false, true, "", -1));
00065         return;
00066     }
00067     _ignoreCallback = false;
00068 
00069     uploadNextPart();
00070 }
00071 
00072 void OneDriveUploadRequest::uploadNextPart() {
00073     const uint32 UPLOAD_PER_ONE_REQUEST = 10 * 1024 * 1024;
00074 
00075     if (_uploadUrl == "" && (uint32)_contentsStream->size() > UPLOAD_PER_ONE_REQUEST) {
00076         Common::String url = Common::String::format(ONEDRIVE_API_SPECIAL_APPROOT_UPLOAD, ConnMan.urlEncode(_savePath).c_str()); //folder must exist
00077         Networking::JsonCallback callback = new Common::Callback<OneDriveUploadRequest, Networking::JsonResponse>(this, &OneDriveUploadRequest::partUploadedCallback);
00078         Networking::ErrorCallback failureCallback = new Common::Callback<OneDriveUploadRequest, Networking::ErrorResponse>(this, &OneDriveUploadRequest::partUploadedErrorCallback);
00079         Networking::CurlJsonRequest *request = new OneDriveTokenRefresher(_storage, callback, failureCallback, url.c_str());
00080         request->addHeader("Authorization: Bearer " + _storage->accessToken());
00081         request->setBuffer(new byte[1], 0); //use POST
00082         _workingRequest = ConnMan.addRequest(request);
00083         return;
00084     }
00085 
00086     Common::String url;
00087     if (_uploadUrl == "") {
00088         url = Common::String::format(ONEDRIVE_API_SPECIAL_APPROOT_CONTENT, ConnMan.urlEncode(_savePath).c_str());
00089     } else {
00090         url = _uploadUrl;
00091     }
00092 
00093     Networking::JsonCallback callback = new Common::Callback<OneDriveUploadRequest, Networking::JsonResponse>(this, &OneDriveUploadRequest::partUploadedCallback);
00094     Networking::ErrorCallback failureCallback = new Common::Callback<OneDriveUploadRequest, Networking::ErrorResponse>(this, &OneDriveUploadRequest::partUploadedErrorCallback);
00095     Networking::CurlJsonRequest *request = new OneDriveTokenRefresher(_storage, callback, failureCallback, url.c_str());
00096     request->addHeader("Authorization: Bearer " + _storage->accessToken());
00097     request->usePut();
00098 
00099     uint32 oldPos = _contentsStream->pos();
00100 
00101     byte *buffer = new byte[UPLOAD_PER_ONE_REQUEST];
00102     uint32 size = _contentsStream->read(buffer, UPLOAD_PER_ONE_REQUEST);
00103     request->setBuffer(buffer, size);
00104 
00105     if (_uploadUrl != "") {
00106         request->addHeader(Common::String::format("Content-Range: bytes %u-%u/%u", oldPos, _contentsStream->pos() - 1, _contentsStream->size()));
00107     } else if (_contentsStream->size() == 0) {
00108         warning("\"Sorry, OneDrive can't upload empty files\"");
00109         finishUpload(StorageFile(_savePath, 0, 0, false));
00110         delete request;
00111         return;
00112     }
00113 
00114     _workingRequest = ConnMan.addRequest(request);
00115 }
00116 
00117 void OneDriveUploadRequest::partUploadedCallback(Networking::JsonResponse response) {
00118     _workingRequest = nullptr;
00119     if (_ignoreCallback)
00120         return;
00121 
00122     Networking::ErrorResponse error(this, false, true, "", -1);
00123     Networking::CurlJsonRequest *rq = (Networking::CurlJsonRequest *)response.request;
00124     if (rq && rq->getNetworkReadStream())
00125         error.httpResponseCode = rq->getNetworkReadStream()->httpResponseCode();
00126 
00127     Common::JSONValue *json = response.value;
00128     if (json == nullptr) {
00129         error.response = "Failed to parse JSON, null passed!";
00130         finishError(error);
00131         return;
00132     }
00133 
00134     if (json->isObject()) {
00135         Common::JSONObject object = json->asObject();
00136 
00137         if (object.contains("error")) {
00138             warning("OneDriveUploadRequest: error: %s", json->stringify(true).c_str());
00139             error.response = json->stringify(true);
00140             finishError(error);
00141             delete json;
00142             return;
00143         }
00144 
00145         if (Networking::CurlJsonRequest::jsonContainsString(object, "id", "OneDriveUploadRequest") &&
00146             Networking::CurlJsonRequest::jsonContainsString(object, "name", "OneDriveUploadRequest") &&
00147             Networking::CurlJsonRequest::jsonContainsIntegerNumber(object, "size", "OneDriveUploadRequest") &&
00148             Networking::CurlJsonRequest::jsonContainsString(object, "lastModifiedDateTime", "OneDriveUploadRequest")) {
00149             //finished
00150             Common::String path = _savePath;
00151             uint32 size = object.getVal("size")->asIntegerNumber();
00152             uint32 timestamp = ISO8601::convertToTimestamp(object.getVal("lastModifiedDateTime")->asString());
00153             finishUpload(StorageFile(path, size, timestamp, false));
00154             return;
00155         }
00156 
00157         if (_uploadUrl == "") {
00158             if (Networking::CurlJsonRequest::jsonContainsString(object, "uploadUrl", "OneDriveUploadRequest"))
00159                 _uploadUrl = object.getVal("uploadUrl")->asString();
00160         }
00161     }
00162 
00163     if (_contentsStream->eos() || _contentsStream->pos() >= _contentsStream->size() - 1) {
00164         warning("OneDriveUploadRequest: no file info to return");
00165         finishUpload(StorageFile(_savePath, 0, 0, false));
00166     } else {
00167         uploadNextPart();
00168     }
00169 
00170     delete json;
00171 }
00172 
00173 void OneDriveUploadRequest::partUploadedErrorCallback(Networking::ErrorResponse error) {
00174     _workingRequest = nullptr;
00175     if (_ignoreCallback)
00176         return;
00177     finishError(error);
00178 }
00179 
00180 void OneDriveUploadRequest::handle() {}
00181 
00182 void OneDriveUploadRequest::restart() { start(); }
00183 
00184 void OneDriveUploadRequest::finishUpload(StorageFile file) {
00185     Request::finishSuccess();
00186     if (_uploadCallback)
00187         (*_uploadCallback)(Storage::UploadResponse(this, file));
00188 }
00189 
00190 } // End of namespace OneDrive
00191 } // End of namespace Cloud


Generated on Sat Jun 22 2019 05:00:37 for ResidualVM by doxygen 1.7.1
curved edge   curved edge