Initial commit
This commit is contained in:
194
backends/cloud/box/boxlistdirectorybyidrequest.cpp
Normal file
194
backends/cloud/box/boxlistdirectorybyidrequest.cpp
Normal file
@@ -0,0 +1,194 @@
|
||||
/* 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 3 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, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "backends/cloud/box/boxlistdirectorybyidrequest.h"
|
||||
#include "backends/cloud/box/boxstorage.h"
|
||||
#include "backends/cloud/box/boxtokenrefresher.h"
|
||||
#include "backends/cloud/iso8601.h"
|
||||
#include "backends/cloud/storage.h"
|
||||
#include "backends/networking/http/connectionmanager.h"
|
||||
#include "backends/networking/http/httpjsonrequest.h"
|
||||
#include "backends/networking/http/networkreadstream.h"
|
||||
#include "common/formats/json.h"
|
||||
|
||||
namespace Cloud {
|
||||
namespace Box {
|
||||
|
||||
#define BOX_LIST_DIRECTORY_LIMIT 1000
|
||||
#define BOX_FOLDERS_API_LINK "https://api.box.com/2.0/folders/%s/items?offset=%u&limit=%u&fields=%s"
|
||||
|
||||
BoxListDirectoryByIdRequest::BoxListDirectoryByIdRequest(BoxStorage *storage, const Common::String &id, Storage::ListDirectoryCallback cb, Networking::ErrorCallback ecb):
|
||||
Networking::Request(nullptr, ecb), _requestedId(id), _storage(storage), _listDirectoryCallback(cb),
|
||||
_workingRequest(nullptr), _ignoreCallback(false) {
|
||||
start();
|
||||
}
|
||||
|
||||
BoxListDirectoryByIdRequest::~BoxListDirectoryByIdRequest() {
|
||||
_ignoreCallback = true;
|
||||
if (_workingRequest) _workingRequest->finish();
|
||||
delete _listDirectoryCallback;
|
||||
}
|
||||
|
||||
void BoxListDirectoryByIdRequest::start() {
|
||||
_ignoreCallback = true;
|
||||
if (_workingRequest) _workingRequest->finish();
|
||||
_files.clear();
|
||||
_ignoreCallback = false;
|
||||
|
||||
makeRequest(0);
|
||||
}
|
||||
|
||||
void BoxListDirectoryByIdRequest::makeRequest(uint32 offset) {
|
||||
Common::String url = Common::String::format(
|
||||
BOX_FOLDERS_API_LINK,
|
||||
_requestedId.c_str(),
|
||||
offset,
|
||||
BOX_LIST_DIRECTORY_LIMIT,
|
||||
"id,type,name,size,modified_at"
|
||||
);
|
||||
|
||||
Networking::JsonCallback callback = new Common::Callback<BoxListDirectoryByIdRequest, const Networking::JsonResponse &>(this, &BoxListDirectoryByIdRequest::responseCallback);
|
||||
Networking::ErrorCallback failureCallback = new Common::Callback<BoxListDirectoryByIdRequest, const Networking::ErrorResponse &>(this, &BoxListDirectoryByIdRequest::errorCallback);
|
||||
Networking::HttpJsonRequest *request = new BoxTokenRefresher(_storage, callback, failureCallback, url.c_str());
|
||||
request->addHeader("Authorization: Bearer " + _storage->accessToken());
|
||||
_workingRequest = ConnMan.addRequest(request);
|
||||
}
|
||||
|
||||
void BoxListDirectoryByIdRequest::responseCallback(const Networking::JsonResponse &response) {
|
||||
_workingRequest = nullptr;
|
||||
if (_ignoreCallback) {
|
||||
delete response.value;
|
||||
return;
|
||||
}
|
||||
|
||||
if (response.request)
|
||||
_date = response.request->date();
|
||||
|
||||
Networking::ErrorResponse error(this, "BoxListDirectoryByIdRequest::responseCallback: unknown error");
|
||||
const Networking::HttpJsonRequest *rq = (const Networking::HttpJsonRequest *)response.request;
|
||||
if (rq && rq->getNetworkReadStream())
|
||||
error.httpResponseCode = rq->getNetworkReadStream()->httpResponseCode();
|
||||
|
||||
const Common::JSONValue *json = response.value;
|
||||
if (json == nullptr) {
|
||||
error.response = "Failed to parse JSON, null passed!";
|
||||
finishError(error);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!json->isObject()) {
|
||||
error.response = "Passed JSON is not an object!";
|
||||
finishError(error);
|
||||
delete json;
|
||||
return;
|
||||
}
|
||||
|
||||
Common::JSONObject responseObject = json->asObject();
|
||||
//debug(9, "%s", json->stringify(true).c_str());
|
||||
|
||||
//TODO: handle error messages passed as JSON
|
||||
/*
|
||||
if (responseObject.contains("error") || responseObject.contains("error_summary")) {
|
||||
warning("Box returned error: %s", responseObject.getVal("error_summary")->asString().c_str());
|
||||
error.failed = true;
|
||||
error.response = json->stringify();
|
||||
finishError(error);
|
||||
delete json;
|
||||
return;
|
||||
}
|
||||
*/
|
||||
|
||||
//check that ALL keys exist AND HAVE RIGHT TYPE to avoid segfaults
|
||||
if (responseObject.contains("entries")) {
|
||||
if (!responseObject.getVal("entries")->isArray()) {
|
||||
error.response = Common::String::format(
|
||||
"\"entries\" found, but that's not an array!\n%s",
|
||||
responseObject.getVal("entries")->stringify(true).c_str()
|
||||
);
|
||||
finishError(error);
|
||||
delete json;
|
||||
return;
|
||||
}
|
||||
|
||||
Common::JSONArray items = responseObject.getVal("entries")->asArray();
|
||||
for (uint32 i = 0; i < items.size(); ++i) {
|
||||
if (!Networking::HttpJsonRequest::jsonIsObject(items[i], "BoxListDirectoryByIdRequest")) continue;
|
||||
|
||||
Common::JSONObject item = items[i]->asObject();
|
||||
|
||||
if (!Networking::HttpJsonRequest::jsonContainsString(item, "id", "BoxListDirectoryByIdRequest")) continue;
|
||||
if (!Networking::HttpJsonRequest::jsonContainsString(item, "name", "BoxListDirectoryByIdRequest")) continue;
|
||||
if (!Networking::HttpJsonRequest::jsonContainsString(item, "type", "BoxListDirectoryByIdRequest")) continue;
|
||||
if (!Networking::HttpJsonRequest::jsonContainsString(item, "modified_at", "BoxListDirectoryByIdRequest")) continue;
|
||||
if (!Networking::HttpJsonRequest::jsonContainsStringOrIntegerNumber(item, "size", "BoxListDirectoryByIdRequest")) continue;
|
||||
|
||||
Common::String id = item.getVal("id")->asString();
|
||||
Common::String name = item.getVal("name")->asString();
|
||||
bool isDirectory = (item.getVal("type")->asString() == "folder");
|
||||
uint32 size;
|
||||
if (item.getVal("size")->isString()) {
|
||||
size = item.getVal("size")->asString().asUint64();
|
||||
} else {
|
||||
size = item.getVal("size")->asIntegerNumber();
|
||||
}
|
||||
uint32 timestamp = ISO8601::convertToTimestamp(item.getVal("modified_at")->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));
|
||||
}
|
||||
}
|
||||
|
||||
uint32 received = 0;
|
||||
uint32 totalCount = 0;
|
||||
if (responseObject.contains("total_count") && responseObject.getVal("total_count")->isIntegerNumber())
|
||||
totalCount = responseObject.getVal("total_count")->asIntegerNumber();
|
||||
if (responseObject.contains("offset") && responseObject.getVal("offset")->isIntegerNumber())
|
||||
received = responseObject.getVal("offset")->asIntegerNumber();
|
||||
if (responseObject.contains("limit") && responseObject.getVal("limit")->isIntegerNumber())
|
||||
received += responseObject.getVal("limit")->asIntegerNumber();
|
||||
bool hasMore = (received < totalCount);
|
||||
|
||||
if (hasMore) makeRequest(received);
|
||||
else finishListing(_files);
|
||||
|
||||
delete json;
|
||||
}
|
||||
|
||||
void BoxListDirectoryByIdRequest::errorCallback(const Networking::ErrorResponse &error) {
|
||||
_workingRequest = nullptr;
|
||||
if (_ignoreCallback) return;
|
||||
if (error.request) _date = error.request->date();
|
||||
finishError(error);
|
||||
}
|
||||
|
||||
void BoxListDirectoryByIdRequest::handle() {}
|
||||
|
||||
void BoxListDirectoryByIdRequest::restart() { start(); }
|
||||
|
||||
Common::String BoxListDirectoryByIdRequest::date() const { return _date; }
|
||||
|
||||
void BoxListDirectoryByIdRequest::finishListing(Common::Array<StorageFile> &files) {
|
||||
Request::finishSuccess();
|
||||
if (_listDirectoryCallback) (*_listDirectoryCallback)(Storage::ListDirectoryResponse(this, files));
|
||||
}
|
||||
|
||||
} // End of namespace Box
|
||||
} // End of namespace Cloud
|
||||
62
backends/cloud/box/boxlistdirectorybyidrequest.h
Normal file
62
backends/cloud/box/boxlistdirectorybyidrequest.h
Normal file
@@ -0,0 +1,62 @@
|
||||
/* 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 3 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, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef BACKENDS_CLOUD_BOX_BOXLISTDIRECTORYBYIDREQUEST_H
|
||||
#define BACKENDS_CLOUD_BOX_BOXLISTDIRECTORYBYIDREQUEST_H
|
||||
|
||||
#include "backends/cloud/storage.h"
|
||||
#include "backends/networking/http/httpjsonrequest.h"
|
||||
#include "backends/networking/http/request.h"
|
||||
#include "common/callback.h"
|
||||
|
||||
namespace Cloud {
|
||||
namespace Box {
|
||||
|
||||
class BoxStorage;
|
||||
|
||||
class BoxListDirectoryByIdRequest: public Networking::Request {
|
||||
Common::String _requestedId;
|
||||
BoxStorage *_storage;
|
||||
|
||||
Storage::ListDirectoryCallback _listDirectoryCallback;
|
||||
Common::Array<StorageFile> _files;
|
||||
Request *_workingRequest;
|
||||
bool _ignoreCallback;
|
||||
Common::String _date;
|
||||
|
||||
void start();
|
||||
void makeRequest(uint32 offset);
|
||||
void responseCallback(const Networking::JsonResponse &response);
|
||||
void errorCallback(const Networking::ErrorResponse &error);
|
||||
void finishListing(Common::Array<StorageFile> &files);
|
||||
public:
|
||||
BoxListDirectoryByIdRequest(BoxStorage *storage, const Common::String &id, Storage::ListDirectoryCallback cb, Networking::ErrorCallback ecb);
|
||||
~BoxListDirectoryByIdRequest() override;
|
||||
|
||||
void handle() override;
|
||||
void restart() override;
|
||||
Common::String date() const override;
|
||||
};
|
||||
|
||||
} // End of namespace Box
|
||||
} // End of namespace Cloud
|
||||
|
||||
#endif
|
||||
243
backends/cloud/box/boxstorage.cpp
Normal file
243
backends/cloud/box/boxstorage.cpp
Normal file
@@ -0,0 +1,243 @@
|
||||
/* 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 3 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, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "backends/cloud/box/boxstorage.h"
|
||||
#include "backends/cloud/box/boxlistdirectorybyidrequest.h"
|
||||
#include "backends/cloud/box/boxtokenrefresher.h"
|
||||
#include "backends/cloud/box/boxuploadrequest.h"
|
||||
#include "backends/cloud/cloudmanager.h"
|
||||
#include "backends/networking/http/connectionmanager.h"
|
||||
#include "backends/networking/http/httpjsonrequest.h"
|
||||
#include "backends/networking/http/networkreadstream.h"
|
||||
#include "common/config-manager.h"
|
||||
#include "common/debug.h"
|
||||
#include "common/formats/json.h"
|
||||
|
||||
namespace Cloud {
|
||||
namespace Box {
|
||||
|
||||
#define BOX_API_FOLDERS "https://api.box.com/2.0/folders"
|
||||
#define BOX_API_FILES_CONTENT "https://api.box.com/2.0/files/%s/content"
|
||||
#define BOX_API_USERS_ME "https://api.box.com/2.0/users/me"
|
||||
|
||||
BoxStorage::BoxStorage(const Common::String &token, const Common::String &refreshToken, bool enabled):
|
||||
IdStorage(token, refreshToken, enabled) {}
|
||||
|
||||
BoxStorage::BoxStorage(const Common::String &code, Networking::ErrorCallback cb) {
|
||||
getAccessToken(code, cb);
|
||||
}
|
||||
|
||||
BoxStorage::BoxStorage(const Networking::JsonResponse &codeFlowJson, Networking::ErrorCallback cb) {
|
||||
codeFlowComplete(cb, codeFlowJson);
|
||||
}
|
||||
|
||||
BoxStorage::~BoxStorage() {}
|
||||
|
||||
Common::String BoxStorage::cloudProvider() { return "box"; }
|
||||
|
||||
uint32 BoxStorage::storageIndex() { return kStorageBoxId; }
|
||||
|
||||
bool BoxStorage::needsRefreshToken() { return true; }
|
||||
|
||||
bool BoxStorage::canReuseRefreshToken() { return false; }
|
||||
|
||||
void BoxStorage::saveConfig(const Common::String &keyPrefix) {
|
||||
ConfMan.set(keyPrefix + "access_token", _token, ConfMan.kCloudDomain);
|
||||
ConfMan.set(keyPrefix + "refresh_token", _refreshToken, ConfMan.kCloudDomain);
|
||||
saveIsEnabledFlag(keyPrefix);
|
||||
}
|
||||
|
||||
Common::String BoxStorage::name() const {
|
||||
return "Box";
|
||||
}
|
||||
|
||||
void BoxStorage::infoInnerCallback(StorageInfoCallback outerCallback, const Networking::JsonResponse &response) {
|
||||
const Common::JSONValue *json = response.value;
|
||||
if (!json) {
|
||||
warning("BoxStorage::infoInnerCallback: NULL passed instead of JSON");
|
||||
delete outerCallback;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Networking::HttpJsonRequest::jsonIsObject(json, "BoxStorage::infoInnerCallback")) {
|
||||
delete json;
|
||||
delete outerCallback;
|
||||
return;
|
||||
}
|
||||
|
||||
Common::JSONObject jsonInfo = json->asObject();
|
||||
|
||||
Common::String uid, displayName, email;
|
||||
uint64 quotaUsed = 0, quotaAllocated = 0;
|
||||
|
||||
// can check that "type": "user"
|
||||
// there is also "max_upload_size", "phone" and "avatar_url"
|
||||
|
||||
if (Networking::HttpJsonRequest::jsonContainsString(jsonInfo, "id", "BoxStorage::infoInnerCallback"))
|
||||
uid = jsonInfo.getVal("id")->asString();
|
||||
|
||||
if (Networking::HttpJsonRequest::jsonContainsString(jsonInfo, "name", "BoxStorage::infoInnerCallback"))
|
||||
displayName = jsonInfo.getVal("name")->asString();
|
||||
|
||||
if (Networking::HttpJsonRequest::jsonContainsString(jsonInfo, "login", "BoxStorage::infoInnerCallback"))
|
||||
email = jsonInfo.getVal("login")->asString();
|
||||
|
||||
if (Networking::HttpJsonRequest::jsonContainsIntegerNumber(jsonInfo, "space_amount", "BoxStorage::infoInnerCallback"))
|
||||
quotaAllocated = jsonInfo.getVal("space_amount")->asIntegerNumber();
|
||||
|
||||
if (Networking::HttpJsonRequest::jsonContainsIntegerNumber(jsonInfo, "space_used", "BoxStorage::infoInnerCallback"))
|
||||
quotaUsed = jsonInfo.getVal("space_used")->asIntegerNumber();
|
||||
|
||||
Common::String username = email;
|
||||
if (username == "") username = displayName;
|
||||
if (username == "") username = uid;
|
||||
CloudMan.setStorageUsername(kStorageBoxId, username);
|
||||
|
||||
if (outerCallback) {
|
||||
(*outerCallback)(StorageInfoResponse(nullptr, StorageInfo(uid, displayName, email, quotaUsed, quotaAllocated)));
|
||||
delete outerCallback;
|
||||
}
|
||||
|
||||
delete json;
|
||||
}
|
||||
|
||||
Networking::Request *BoxStorage::listDirectoryById(const Common::String &id, ListDirectoryCallback callback, Networking::ErrorCallback errorCallback) {
|
||||
if (!errorCallback)
|
||||
errorCallback = getErrorPrintingCallback();
|
||||
if (!callback)
|
||||
callback = getPrintFilesCallback();
|
||||
return addRequest(new BoxListDirectoryByIdRequest(this, id, callback, errorCallback));
|
||||
}
|
||||
|
||||
void BoxStorage::createDirectoryInnerCallback(BoolCallback outerCallback, const Networking::JsonResponse &response) {
|
||||
const Common::JSONValue *json = response.value;
|
||||
if (!json) {
|
||||
warning("BoxStorage::createDirectoryInnerCallback: NULL passed instead of JSON");
|
||||
delete outerCallback;
|
||||
return;
|
||||
}
|
||||
|
||||
if (outerCallback) {
|
||||
if (Networking::HttpJsonRequest::jsonIsObject(json, "BoxStorage::createDirectoryInnerCallback")) {
|
||||
Common::JSONObject jsonInfo = json->asObject();
|
||||
(*outerCallback)(BoolResponse(nullptr, jsonInfo.contains("id")));
|
||||
} else {
|
||||
(*outerCallback)(BoolResponse(nullptr, false));
|
||||
}
|
||||
delete outerCallback;
|
||||
}
|
||||
|
||||
delete json;
|
||||
}
|
||||
|
||||
Networking::Request *BoxStorage::createDirectoryWithParentId(const Common::String &parentId, const Common::String &directoryName, BoolCallback callback, Networking::ErrorCallback errorCallback) {
|
||||
if (!errorCallback)
|
||||
errorCallback = getErrorPrintingCallback();
|
||||
|
||||
Common::String url = BOX_API_FOLDERS;
|
||||
Networking::JsonCallback innerCallback = new Common::CallbackBridge<BoxStorage, const BoolResponse &, const Networking::JsonResponse &>(this, &BoxStorage::createDirectoryInnerCallback, callback);
|
||||
Networking::HttpJsonRequest *request = new BoxTokenRefresher(this, innerCallback, errorCallback, url.c_str());
|
||||
request->addHeader("Authorization: Bearer " + accessToken());
|
||||
request->addHeader("Content-Type: application/json");
|
||||
|
||||
Common::JSONObject parentObject;
|
||||
parentObject.setVal("id", new Common::JSONValue(parentId));
|
||||
|
||||
Common::JSONObject jsonRequestParameters;
|
||||
jsonRequestParameters.setVal("name", new Common::JSONValue(directoryName));
|
||||
jsonRequestParameters.setVal("parent", new Common::JSONValue(parentObject));
|
||||
|
||||
Common::JSONValue value(jsonRequestParameters);
|
||||
request->addPostField(Common::JSON::stringify(&value));
|
||||
|
||||
return addRequest(request);
|
||||
}
|
||||
|
||||
Networking::Request *BoxStorage::upload(const Common::String &remotePath, const Common::Path &localPath, UploadCallback callback, Networking::ErrorCallback errorCallback) {
|
||||
if (!errorCallback)
|
||||
errorCallback = getErrorPrintingCallback();
|
||||
return addRequest(new BoxUploadRequest(this, remotePath, localPath, callback, errorCallback));
|
||||
}
|
||||
|
||||
Networking::Request *BoxStorage::upload(const Common::String &path, Common::SeekableReadStream *contents, UploadCallback callback, Networking::ErrorCallback errorCallback) {
|
||||
warning("BoxStorage::upload(ReadStream) not implemented");
|
||||
if (errorCallback)
|
||||
(*errorCallback)(Networking::ErrorResponse(nullptr, false, true, "BoxStorage::upload(ReadStream) not implemented", -1));
|
||||
delete callback;
|
||||
delete errorCallback;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool BoxStorage::uploadStreamSupported() {
|
||||
return false;
|
||||
}
|
||||
|
||||
Networking::Request *BoxStorage::streamFileById(const Common::String &id, Networking::NetworkReadStreamCallback callback, Networking::ErrorCallback errorCallback) {
|
||||
if (callback) {
|
||||
Common::String url = Common::String::format(BOX_API_FILES_CONTENT, id.c_str());
|
||||
Common::String header = "Authorization: Bearer " + _token;
|
||||
Networking::RequestHeaders *headersList = new Networking::RequestHeaders();
|
||||
headersList->push_back(header);
|
||||
Networking::NetworkReadStream *stream = Networking::NetworkReadStream::make(url.c_str(), headersList, "");
|
||||
(*callback)(Networking::NetworkReadStreamResponse(nullptr, stream));
|
||||
}
|
||||
delete callback;
|
||||
delete errorCallback;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Networking::Request *BoxStorage::info(StorageInfoCallback callback, Networking::ErrorCallback errorCallback) {
|
||||
Networking::JsonCallback innerCallback = new Common::CallbackBridge<BoxStorage, const StorageInfoResponse &, const Networking::JsonResponse &>(this, &BoxStorage::infoInnerCallback, callback);
|
||||
Networking::HttpJsonRequest *request = new BoxTokenRefresher(this, innerCallback, errorCallback, BOX_API_USERS_ME);
|
||||
request->addHeader("Authorization: Bearer " + _token);
|
||||
return addRequest(request);
|
||||
}
|
||||
|
||||
Common::String BoxStorage::savesDirectoryPath() { return "scummvm/saves/"; }
|
||||
|
||||
BoxStorage *BoxStorage::loadFromConfig(const Common::String &keyPrefix) {
|
||||
if (!ConfMan.hasKey(keyPrefix + "access_token", ConfMan.kCloudDomain)) {
|
||||
warning("BoxStorage: no access_token found");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!ConfMan.hasKey(keyPrefix + "refresh_token", ConfMan.kCloudDomain)) {
|
||||
warning("BoxStorage: 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 BoxStorage(accessToken, refreshToken, loadIsEnabledFlag(keyPrefix));
|
||||
}
|
||||
|
||||
void BoxStorage::removeFromConfig(const Common::String &keyPrefix) {
|
||||
ConfMan.removeKey(keyPrefix + "access_token", ConfMan.kCloudDomain);
|
||||
ConfMan.removeKey(keyPrefix + "refresh_token", ConfMan.kCloudDomain);
|
||||
removeIsEnabledFlag(keyPrefix);
|
||||
}
|
||||
|
||||
Common::String BoxStorage::getRootDirectoryId() {
|
||||
return "0";
|
||||
}
|
||||
|
||||
} // End of namespace Box
|
||||
} // End of namespace Cloud
|
||||
123
backends/cloud/box/boxstorage.h
Normal file
123
backends/cloud/box/boxstorage.h
Normal file
@@ -0,0 +1,123 @@
|
||||
/* 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 3 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, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef BACKENDS_CLOUD_BOX_BOXSTORAGE_H
|
||||
#define BACKENDS_CLOUD_BOX_BOXSTORAGE_H
|
||||
|
||||
#include "backends/cloud/id/idstorage.h"
|
||||
#include "backends/networking/http/httpjsonrequest.h"
|
||||
|
||||
namespace Cloud {
|
||||
namespace Box {
|
||||
|
||||
class BoxStorage: public Id::IdStorage {
|
||||
/** This private constructor is called from loadFromConfig(). */
|
||||
BoxStorage(const Common::String &token, const Common::String &refreshToken, bool enabled);
|
||||
|
||||
/** Constructs StorageInfo based on JSON response from cloud. */
|
||||
void infoInnerCallback(StorageInfoCallback outerCallback, const Networking::JsonResponse &json);
|
||||
|
||||
void createDirectoryInnerCallback(BoolCallback outerCallback, const Networking::JsonResponse &response);
|
||||
|
||||
protected:
|
||||
/**
|
||||
* @return "box"
|
||||
*/
|
||||
Common::String cloudProvider() override;
|
||||
|
||||
/**
|
||||
* @return kStorageBoxId
|
||||
*/
|
||||
uint32 storageIndex() override;
|
||||
|
||||
bool needsRefreshToken() override;
|
||||
|
||||
bool canReuseRefreshToken() override;
|
||||
|
||||
public:
|
||||
/** This constructor uses OAuth code flow to get tokens. */
|
||||
BoxStorage(const Common::String &code, Networking::ErrorCallback cb);
|
||||
|
||||
/** This constructor extracts tokens from JSON acquired via OAuth code flow. */
|
||||
BoxStorage(const Networking::JsonResponse &codeFlowJson, Networking::ErrorCallback cb);
|
||||
|
||||
~BoxStorage() override;
|
||||
|
||||
/**
|
||||
* 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").
|
||||
*/
|
||||
void saveConfig(const Common::String &keyPrefix) override;
|
||||
|
||||
/**
|
||||
* Return unique storage name.
|
||||
* @returns some unique storage name (for example, "Dropbox (user@example.com)")
|
||||
*/
|
||||
Common::String name() const override;
|
||||
|
||||
/** Public Cloud API comes down there. */
|
||||
|
||||
Networking::Request *listDirectoryById(const Common::String &id, ListDirectoryCallback callback, Networking::ErrorCallback errorCallback) override;
|
||||
Networking::Request *createDirectoryWithParentId(const Common::String &parentId, const Common::String &directoryName, BoolCallback callback, Networking::ErrorCallback errorCallback) override;
|
||||
|
||||
/** Returns UploadStatus struct with info about uploaded file. */
|
||||
Networking::Request *upload(const Common::String &remotePath, const Common::Path &localPath, UploadCallback callback, Networking::ErrorCallback errorCallback) override;
|
||||
Networking::Request *upload(const Common::String &path, Common::SeekableReadStream *contents, UploadCallback callback, Networking::ErrorCallback errorCallback) override;
|
||||
|
||||
/** Returns whether Storage supports upload(ReadStream). */
|
||||
bool uploadStreamSupported() override;
|
||||
|
||||
/** Returns pointer to Networking::NetworkReadStream. */
|
||||
Networking::Request *streamFileById(const Common::String &path, Networking::NetworkReadStreamCallback callback, Networking::ErrorCallback errorCallback) override;
|
||||
|
||||
/** Returns the StorageInfo struct. */
|
||||
Networking::Request *info(StorageInfoCallback callback, Networking::ErrorCallback errorCallback) override;
|
||||
|
||||
/** Returns storage's saves directory path with the trailing slash. */
|
||||
Common::String savesDirectoryPath() override;
|
||||
|
||||
/**
|
||||
* Load token and user id from configs and return BoxStorage for those.
|
||||
* @return pointer to the newly created BoxStorage or 0 if some problem occurred.
|
||||
*/
|
||||
static BoxStorage *loadFromConfig(const Common::String &keyPrefix);
|
||||
|
||||
/**
|
||||
* Remove all BoxStorage-related data from config.
|
||||
*/
|
||||
static void removeFromConfig(const Common::String &keyPrefix);
|
||||
|
||||
Common::String getRootDirectoryId() override;
|
||||
|
||||
Common::String accessToken() const { return _token; }
|
||||
};
|
||||
|
||||
} // End of namespace Box
|
||||
} // End of namespace Cloud
|
||||
|
||||
#endif
|
||||
120
backends/cloud/box/boxtokenrefresher.cpp
Normal file
120
backends/cloud/box/boxtokenrefresher.cpp
Normal file
@@ -0,0 +1,120 @@
|
||||
/* 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 3 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, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "backends/cloud/box/boxtokenrefresher.h"
|
||||
#include "backends/cloud/box/boxstorage.h"
|
||||
#include "backends/networking/http/networkreadstream.h"
|
||||
#include "common/debug.h"
|
||||
#include "common/formats/json.h"
|
||||
|
||||
namespace Cloud {
|
||||
namespace Box {
|
||||
|
||||
BoxTokenRefresher::BoxTokenRefresher(BoxStorage *parent, Networking::JsonCallback callback, Networking::ErrorCallback ecb, const char *url):
|
||||
HttpJsonRequest(callback, ecb, url), _parentStorage(parent) {}
|
||||
|
||||
BoxTokenRefresher::~BoxTokenRefresher() {}
|
||||
|
||||
void BoxTokenRefresher::tokenRefreshed(const Storage::BoolResponse &response) {
|
||||
if (!response.value) {
|
||||
//failed to refresh token, notify user with NULL in original callback
|
||||
warning("BoxTokenRefresher: failed to refresh token");
|
||||
finishError(Networking::ErrorResponse(this, false, true, "BoxTokenRefresher::tokenRefreshed: failed to refresh token", -1));
|
||||
return;
|
||||
}
|
||||
|
||||
//update headers: first change header with token, then pass those to request
|
||||
for (uint32 i = 0; i < _headersList.size(); ++i) {
|
||||
if (_headersList[i].contains("Authorization")) {
|
||||
_headersList[i] = "Authorization: Bearer " + _parentStorage->accessToken();
|
||||
}
|
||||
}
|
||||
|
||||
//successfully received refreshed token, can restart the original request now
|
||||
retry(0);
|
||||
}
|
||||
|
||||
void BoxTokenRefresher::finishJson(const Common::JSONValue *json) {
|
||||
if (!json) {
|
||||
//that's probably not an error (200 OK)
|
||||
HttpJsonRequest::finishJson(nullptr);
|
||||
return;
|
||||
}
|
||||
|
||||
if (jsonIsObject(json, "BoxTokenRefresher")) {
|
||||
Common::JSONObject result = json->asObject();
|
||||
if (result.contains("type") && result.getVal("type")->isString() && result.getVal("type")->asString() == "error") {
|
||||
//new token needed => request token & then retry original request
|
||||
long httpCode = -1;
|
||||
if (_stream) {
|
||||
httpCode = _stream->httpResponseCode();
|
||||
debug(9, "BoxTokenRefresher: code %ld", httpCode);
|
||||
}
|
||||
|
||||
bool irrecoverable = true;
|
||||
|
||||
Common::String code, message;
|
||||
if (jsonContainsString(result, "code", "BoxTokenRefresher")) {
|
||||
code = result.getVal("code")->asString();
|
||||
debug(9, "BoxTokenRefresher: code = %s", code.c_str());
|
||||
}
|
||||
|
||||
if (jsonContainsString(result, "message", "BoxTokenRefresher")) {
|
||||
message = result.getVal("message")->asString();
|
||||
debug(9, "BoxTokenRefresher: message = %s", message.c_str());
|
||||
}
|
||||
|
||||
//TODO: decide when token refreshment will help
|
||||
//for now refreshment is used only when HTTP 401 is passed in finishError()
|
||||
//if (code == "unauthenticated") irrecoverable = false;
|
||||
|
||||
if (irrecoverable) {
|
||||
finishError(Networking::ErrorResponse(this, false, true, json->stringify(true), httpCode));
|
||||
delete json;
|
||||
return;
|
||||
}
|
||||
|
||||
pause();
|
||||
delete json;
|
||||
_parentStorage->refreshAccessToken(new Common::Callback<BoxTokenRefresher, const Storage::BoolResponse &>(this, &BoxTokenRefresher::tokenRefreshed));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
//notify user of success
|
||||
HttpJsonRequest::finishJson(json);
|
||||
}
|
||||
|
||||
void BoxTokenRefresher::finishError(const Networking::ErrorResponse &error, Networking::RequestState state) {
|
||||
if (error.httpResponseCode == 401) { // invalid_token
|
||||
pause();
|
||||
_parentStorage->refreshAccessToken(new Common::Callback<BoxTokenRefresher, const Storage::BoolResponse &>(this, &BoxTokenRefresher::tokenRefreshed));
|
||||
return;
|
||||
}
|
||||
|
||||
// there are also 400 == invalid_request and 403 == insufficient_scope
|
||||
// but TokenRefresher is there to refresh token when it's invalid only
|
||||
|
||||
Request::finishError(error);
|
||||
}
|
||||
|
||||
} // End of namespace Box
|
||||
} // End of namespace Cloud
|
||||
48
backends/cloud/box/boxtokenrefresher.h
Normal file
48
backends/cloud/box/boxtokenrefresher.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/* 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 3 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, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef BACKENDS_CLOUD_BOX_BOXTOKENREFRESHER_H
|
||||
#define BACKENDS_CLOUD_BOX_BOXTOKENREFRESHER_H
|
||||
|
||||
#include "backends/cloud/storage.h"
|
||||
#include "backends/networking/http/httpjsonrequest.h"
|
||||
|
||||
namespace Cloud {
|
||||
namespace Box {
|
||||
|
||||
class BoxStorage;
|
||||
|
||||
class BoxTokenRefresher: public Networking::HttpJsonRequest {
|
||||
BoxStorage *_parentStorage;
|
||||
|
||||
void tokenRefreshed(const Storage::BoolResponse &response);
|
||||
|
||||
void finishJson(const Common::JSONValue *json) override;
|
||||
void finishError(const Networking::ErrorResponse &error, Networking::RequestState state = Networking::FINISHED) override;
|
||||
public:
|
||||
BoxTokenRefresher(BoxStorage *parent, Networking::JsonCallback callback, Networking::ErrorCallback ecb, const char *url);
|
||||
~BoxTokenRefresher() override;
|
||||
};
|
||||
|
||||
} // End of namespace Box
|
||||
} // End of namespace Cloud
|
||||
|
||||
#endif
|
||||
231
backends/cloud/box/boxuploadrequest.cpp
Normal file
231
backends/cloud/box/boxuploadrequest.cpp
Normal file
@@ -0,0 +1,231 @@
|
||||
/* 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 3 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, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "backends/cloud/box/boxuploadrequest.h"
|
||||
#include "backends/cloud/box/boxstorage.h"
|
||||
#include "backends/cloud/box/boxtokenrefresher.h"
|
||||
#include "backends/cloud/iso8601.h"
|
||||
#include "backends/cloud/storage.h"
|
||||
#include "backends/networking/http/connectionmanager.h"
|
||||
#include "backends/networking/http/httpjsonrequest.h"
|
||||
#include "backends/networking/http/networkreadstream.h"
|
||||
#include "common/formats/json.h"
|
||||
|
||||
namespace Cloud {
|
||||
namespace Box {
|
||||
|
||||
#define BOX_API_FILES "https://upload.box.com/api/2.0/files"
|
||||
|
||||
BoxUploadRequest::BoxUploadRequest(BoxStorage *storage, const Common::String &path, const Common::Path &localPath, Storage::UploadCallback callback, Networking::ErrorCallback ecb):
|
||||
Networking::Request(nullptr, ecb), _storage(storage), _savePath(path), _localPath(localPath), _uploadCallback(callback),
|
||||
_workingRequest(nullptr), _ignoreCallback(false) {
|
||||
start();
|
||||
}
|
||||
|
||||
BoxUploadRequest::~BoxUploadRequest() {
|
||||
_ignoreCallback = true;
|
||||
if (_workingRequest)
|
||||
_workingRequest->finish();
|
||||
delete _uploadCallback;
|
||||
}
|
||||
|
||||
void BoxUploadRequest::start() {
|
||||
_ignoreCallback = true;
|
||||
if (_workingRequest)
|
||||
_workingRequest->finish();
|
||||
_resolvedId = ""; //used to update file contents
|
||||
_parentId = ""; //used to create file within parent directory
|
||||
_ignoreCallback = false;
|
||||
|
||||
resolveId();
|
||||
}
|
||||
|
||||
void BoxUploadRequest::resolveId() {
|
||||
//check whether such file already exists
|
||||
Storage::UploadCallback innerCallback = new Common::Callback<BoxUploadRequest, const Storage::UploadResponse &>(this, &BoxUploadRequest::idResolvedCallback);
|
||||
Networking::ErrorCallback innerErrorCallback = new Common::Callback<BoxUploadRequest, const Networking::ErrorResponse &>(this, &BoxUploadRequest::idResolveFailedCallback);
|
||||
_workingRequest = _storage->resolveFileId(_savePath, innerCallback, innerErrorCallback);
|
||||
}
|
||||
|
||||
void BoxUploadRequest::idResolvedCallback(const Storage::UploadResponse &response) {
|
||||
_workingRequest = nullptr;
|
||||
if (_ignoreCallback) return;
|
||||
_resolvedId = response.value.id();
|
||||
upload();
|
||||
}
|
||||
|
||||
void BoxUploadRequest::idResolveFailedCallback(const 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;
|
||||
upload();
|
||||
return;
|
||||
}
|
||||
|
||||
finishError(error);
|
||||
}
|
||||
|
||||
void BoxUploadRequest::upload() {
|
||||
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 = BOX_API_FILES;
|
||||
if (_resolvedId != "")
|
||||
url += "/" + _resolvedId;
|
||||
url += "/content";
|
||||
Networking::JsonCallback callback = new Common::Callback<BoxUploadRequest, const Networking::JsonResponse &>(this, &BoxUploadRequest::uploadedCallback);
|
||||
Networking::ErrorCallback failureCallback = new Common::Callback<BoxUploadRequest, const Networking::ErrorResponse &>(this, &BoxUploadRequest::notUploadedCallback);
|
||||
Networking::HttpJsonRequest *request = new BoxTokenRefresher(_storage, callback, failureCallback, url.c_str());
|
||||
request->addHeader("Authorization: Bearer " + _storage->accessToken());
|
||||
|
||||
Common::JSONObject jsonRequestParameters;
|
||||
if (_resolvedId == "") {
|
||||
Common::JSONObject parentObject;
|
||||
parentObject.setVal("id", new Common::JSONValue(_parentId));
|
||||
jsonRequestParameters.setVal("parent", new Common::JSONValue(parentObject));
|
||||
jsonRequestParameters.setVal("name", new Common::JSONValue(name));
|
||||
}
|
||||
|
||||
Common::JSONValue value(jsonRequestParameters);
|
||||
request->addFormField("attributes", Common::JSON::stringify(&value));
|
||||
request->addFormFile("file", _localPath);
|
||||
|
||||
_workingRequest = ConnMan.addRequest(request);
|
||||
}
|
||||
|
||||
void BoxUploadRequest::uploadedCallback(const Networking::JsonResponse &response) {
|
||||
_workingRequest = nullptr;
|
||||
if (_ignoreCallback) return;
|
||||
|
||||
Networking::ErrorResponse error(this, false, true, "", -1);
|
||||
const Networking::HttpJsonRequest *rq = (const Networking::HttpJsonRequest *)response.request;
|
||||
if (rq) {
|
||||
const Networking::NetworkReadStream *stream = rq->getNetworkReadStream();
|
||||
if (stream) {
|
||||
long code = stream->httpResponseCode();
|
||||
error.httpResponseCode = code;
|
||||
}
|
||||
}
|
||||
|
||||
if (error.httpResponseCode != 200 && error.httpResponseCode != 201)
|
||||
warning("BoxUploadRequest: looks like an error (bad HTTP code)");
|
||||
|
||||
//check JSON and show warnings if it's malformed
|
||||
const Common::JSONValue *json = response.value;
|
||||
if (json == nullptr) {
|
||||
error.response = "Failed to parse JSON, null passed!";
|
||||
finishError(error);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!json->isObject()) {
|
||||
error.response = "Passed JSON is not an object!";
|
||||
finishError(error);
|
||||
delete json;
|
||||
return;
|
||||
}
|
||||
|
||||
Common::JSONObject object = json->asObject();
|
||||
if (Networking::HttpJsonRequest::jsonContainsArray(object, "entries", "BoxUploadRequest")) {
|
||||
Common::JSONArray entries = object.getVal("entries")->asArray();
|
||||
if (entries.size() == 0) {
|
||||
warning("BoxUploadRequest: 'entries' found, but it's empty");
|
||||
} else if (!Networking::HttpJsonRequest::jsonIsObject(entries[0], "BoxUploadRequest")) {
|
||||
warning("BoxUploadRequest: 'entries' first item is not an object");
|
||||
} else {
|
||||
Common::JSONObject item = entries[0]->asObject();
|
||||
|
||||
if (Networking::HttpJsonRequest::jsonContainsString(item, "id", "BoxUploadRequest") &&
|
||||
Networking::HttpJsonRequest::jsonContainsString(item, "name", "BoxUploadRequest") &&
|
||||
Networking::HttpJsonRequest::jsonContainsString(item, "type", "BoxUploadRequest") &&
|
||||
Networking::HttpJsonRequest::jsonContainsString(item, "modified_at", "BoxUploadRequest") &&
|
||||
Networking::HttpJsonRequest::jsonContainsStringOrIntegerNumber(item, "size", "BoxUploadRequest")) {
|
||||
|
||||
//finished
|
||||
Common::String id = item.getVal("id")->asString();
|
||||
Common::String name = item.getVal("name")->asString();
|
||||
bool isDirectory = (item.getVal("type")->asString() == "folder");
|
||||
uint32 size;
|
||||
if (item.getVal("size")->isString()) {
|
||||
size = item.getVal("size")->asString().asUint64();
|
||||
} else {
|
||||
size = item.getVal("size")->asIntegerNumber();
|
||||
}
|
||||
uint32 timestamp = ISO8601::convertToTimestamp(item.getVal("modified_at")->asString());
|
||||
|
||||
finishUpload(StorageFile(id, _savePath, name, size, timestamp, isDirectory));
|
||||
delete json;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: check errors
|
||||
/*
|
||||
if (object.contains("error")) {
|
||||
warning("Box returned error: %s", json->stringify(true).c_str());
|
||||
delete json;
|
||||
error.response = json->stringify(true);
|
||||
finishError(error);
|
||||
return;
|
||||
}
|
||||
*/
|
||||
|
||||
warning("BoxUploadRequest: no file info to return");
|
||||
finishUpload(StorageFile(_savePath, 0, 0, false));
|
||||
|
||||
delete json;
|
||||
}
|
||||
|
||||
void BoxUploadRequest::notUploadedCallback(const Networking::ErrorResponse &error) {
|
||||
_workingRequest = nullptr;
|
||||
if (_ignoreCallback) return;
|
||||
finishError(error);
|
||||
}
|
||||
|
||||
void BoxUploadRequest::handle() {}
|
||||
|
||||
void BoxUploadRequest::restart() { start(); }
|
||||
|
||||
void BoxUploadRequest::finishUpload(const StorageFile &file) {
|
||||
Request::finishSuccess();
|
||||
if (_uploadCallback)
|
||||
(*_uploadCallback)(Storage::UploadResponse(this, file));
|
||||
}
|
||||
|
||||
} // End of namespace Box
|
||||
} // End of namespace Cloud
|
||||
63
backends/cloud/box/boxuploadrequest.h
Normal file
63
backends/cloud/box/boxuploadrequest.h
Normal file
@@ -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 3 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, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef BACKENDS_CLOUD_BOX_BOXUPLOADREQUEST_H
|
||||
#define BACKENDS_CLOUD_BOX_BOXUPLOADREQUEST_H
|
||||
|
||||
#include "backends/cloud/storage.h"
|
||||
#include "backends/networking/http/httpjsonrequest.h"
|
||||
#include "backends/networking/http/request.h"
|
||||
#include "common/callback.h"
|
||||
|
||||
namespace Cloud {
|
||||
namespace Box {
|
||||
class BoxStorage;
|
||||
|
||||
class BoxUploadRequest: public Networking::Request {
|
||||
BoxStorage *_storage;
|
||||
Common::String _savePath;
|
||||
Common::Path _localPath;
|
||||
Storage::UploadCallback _uploadCallback;
|
||||
Request *_workingRequest;
|
||||
bool _ignoreCallback;
|
||||
Common::String _resolvedId, _parentId;
|
||||
|
||||
void start();
|
||||
void resolveId();
|
||||
void idResolvedCallback(const Storage::UploadResponse &response);
|
||||
void idResolveFailedCallback(const Networking::ErrorResponse &error);
|
||||
void upload();
|
||||
void uploadedCallback(const Networking::JsonResponse &response);
|
||||
void notUploadedCallback(const Networking::ErrorResponse &error);
|
||||
void finishUpload(const StorageFile &status);
|
||||
|
||||
public:
|
||||
BoxUploadRequest(BoxStorage *storage, const Common::String &path, const Common::Path &localPath, Storage::UploadCallback callback, Networking::ErrorCallback ecb);
|
||||
~BoxUploadRequest() override;
|
||||
|
||||
void handle() override;
|
||||
void restart() override;
|
||||
};
|
||||
|
||||
} // End of namespace Box
|
||||
} // End of namespace Cloud
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user