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

googledrivestorage.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 #define FORBIDDEN_SYMBOL_ALLOW_ALL
00024 
00025 #include <curl/curl.h>
00026 #include "backends/cloud/googledrive/googledrivestorage.h"
00027 #include "backends/cloud/cloudmanager.h"
00028 #include "backends/cloud/googledrive/googledrivetokenrefresher.h"
00029 #include "backends/cloud/googledrive/googledrivelistdirectorybyidrequest.h"
00030 #include "backends/cloud/googledrive/googledriveuploadrequest.h"
00031 #include "backends/networking/curl/connectionmanager.h"
00032 #include "backends/networking/curl/curljsonrequest.h"
00033 #include "backends/networking/curl/networkreadstream.h"
00034 #include "common/config-manager.h"
00035 #include "common/debug.h"
00036 #include "common/json.h"
00037 #include "common/debug.h"
00038 
00039 namespace Cloud {
00040 namespace GoogleDrive {
00041 
00042 #define GOOGLEDRIVE_API_FILES_ALT_MEDIA "https://www.googleapis.com/drive/v3/files/%s?alt=media"
00043 #define GOOGLEDRIVE_API_FILES "https://www.googleapis.com/drive/v3/files"
00044 #define GOOGLEDRIVE_API_ABOUT "https://www.googleapis.com/drive/v3/about?fields=storageQuota,user"
00045 
00046 GoogleDriveStorage::GoogleDriveStorage(Common::String token, Common::String refreshToken, bool enabled):
00047     IdStorage(token, refreshToken, enabled) {}
00048 
00049 GoogleDriveStorage::GoogleDriveStorage(Common::String code, Networking::ErrorCallback cb) {
00050     getAccessToken(code, cb);
00051 }
00052 
00053 GoogleDriveStorage::~GoogleDriveStorage() {}
00054 
00055 Common::String GoogleDriveStorage::cloudProvider() { return "gdrive"; }
00056 
00057 uint32 GoogleDriveStorage::storageIndex() { return kStorageGoogleDriveId; }
00058 
00059 bool GoogleDriveStorage::needsRefreshToken() { return true; }
00060 
00061 bool GoogleDriveStorage::canReuseRefreshToken() { return true; }
00062 
00063 void GoogleDriveStorage::saveConfig(Common::String keyPrefix) {
00064     ConfMan.set(keyPrefix + "access_token", _token, ConfMan.kCloudDomain);
00065     ConfMan.set(keyPrefix + "refresh_token", _refreshToken, ConfMan.kCloudDomain);
00066     saveIsEnabledFlag(keyPrefix);
00067 }
00068 
00069 Common::String GoogleDriveStorage::name() const {
00070     return "Google Drive";
00071 }
00072 
00073 void GoogleDriveStorage::infoInnerCallback(StorageInfoCallback outerCallback, Networking::JsonResponse response) {
00074     Common::JSONValue *json = response.value;
00075     if (!json) {
00076         warning("GoogleDriveStorage::infoInnerCallback: NULL passed instead of JSON");
00077         delete outerCallback;
00078         return;
00079     }
00080 
00081     if (!Networking::CurlJsonRequest::jsonIsObject(json, "GoogleDriveStorage::infoInnerCallback")) {
00082         delete json;
00083         delete outerCallback;
00084         return;
00085     }
00086 
00087     Common::JSONObject jsonInfo = json->asObject();
00088 
00089     Common::String uid, displayName, email;
00090     uint64 quotaUsed = 0, quotaAllocated = 0;
00091 
00092     if (Networking::CurlJsonRequest::jsonContainsAttribute(jsonInfo, "user", "GoogleDriveStorage::infoInnerCallback") &&
00093         Networking::CurlJsonRequest::jsonIsObject(jsonInfo.getVal("user"), "GoogleDriveStorage::infoInnerCallback")) {
00094         //"me":true, "kind":"drive#user","photoLink": "",
00095         //"displayName":"Alexander Tkachev","emailAddress":"alexander@tkachov.ru","permissionId":""
00096         Common::JSONObject user = jsonInfo.getVal("user")->asObject();
00097         if (Networking::CurlJsonRequest::jsonContainsString(user, "permissionId", "GoogleDriveStorage::infoInnerCallback"))
00098             uid = user.getVal("permissionId")->asString(); //not sure it's user's id, but who cares anyway?
00099         if (Networking::CurlJsonRequest::jsonContainsString(user, "displayName", "GoogleDriveStorage::infoInnerCallback"))
00100             displayName = user.getVal("displayName")->asString();
00101         if (Networking::CurlJsonRequest::jsonContainsString(user, "emailAddress", "GoogleDriveStorage::infoInnerCallback"))
00102             email = user.getVal("emailAddress")->asString();
00103     }
00104 
00105     if (Networking::CurlJsonRequest::jsonContainsAttribute(jsonInfo, "storageQuota", "GoogleDriveStorage::infoInnerCallback") &&
00106         Networking::CurlJsonRequest::jsonIsObject(jsonInfo.getVal("storageQuota"), "GoogleDriveStorage::infoInnerCallback")) {
00107         //"usageInDrive":"6332462","limit":"18253611008","usage":"6332462","usageInDriveTrash":"0"
00108         Common::JSONObject storageQuota = jsonInfo.getVal("storageQuota")->asObject();
00109 
00110         if (Networking::CurlJsonRequest::jsonContainsString(storageQuota, "usage", "GoogleDriveStorage::infoInnerCallback")) {
00111             Common::String usage = storageQuota.getVal("usage")->asString();
00112             quotaUsed = usage.asUint64();
00113         }
00114 
00115         if (Networking::CurlJsonRequest::jsonContainsString(storageQuota, "limit", "GoogleDriveStorage::infoInnerCallback")) {
00116             Common::String limit = storageQuota.getVal("limit")->asString();
00117             quotaAllocated = limit.asUint64();
00118         }
00119     }
00120 
00121     CloudMan.setStorageUsername(kStorageGoogleDriveId, email);
00122 
00123     if (outerCallback) {
00124         (*outerCallback)(StorageInfoResponse(nullptr, StorageInfo(uid, displayName, email, quotaUsed, quotaAllocated)));
00125         delete outerCallback;
00126     }
00127 
00128     delete json;
00129 }
00130 
00131 void GoogleDriveStorage::createDirectoryInnerCallback(BoolCallback outerCallback, Networking::JsonResponse response) {
00132     Common::JSONValue *json = response.value;
00133     if (!json) {
00134         warning("GoogleDriveStorage::createDirectoryInnerCallback: NULL passed instead of JSON");
00135         delete outerCallback;
00136         return;
00137     }
00138 
00139     if (outerCallback) {
00140         if (Networking::CurlJsonRequest::jsonIsObject(json, "GoogleDriveStorage::createDirectoryInnerCallback")) {
00141             Common::JSONObject jsonInfo = json->asObject();
00142             (*outerCallback)(BoolResponse(nullptr, jsonInfo.contains("id")));
00143         } else {
00144             (*outerCallback)(BoolResponse(nullptr, false));
00145         }
00146         delete outerCallback;
00147     }
00148 
00149     delete json;
00150 }
00151 
00152 Networking::Request *GoogleDriveStorage::listDirectoryById(Common::String id, ListDirectoryCallback callback, Networking::ErrorCallback errorCallback) {
00153     if (!errorCallback)
00154         errorCallback = getErrorPrintingCallback();
00155     if (!callback)
00156         callback = new Common::Callback<GoogleDriveStorage, FileArrayResponse>(this, &GoogleDriveStorage::printFiles);
00157     return addRequest(new GoogleDriveListDirectoryByIdRequest(this, id, callback, errorCallback));
00158 }
00159 
00160 Networking::Request *GoogleDriveStorage::upload(Common::String path, Common::SeekableReadStream *contents, UploadCallback callback, Networking::ErrorCallback errorCallback) {
00161     return addRequest(new GoogleDriveUploadRequest(this, path, contents, callback, errorCallback));
00162 }
00163 
00164 Networking::Request *GoogleDriveStorage::streamFileById(Common::String id, Networking::NetworkReadStreamCallback callback, Networking::ErrorCallback errorCallback) {
00165     if (callback) {
00166         Common::String url = Common::String::format(GOOGLEDRIVE_API_FILES_ALT_MEDIA, ConnMan.urlEncode(id).c_str());
00167         Common::String header = "Authorization: Bearer " + _token;
00168         curl_slist *headersList = curl_slist_append(nullptr, header.c_str());
00169         Networking::NetworkReadStream *stream = new Networking::NetworkReadStream(url.c_str(), headersList, "");
00170         (*callback)(Networking::NetworkReadStreamResponse(nullptr, stream));
00171     }
00172     delete callback;
00173     delete errorCallback;
00174     return nullptr;
00175 }
00176 
00177 void GoogleDriveStorage::printInfo(StorageInfoResponse response) {
00178     debug(9, "\nGoogleDriveStorage: user info:");
00179     debug(9, "\tname: %s", response.value.name().c_str());
00180     debug(9, "\temail: %s", response.value.email().c_str());
00181     debug(9, "\tdisk usage: %lu/%lu", response.value.used(), response.value.available());
00182 }
00183 
00184 Networking::Request *GoogleDriveStorage::createDirectoryWithParentId(Common::String parentId, Common::String directoryName, BoolCallback callback, Networking::ErrorCallback errorCallback) {
00185     if (!errorCallback)
00186         errorCallback = getErrorPrintingCallback();
00187 
00188     Common::String url = GOOGLEDRIVE_API_FILES;
00189     Networking::JsonCallback innerCallback = new Common::CallbackBridge<GoogleDriveStorage, BoolResponse, Networking::JsonResponse>(this, &GoogleDriveStorage::createDirectoryInnerCallback, callback);
00190     Networking::CurlJsonRequest *request = new GoogleDriveTokenRefresher(this, innerCallback, errorCallback, url.c_str());
00191     request->addHeader("Authorization: Bearer " + accessToken());
00192     request->addHeader("Content-Type: application/json");
00193 
00194     Common::JSONArray parentsArray;
00195     parentsArray.push_back(new Common::JSONValue(parentId));
00196 
00197     Common::JSONObject jsonRequestParameters;
00198     jsonRequestParameters.setVal("mimeType", new Common::JSONValue("application/vnd.google-apps.folder"));
00199     jsonRequestParameters.setVal("name", new Common::JSONValue(directoryName));
00200     jsonRequestParameters.setVal("parents", new Common::JSONValue(parentsArray));
00201 
00202     Common::JSONValue value(jsonRequestParameters);
00203     request->addPostField(Common::JSON::stringify(&value));
00204 
00205     return addRequest(request);
00206 }
00207 
00208 Networking::Request *GoogleDriveStorage::info(StorageInfoCallback callback, Networking::ErrorCallback errorCallback) {
00209     if (!callback)
00210         callback = new Common::Callback<GoogleDriveStorage, StorageInfoResponse>(this, &GoogleDriveStorage::printInfo);
00211     Networking::JsonCallback innerCallback = new Common::CallbackBridge<GoogleDriveStorage, StorageInfoResponse, Networking::JsonResponse>(this, &GoogleDriveStorage::infoInnerCallback, callback);
00212     Networking::CurlJsonRequest *request = new GoogleDriveTokenRefresher(this, innerCallback, errorCallback, GOOGLEDRIVE_API_ABOUT);
00213     request->addHeader("Authorization: Bearer " + _token);
00214     return addRequest(request);
00215 }
00216 
00217 Common::String GoogleDriveStorage::savesDirectoryPath() { return "residualvm/saves/"; }
00218 
00219 GoogleDriveStorage *GoogleDriveStorage::loadFromConfig(Common::String keyPrefix) {
00220     if (!ConfMan.hasKey(keyPrefix + "access_token", ConfMan.kCloudDomain)) {
00221         warning("GoogleDriveStorage: no access_token found");
00222         return nullptr;
00223     }
00224 
00225     if (!ConfMan.hasKey(keyPrefix + "refresh_token", ConfMan.kCloudDomain)) {
00226         warning("GoogleDriveStorage: no refresh_token found");
00227         return nullptr;
00228     }
00229 
00230     Common::String accessToken = ConfMan.get(keyPrefix + "access_token", ConfMan.kCloudDomain);
00231     Common::String refreshToken = ConfMan.get(keyPrefix + "refresh_token", ConfMan.kCloudDomain);
00232     return new GoogleDriveStorage(accessToken, refreshToken, loadIsEnabledFlag(keyPrefix));
00233 }
00234 
00235 void GoogleDriveStorage::removeFromConfig(Common::String keyPrefix) {
00236     ConfMan.removeKey(keyPrefix + "access_token", ConfMan.kCloudDomain);
00237     ConfMan.removeKey(keyPrefix + "refresh_token", ConfMan.kCloudDomain);
00238     removeIsEnabledFlag(keyPrefix);
00239 }
00240 
00241 Common::String GoogleDriveStorage::getRootDirectoryId() {
00242     return "root";
00243 }
00244 
00245 } // End of namespace GoogleDrive
00246 } // End of namespace Cloud


Generated on Sat Aug 15 2020 05:00:58 for ResidualVM by doxygen 1.7.1
curved edge   curved edge