Initial commit
This commit is contained in:
138
backends/cloud/dropbox/dropboxcreatedirectoryrequest.cpp
Normal file
138
backends/cloud/dropbox/dropboxcreatedirectoryrequest.cpp
Normal file
@@ -0,0 +1,138 @@
|
||||
/* 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/dropbox/dropboxcreatedirectoryrequest.h"
|
||||
#include "backends/cloud/dropbox/dropboxstorage.h"
|
||||
#include "backends/cloud/dropbox/dropboxtokenrefresher.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 Dropbox {
|
||||
|
||||
#define DROPBOX_API_CREATE_FOLDER "https://api.dropboxapi.com/2/files/create_folder"
|
||||
|
||||
DropboxCreateDirectoryRequest::DropboxCreateDirectoryRequest(DropboxStorage *storage, const Common::String &path, Storage::BoolCallback cb, Networking::ErrorCallback ecb):
|
||||
Networking::Request(nullptr, ecb), _storage(storage), _path(path), _boolCallback(cb),
|
||||
_workingRequest(nullptr), _ignoreCallback(false) {
|
||||
start();
|
||||
}
|
||||
|
||||
DropboxCreateDirectoryRequest::~DropboxCreateDirectoryRequest() {
|
||||
_ignoreCallback = true;
|
||||
if (_workingRequest)
|
||||
_workingRequest->finish();
|
||||
delete _boolCallback;
|
||||
}
|
||||
|
||||
void DropboxCreateDirectoryRequest::start() {
|
||||
_ignoreCallback = true;
|
||||
if (_workingRequest)
|
||||
_workingRequest->finish();
|
||||
_ignoreCallback = false;
|
||||
|
||||
Networking::JsonCallback innerCallback = new Common::Callback<DropboxCreateDirectoryRequest, const Networking::JsonResponse &>(this, &DropboxCreateDirectoryRequest::responseCallback);
|
||||
Networking::ErrorCallback errorResponseCallback = new Common::Callback<DropboxCreateDirectoryRequest, const Networking::ErrorResponse &>(this, &DropboxCreateDirectoryRequest::errorCallback);
|
||||
Networking::HttpJsonRequest *request = new DropboxTokenRefresher(_storage, innerCallback, errorResponseCallback, DROPBOX_API_CREATE_FOLDER);
|
||||
request->addHeader("Authorization: Bearer " + _storage->accessToken());
|
||||
request->addHeader("Content-Type: application/json");
|
||||
|
||||
Common::JSONObject jsonRequestParameters;
|
||||
jsonRequestParameters.setVal("path", new Common::JSONValue(_path));
|
||||
Common::JSONValue value(jsonRequestParameters);
|
||||
request->addPostField(Common::JSON::stringify(&value));
|
||||
|
||||
_workingRequest = ConnMan.addRequest(request);
|
||||
}
|
||||
|
||||
void DropboxCreateDirectoryRequest::responseCallback(const Networking::JsonResponse &response) {
|
||||
const Common::JSONValue *json = response.value;
|
||||
_workingRequest = nullptr;
|
||||
if (_ignoreCallback) {
|
||||
delete json;
|
||||
return;
|
||||
}
|
||||
if (response.request) _date = response.request->date();
|
||||
|
||||
Networking::ErrorResponse error(this, "DropboxCreateDirectoryRequest::responseCallback: unknown error");
|
||||
const Networking::HttpJsonRequest *rq = (const Networking::HttpJsonRequest *)response.request;
|
||||
if (rq && rq->getNetworkReadStream())
|
||||
error.httpResponseCode = rq->getNetworkReadStream()->httpResponseCode();
|
||||
|
||||
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 info = json->asObject();
|
||||
if (info.contains("id")) {
|
||||
finishCreation(true);
|
||||
} else {
|
||||
if (Networking::HttpJsonRequest::jsonContainsString(info, "error_summary", "DropboxCreateDirectoryRequest")) {
|
||||
Common::String summary = info.getVal("error_summary")->asString();
|
||||
if (summary.contains("path") && summary.contains("conflict") && summary.contains("folder")) {
|
||||
// existing directory - not an error for CreateDirectoryRequest
|
||||
finishCreation(false);
|
||||
delete json;
|
||||
return;
|
||||
}
|
||||
}
|
||||
error.response = json->stringify(true);
|
||||
finishError(error);
|
||||
}
|
||||
|
||||
delete json;
|
||||
}
|
||||
|
||||
void DropboxCreateDirectoryRequest::errorCallback(const Networking::ErrorResponse &error) {
|
||||
_workingRequest = nullptr;
|
||||
if (_ignoreCallback)
|
||||
return;
|
||||
if (error.request)
|
||||
_date = error.request->date();
|
||||
finishError(error);
|
||||
}
|
||||
|
||||
void DropboxCreateDirectoryRequest::handle() {}
|
||||
|
||||
void DropboxCreateDirectoryRequest::restart() { start(); }
|
||||
|
||||
Common::String DropboxCreateDirectoryRequest::date() const { return _date; }
|
||||
|
||||
void DropboxCreateDirectoryRequest::finishCreation(bool success) {
|
||||
Request::finishSuccess();
|
||||
if (_boolCallback)
|
||||
(*_boolCallback)(Storage::BoolResponse(this, success));
|
||||
}
|
||||
|
||||
} // End of namespace Dropbox
|
||||
} // End of namespace Cloud
|
||||
58
backends/cloud/dropbox/dropboxcreatedirectoryrequest.h
Normal file
58
backends/cloud/dropbox/dropboxcreatedirectoryrequest.h
Normal file
@@ -0,0 +1,58 @@
|
||||
/* 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_DROPBOX_DROPBOXCREATEDIRECTORYREQUEST_H
|
||||
#define BACKENDS_CLOUD_DROPBOX_DROPBOXCREATEDIRECTORYREQUEST_H
|
||||
|
||||
#include "backends/cloud/storage.h"
|
||||
#include "backends/networking/http/request.h"
|
||||
#include "backends/networking/http/httpjsonrequest.h"
|
||||
|
||||
namespace Cloud {
|
||||
namespace Dropbox {
|
||||
|
||||
class DropboxStorage;
|
||||
|
||||
class DropboxCreateDirectoryRequest: public Networking::Request {
|
||||
DropboxStorage *_storage;
|
||||
Common::String _path;
|
||||
Storage::BoolCallback _boolCallback;
|
||||
Request *_workingRequest;
|
||||
bool _ignoreCallback;
|
||||
Common::String _date;
|
||||
|
||||
void start();
|
||||
void responseCallback(const Networking::JsonResponse &response);
|
||||
void errorCallback(const Networking::ErrorResponse &error);
|
||||
void finishCreation(bool success);
|
||||
public:
|
||||
DropboxCreateDirectoryRequest(DropboxStorage *storage, const Common::String &path, Storage::BoolCallback cb, Networking::ErrorCallback ecb);
|
||||
~DropboxCreateDirectoryRequest() override;
|
||||
|
||||
void handle() override;
|
||||
void restart() override;
|
||||
Common::String date() const override;
|
||||
};
|
||||
|
||||
} // End of namespace Dropbox
|
||||
} // End of namespace Cloud
|
||||
|
||||
#endif
|
||||
193
backends/cloud/dropbox/dropboxinforequest.cpp
Normal file
193
backends/cloud/dropbox/dropboxinforequest.cpp
Normal file
@@ -0,0 +1,193 @@
|
||||
/* 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/dropbox/dropboxinforequest.h"
|
||||
#include "backends/cloud/dropbox/dropboxstorage.h"
|
||||
#include "backends/cloud/dropbox/dropboxtokenrefresher.h"
|
||||
#include "backends/cloud/cloudmanager.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 Dropbox {
|
||||
|
||||
#define DROPBOX_API_GET_CURRENT_ACCOUNT "https://api.dropboxapi.com/2/users/get_current_account"
|
||||
#define DROPBOX_API_GET_SPACE_USAGE "https://api.dropboxapi.com/2/users/get_space_usage"
|
||||
|
||||
DropboxInfoRequest::DropboxInfoRequest(DropboxStorage *storage, Storage::StorageInfoCallback cb, Networking::ErrorCallback ecb):
|
||||
Networking::Request(nullptr, ecb), _storage(storage), _infoCallback(cb),
|
||||
_workingRequest(nullptr), _ignoreCallback(false) {
|
||||
start();
|
||||
}
|
||||
|
||||
DropboxInfoRequest::~DropboxInfoRequest() {
|
||||
_ignoreCallback = true;
|
||||
if (_workingRequest)
|
||||
_workingRequest->finish();
|
||||
delete _infoCallback;
|
||||
}
|
||||
|
||||
void DropboxInfoRequest::start() {
|
||||
_ignoreCallback = true;
|
||||
if (_workingRequest)
|
||||
_workingRequest->finish();
|
||||
_ignoreCallback = false;
|
||||
|
||||
Networking::JsonCallback innerCallback = new Common::Callback<DropboxInfoRequest, const Networking::JsonResponse &>(this, &DropboxInfoRequest::userResponseCallback);
|
||||
Networking::ErrorCallback errorResponseCallback = new Common::Callback<DropboxInfoRequest, const Networking::ErrorResponse &>(this, &DropboxInfoRequest::errorCallback);
|
||||
Networking::HttpJsonRequest *request = new DropboxTokenRefresher(_storage, innerCallback, errorResponseCallback, DROPBOX_API_GET_CURRENT_ACCOUNT);
|
||||
request->addHeader("Authorization: Bearer " + _storage->accessToken());
|
||||
request->addHeader("Content-Type: application/json");
|
||||
request->addPostField("null"); //use POST
|
||||
|
||||
_workingRequest = ConnMan.addRequest(request);
|
||||
}
|
||||
|
||||
void DropboxInfoRequest::userResponseCallback(const Networking::JsonResponse &response) {
|
||||
const Common::JSONValue *json = response.value;
|
||||
_workingRequest = nullptr;
|
||||
if (_ignoreCallback) {
|
||||
delete json;
|
||||
return;
|
||||
}
|
||||
|
||||
Networking::ErrorResponse error(this, "DropboxInfoRequest::userResponseCallback: unknown error");
|
||||
const Networking::HttpJsonRequest *rq = (const Networking::HttpJsonRequest *)response.request;
|
||||
if (rq && rq->getNetworkReadStream())
|
||||
error.httpResponseCode = rq->getNetworkReadStream()->httpResponseCode();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
//Dropbox documentation states there are no errors for this API method
|
||||
Common::JSONObject info = json->asObject();
|
||||
if (Networking::HttpJsonRequest::jsonContainsAttribute(info, "name", "DropboxInfoRequest") &&
|
||||
Networking::HttpJsonRequest::jsonIsObject(info.getVal("name"), "DropboxInfoRequest")) {
|
||||
Common::JSONObject nameInfo = info.getVal("name")->asObject();
|
||||
if (Networking::HttpJsonRequest::jsonContainsString(nameInfo, "display_name", "DropboxInfoRequest")) {
|
||||
_name = nameInfo.getVal("display_name")->asString();
|
||||
}
|
||||
}
|
||||
if (Networking::HttpJsonRequest::jsonContainsString(info, "account_id", "DropboxInfoRequest")) {
|
||||
_uid = info.getVal("account_id")->asString();
|
||||
}
|
||||
if (Networking::HttpJsonRequest::jsonContainsString(info, "email", "DropboxInfoRequest")) {
|
||||
_email = info.getVal("email")->asString();
|
||||
}
|
||||
CloudMan.setStorageUsername(kStorageDropboxId, _email);
|
||||
delete json;
|
||||
|
||||
Networking::JsonCallback innerCallback = new Common::Callback<DropboxInfoRequest, const Networking::JsonResponse &>(this, &DropboxInfoRequest::quotaResponseCallback);
|
||||
Networking::ErrorCallback errorResponseCallback = new Common::Callback<DropboxInfoRequest, const Networking::ErrorResponse &>(this, &DropboxInfoRequest::errorCallback);
|
||||
Networking::HttpJsonRequest *request = new DropboxTokenRefresher(_storage, innerCallback, errorResponseCallback, DROPBOX_API_GET_SPACE_USAGE);
|
||||
request->addHeader("Authorization: Bearer " + _storage->accessToken());
|
||||
request->addHeader("Content-Type: application/json");
|
||||
request->addPostField("null"); //use POST
|
||||
|
||||
_workingRequest = ConnMan.addRequest(request);
|
||||
}
|
||||
|
||||
void DropboxInfoRequest::quotaResponseCallback(const Networking::JsonResponse &response) {
|
||||
const Common::JSONValue *json = response.value;
|
||||
_workingRequest = nullptr;
|
||||
if (_ignoreCallback) {
|
||||
delete json;
|
||||
return;
|
||||
}
|
||||
|
||||
Networking::ErrorResponse error(this, "DropboxInfoRequest::quotaResponseCallback: unknown error");
|
||||
const Networking::HttpJsonRequest *rq = (const Networking::HttpJsonRequest *)response.request;
|
||||
if (rq && rq->getNetworkReadStream())
|
||||
error.httpResponseCode = rq->getNetworkReadStream()->httpResponseCode();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
//Dropbox documentation states there are no errors for this API method
|
||||
Common::JSONObject info = json->asObject();
|
||||
|
||||
if (!Networking::HttpJsonRequest::jsonContainsIntegerNumber(info, "used", "DropboxInfoRequest")) {
|
||||
error.response = "Passed JSON misses 'used' attribute!";
|
||||
finishError(error);
|
||||
delete json;
|
||||
return;
|
||||
}
|
||||
|
||||
uint64 used = info.getVal("used")->asIntegerNumber(), allocated = 0;
|
||||
|
||||
if (Networking::HttpJsonRequest::jsonContainsAttribute(info, "allocation", "DropboxInfoRequest") &&
|
||||
Networking::HttpJsonRequest::jsonIsObject(info.getVal("allocation"), "DropboxInfoRequest")) {
|
||||
Common::JSONObject allocation = info.getVal("allocation")->asObject();
|
||||
if (!Networking::HttpJsonRequest::jsonContainsIntegerNumber(allocation, "allocated", "DropboxInfoRequest")) {
|
||||
error.response = "Passed JSON misses 'allocation/allocated' attribute!";
|
||||
finishError(error);
|
||||
delete json;
|
||||
return;
|
||||
}
|
||||
|
||||
allocated = allocation.getVal("allocated")->asIntegerNumber();
|
||||
}
|
||||
|
||||
finishInfo(StorageInfo(_uid, _name, _email, used, allocated));
|
||||
delete json;
|
||||
}
|
||||
|
||||
void DropboxInfoRequest::errorCallback(const Networking::ErrorResponse &error) {
|
||||
_workingRequest = nullptr;
|
||||
if (_ignoreCallback) return;
|
||||
finishError(error);
|
||||
}
|
||||
|
||||
void DropboxInfoRequest::handle() {}
|
||||
|
||||
void DropboxInfoRequest::restart() { start(); }
|
||||
|
||||
void DropboxInfoRequest::finishInfo(const StorageInfo &info) {
|
||||
Request::finishSuccess();
|
||||
if (_infoCallback)
|
||||
(*_infoCallback)(Storage::StorageInfoResponse(this, info));
|
||||
}
|
||||
|
||||
} // End of namespace Dropbox
|
||||
} // End of namespace Cloud
|
||||
57
backends/cloud/dropbox/dropboxinforequest.h
Normal file
57
backends/cloud/dropbox/dropboxinforequest.h
Normal file
@@ -0,0 +1,57 @@
|
||||
/* 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_DROPBOX_DROPBOXINFOREQUEST_H
|
||||
#define BACKENDS_CLOUD_DROPBOX_DROPBOXINFOREQUEST_H
|
||||
|
||||
#include "backends/cloud/storage.h"
|
||||
#include "backends/networking/http/request.h"
|
||||
#include "backends/networking/http/httpjsonrequest.h"
|
||||
|
||||
namespace Cloud {
|
||||
namespace Dropbox {
|
||||
|
||||
class DropboxStorage;
|
||||
|
||||
class DropboxInfoRequest: public Networking::Request {
|
||||
DropboxStorage *_storage;
|
||||
Common::String _uid, _name, _email;
|
||||
Storage::StorageInfoCallback _infoCallback;
|
||||
Request *_workingRequest;
|
||||
bool _ignoreCallback;
|
||||
|
||||
void start();
|
||||
void userResponseCallback(const Networking::JsonResponse &response);
|
||||
void quotaResponseCallback(const Networking::JsonResponse &response);
|
||||
void errorCallback(const Networking::ErrorResponse &error);
|
||||
void finishInfo(const StorageInfo &info);
|
||||
public:
|
||||
DropboxInfoRequest(DropboxStorage *storage, Storage::StorageInfoCallback cb, Networking::ErrorCallback ecb);
|
||||
~DropboxInfoRequest() override;
|
||||
|
||||
void handle() override;
|
||||
void restart() override;
|
||||
};
|
||||
|
||||
} // End of namespace Dropbox
|
||||
} // End of namespace Cloud
|
||||
|
||||
#endif
|
||||
224
backends/cloud/dropbox/dropboxlistdirectoryrequest.cpp
Normal file
224
backends/cloud/dropbox/dropboxlistdirectoryrequest.cpp
Normal file
@@ -0,0 +1,224 @@
|
||||
/* 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/dropbox/dropboxlistdirectoryrequest.h"
|
||||
#include "backends/cloud/dropbox/dropboxstorage.h"
|
||||
#include "backends/cloud/dropbox/dropboxtokenrefresher.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"
|
||||
#include "common/debug.h"
|
||||
|
||||
namespace Cloud {
|
||||
namespace Dropbox {
|
||||
|
||||
#define DROPBOX_API_LIST_FOLDER "https://api.dropboxapi.com/2/files/list_folder"
|
||||
#define DROPBOX_API_LIST_FOLDER_CONTINUE "https://api.dropboxapi.com/2/files/list_folder/continue"
|
||||
|
||||
DropboxListDirectoryRequest::DropboxListDirectoryRequest(DropboxStorage *storage, const Common::String &path, Storage::ListDirectoryCallback cb, Networking::ErrorCallback ecb, bool recursive):
|
||||
Networking::Request(nullptr, ecb), _requestedPath(path), _requestedRecursive(recursive), _listDirectoryCallback(cb),
|
||||
_storage(storage), _workingRequest(nullptr), _ignoreCallback(false) {
|
||||
start();
|
||||
}
|
||||
|
||||
DropboxListDirectoryRequest::~DropboxListDirectoryRequest() {
|
||||
_ignoreCallback = true;
|
||||
if (_workingRequest)
|
||||
_workingRequest->finish();
|
||||
delete _listDirectoryCallback;
|
||||
}
|
||||
|
||||
void DropboxListDirectoryRequest::start() {
|
||||
_ignoreCallback = true;
|
||||
if (_workingRequest)
|
||||
_workingRequest->finish();
|
||||
_files.clear();
|
||||
_ignoreCallback = false;
|
||||
|
||||
Networking::JsonCallback callback = new Common::Callback<DropboxListDirectoryRequest, const Networking::JsonResponse &>(this, &DropboxListDirectoryRequest::responseCallback);
|
||||
Networking::ErrorCallback failureCallback = new Common::Callback<DropboxListDirectoryRequest, const Networking::ErrorResponse &>(this, &DropboxListDirectoryRequest::errorCallback);
|
||||
Networking::HttpJsonRequest *request = new DropboxTokenRefresher(_storage, callback, failureCallback, DROPBOX_API_LIST_FOLDER);
|
||||
request->addHeader("Authorization: Bearer " + _storage->accessToken());
|
||||
request->addHeader("Content-Type: application/json");
|
||||
|
||||
Common::JSONObject jsonRequestParameters;
|
||||
jsonRequestParameters.setVal("path", new Common::JSONValue(_requestedPath));
|
||||
jsonRequestParameters.setVal("recursive", new Common::JSONValue(_requestedRecursive));
|
||||
jsonRequestParameters.setVal("include_media_info", new Common::JSONValue(false));
|
||||
jsonRequestParameters.setVal("include_deleted", new Common::JSONValue(false));
|
||||
|
||||
Common::JSONValue value(jsonRequestParameters);
|
||||
request->addPostField(Common::JSON::stringify(&value));
|
||||
|
||||
_workingRequest = ConnMan.addRequest(request);
|
||||
}
|
||||
|
||||
void DropboxListDirectoryRequest::responseCallback(const Networking::JsonResponse &response) {
|
||||
_workingRequest = nullptr;
|
||||
|
||||
if (_ignoreCallback) {
|
||||
delete response.value;
|
||||
return;
|
||||
}
|
||||
|
||||
if (response.request)
|
||||
_date = response.request->date();
|
||||
|
||||
Networking::ErrorResponse error(this, "DropboxListDirectoryRequest::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();
|
||||
|
||||
if (responseObject.contains("error") || responseObject.contains("error_summary")) {
|
||||
if (responseObject.contains("error_summary") && responseObject.getVal("error_summary")->isString()) {
|
||||
warning("Dropbox 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], "DropboxListDirectoryRequest"))
|
||||
continue;
|
||||
|
||||
Common::JSONObject item = items[i]->asObject();
|
||||
|
||||
if (!Networking::HttpJsonRequest::jsonContainsString(item, "path_lower", "DropboxListDirectoryRequest"))
|
||||
continue;
|
||||
if (!Networking::HttpJsonRequest::jsonContainsString(item, ".tag", "DropboxListDirectoryRequest"))
|
||||
continue;
|
||||
|
||||
Common::String path = item.getVal("path_lower")->asString();
|
||||
bool isDirectory = (item.getVal(".tag")->asString() == "folder");
|
||||
uint32 size = 0, timestamp = 0;
|
||||
if (!isDirectory) {
|
||||
if (!Networking::HttpJsonRequest::jsonContainsString(item, "server_modified", "DropboxListDirectoryRequest"))
|
||||
continue;
|
||||
if (!Networking::HttpJsonRequest::jsonContainsIntegerNumber(item, "size", "DropboxListDirectoryRequest"))
|
||||
continue;
|
||||
|
||||
size = item.getVal("size")->asIntegerNumber();
|
||||
timestamp = ISO8601::convertToTimestamp(item.getVal("server_modified")->asString());
|
||||
}
|
||||
_files.push_back(StorageFile(path, size, timestamp, isDirectory));
|
||||
}
|
||||
}
|
||||
|
||||
bool hasMore = false;
|
||||
if (responseObject.contains("has_more")) {
|
||||
if (!responseObject.getVal("has_more")->isBool()) {
|
||||
warning("DropboxListDirectoryRequest: \"has_more\" is not a boolean");
|
||||
debug(9, "%s", responseObject.getVal("has_more")->stringify(true).c_str());
|
||||
error.response = "\"has_more\" is not a boolean!";
|
||||
finishError(error);
|
||||
delete json;
|
||||
return;
|
||||
}
|
||||
|
||||
hasMore = responseObject.getVal("has_more")->asBool();
|
||||
}
|
||||
|
||||
if (hasMore) {
|
||||
if (!Networking::HttpJsonRequest::jsonContainsString(responseObject, "cursor", "DropboxListDirectoryRequest")) {
|
||||
error.response = "\"has_more\" found, but \"cursor\" is not (or it's not a string)!";
|
||||
finishError(error);
|
||||
delete json;
|
||||
return;
|
||||
}
|
||||
|
||||
Networking::JsonCallback callback = new Common::Callback<DropboxListDirectoryRequest, const Networking::JsonResponse &>(this, &DropboxListDirectoryRequest::responseCallback);
|
||||
Networking::ErrorCallback failureCallback = new Common::Callback<DropboxListDirectoryRequest, const Networking::ErrorResponse &>(this, &DropboxListDirectoryRequest::errorCallback);
|
||||
Networking::HttpJsonRequest *request = new DropboxTokenRefresher(_storage, callback, failureCallback, DROPBOX_API_LIST_FOLDER_CONTINUE);
|
||||
request->addHeader("Authorization: Bearer " + _storage->accessToken());
|
||||
request->addHeader("Content-Type: application/json");
|
||||
|
||||
Common::JSONObject jsonRequestParameters;
|
||||
jsonRequestParameters.setVal("cursor", new Common::JSONValue(responseObject.getVal("cursor")->asString()));
|
||||
|
||||
Common::JSONValue value(jsonRequestParameters);
|
||||
request->addPostField(Common::JSON::stringify(&value));
|
||||
|
||||
_workingRequest = ConnMan.addRequest(request);
|
||||
} else {
|
||||
finishListing(_files);
|
||||
}
|
||||
|
||||
delete json;
|
||||
}
|
||||
|
||||
void DropboxListDirectoryRequest::errorCallback(const Networking::ErrorResponse &error) {
|
||||
_workingRequest = nullptr;
|
||||
if (_ignoreCallback)
|
||||
return;
|
||||
if (error.request)
|
||||
_date = error.request->date();
|
||||
finishError(error);
|
||||
}
|
||||
|
||||
void DropboxListDirectoryRequest::handle() {}
|
||||
|
||||
void DropboxListDirectoryRequest::restart() { start(); }
|
||||
|
||||
Common::String DropboxListDirectoryRequest::date() const { return _date; }
|
||||
|
||||
void DropboxListDirectoryRequest::finishListing(const Common::Array<StorageFile> &files) {
|
||||
Request::finishSuccess();
|
||||
if (_listDirectoryCallback)
|
||||
(*_listDirectoryCallback)(Storage::ListDirectoryResponse(this, files));
|
||||
}
|
||||
|
||||
} // End of namespace Dropbox
|
||||
} // End of namespace Cloud
|
||||
62
backends/cloud/dropbox/dropboxlistdirectoryrequest.h
Normal file
62
backends/cloud/dropbox/dropboxlistdirectoryrequest.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_DROPBOX_DROPBOXLISTDIRECTORYREQUEST_H
|
||||
#define BACKENDS_CLOUD_DROPBOX_DROPBOXLISTDIRECTORYREQUEST_H
|
||||
|
||||
#include "backends/cloud/storage.h"
|
||||
#include "backends/networking/http/request.h"
|
||||
#include "common/callback.h"
|
||||
#include "backends/networking/http/httpjsonrequest.h"
|
||||
|
||||
namespace Cloud {
|
||||
namespace Dropbox {
|
||||
|
||||
class DropboxStorage;
|
||||
|
||||
class DropboxListDirectoryRequest: public Networking::Request {
|
||||
Common::String _requestedPath;
|
||||
bool _requestedRecursive;
|
||||
|
||||
Storage::ListDirectoryCallback _listDirectoryCallback;
|
||||
DropboxStorage *_storage;
|
||||
Common::Array<StorageFile> _files;
|
||||
Request *_workingRequest;
|
||||
bool _ignoreCallback;
|
||||
Common::String _date;
|
||||
|
||||
void start();
|
||||
void responseCallback(const Networking::JsonResponse &response);
|
||||
void errorCallback(const Networking::ErrorResponse &error);
|
||||
void finishListing(const Common::Array<StorageFile> &files);
|
||||
public:
|
||||
DropboxListDirectoryRequest(DropboxStorage *storage, const Common::String &path, Storage::ListDirectoryCallback cb, Networking::ErrorCallback ecb, bool recursive = false);
|
||||
~DropboxListDirectoryRequest() override;
|
||||
|
||||
void handle() override;
|
||||
void restart() override;
|
||||
Common::String date() const override;
|
||||
};
|
||||
|
||||
} // End of namespace Dropbox
|
||||
} // End of namespace Cloud
|
||||
|
||||
#endif
|
||||
131
backends/cloud/dropbox/dropboxstorage.cpp
Normal file
131
backends/cloud/dropbox/dropboxstorage.cpp
Normal file
@@ -0,0 +1,131 @@
|
||||
/* 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/dropbox/dropboxstorage.h"
|
||||
#include "backends/cloud/dropbox/dropboxcreatedirectoryrequest.h"
|
||||
#include "backends/cloud/dropbox/dropboxinforequest.h"
|
||||
#include "backends/cloud/dropbox/dropboxlistdirectoryrequest.h"
|
||||
#include "backends/cloud/dropbox/dropboxuploadrequest.h"
|
||||
#include "backends/cloud/cloudmanager.h"
|
||||
#include "backends/networking/http/connectionmanager.h"
|
||||
#include "backends/networking/http/httpjsonrequest.h"
|
||||
#include "common/config-manager.h"
|
||||
#include "common/debug.h"
|
||||
#include "common/formats/json.h"
|
||||
|
||||
namespace Cloud {
|
||||
namespace Dropbox {
|
||||
|
||||
#define DROPBOX_API_FILES_DOWNLOAD "https://content.dropboxapi.com/2/files/download"
|
||||
|
||||
DropboxStorage::DropboxStorage(const Common::String &accessToken, const Common::String &refreshToken, bool enabled):
|
||||
BaseStorage(accessToken, refreshToken, enabled) {}
|
||||
|
||||
DropboxStorage::DropboxStorage(const Common::String &code, Networking::ErrorCallback cb): BaseStorage() {
|
||||
getAccessToken(code, cb);
|
||||
}
|
||||
|
||||
DropboxStorage::DropboxStorage(const Networking::JsonResponse &codeFlowJson, Networking::ErrorCallback cb) : BaseStorage() {
|
||||
codeFlowComplete(cb, codeFlowJson);
|
||||
}
|
||||
|
||||
DropboxStorage::~DropboxStorage() {}
|
||||
|
||||
Common::String DropboxStorage::cloudProvider() { return "dropbox"; }
|
||||
|
||||
uint32 DropboxStorage::storageIndex() { return kStorageDropboxId; }
|
||||
|
||||
bool DropboxStorage::needsRefreshToken() { return true; }
|
||||
|
||||
bool DropboxStorage::canReuseRefreshToken() { return true; }
|
||||
|
||||
void DropboxStorage::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 DropboxStorage::name() const {
|
||||
return "Dropbox";
|
||||
}
|
||||
|
||||
Networking::Request *DropboxStorage::listDirectory(const Common::String &path, ListDirectoryCallback outerCallback, Networking::ErrorCallback errorCallback, bool recursive) {
|
||||
return addRequest(new DropboxListDirectoryRequest(this, path, outerCallback, errorCallback, recursive));
|
||||
}
|
||||
|
||||
Networking::Request *DropboxStorage::upload(const Common::String &path, Common::SeekableReadStream *contents, UploadCallback callback, Networking::ErrorCallback errorCallback) {
|
||||
return addRequest(new DropboxUploadRequest(this, path, contents, callback, errorCallback));
|
||||
}
|
||||
|
||||
Networking::Request *DropboxStorage::streamFileById(const Common::String &path, Networking::NetworkReadStreamCallback callback, Networking::ErrorCallback errorCallback) {
|
||||
Common::JSONObject jsonRequestParameters;
|
||||
jsonRequestParameters.setVal("path", new Common::JSONValue(path));
|
||||
Common::JSONValue value(jsonRequestParameters);
|
||||
|
||||
Networking::HttpRequest *request = new Networking::HttpRequest(nullptr, nullptr, DROPBOX_API_FILES_DOWNLOAD); //TODO: is it OK to pass no callbacks?
|
||||
request->addHeader("Authorization: Bearer " + _token);
|
||||
request->addHeader("Dropbox-API-Arg: " + Common::JSON::stringify(&value));
|
||||
request->addHeader("Content-Type: "); //required to be empty (as we do POST, it's usually app/form-url-encoded)
|
||||
|
||||
Networking::NetworkReadStreamResponse response = request->execute();
|
||||
if (callback)
|
||||
(*callback)(response);
|
||||
return request; // no leak here, response.request == request
|
||||
}
|
||||
|
||||
Networking::Request *DropboxStorage::createDirectory(const Common::String &path, BoolCallback callback, Networking::ErrorCallback errorCallback) {
|
||||
if (!errorCallback)
|
||||
errorCallback = getErrorPrintingCallback();
|
||||
return addRequest(new DropboxCreateDirectoryRequest(this, path, callback, errorCallback));
|
||||
}
|
||||
|
||||
Networking::Request *DropboxStorage::info(StorageInfoCallback callback, Networking::ErrorCallback errorCallback) {
|
||||
if (!errorCallback)
|
||||
errorCallback = getErrorPrintingCallback();
|
||||
return addRequest(new DropboxInfoRequest(this, callback, errorCallback));
|
||||
}
|
||||
|
||||
Common::String DropboxStorage::savesDirectoryPath() { return "/saves/"; }
|
||||
|
||||
DropboxStorage *DropboxStorage::loadFromConfig(const Common::String &keyPrefix) {
|
||||
if (!ConfMan.hasKey(keyPrefix + "access_token", ConfMan.kCloudDomain)) {
|
||||
warning("DropboxStorage: no access_token found");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!ConfMan.hasKey(keyPrefix + "refresh_token", ConfMan.kCloudDomain)) {
|
||||
warning("DropboxStorage: 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 DropboxStorage(accessToken, refreshToken, loadIsEnabledFlag(keyPrefix));
|
||||
}
|
||||
|
||||
void DropboxStorage::removeFromConfig(const Common::String &keyPrefix) {
|
||||
ConfMan.removeKey(keyPrefix + "access_token", ConfMan.kCloudDomain);
|
||||
ConfMan.removeKey(keyPrefix + "refresh_token", ConfMan.kCloudDomain);
|
||||
removeIsEnabledFlag(keyPrefix);
|
||||
}
|
||||
|
||||
} // End of namespace Dropbox
|
||||
} // End of namespace Cloud
|
||||
116
backends/cloud/dropbox/dropboxstorage.h
Normal file
116
backends/cloud/dropbox/dropboxstorage.h
Normal file
@@ -0,0 +1,116 @@
|
||||
/* 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_DROPBOX_STORAGE_H
|
||||
#define BACKENDS_CLOUD_DROPBOX_STORAGE_H
|
||||
|
||||
#include "backends/cloud/basestorage.h"
|
||||
#include "common/callback.h"
|
||||
#include "backends/networking/http/httpjsonrequest.h"
|
||||
|
||||
namespace Cloud {
|
||||
namespace Dropbox {
|
||||
|
||||
class DropboxStorage: public Cloud::BaseStorage {
|
||||
/** This private constructor is called from loadFromConfig(). */
|
||||
DropboxStorage(const Common::String &token, const Common::String &refreshToken, bool enabled);
|
||||
|
||||
protected:
|
||||
/**
|
||||
* @return "dropbox"
|
||||
*/
|
||||
Common::String cloudProvider() override;
|
||||
|
||||
/**
|
||||
* @return kStorageDropboxId
|
||||
*/
|
||||
uint32 storageIndex() override;
|
||||
|
||||
bool needsRefreshToken() override;
|
||||
|
||||
bool canReuseRefreshToken() override;
|
||||
|
||||
public:
|
||||
/** This constructor uses OAuth code flow to get tokens. */
|
||||
DropboxStorage(const Common::String &code, Networking::ErrorCallback cb);
|
||||
|
||||
/** This constructor extracts tokens from JSON acquired via OAuth code flow. */
|
||||
DropboxStorage(const Networking::JsonResponse &codeFlowJson, Networking::ErrorCallback cb);
|
||||
|
||||
~DropboxStorage() 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. */
|
||||
|
||||
/** Returns ListDirectoryStatus struct with list of files. */
|
||||
Networking::Request *listDirectory(const Common::String &path, ListDirectoryCallback callback, Networking::ErrorCallback errorCallback, bool recursive = false) override;
|
||||
|
||||
/** Returns UploadStatus struct with info about uploaded file. */
|
||||
Networking::Request *upload(const Common::String &path, Common::SeekableReadStream *contents, UploadCallback callback, Networking::ErrorCallback errorCallback) override;
|
||||
|
||||
/** Returns pointer to Networking::NetworkReadStream. */
|
||||
Networking::Request *streamFileById(const Common::String &path, Networking::NetworkReadStreamCallback callback, Networking::ErrorCallback errorCallback) override;
|
||||
|
||||
/** Calls the callback when finished. */
|
||||
Networking::Request *createDirectory(const Common::String &path, BoolCallback 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 DropboxStorage for those.
|
||||
* @return pointer to the newly created DropboxStorage or 0 if some problem occurred.
|
||||
*/
|
||||
static DropboxStorage *loadFromConfig(const Common::String &keyPrefix);
|
||||
|
||||
/**
|
||||
* Remove all DropboxStorage-related data from config.
|
||||
*/
|
||||
static void removeFromConfig(const Common::String &keyPrefix);
|
||||
|
||||
Common::String accessToken() const { return _token; }
|
||||
};
|
||||
|
||||
} // End of namespace Dropbox
|
||||
} // End of namespace Cloud
|
||||
|
||||
#endif
|
||||
107
backends/cloud/dropbox/dropboxtokenrefresher.cpp
Normal file
107
backends/cloud/dropbox/dropboxtokenrefresher.cpp
Normal file
@@ -0,0 +1,107 @@
|
||||
/* 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/dropbox/dropboxtokenrefresher.h"
|
||||
#include "backends/cloud/dropbox/dropboxstorage.h"
|
||||
#include "backends/networking/http/networkreadstream.h"
|
||||
#include "common/debug.h"
|
||||
#include "common/formats/json.h"
|
||||
|
||||
namespace Cloud {
|
||||
namespace Dropbox {
|
||||
|
||||
DropboxTokenRefresher::DropboxTokenRefresher(DropboxStorage *parent, Networking::JsonCallback callback, Networking::ErrorCallback ecb, const char *url):
|
||||
HttpJsonRequest(callback, ecb, url), _parentStorage(parent) {}
|
||||
|
||||
DropboxTokenRefresher::~DropboxTokenRefresher() {}
|
||||
|
||||
void DropboxTokenRefresher::tokenRefreshed(const Storage::BoolResponse &response) {
|
||||
if (!response.value) {
|
||||
//failed to refresh token, notify user with NULL in original callback
|
||||
warning("DropboxTokenRefresher: failed to refresh token");
|
||||
finishError(Networking::ErrorResponse(this, false, true, "DropboxTokenRefresher::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 DropboxTokenRefresher::finishJson(const Common::JSONValue *json) {
|
||||
if (!json) {
|
||||
//that's probably not an error (200 OK)
|
||||
HttpJsonRequest::finishJson(nullptr);
|
||||
return;
|
||||
}
|
||||
|
||||
if (jsonIsObject(json, "DropboxTokenRefresher")) {
|
||||
Common::JSONObject result = json->asObject();
|
||||
|
||||
if (result.contains("error") || result.contains("error_summary")) {
|
||||
long httpCode = -1;
|
||||
if (_stream) {
|
||||
httpCode = _stream->httpResponseCode();
|
||||
debug(9, "DropboxTokenRefresher: code %ld", httpCode);
|
||||
}
|
||||
|
||||
bool irrecoverable = true;
|
||||
if (jsonContainsString(result, "error_summary", "DropboxTokenRefresher")) {
|
||||
if (result.getVal("error_summary")->asString().contains("expired_access_token")) {
|
||||
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<DropboxTokenRefresher, const Storage::BoolResponse &>(this, &DropboxTokenRefresher::tokenRefreshed));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
//notify user of success
|
||||
HttpJsonRequest::finishJson(json);
|
||||
}
|
||||
|
||||
void DropboxTokenRefresher::finishError(const Networking::ErrorResponse &error, Networking::RequestState state) {
|
||||
if (error.httpResponseCode == 401) {
|
||||
pause();
|
||||
_parentStorage->refreshAccessToken(new Common::Callback<DropboxTokenRefresher, const Storage::BoolResponse &>(this, &DropboxTokenRefresher::tokenRefreshed));
|
||||
return;
|
||||
}
|
||||
|
||||
Request::finishError(error);
|
||||
}
|
||||
|
||||
} // End of namespace Dropbox
|
||||
} // End of namespace Cloud
|
||||
48
backends/cloud/dropbox/dropboxtokenrefresher.h
Normal file
48
backends/cloud/dropbox/dropboxtokenrefresher.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_DROPBOX_DROPBOXTOKENREFRESHER_H
|
||||
#define BACKENDS_CLOUD_DROPBOX_DROPBOXTOKENREFRESHER_H
|
||||
|
||||
#include "backends/cloud/storage.h"
|
||||
#include "backends/networking/http/httpjsonrequest.h"
|
||||
|
||||
namespace Cloud {
|
||||
namespace Dropbox {
|
||||
|
||||
class DropboxStorage;
|
||||
|
||||
class DropboxTokenRefresher: public Networking::HttpJsonRequest {
|
||||
DropboxStorage *_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:
|
||||
DropboxTokenRefresher(DropboxStorage *parent, Networking::JsonCallback callback, Networking::ErrorCallback ecb, const char *url);
|
||||
~DropboxTokenRefresher() override;
|
||||
};
|
||||
|
||||
} // End of namespace Dropbox
|
||||
} // End of namespace Cloud
|
||||
|
||||
#endif
|
||||
205
backends/cloud/dropbox/dropboxuploadrequest.cpp
Normal file
205
backends/cloud/dropbox/dropboxuploadrequest.cpp
Normal file
@@ -0,0 +1,205 @@
|
||||
/* 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/dropbox/dropboxuploadrequest.h"
|
||||
#include "backends/cloud/dropbox/dropboxstorage.h"
|
||||
#include "backends/cloud/dropbox/dropboxtokenrefresher.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 Dropbox {
|
||||
|
||||
#define DROPBOX_API_FILES_UPLOAD "https://content.dropboxapi.com/2/files/upload"
|
||||
#define DROPBOX_API_FILES_UPLOAD_SESSION "https://content.dropboxapi.com/2/files/upload_session/"
|
||||
|
||||
DropboxUploadRequest::DropboxUploadRequest(DropboxStorage *storage, const 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();
|
||||
}
|
||||
|
||||
DropboxUploadRequest::~DropboxUploadRequest() {
|
||||
_ignoreCallback = true;
|
||||
if (_workingRequest)
|
||||
_workingRequest->finish();
|
||||
delete _contentsStream;
|
||||
delete _uploadCallback;
|
||||
}
|
||||
|
||||
void DropboxUploadRequest::start() {
|
||||
_ignoreCallback = true;
|
||||
if (_workingRequest)
|
||||
_workingRequest->finish();
|
||||
if (!_contentsStream) {
|
||||
warning("DropboxUploadRequest: cannot start because stream is invalid");
|
||||
finishError(Networking::ErrorResponse(this, false, true, "DropboxUploadRequest::start: cannot start because stream is invalid", -1));
|
||||
return;
|
||||
}
|
||||
if (!_contentsStream->seek(0)) {
|
||||
warning("DropboxUploadRequest: cannot restart because stream couldn't seek(0)");
|
||||
finishError(Networking::ErrorResponse(this, false, true, "DropboxUploadRequest::start: cannot restart because stream couldn't seek(0)", -1));
|
||||
return;
|
||||
}
|
||||
_ignoreCallback = false;
|
||||
|
||||
uploadNextPart();
|
||||
}
|
||||
|
||||
void DropboxUploadRequest::uploadNextPart() {
|
||||
const uint32 UPLOAD_PER_ONE_REQUEST = 10 * 1024 * 1024;
|
||||
|
||||
Common::String url = DROPBOX_API_FILES_UPLOAD_SESSION;
|
||||
Common::JSONObject jsonRequestParameters;
|
||||
|
||||
if (_contentsStream->pos() == 0 || _sessionId == "") {
|
||||
if ((uint32)_contentsStream->size() <= UPLOAD_PER_ONE_REQUEST) {
|
||||
url = DROPBOX_API_FILES_UPLOAD;
|
||||
jsonRequestParameters.setVal("path", new Common::JSONValue(_savePath));
|
||||
jsonRequestParameters.setVal("mode", new Common::JSONValue("overwrite"));
|
||||
jsonRequestParameters.setVal("autorename", new Common::JSONValue(false));
|
||||
jsonRequestParameters.setVal("mute", new Common::JSONValue(false));
|
||||
} else {
|
||||
url += "start";
|
||||
jsonRequestParameters.setVal("close", new Common::JSONValue(false));
|
||||
}
|
||||
} else {
|
||||
if ((uint32)(_contentsStream->size() - _contentsStream->pos()) <= UPLOAD_PER_ONE_REQUEST) {
|
||||
url += "finish";
|
||||
Common::JSONObject jsonCursor, jsonCommit;
|
||||
jsonCursor.setVal("session_id", new Common::JSONValue(_sessionId));
|
||||
jsonCursor.setVal("offset", new Common::JSONValue((long long int)_contentsStream->pos()));
|
||||
jsonCommit.setVal("path", new Common::JSONValue(_savePath));
|
||||
jsonCommit.setVal("mode", new Common::JSONValue("overwrite"));
|
||||
jsonCommit.setVal("autorename", new Common::JSONValue(false));
|
||||
jsonCommit.setVal("mute", new Common::JSONValue(false));
|
||||
jsonRequestParameters.setVal("cursor", new Common::JSONValue(jsonCursor));
|
||||
jsonRequestParameters.setVal("commit", new Common::JSONValue(jsonCommit));
|
||||
} else {
|
||||
url += "append_v2";
|
||||
Common::JSONObject jsonCursor;
|
||||
jsonCursor.setVal("session_id", new Common::JSONValue(_sessionId));
|
||||
jsonCursor.setVal("offset", new Common::JSONValue((long long int)_contentsStream->pos()));
|
||||
jsonRequestParameters.setVal("cursor", new Common::JSONValue(jsonCursor));
|
||||
jsonRequestParameters.setVal("close", new Common::JSONValue(false));
|
||||
}
|
||||
}
|
||||
|
||||
Common::JSONValue value(jsonRequestParameters);
|
||||
Networking::JsonCallback callback = new Common::Callback<DropboxUploadRequest, const Networking::JsonResponse &>(this, &DropboxUploadRequest::partUploadedCallback);
|
||||
Networking::ErrorCallback failureCallback = new Common::Callback<DropboxUploadRequest, const Networking::ErrorResponse &>(this, &DropboxUploadRequest::partUploadedErrorCallback);
|
||||
Networking::HttpJsonRequest *request = new DropboxTokenRefresher(_storage, callback, failureCallback, url.c_str());
|
||||
request->addHeader("Authorization: Bearer " + _storage->accessToken());
|
||||
request->addHeader("Content-Type: application/octet-stream");
|
||||
request->addHeader("Dropbox-API-Arg: " + Common::JSON::stringify(&value));
|
||||
|
||||
byte *buffer = new byte[UPLOAD_PER_ONE_REQUEST];
|
||||
uint32 size = _contentsStream->read(buffer, UPLOAD_PER_ONE_REQUEST);
|
||||
request->setBuffer(buffer, size);
|
||||
|
||||
_workingRequest = ConnMan.addRequest(request);
|
||||
}
|
||||
|
||||
void DropboxUploadRequest::partUploadedCallback(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 && 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;
|
||||
}
|
||||
|
||||
bool needsFinishRequest = false;
|
||||
|
||||
if (json->isObject()) {
|
||||
Common::JSONObject object = json->asObject();
|
||||
|
||||
//debug(9, "%s", json->stringify(true).c_str());
|
||||
|
||||
if (object.contains("error") || object.contains("error_summary")) {
|
||||
if (Networking::HttpJsonRequest::jsonContainsString(object, "error_summary", "DropboxUploadRequest")) {
|
||||
warning("Dropbox returned error: %s", object.getVal("error_summary")->asString().c_str());
|
||||
}
|
||||
error.response = json->stringify(true);
|
||||
finishError(error);
|
||||
delete json;
|
||||
return;
|
||||
}
|
||||
|
||||
if (Networking::HttpJsonRequest::jsonContainsString(object, "path_lower", "DropboxUploadRequest") &&
|
||||
Networking::HttpJsonRequest::jsonContainsString(object, "server_modified", "DropboxUploadRequest") &&
|
||||
Networking::HttpJsonRequest::jsonContainsIntegerNumber(object, "size", "DropboxUploadRequest")) {
|
||||
//finished
|
||||
Common::String path = object.getVal("path_lower")->asString();
|
||||
uint32 size = object.getVal("size")->asIntegerNumber();
|
||||
uint32 timestamp = ISO8601::convertToTimestamp(object.getVal("server_modified")->asString());
|
||||
finishUpload(StorageFile(path, size, timestamp, false));
|
||||
return;
|
||||
}
|
||||
|
||||
if (_sessionId == "") {
|
||||
if (Networking::HttpJsonRequest::jsonContainsString(object, "session_id", "DropboxUploadRequest"))
|
||||
_sessionId = object.getVal("session_id")->asString();
|
||||
needsFinishRequest = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!needsFinishRequest && (_contentsStream->eos() || _contentsStream->pos() >= _contentsStream->size() - 1)) {
|
||||
warning("DropboxUploadRequest: no file info to return");
|
||||
finishUpload(StorageFile(_savePath, 0, 0, false));
|
||||
} else {
|
||||
uploadNextPart();
|
||||
}
|
||||
|
||||
delete json;
|
||||
}
|
||||
|
||||
void DropboxUploadRequest::partUploadedErrorCallback(const Networking::ErrorResponse &error) {
|
||||
_workingRequest = nullptr;
|
||||
if (_ignoreCallback)
|
||||
return;
|
||||
finishError(error);
|
||||
}
|
||||
|
||||
void DropboxUploadRequest::handle() {}
|
||||
|
||||
void DropboxUploadRequest::restart() { start(); }
|
||||
|
||||
void DropboxUploadRequest::finishUpload(const StorageFile &file) {
|
||||
Request::finishSuccess();
|
||||
if (_uploadCallback)
|
||||
(*_uploadCallback)(Storage::UploadResponse(this, file));
|
||||
}
|
||||
|
||||
} // End of namespace Dropbox
|
||||
} // End of namespace Cloud
|
||||
61
backends/cloud/dropbox/dropboxuploadrequest.h
Normal file
61
backends/cloud/dropbox/dropboxuploadrequest.h
Normal file
@@ -0,0 +1,61 @@
|
||||
/* 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_DROPBOX_DROPBOXUPLOADREQUEST_H
|
||||
#define BACKENDS_CLOUD_DROPBOX_DROPBOXUPLOADREQUEST_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 Dropbox {
|
||||
|
||||
class DropboxStorage;
|
||||
|
||||
class DropboxUploadRequest: public Networking::Request {
|
||||
DropboxStorage *_storage;
|
||||
Common::String _savePath;
|
||||
Common::SeekableReadStream *_contentsStream;
|
||||
Storage::UploadCallback _uploadCallback;
|
||||
Request *_workingRequest;
|
||||
bool _ignoreCallback;
|
||||
Common::String _sessionId;
|
||||
|
||||
void start();
|
||||
void uploadNextPart();
|
||||
void partUploadedCallback(const Networking::JsonResponse &response);
|
||||
void partUploadedErrorCallback(const Networking::ErrorResponse &error);
|
||||
void finishUpload(const StorageFile &status);
|
||||
|
||||
public:
|
||||
DropboxUploadRequest(DropboxStorage *storage, const Common::String &path, Common::SeekableReadStream *contents, Storage::UploadCallback callback, Networking::ErrorCallback ecb);
|
||||
~DropboxUploadRequest() override;
|
||||
|
||||
void handle() override;
|
||||
void restart() override;
|
||||
};
|
||||
|
||||
} // End of namespace Dropbox
|
||||
} // End of namespace Cloud
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user