aboutsummaryrefslogtreecommitdiff
path: root/backends/cloud/googledrive
diff options
context:
space:
mode:
Diffstat (limited to 'backends/cloud/googledrive')
-rw-r--r--backends/cloud/googledrive/googledrivelistdirectorybyidrequest.cpp163
-rw-r--r--backends/cloud/googledrive/googledrivelistdirectorybyidrequest.h63
-rw-r--r--backends/cloud/googledrive/googledrivestorage.cpp348
-rw-r--r--backends/cloud/googledrive/googledrivestorage.h118
-rw-r--r--backends/cloud/googledrive/googledrivetokenrefresher.cpp125
-rw-r--r--backends/cloud/googledrive/googledrivetokenrefresher.h52
-rw-r--r--backends/cloud/googledrive/googledriveuploadrequest.cpp353
-rw-r--r--backends/cloud/googledrive/googledriveuploadrequest.h70
8 files changed, 1292 insertions, 0 deletions
diff --git a/backends/cloud/googledrive/googledrivelistdirectorybyidrequest.cpp b/backends/cloud/googledrive/googledrivelistdirectorybyidrequest.cpp
new file mode 100644
index 0000000000..52611126a0
--- /dev/null
+++ b/backends/cloud/googledrive/googledrivelistdirectorybyidrequest.cpp
@@ -0,0 +1,163 @@
+/* ScummVM - Graphic Adventure Engine
+*
+* ScummVM is the legal property of its developers, whose names
+* are too numerous to list here. Please refer to the COPYRIGHT
+* file distributed with this source distribution.
+*
+* This program is free software; you can redistribute it and/or
+* modify it under the terms of the GNU General Public License
+* as published by the Free Software Foundation; either version 2
+* of the License, or (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software
+* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*
+*/
+
+#include "backends/cloud/googledrive/googledrivelistdirectorybyidrequest.h"
+#include "backends/cloud/googledrive/googledrivestorage.h"
+#include "backends/cloud/iso8601.h"
+#include "backends/cloud/storage.h"
+#include "backends/networking/curl/connectionmanager.h"
+#include "backends/networking/curl/curljsonrequest.h"
+#include "backends/networking/curl/networkreadstream.h"
+#include "common/json.h"
+#include "googledrivetokenrefresher.h"
+
+namespace Cloud {
+namespace GoogleDrive {
+
+#define GOOGLEDRIVE_API_FILES "https://www.googleapis.com/drive/v3/files?spaces=drive&fields=files%28id,mimeType,modifiedTime,name,size%29,nextPageToken&orderBy=folder,name"
+//files(id,mimeType,modifiedTime,name,size),nextPageToken
+
+GoogleDriveListDirectoryByIdRequest::GoogleDriveListDirectoryByIdRequest(GoogleDriveStorage *storage, Common::String id, Storage::ListDirectoryCallback cb, Networking::ErrorCallback ecb):
+ Networking::Request(nullptr, ecb), _requestedId(id), _storage(storage), _listDirectoryCallback(cb),
+ _workingRequest(nullptr), _ignoreCallback(false) {
+ start();
+}
+
+GoogleDriveListDirectoryByIdRequest::~GoogleDriveListDirectoryByIdRequest() {
+ _ignoreCallback = true;
+ if (_workingRequest)
+ _workingRequest->finish();
+ delete _listDirectoryCallback;
+}
+
+void GoogleDriveListDirectoryByIdRequest::start() {
+ _ignoreCallback = true;
+ if (_workingRequest)
+ _workingRequest->finish();
+ _files.clear();
+ _ignoreCallback = false;
+
+ makeRequest("");
+}
+
+void GoogleDriveListDirectoryByIdRequest::makeRequest(Common::String pageToken) {
+ Common::String url = GOOGLEDRIVE_API_FILES;
+ if (pageToken != "")
+ url += "&pageToken=" + pageToken;
+ url += "&q=%27" + _requestedId + "%27+in+parents";
+
+ Networking::JsonCallback callback = new Common::Callback<GoogleDriveListDirectoryByIdRequest, Networking::JsonResponse>(this, &GoogleDriveListDirectoryByIdRequest::responseCallback);
+ Networking::ErrorCallback failureCallback = new Common::Callback<GoogleDriveListDirectoryByIdRequest, Networking::ErrorResponse>(this, &GoogleDriveListDirectoryByIdRequest::errorCallback);
+ Networking::CurlJsonRequest *request = new GoogleDriveTokenRefresher(_storage, callback, failureCallback, url.c_str());
+ request->addHeader("Authorization: Bearer " + _storage->accessToken());
+ _workingRequest = ConnMan.addRequest(request);
+}
+
+void GoogleDriveListDirectoryByIdRequest::responseCallback(Networking::JsonResponse response) {
+ _workingRequest = nullptr;
+ if (_ignoreCallback) {
+ delete response.value;
+ return;
+ }
+ if (response.request)
+ _date = response.request->date();
+
+ Networking::ErrorResponse error(this);
+ Networking::CurlJsonRequest *rq = (Networking::CurlJsonRequest *)response.request;
+ if (rq && rq->getNetworkReadStream())
+ error.httpResponseCode = rq->getNetworkReadStream()->httpResponseCode();
+
+ Common::JSONValue *json = response.value;
+ if (json) {
+ Common::JSONObject responseObject = json->asObject();
+
+ ///debug("%s", json->stringify(true).c_str());
+
+ if (responseObject.contains("error") || responseObject.contains("error_summary")) {
+ warning("GoogleDrive returned error: %s", responseObject.getVal("error_summary")->asString().c_str());
+ error.failed = true;
+ error.response = json->stringify();
+ finishError(error);
+ delete json;
+ return;
+ }
+
+ //TODO: check that ALL keys exist AND HAVE RIGHT TYPE to avoid segfaults
+
+ if (responseObject.contains("files") && responseObject.getVal("files")->isArray()) {
+ Common::JSONArray items = responseObject.getVal("files")->asArray();
+ for (uint32 i = 0; i < items.size(); ++i) {
+ Common::JSONObject item = items[i]->asObject();
+ Common::String id = item.getVal("id")->asString();
+ Common::String name = item.getVal("name")->asString();
+ bool isDirectory = (item.getVal("mimeType")->asString() == "application/vnd.google-apps.folder");
+ uint32 size = 0, timestamp = 0;
+ if (item.contains("size") && item.getVal("size")->isString())
+ size = item.getVal("size")->asString().asUint64();
+ if (item.contains("modifiedTime") && item.getVal("modifiedTime")->isString())
+ timestamp = ISO8601::convertToTimestamp(item.getVal("modifiedTime")->asString());
+
+ //as we list directory by id, we can't determine full path for the file, so we leave it empty
+ _files.push_back(StorageFile(id, "", name, size, timestamp, isDirectory));
+ }
+ }
+
+ bool hasMore = (responseObject.contains("nextPageToken"));
+
+ if (hasMore) {
+ Common::String token = responseObject.getVal("nextPageToken")->asString();
+ makeRequest(token);
+ } else {
+ finishListing(_files);
+ }
+ } else {
+ warning("null, not json");
+ error.failed = true;
+ finishError(error);
+ }
+
+ delete json;
+}
+
+void GoogleDriveListDirectoryByIdRequest::errorCallback(Networking::ErrorResponse error) {
+ _workingRequest = nullptr;
+ if (_ignoreCallback)
+ return;
+ if (error.request)
+ _date = error.request->date();
+ finishError(error);
+}
+
+void GoogleDriveListDirectoryByIdRequest::handle() {}
+
+void GoogleDriveListDirectoryByIdRequest::restart() { start(); }
+
+Common::String GoogleDriveListDirectoryByIdRequest::date() const { return _date; }
+
+void GoogleDriveListDirectoryByIdRequest::finishListing(Common::Array<StorageFile> &files) {
+ Request::finishSuccess();
+ if (_listDirectoryCallback)
+ (*_listDirectoryCallback)(Storage::ListDirectoryResponse(this, files));
+}
+
+} // End of namespace GoogleDrive
+} // End of namespace Cloud
diff --git a/backends/cloud/googledrive/googledrivelistdirectorybyidrequest.h b/backends/cloud/googledrive/googledrivelistdirectorybyidrequest.h
new file mode 100644
index 0000000000..e94c6b1f4e
--- /dev/null
+++ b/backends/cloud/googledrive/googledrivelistdirectorybyidrequest.h
@@ -0,0 +1,63 @@
+/* ScummVM - Graphic Adventure Engine
+*
+* ScummVM is the legal property of its developers, whose names
+* are too numerous to list here. Please refer to the COPYRIGHT
+* file distributed with this source distribution.
+*
+* This program is free software; you can redistribute it and/or
+* modify it under the terms of the GNU General Public License
+* as published by the Free Software Foundation; either version 2
+* of the License, or (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software
+* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*
+*/
+
+#ifndef BACKENDS_CLOUD_GOOGLEDRIVE_GOOGLEDRIVELISTDIRECTORYBYIDREQUEST_H
+#define BACKENDS_CLOUD_GOOGLEDRIVE_GOOGLEDRIVELISTDIRECTORYBYIDREQUEST_H
+
+#include "backends/cloud/storage.h"
+#include "backends/networking/curl/request.h"
+#include "common/callback.h"
+#include "backends/networking/curl/curljsonrequest.h"
+
+namespace Cloud {
+namespace GoogleDrive {
+
+class GoogleDriveStorage;
+
+class GoogleDriveListDirectoryByIdRequest: public Networking::Request {
+ Common::String _requestedId;
+ GoogleDriveStorage *_storage;
+
+ Storage::ListDirectoryCallback _listDirectoryCallback;
+ Common::Array<StorageFile> _files;
+ Request *_workingRequest;
+ bool _ignoreCallback;
+ Common::String _date;
+
+ void start();
+ void makeRequest(Common::String pageToken);
+ void responseCallback(Networking::JsonResponse response);
+ void errorCallback(Networking::ErrorResponse error);
+ void finishListing(Common::Array<StorageFile> &files);
+public:
+ GoogleDriveListDirectoryByIdRequest(GoogleDriveStorage *storage, Common::String id, Storage::ListDirectoryCallback cb, Networking::ErrorCallback ecb);
+ virtual ~GoogleDriveListDirectoryByIdRequest();
+
+ virtual void handle();
+ virtual void restart();
+ virtual Common::String date() const;
+};
+
+} // End of namespace GoogleDrive
+} // End of namespace Cloud
+
+#endif
diff --git a/backends/cloud/googledrive/googledrivestorage.cpp b/backends/cloud/googledrive/googledrivestorage.cpp
new file mode 100644
index 0000000000..1b4b8baf56
--- /dev/null
+++ b/backends/cloud/googledrive/googledrivestorage.cpp
@@ -0,0 +1,348 @@
+/* ScummVM - Graphic Adventure Engine
+*
+* ScummVM is the legal property of its developers, whose names
+* are too numerous to list here. Please refer to the COPYRIGHT
+* file distributed with this source distribution.
+*
+* This program is free software; you can redistribute it and/or
+* modify it under the terms of the GNU General Public License
+* as published by the Free Software Foundation; either version 2
+* of the License, or (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software
+* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*
+*/
+#define FORBIDDEN_SYMBOL_ALLOW_ALL
+
+#include "backends/cloud/googledrive/googledrivestorage.h"
+#include "backends/cloud/cloudmanager.h"
+#include "backends/cloud/googledrive/googledrivetokenrefresher.h"
+#include "backends/cloud/googledrive/googledrivelistdirectorybyidrequest.h"
+#include "backends/cloud/googledrive/googledriveuploadrequest.h"
+#include "backends/networking/curl/connectionmanager.h"
+#include "backends/networking/curl/curljsonrequest.h"
+#include "backends/networking/curl/networkreadstream.h"
+#include "common/config-manager.h"
+#include "common/debug.h"
+#include "common/json.h"
+#include <curl/curl.h>
+
+#ifdef ENABLE_RELEASE
+#include "dists/clouds/cloud_keys.h"
+#endif
+
+namespace Cloud {
+namespace GoogleDrive {
+
+#define GOOGLEDRIVE_OAUTH2_TOKEN "https://accounts.google.com/o/oauth2/token"
+#define GOOGLEDRIVE_API_FILES_ALT_MEDIA "https://www.googleapis.com/drive/v3/files/%s?alt=media"
+#define GOOGLEDRIVE_API_FILES "https://www.googleapis.com/drive/v3/files"
+#define GOOGLEDRIVE_API_ABOUT "https://www.googleapis.com/drive/v3/about?fields=storageQuota,user"
+
+char *GoogleDriveStorage::KEY = nullptr; //can't use CloudConfig there yet, loading it on instance creation/auth
+char *GoogleDriveStorage::SECRET = nullptr;
+
+void GoogleDriveStorage::loadKeyAndSecret() {
+#ifdef ENABLE_RELEASE
+ KEY = RELEASE_GOOGLE_DRIVE_KEY;
+ SECRET = RELEASE_GOOGLE_DRIVE_SECRET;
+#else
+ Common::String k = ConfMan.get("GOOGLE_DRIVE_KEY", ConfMan.kCloudDomain);
+ KEY = new char[k.size() + 1];
+ memcpy(KEY, k.c_str(), k.size());
+ KEY[k.size()] = 0;
+
+ k = ConfMan.get("GOOGLE_DRIVE_SECRET", ConfMan.kCloudDomain);
+ SECRET = new char[k.size() + 1];
+ memcpy(SECRET, k.c_str(), k.size());
+ SECRET[k.size()] = 0;
+#endif
+}
+
+GoogleDriveStorage::GoogleDriveStorage(Common::String accessToken, Common::String refreshToken):
+ _token(accessToken), _refreshToken(refreshToken) {}
+
+GoogleDriveStorage::GoogleDriveStorage(Common::String code) {
+ getAccessToken(
+ new Common::Callback<GoogleDriveStorage, BoolResponse>(this, &GoogleDriveStorage::codeFlowComplete),
+ new Common::Callback<GoogleDriveStorage, Networking::ErrorResponse>(this, &GoogleDriveStorage::codeFlowFailed),
+ code
+ );
+}
+
+GoogleDriveStorage::~GoogleDriveStorage() {}
+
+void GoogleDriveStorage::getAccessToken(BoolCallback callback, Networking::ErrorCallback errorCallback, Common::String code) {
+ if (!KEY || !SECRET) loadKeyAndSecret();
+ bool codeFlow = (code != "");
+
+ if (!codeFlow && _refreshToken == "") {
+ warning("GoogleDriveStorage: no refresh token available to get new access token.");
+ if (callback)
+ (*callback)(BoolResponse(nullptr, false));
+ return;
+ }
+
+ Networking::JsonCallback innerCallback = new Common::CallbackBridge<GoogleDriveStorage, BoolResponse, Networking::JsonResponse>(this, &GoogleDriveStorage::tokenRefreshed, callback);
+ if (errorCallback == nullptr)
+ errorCallback = getErrorPrintingCallback();
+ Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, errorCallback, GOOGLEDRIVE_OAUTH2_TOKEN);
+ if (codeFlow) {
+ request->addPostField("code=" + code);
+ request->addPostField("grant_type=authorization_code");
+ } else {
+ request->addPostField("refresh_token=" + _refreshToken);
+ request->addPostField("grant_type=refresh_token");
+ }
+ request->addPostField("client_id=" + Common::String(KEY));
+ request->addPostField("client_secret=" + Common::String(SECRET));
+ if (Cloud::CloudManager::couldUseLocalServer()) {
+ request->addPostField("&redirect_uri=http%3A%2F%2Flocalhost%3A12345");
+ } else {
+ request->addPostField("&redirect_uri=https%3A%2F%2Fwww.scummvm.org/c/code");
+ }
+ addRequest(request);
+}
+
+void GoogleDriveStorage::tokenRefreshed(BoolCallback callback, Networking::JsonResponse response) {
+ Common::JSONValue *json = response.value;
+ if (!json) {
+ warning("GoogleDriveStorage: got NULL instead of JSON");
+ if (callback)
+ (*callback)(BoolResponse(nullptr, false));
+ delete callback;
+ return;
+ }
+
+ if (!Networking::CurlJsonRequest::jsonIsObject(json, "GoogleDriveStorage")) {
+ if (callback)
+ (*callback)(BoolResponse(nullptr, false));
+ delete json;
+ delete callback;
+ return;
+ }
+
+ Common::JSONObject result = json->asObject();
+ if (!Networking::CurlJsonRequest::jsonContainsString(result, "access_token", "GoogleDriveStorage")) {
+ warning("GoogleDriveStorage: bad response, no token passed");
+ debug(9, "%s", json->stringify().c_str());
+ if (callback)
+ (*callback)(BoolResponse(nullptr, false));
+ } else {
+ _token = result.getVal("access_token")->asString();
+ if (!Networking::CurlJsonRequest::jsonContainsString(result, "refresh_token", "GoogleDriveStorage"))
+ warning("GoogleDriveStorage: no refresh_token passed");
+ else
+ _refreshToken = result.getVal("refresh_token")->asString();
+ CloudMan.save(); //ask CloudManager to save our new refreshToken
+ if (callback)
+ (*callback)(BoolResponse(nullptr, true));
+ }
+ delete json;
+ delete callback;
+}
+
+void GoogleDriveStorage::codeFlowComplete(BoolResponse response) {
+ if (!response.value) {
+ warning("GoogleDriveStorage: failed to get access token through code flow");
+ CloudMan.removeStorage(this);
+ return;
+ }
+
+ ConfMan.removeKey("googledrive_code", ConfMan.kCloudDomain);
+ CloudMan.replaceStorage(this, kStorageGoogleDriveId);
+ ConfMan.flushToDisk();
+}
+
+void GoogleDriveStorage::codeFlowFailed(Networking::ErrorResponse error) {
+ debug(9, "GoogleDriveStorage: code flow failed (%s, %ld):", (error.failed ? "failed" : "interrupted"), error.httpResponseCode);
+ debug(9, "%s", error.response.c_str());
+ CloudMan.removeStorage(this);
+}
+
+void GoogleDriveStorage::saveConfig(Common::String keyPrefix) {
+ ConfMan.set(keyPrefix + "access_token", _token, ConfMan.kCloudDomain);
+ ConfMan.set(keyPrefix + "refresh_token", _refreshToken, ConfMan.kCloudDomain);
+}
+
+Common::String GoogleDriveStorage::name() const {
+ return "Google Drive";
+}
+
+void GoogleDriveStorage::infoInnerCallback(StorageInfoCallback outerCallback, Networking::JsonResponse response) {
+ Common::JSONValue *json = response.value;
+ if (!json) {
+ warning("GoogleDriveStorage::infoInnerCallback: NULL passed instead of JSON");
+ delete outerCallback;
+ return;
+ }
+
+ if (!Networking::CurlJsonRequest::jsonIsObject(json, "GoogleDriveStorage::infoInnerCallback")) {
+ delete json;
+ delete outerCallback;
+ return;
+ }
+
+ Common::JSONObject info = json->asObject();
+
+ Common::String uid, name, email;
+ uint64 quotaUsed = 0, quotaAllocated = 0;
+
+ if (Networking::CurlJsonRequest::jsonContainsAttribute(info, "user", "GoogleDriveStorage::infoInnerCallback") &&
+ Networking::CurlJsonRequest::jsonIsObject(info.getVal("user"), "GoogleDriveStorage::infoInnerCallback")) {
+ //"me":true, "kind":"drive#user","photoLink": "",
+ //"displayName":"Alexander Tkachev","emailAddress":"alexander@tkachov.ru","permissionId":""
+ Common::JSONObject user = info.getVal("user")->asObject();
+ if (Networking::CurlJsonRequest::jsonContainsString(user, "permissionId", "GoogleDriveStorage::infoInnerCallback"))
+ uid = user.getVal("permissionId")->asString(); //not sure it's user's id, but who cares anyway?
+ if (Networking::CurlJsonRequest::jsonContainsString(user, "displayName", "GoogleDriveStorage::infoInnerCallback"))
+ name = user.getVal("displayName")->asString();
+ if (Networking::CurlJsonRequest::jsonContainsString(user, "emailAddress", "GoogleDriveStorage::infoInnerCallback"))
+ email = user.getVal("emailAddress")->asString();
+ }
+
+ if (Networking::CurlJsonRequest::jsonContainsAttribute(info, "storageQuota", "GoogleDriveStorage::infoInnerCallback") &&
+ Networking::CurlJsonRequest::jsonIsObject(info.getVal("storageQuota"), "GoogleDriveStorage::infoInnerCallback")) {
+ //"usageInDrive":"6332462","limit":"18253611008","usage":"6332462","usageInDriveTrash":"0"
+ Common::JSONObject storageQuota = info.getVal("storageQuota")->asObject();
+
+ if (Networking::CurlJsonRequest::jsonContainsString(storageQuota, "usage", "GoogleDriveStorage::infoInnerCallback")) {
+ Common::String usage = storageQuota.getVal("usage")->asString();
+ quotaUsed = usage.asUint64();
+ }
+
+ if (Networking::CurlJsonRequest::jsonContainsString(storageQuota, "limit", "GoogleDriveStorage::infoInnerCallback")) {
+ Common::String limit = storageQuota.getVal("limit")->asString();
+ quotaAllocated = limit.asUint64();
+ }
+ }
+
+ CloudMan.setStorageUsername(kStorageGoogleDriveId, email);
+
+ if (outerCallback) {
+ (*outerCallback)(StorageInfoResponse(nullptr, StorageInfo(uid, name, email, quotaUsed, quotaAllocated)));
+ delete outerCallback;
+ }
+
+ delete json;
+}
+
+void GoogleDriveStorage::createDirectoryInnerCallback(BoolCallback outerCallback, Networking::JsonResponse response) {
+ Common::JSONValue *json = response.value;
+ if (!json) {
+ warning("GoogleDriveStorage::createDirectoryInnerCallback: NULL passed instead of JSON");
+ delete outerCallback;
+ return;
+ }
+
+ if (outerCallback) {
+ if (Networking::CurlJsonRequest::jsonIsObject(json, "GoogleDriveStorage::createDirectoryInnerCallback")) {
+ Common::JSONObject info = json->asObject();
+ (*outerCallback)(BoolResponse(nullptr, info.contains("id")));
+ } else {
+ (*outerCallback)(BoolResponse(nullptr, false));
+ }
+ delete outerCallback;
+ }
+
+ delete json;
+}
+
+Networking::Request *GoogleDriveStorage::listDirectoryById(Common::String id, ListDirectoryCallback callback, Networking::ErrorCallback errorCallback) {
+ if (!errorCallback)
+ errorCallback = getErrorPrintingCallback();
+ if (!callback)
+ callback = new Common::Callback<GoogleDriveStorage, FileArrayResponse>(this, &GoogleDriveStorage::printFiles);
+ return addRequest(new GoogleDriveListDirectoryByIdRequest(this, id, callback, errorCallback));
+}
+
+Networking::Request *GoogleDriveStorage::upload(Common::String path, Common::SeekableReadStream *contents, UploadCallback callback, Networking::ErrorCallback errorCallback) {
+ return addRequest(new GoogleDriveUploadRequest(this, path, contents, callback, errorCallback));
+}
+
+Networking::Request *GoogleDriveStorage::streamFileById(Common::String id, Networking::NetworkReadStreamCallback callback, Networking::ErrorCallback errorCallback) {
+ if (callback) {
+ Common::String url = Common::String::format(GOOGLEDRIVE_API_FILES_ALT_MEDIA, ConnMan.urlEncode(id).c_str());
+ Common::String header = "Authorization: Bearer " + _token;
+ curl_slist *headersList = curl_slist_append(nullptr, header.c_str());
+ Networking::NetworkReadStream *stream = new Networking::NetworkReadStream(url.c_str(), headersList, "");
+ (*callback)(Networking::NetworkReadStreamResponse(nullptr, stream));
+ }
+ delete callback;
+ delete errorCallback;
+ return nullptr;
+}
+
+void GoogleDriveStorage::printInfo(StorageInfoResponse response) {
+ debug(9, "\nGoogleDriveStorage: user info:");
+ debug(9, "\tname: %s", response.value.name().c_str());
+ debug(9, "\temail: %s", response.value.email().c_str());
+ debug(9, "\tdisk usage: %lu/%lu", response.value.used(), response.value.available());
+}
+
+Networking::Request *GoogleDriveStorage::createDirectoryWithParentId(Common::String parentId, Common::String name, BoolCallback callback, Networking::ErrorCallback errorCallback) {
+ if (!errorCallback)
+ errorCallback = getErrorPrintingCallback();
+
+ Common::String url = GOOGLEDRIVE_API_FILES;
+ Networking::JsonCallback innerCallback = new Common::CallbackBridge<GoogleDriveStorage, BoolResponse, Networking::JsonResponse>(this, &GoogleDriveStorage::createDirectoryInnerCallback, callback);
+ Networking::CurlJsonRequest *request = new GoogleDriveTokenRefresher(this, innerCallback, errorCallback, url.c_str());
+ request->addHeader("Authorization: Bearer " + accessToken());
+ request->addHeader("Content-Type: application/json");
+
+ Common::JSONArray parentsArray;
+ parentsArray.push_back(new Common::JSONValue(parentId));
+
+ Common::JSONObject jsonRequestParameters;
+ jsonRequestParameters.setVal("mimeType", new Common::JSONValue("application/vnd.google-apps.folder"));
+ jsonRequestParameters.setVal("name", new Common::JSONValue(name));
+ jsonRequestParameters.setVal("parents", new Common::JSONValue(parentsArray));
+
+ Common::JSONValue value(jsonRequestParameters);
+ request->addPostField(Common::JSON::stringify(&value));
+
+ return addRequest(request);
+}
+
+Networking::Request *GoogleDriveStorage::info(StorageInfoCallback callback, Networking::ErrorCallback errorCallback) {
+ if (!callback)
+ callback = new Common::Callback<GoogleDriveStorage, StorageInfoResponse>(this, &GoogleDriveStorage::printInfo);
+ Networking::JsonCallback innerCallback = new Common::CallbackBridge<GoogleDriveStorage, StorageInfoResponse, Networking::JsonResponse>(this, &GoogleDriveStorage::infoInnerCallback, callback);
+ Networking::CurlJsonRequest *request = new GoogleDriveTokenRefresher(this, innerCallback, errorCallback, GOOGLEDRIVE_API_ABOUT);
+ request->addHeader("Authorization: Bearer " + _token);
+ return addRequest(request);
+}
+
+Common::String GoogleDriveStorage::savesDirectoryPath() { return "scummvm/saves/"; }
+
+GoogleDriveStorage *GoogleDriveStorage::loadFromConfig(Common::String keyPrefix) {
+ loadKeyAndSecret();
+
+ if (!ConfMan.hasKey(keyPrefix + "access_token", ConfMan.kCloudDomain)) {
+ warning("GoogleDriveStorage: no access_token found");
+ return nullptr;
+ }
+
+ if (!ConfMan.hasKey(keyPrefix + "refresh_token", ConfMan.kCloudDomain)) {
+ warning("GoogleDriveStorage: no refresh_token found");
+ return nullptr;
+ }
+
+ Common::String accessToken = ConfMan.get(keyPrefix + "access_token", ConfMan.kCloudDomain);
+ Common::String refreshToken = ConfMan.get(keyPrefix + "refresh_token", ConfMan.kCloudDomain);
+ return new GoogleDriveStorage(accessToken, refreshToken);
+}
+
+Common::String GoogleDriveStorage::getRootDirectoryId() {
+ return "root";
+}
+
+} // End of namespace GoogleDrive
+} // End of namespace Cloud
diff --git a/backends/cloud/googledrive/googledrivestorage.h b/backends/cloud/googledrive/googledrivestorage.h
new file mode 100644
index 0000000000..6a834c44ca
--- /dev/null
+++ b/backends/cloud/googledrive/googledrivestorage.h
@@ -0,0 +1,118 @@
+/* ScummVM - Graphic Adventure Engine
+*
+* ScummVM is the legal property of its developers, whose names
+* are too numerous to list here. Please refer to the COPYRIGHT
+* file distributed with this source distribution.
+*
+* This program is free software; you can redistribute it and/or
+* modify it under the terms of the GNU General Public License
+* as published by the Free Software Foundation; either version 2
+* of the License, or (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software
+* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*
+*/
+
+#ifndef BACKENDS_CLOUD_GOOGLEDRIVE_GOOGLEDRIVESTORAGE_H
+#define BACKENDS_CLOUD_GOOGLEDRIVE_GOOGLEDRIVESTORAGE_H
+
+#include "backends/cloud/id/idstorage.h"
+#include "backends/networking/curl/curljsonrequest.h"
+
+namespace Cloud {
+namespace GoogleDrive {
+
+class GoogleDriveStorage: public Id::IdStorage {
+ static char *KEY, *SECRET;
+
+ static void loadKeyAndSecret();
+
+ Common::String _token, _refreshToken;
+
+ /** This private constructor is called from loadFromConfig(). */
+ GoogleDriveStorage(Common::String token, Common::String refreshToken);
+
+ void tokenRefreshed(BoolCallback callback, Networking::JsonResponse response);
+ void codeFlowComplete(BoolResponse response);
+ void codeFlowFailed(Networking::ErrorResponse error);
+
+ /** Constructs StorageInfo based on JSON response from cloud. */
+ void infoInnerCallback(StorageInfoCallback outerCallback, Networking::JsonResponse json);
+
+ /** Returns bool based on JSON response from cloud. */
+ void createDirectoryInnerCallback(BoolCallback outerCallback, Networking::JsonResponse json);
+
+ void printInfo(StorageInfoResponse response);
+public:
+ /** This constructor uses OAuth code flow to get tokens. */
+ GoogleDriveStorage(Common::String code);
+ virtual ~GoogleDriveStorage();
+
+ /**
+ * Storage methods, which are used by CloudManager to save
+ * storage in configuration file.
+ */
+
+ /**
+ * Save storage data using ConfMan.
+ * @param keyPrefix all saved keys must start with this prefix.
+ * @note every Storage must write keyPrefix + "type" key
+ * with common value (e.g. "Dropbox").
+ */
+ virtual void saveConfig(Common::String keyPrefix);
+
+ /**
+ * Return unique storage name.
+ * @returns some unique storage name (for example, "Dropbox (user@example.com)")
+ */
+ virtual Common::String name() const;
+
+ /** Public Cloud API comes down there. */
+
+ /** Returns Array<StorageFile> - the list of files. */
+ virtual Networking::Request *listDirectoryById(Common::String id, ListDirectoryCallback callback, Networking::ErrorCallback errorCallback);
+
+ /** Returns UploadStatus struct with info about uploaded file. */
+ virtual Networking::Request *upload(Common::String path, Common::SeekableReadStream *contents, UploadCallback callback, Networking::ErrorCallback errorCallback);
+
+ /** Returns pointer to Networking::NetworkReadStream. */
+ virtual Networking::Request *streamFileById(Common::String id, Networking::NetworkReadStreamCallback callback, Networking::ErrorCallback errorCallback);
+
+ /** Calls the callback when finished. */
+ virtual Networking::Request *createDirectoryWithParentId(Common::String parentId, Common::String name, BoolCallback callback, Networking::ErrorCallback errorCallback);
+
+ /** Returns the StorageInfo struct. */
+ virtual Networking::Request *info(StorageInfoCallback callback, Networking::ErrorCallback errorCallback);
+
+ /** Returns storage's saves directory path with the trailing slash. */
+ virtual Common::String savesDirectoryPath();
+
+ /**
+ * Load token and user id from configs and return GoogleDriveStorage for those.
+ * @return pointer to the newly created GoogleDriveStorage or 0 if some problem occured.
+ */
+ static GoogleDriveStorage *loadFromConfig(Common::String keyPrefix);
+
+ virtual Common::String getRootDirectoryId();
+
+ /**
+ * Gets new access_token. If <code> passed is "", refresh_token is used.
+ * Use "" in order to refresh token and pass a callback, so you could
+ * continue your work when new token is available.
+ */
+ void getAccessToken(BoolCallback callback, Networking::ErrorCallback errorCallback = nullptr, Common::String code = "");
+
+ Common::String accessToken() const { return _token; }
+};
+
+} // End of namespace GoogleDrive
+} // End of namespace Cloud
+
+#endif
diff --git a/backends/cloud/googledrive/googledrivetokenrefresher.cpp b/backends/cloud/googledrive/googledrivetokenrefresher.cpp
new file mode 100644
index 0000000000..8cc492d6b4
--- /dev/null
+++ b/backends/cloud/googledrive/googledrivetokenrefresher.cpp
@@ -0,0 +1,125 @@
+/* ScummVM - Graphic Adventure Engine
+*
+* ScummVM is the legal property of its developers, whose names
+* are too numerous to list here. Please refer to the COPYRIGHT
+* file distributed with this source distribution.
+*
+* This program is free software; you can redistribute it and/or
+* modify it under the terms of the GNU General Public License
+* as published by the Free Software Foundation; either version 2
+* of the License, or (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software
+* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*
+*/
+#define FORBIDDEN_SYMBOL_ALLOW_ALL
+
+#include "backends/cloud/googledrive/googledrivetokenrefresher.h"
+#include "backends/cloud/googledrive/googledrivestorage.h"
+#include "backends/networking/curl/networkreadstream.h"
+#include "common/debug.h"
+#include "common/json.h"
+#include <curl/curl.h>
+
+namespace Cloud {
+namespace GoogleDrive {
+
+GoogleDriveTokenRefresher::GoogleDriveTokenRefresher(GoogleDriveStorage *parent, Networking::JsonCallback callback, Networking::ErrorCallback ecb, const char *url):
+ CurlJsonRequest(callback, ecb, url), _parentStorage(parent) {}
+
+GoogleDriveTokenRefresher::~GoogleDriveTokenRefresher() {}
+
+void GoogleDriveTokenRefresher::tokenRefreshed(Storage::BoolResponse response) {
+ if (!response.value) {
+ //failed to refresh token, notify user with NULL in original callback
+ warning("GoogleDriveTokenRefresher: failed to refresh token");
+ finishError(Networking::ErrorResponse(this, false, true, "", -1));
+ return;
+ }
+
+ //update headers: first change header with token, then pass those to request
+ for (uint32 i = 0; i < _headers.size(); ++i) {
+ if (_headers[i].contains("Authorization")) {
+ _headers[i] = "Authorization: Bearer " + _parentStorage->accessToken();
+ }
+ }
+ setHeaders(_headers);
+
+ //successfully received refreshed token, can restart the original request now
+ retry(0);
+}
+
+void GoogleDriveTokenRefresher::finishJson(Common::JSONValue *json) {
+ if (!json) {
+ //that's probably not an error (200 OK)
+ CurlJsonRequest::finishJson(nullptr);
+ return;
+ }
+
+ if (jsonIsObject(json, "GoogleDriveTokenRefresher")) {
+ Common::JSONObject result = json->asObject();
+ long httpResponseCode = -1;
+ if (result.contains("error") && jsonIsObject(result.getVal("error"), "GoogleDriveTokenRefresher")) {
+ //new token needed => request token & then retry original request
+ if (_stream) {
+ httpResponseCode = _stream->httpResponseCode();
+ debug(9, "GoogleDriveTokenRefresher: code = %ld", httpResponseCode);
+ }
+
+ Common::JSONObject error = result.getVal("error")->asObject();
+ bool irrecoverable = true;
+
+ uint32 code = -1;
+ Common::String message;
+ if (jsonContainsIntegerNumber(error, "code", "GoogleDriveTokenRefresher")) {
+ code = error.getVal("code")->asIntegerNumber();
+ debug(9, "GoogleDriveTokenRefresher: code = %u", code);
+ }
+
+ if (jsonContainsString(error, "message", "GoogleDriveTokenRefresher")) {
+ message = error.getVal("message")->asString();
+ debug(9, "GoogleDriveTokenRefresher: message = %s", message.c_str());
+ }
+
+ if (code == 401 || message == "Invalid Credentials")
+ irrecoverable = false;
+
+ if (irrecoverable) {
+ finishError(Networking::ErrorResponse(this, false, true, json->stringify(true), httpResponseCode));
+ delete json;
+ return;
+ }
+
+ pause();
+ delete json;
+ _parentStorage->getAccessToken(new Common::Callback<GoogleDriveTokenRefresher, Storage::BoolResponse>(this, &GoogleDriveTokenRefresher::tokenRefreshed));
+ return;
+ }
+ }
+
+ //notify user of success
+ CurlJsonRequest::finishJson(json);
+}
+
+void GoogleDriveTokenRefresher::setHeaders(Common::Array<Common::String> &headers) {
+ _headers = headers;
+ curl_slist_free_all(_headersList);
+ _headersList = 0;
+ for (uint32 i = 0; i < headers.size(); ++i)
+ CurlJsonRequest::addHeader(headers[i]);
+}
+
+void GoogleDriveTokenRefresher::addHeader(Common::String header) {
+ _headers.push_back(header);
+ CurlJsonRequest::addHeader(header);
+}
+
+} // End of namespace GoogleDrive
+} // End of namespace Cloud
diff --git a/backends/cloud/googledrive/googledrivetokenrefresher.h b/backends/cloud/googledrive/googledrivetokenrefresher.h
new file mode 100644
index 0000000000..6cb3e41849
--- /dev/null
+++ b/backends/cloud/googledrive/googledrivetokenrefresher.h
@@ -0,0 +1,52 @@
+/* ScummVM - Graphic Adventure Engine
+*
+* ScummVM is the legal property of its developers, whose names
+* are too numerous to list here. Please refer to the COPYRIGHT
+* file distributed with this source distribution.
+*
+* This program is free software; you can redistribute it and/or
+* modify it under the terms of the GNU General Public License
+* as published by the Free Software Foundation; either version 2
+* of the License, or (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software
+* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*
+*/
+
+#ifndef BACKENDS_CLOUD_GOOGLEDRIVE_GOOGLEDRIVETOKENREFRESHER_H
+#define BACKENDS_CLOUD_GOOGLEDRIVE_GOOGLEDRIVETOKENREFRESHER_H
+
+#include "backends/cloud/storage.h"
+#include "backends/networking/curl/curljsonrequest.h"
+
+namespace Cloud {
+namespace GoogleDrive {
+
+class GoogleDriveStorage;
+
+class GoogleDriveTokenRefresher: public Networking::CurlJsonRequest {
+ GoogleDriveStorage *_parentStorage;
+ Common::Array<Common::String> _headers;
+
+ void tokenRefreshed(Storage::BoolResponse response);
+
+ virtual void finishJson(Common::JSONValue *json);
+public:
+ GoogleDriveTokenRefresher(GoogleDriveStorage *parent, Networking::JsonCallback callback, Networking::ErrorCallback ecb, const char *url);
+ virtual ~GoogleDriveTokenRefresher();
+
+ virtual void setHeaders(Common::Array<Common::String> &headers);
+ virtual void addHeader(Common::String header);
+};
+
+} // End of namespace GoogleDrive
+} // End of namespace Cloud
+
+#endif
diff --git a/backends/cloud/googledrive/googledriveuploadrequest.cpp b/backends/cloud/googledrive/googledriveuploadrequest.cpp
new file mode 100644
index 0000000000..5f61dcd2a8
--- /dev/null
+++ b/backends/cloud/googledrive/googledriveuploadrequest.cpp
@@ -0,0 +1,353 @@
+/* ScummVM - Graphic Adventure Engine
+*
+* ScummVM is the legal property of its developers, whose names
+* are too numerous to list here. Please refer to the COPYRIGHT
+* file distributed with this source distribution.
+*
+* This program is free software; you can redistribute it and/or
+* modify it under the terms of the GNU General Public License
+* as published by the Free Software Foundation; either version 2
+* of the License, or (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software
+* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*
+*/
+
+#include "backends/cloud/googledrive/googledriveuploadrequest.h"
+#include "backends/cloud/googledrive/googledrivestorage.h"
+#include "backends/cloud/iso8601.h"
+#include "backends/cloud/storage.h"
+#include "backends/networking/curl/connectionmanager.h"
+#include "backends/networking/curl/curljsonrequest.h"
+#include "backends/networking/curl/networkreadstream.h"
+#include "common/json.h"
+#include "googledrivetokenrefresher.h"
+
+namespace Cloud {
+namespace GoogleDrive {
+
+#define GOOGLEDRIVE_API_FILES "https://www.googleapis.com/upload/drive/v3/files"
+
+GoogleDriveUploadRequest::GoogleDriveUploadRequest(GoogleDriveStorage *storage, Common::String path, Common::SeekableReadStream *contents, Storage::UploadCallback callback, Networking::ErrorCallback ecb):
+ Networking::Request(nullptr, ecb), _storage(storage), _savePath(path), _contentsStream(contents), _uploadCallback(callback),
+ _workingRequest(nullptr), _ignoreCallback(false) {
+ start();
+}
+
+GoogleDriveUploadRequest::~GoogleDriveUploadRequest() {
+ _ignoreCallback = true;
+ if (_workingRequest)
+ _workingRequest->finish();
+ delete _contentsStream;
+ delete _uploadCallback;
+}
+
+void GoogleDriveUploadRequest::start() {
+ _ignoreCallback = true;
+ if (_workingRequest)
+ _workingRequest->finish();
+ if (_contentsStream == nullptr || !_contentsStream->seek(0)) {
+ warning("GoogleDriveUploadRequest: cannot restart because stream couldn't seek(0)");
+ finishError(Networking::ErrorResponse(this, false, true, "", -1));
+ return;
+ }
+ _resolvedId = ""; //used to update file contents
+ _parentId = ""; //used to create file within parent directory
+ _serverReceivedBytes = 0;
+ _ignoreCallback = false;
+
+ resolveId();
+}
+
+void GoogleDriveUploadRequest::resolveId() {
+ //check whether such file already exists
+ Storage::UploadCallback innerCallback = new Common::Callback<GoogleDriveUploadRequest, Storage::UploadResponse>(this, &GoogleDriveUploadRequest::idResolvedCallback);
+ Networking::ErrorCallback innerErrorCallback = new Common::Callback<GoogleDriveUploadRequest, Networking::ErrorResponse>(this, &GoogleDriveUploadRequest::idResolveFailedCallback);
+ _workingRequest = _storage->resolveFileId(_savePath, innerCallback, innerErrorCallback);
+}
+
+void GoogleDriveUploadRequest::idResolvedCallback(Storage::UploadResponse response) {
+ _workingRequest = nullptr;
+ if (_ignoreCallback)
+ return;
+ _resolvedId = response.value.id();
+ startUpload();
+}
+
+void GoogleDriveUploadRequest::idResolveFailedCallback(Networking::ErrorResponse error) {
+ _workingRequest = nullptr;
+ if (_ignoreCallback)
+ return;
+
+ //not resolved => error or no such file
+ if (error.response.contains("no such file found in its parent directory")) {
+ //parent's id after the '\n'
+ Common::String parentId = error.response;
+ for (uint32 i = 0; i < parentId.size(); ++i)
+ if (parentId[i] == '\n') {
+ parentId.erase(0, i + 1);
+ break;
+ }
+
+ _parentId = parentId;
+ startUpload();
+ return;
+ }
+
+ finishError(error);
+}
+
+void GoogleDriveUploadRequest::startUpload() {
+ Common::String name = _savePath;
+ for (uint32 i = name.size(); i > 0; --i) {
+ if (name[i - 1] == '/' || name[i - 1] == '\\') {
+ name.erase(0, i);
+ break;
+ }
+ }
+
+ Common::String url = GOOGLEDRIVE_API_FILES;
+ if (_resolvedId != "")
+ url += "/" + ConnMan.urlEncode(_resolvedId);
+ url += "?uploadType=resumable&fields=id,mimeType,modifiedTime,name,size";
+ Networking::JsonCallback callback = new Common::Callback<GoogleDriveUploadRequest, Networking::JsonResponse>(this, &GoogleDriveUploadRequest::startUploadCallback);
+ Networking::ErrorCallback failureCallback = new Common::Callback<GoogleDriveUploadRequest, Networking::ErrorResponse>(this, &GoogleDriveUploadRequest::startUploadErrorCallback);
+ Networking::CurlJsonRequest *request = new GoogleDriveTokenRefresher(_storage, callback, failureCallback, url.c_str());
+ request->addHeader("Authorization: Bearer " + _storage->accessToken());
+ request->addHeader("Content-Type: application/json");
+ if (_resolvedId != "")
+ request->usePatch();
+
+ Common::JSONObject jsonRequestParameters;
+ if (_resolvedId != "") {
+ jsonRequestParameters.setVal("id", new Common::JSONValue(_resolvedId));
+ } else {
+ Common::JSONArray parentsArray;
+ parentsArray.push_back(new Common::JSONValue(_parentId));
+ jsonRequestParameters.setVal("parents", new Common::JSONValue(parentsArray));
+ }
+ jsonRequestParameters.setVal("name", new Common::JSONValue(name));
+
+ Common::JSONValue value(jsonRequestParameters);
+ request->addPostField(Common::JSON::stringify(&value));
+
+ _workingRequest = ConnMan.addRequest(request);
+}
+
+void GoogleDriveUploadRequest::startUploadCallback(Networking::JsonResponse response) {
+ _workingRequest = nullptr;
+ if (_ignoreCallback)
+ return;
+
+ Networking::ErrorResponse error(this, false, true, "", -1);
+ Networking::CurlJsonRequest *rq = (Networking::CurlJsonRequest *)response.request;
+ if (rq) {
+ const Networking::NetworkReadStream *stream = rq->getNetworkReadStream();
+ if (stream) {
+ long code = stream->httpResponseCode();
+ Common::String headers = stream->responseHeaders();
+ if (code == 200) {
+ const char *cstr = headers.c_str();
+ const char *position = strstr(cstr, "Location: ");
+
+ if (position) {
+ Common::String result = "";
+ char c;
+ for (const char *i = position + 10; c = *i, c != 0; ++i) {
+ if (c == '\n' || c == '\r')
+ break;
+ result += c;
+ }
+ _uploadUrl = result;
+ uploadNextPart();
+ return;
+ }
+ }
+
+ error.httpResponseCode = code;
+ }
+ }
+
+ Common::JSONValue *json = response.value;
+ delete json;
+
+ finishError(error);
+}
+
+void GoogleDriveUploadRequest::startUploadErrorCallback(Networking::ErrorResponse error) {
+ _workingRequest = nullptr;
+ if (_ignoreCallback)
+ return;
+ finishError(error);
+}
+
+void GoogleDriveUploadRequest::uploadNextPart() {
+ const uint32 UPLOAD_PER_ONE_REQUEST = 10 * 1024 * 1024;
+ Common::String url = _uploadUrl;
+
+ Networking::JsonCallback callback = new Common::Callback<GoogleDriveUploadRequest, Networking::JsonResponse>(this, &GoogleDriveUploadRequest::partUploadedCallback);
+ Networking::ErrorCallback failureCallback = new Common::Callback<GoogleDriveUploadRequest, Networking::ErrorResponse>(this, &GoogleDriveUploadRequest::partUploadedErrorCallback);
+ Networking::CurlJsonRequest *request = new GoogleDriveTokenRefresher(_storage, callback, failureCallback, url.c_str());
+ request->addHeader("Authorization: Bearer " + _storage->accessToken());
+ request->usePut();
+
+ uint32 oldPos = _contentsStream->pos();
+ if (oldPos != _serverReceivedBytes) {
+ if (!_contentsStream->seek(_serverReceivedBytes)) {
+ warning("GoogleDriveUploadRequest: cannot upload because stream couldn't seek(%lu)", _serverReceivedBytes);
+ finishError(Networking::ErrorResponse(this, false, true, "", -1));
+ return;
+ }
+ oldPos = _serverReceivedBytes;
+ }
+
+ byte *buffer = new byte[UPLOAD_PER_ONE_REQUEST];
+ uint32 size = _contentsStream->read(buffer, UPLOAD_PER_ONE_REQUEST);
+ if (size != 0)
+ request->setBuffer(buffer, size);
+
+ if (_uploadUrl != "") {
+ if (_contentsStream->pos() == 0)
+ request->addHeader(Common::String::format("Content-Length: 0"));
+ else
+ request->addHeader(Common::String::format("Content-Range: bytes %u-%u/%u", oldPos, _contentsStream->pos() - 1, _contentsStream->size()));
+ }
+
+ _workingRequest = ConnMan.addRequest(request);
+}
+
+bool GoogleDriveUploadRequest::handleHttp308(const Networking::NetworkReadStream *stream) {
+ //308 Resume Incomplete, with Range: X-Y header
+ if (!stream)
+ return false;
+ if (stream->httpResponseCode() != 308)
+ return false; //seriously
+
+ Common::String headers = stream->responseHeaders();
+ const char *cstr = headers.c_str();
+ for (int rangeTry = 0; rangeTry < 2; ++rangeTry) {
+ const char *needle = (rangeTry == 0 ? "Range: 0-" : "Range: bytes=0-");
+ uint32 needleLength = (rangeTry == 0 ? 9 : 15);
+
+ const char *position = strstr(cstr, needle); //if it lost the first part, I refuse to talk with it
+
+ if (position) {
+ Common::String result = "";
+ char c;
+ for (const char *i = position + needleLength; c = *i, c != 0; ++i) {
+ if (c == '\n' || c == '\r')
+ break;
+ result += c;
+ }
+ _serverReceivedBytes = result.asUint64() + 1;
+ uploadNextPart();
+ return true;
+ }
+ }
+
+ return false;
+}
+
+void GoogleDriveUploadRequest::partUploadedCallback(Networking::JsonResponse response) {
+ _workingRequest = nullptr;
+ if (_ignoreCallback)
+ return;
+
+ Networking::ErrorResponse error(this, false, true, "", -1);
+ Networking::CurlJsonRequest *rq = (Networking::CurlJsonRequest *)response.request;
+ if (rq) {
+ const Networking::NetworkReadStream *stream = rq->getNetworkReadStream();
+ if (stream) {
+ long code = stream->httpResponseCode();
+ error.httpResponseCode = code;
+ if (code == 308 && handleHttp308(stream)) {
+ delete (Common::JSONValue *)response.value;
+ return;
+ }
+ }
+ }
+
+ Common::JSONValue *json = response.value;
+ if (json == nullptr) {
+ error.response = "Failed to parse JSON, null passed!";
+ finishError(error);
+ return;
+ }
+
+ if (json->isObject()) {
+ Common::JSONObject object = json->asObject();
+
+ if (object.contains("error")) {
+ warning("GoogleDrive returned error: %s", json->stringify(true).c_str());
+ error.response = json->stringify(true);
+ finishError(error);
+ delete json;
+ return;
+ }
+
+ if (Networking::CurlJsonRequest::jsonContainsString(object, "id", "GoogleDriveUploadRequest") &&
+ Networking::CurlJsonRequest::jsonContainsString(object, "name", "GoogleDriveUploadRequest") &&
+ Networking::CurlJsonRequest::jsonContainsString(object, "mimeType", "GoogleDriveUploadRequest")) {
+ //finished
+ Common::String id = object.getVal("id")->asString();
+ Common::String name = object.getVal("name")->asString();
+ bool isDirectory = (object.getVal("mimeType")->asString() == "application/vnd.google-apps.folder");
+ uint32 size = 0, timestamp = 0;
+ if (Networking::CurlJsonRequest::jsonContainsString(object, "size", "GoogleDriveUploadRequest", true))
+ size = object.getVal("size")->asString().asUint64();
+ if (Networking::CurlJsonRequest::jsonContainsString(object, "modifiedTime", "GoogleDriveUploadRequest", true))
+ timestamp = ISO8601::convertToTimestamp(object.getVal("modifiedTime")->asString());
+
+ finishUpload(StorageFile(id, _savePath, name, size, timestamp, isDirectory));
+ return;
+ }
+ }
+
+ if (_contentsStream->eos() || _contentsStream->pos() >= _contentsStream->size() - 1) {
+ warning("GoogleDriveUploadRequest: no file info to return");
+ finishUpload(StorageFile(_savePath, 0, 0, false));
+ } else {
+ uploadNextPart();
+ }
+
+ delete json;
+}
+
+void GoogleDriveUploadRequest::partUploadedErrorCallback(Networking::ErrorResponse error) {
+ _workingRequest = nullptr;
+ if (_ignoreCallback)
+ return;
+
+ Networking::CurlJsonRequest *rq = (Networking::CurlJsonRequest *)error.request;
+ if (rq) {
+ const Networking::NetworkReadStream *stream = rq->getNetworkReadStream();
+ if (stream) {
+ long code = stream->httpResponseCode();
+ if (code == 308 && handleHttp308(stream)) {
+ return;
+ }
+ }
+ }
+
+ finishError(error);
+}
+
+void GoogleDriveUploadRequest::handle() {}
+
+void GoogleDriveUploadRequest::restart() { start(); }
+
+void GoogleDriveUploadRequest::finishUpload(StorageFile file) {
+ Request::finishSuccess();
+ if (_uploadCallback)
+ (*_uploadCallback)(Storage::UploadResponse(this, file));
+}
+
+} // End of namespace GoogleDrive
+} // End of namespace Cloud
diff --git a/backends/cloud/googledrive/googledriveuploadrequest.h b/backends/cloud/googledrive/googledriveuploadrequest.h
new file mode 100644
index 0000000000..73acab5bbd
--- /dev/null
+++ b/backends/cloud/googledrive/googledriveuploadrequest.h
@@ -0,0 +1,70 @@
+/* ScummVM - Graphic Adventure Engine
+*
+* ScummVM is the legal property of its developers, whose names
+* are too numerous to list here. Please refer to the COPYRIGHT
+* file distributed with this source distribution.
+*
+* This program is free software; you can redistribute it and/or
+* modify it under the terms of the GNU General Public License
+* as published by the Free Software Foundation; either version 2
+* of the License, or (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software
+* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*
+*/
+
+#ifndef BACKENDS_CLOUD_GOOGLEDRIVE_GOOGLEDRIVEUPLOADREQUEST_H
+#define BACKENDS_CLOUD_GOOGLEDRIVE_GOOGLEDRIVEUPLOADREQUEST_H
+
+#include "backends/cloud/storage.h"
+#include "backends/networking/curl/curljsonrequest.h"
+#include "backends/networking/curl/request.h"
+#include "common/callback.h"
+
+namespace Cloud {
+namespace GoogleDrive {
+class GoogleDriveStorage;
+
+class GoogleDriveUploadRequest: public Networking::Request {
+ GoogleDriveStorage *_storage;
+ Common::String _savePath;
+ Common::SeekableReadStream *_contentsStream;
+ Storage::UploadCallback _uploadCallback;
+ Request *_workingRequest;
+ bool _ignoreCallback;
+ Common::String _resolvedId, _parentId;
+ Common::String _uploadUrl;
+ uint64 _serverReceivedBytes;
+
+ void start();
+ void resolveId();
+ void idResolvedCallback(Storage::UploadResponse response);
+ void idResolveFailedCallback(Networking::ErrorResponse error);
+ void startUpload();
+ void startUploadCallback(Networking::JsonResponse response);
+ void startUploadErrorCallback(Networking::ErrorResponse error);
+ void uploadNextPart();
+ void partUploadedCallback(Networking::JsonResponse response);
+ void partUploadedErrorCallback(Networking::ErrorResponse error);
+ bool handleHttp308(const Networking::NetworkReadStream *stream);
+ void finishUpload(StorageFile status);
+
+public:
+ GoogleDriveUploadRequest(GoogleDriveStorage *storage, Common::String path, Common::SeekableReadStream *contents, Storage::UploadCallback callback, Networking::ErrorCallback ecb);
+ virtual ~GoogleDriveUploadRequest();
+
+ virtual void handle();
+ virtual void restart();
+};
+
+} // End of namespace GoogleDrive
+} // End of namespace Cloud
+
+#endif