CLOUD: Add IdDownloadRequest and IdStreamFileRequest

Used for downloading files in Box.
This commit is contained in:
Alexander Tkachev 2016-07-13 15:48:55 +06:00
parent 34ee1d29d5
commit 19ae61dffc
8 changed files with 331 additions and 9 deletions

View file

@ -249,15 +249,18 @@ Networking::Request *BoxStorage::upload(Common::String path, Common::SeekableRea
return nullptr; //TODO return nullptr; //TODO
} }
Networking::Request *BoxStorage::streamFileById(Common::String path, Networking::NetworkReadStreamCallback outerCallback, Networking::ErrorCallback errorCallback) { Networking::Request *BoxStorage::streamFileById(Common::String id, Networking::NetworkReadStreamCallback callback, Networking::ErrorCallback errorCallback) {
/* if (callback) {
Common::String url = "https://api.Box.com/v1.0/drive/special/approot:/" + ConnMan.urlEncode(path); Common::String url = "https://api.box.com/2.0/files/" + id + "/content";
Networking::JsonCallback innerCallback = new Common::CallbackBridge<BoxStorage, Networking::NetworkReadStreamResponse, Networking::JsonResponse>(this, &BoxStorage::fileInfoCallback, outerCallback); debug("%s", url.c_str());
Networking::CurlJsonRequest *request = new BoxTokenRefresher(this, innerCallback, errorCallback, url.c_str()); Common::String header = "Authorization: Bearer " + _token;
request->addHeader("Authorization: Bearer " + _token); curl_slist *headersList = curl_slist_append(nullptr, header.c_str());
return addRequest(request); Networking::NetworkReadStream *stream = new Networking::NetworkReadStream(url.c_str(), headersList, "");
*/ (*callback)(Networking::NetworkReadStreamResponse(nullptr, stream));
return nullptr; //TODO }
delete callback;
delete errorCallback;
return nullptr;
} }
void BoxStorage::fileDownloaded(BoolResponse response) { void BoxStorage::fileDownloaded(BoolResponse response) {

View file

@ -0,0 +1,91 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "backends/cloud/id/iddownloadrequest.h"
#include "backends/cloud/id/idstorage.h"
namespace Cloud {
namespace Id {
IdDownloadRequest::IdDownloadRequest(IdStorage *storage, Common::String remotePath, Common::String localPath, Storage::BoolCallback cb, Networking::ErrorCallback ecb):
Networking::Request(nullptr, ecb), _requestedFile(remotePath), _requestedLocalFile(localPath), _storage(storage), _boolCallback(cb),
_workingRequest(nullptr), _ignoreCallback(false) {
start();
}
IdDownloadRequest::~IdDownloadRequest() {
_ignoreCallback = true;
if (_workingRequest) _workingRequest->finish();
delete _boolCallback;
}
void IdDownloadRequest::start() {
//cleanup
_ignoreCallback = true;
if (_workingRequest) _workingRequest->finish();
_workingRequest = nullptr;
_ignoreCallback = false;
//find file's id
Storage::UploadCallback innerCallback = new Common::Callback<IdDownloadRequest, Storage::UploadResponse>(this, &IdDownloadRequest::idResolvedCallback);
Networking::ErrorCallback innerErrorCallback = new Common::Callback<IdDownloadRequest, Networking::ErrorResponse>(this, &IdDownloadRequest::idResolveFailedCallback);
_workingRequest = _storage->resolveFileId(_requestedFile, innerCallback, innerErrorCallback);
}
void IdDownloadRequest::idResolvedCallback(Storage::UploadResponse response) {
_workingRequest = nullptr;
if (_ignoreCallback) return;
Storage::BoolCallback innerCallback = new Common::Callback<IdDownloadRequest, Storage::BoolResponse>(this, &IdDownloadRequest::downloadCallback);
Networking::ErrorCallback innerErrorCallback = new Common::Callback<IdDownloadRequest, Networking::ErrorResponse>(this, &IdDownloadRequest::downloadErrorCallback);
_workingRequest = _storage->downloadById(response.value.id(), _requestedLocalFile, innerCallback, innerErrorCallback);
}
void IdDownloadRequest::idResolveFailedCallback(Networking::ErrorResponse error) {
_workingRequest = nullptr;
if (_ignoreCallback) return;
finishError(error);
}
void IdDownloadRequest::downloadCallback(Storage::BoolResponse response) {
_workingRequest = nullptr;
if (_ignoreCallback) return;
finishDownload(response.value);
}
void IdDownloadRequest::downloadErrorCallback(Networking::ErrorResponse error) {
_workingRequest = nullptr;
if (_ignoreCallback) return;
finishError(error);
}
void IdDownloadRequest::handle() {}
void IdDownloadRequest::restart() { start(); }
void IdDownloadRequest::finishDownload(bool success) {
Request::finishSuccess();
if (_boolCallback) (*_boolCallback)(Storage::BoolResponse(this, success));
}
} // End of namespace Id
} // End of namespace Cloud

View file

@ -0,0 +1,59 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#ifndef BACKENDS_CLOUD_ID_IDDOWNLOADREQUEST_H
#define BACKENDS_CLOUD_ID_IDDOWNLOADREQUEST_H
#include "backends/cloud/storage.h"
#include "backends/networking/curl/request.h"
#include "common/callback.h"
namespace Cloud {
namespace Id {
class IdStorage;
class IdDownloadRequest: public Networking::Request {
Common::String _requestedFile, _requestedLocalFile;
IdStorage *_storage;
Storage::BoolCallback _boolCallback;
Request *_workingRequest;
bool _ignoreCallback;
void start();
void idResolvedCallback(Storage::UploadResponse response);
void idResolveFailedCallback(Networking::ErrorResponse error);
void downloadCallback(Storage::BoolResponse response);
void downloadErrorCallback(Networking::ErrorResponse error);
void finishDownload(bool success);
public:
IdDownloadRequest(IdStorage *storage, Common::String remotePath, Common::String localPath, Storage::BoolCallback cb, Networking::ErrorCallback ecb);
virtual ~IdDownloadRequest();
virtual void handle();
virtual void restart();
};
} // End of namespace Id
} // End of namespace Cloud
#endif

View file

@ -23,8 +23,10 @@
#include "backends/cloud/id/idstorage.h" #include "backends/cloud/id/idstorage.h"
#include "backends/cloud/id/idcreatedirectoryrequest.h" #include "backends/cloud/id/idcreatedirectoryrequest.h"
#include "backends/cloud/id/iddownloadrequest.h"
#include "backends/cloud/id/idlistdirectoryrequest.h" #include "backends/cloud/id/idlistdirectoryrequest.h"
#include "backends/cloud/id/idresolveidrequest.h" #include "backends/cloud/id/idresolveidrequest.h"
#include "backends/cloud/id/idstreamfilerequest.h"
#include "common/debug.h" #include "common/debug.h"
#include "common/json.h" #include "common/json.h"
@ -101,5 +103,13 @@ Networking::Request *IdStorage::createDirectory(Common::String path, BoolCallbac
return addRequest(new IdCreateDirectoryRequest(this, parentPath, directoryName, callback, errorCallback)); return addRequest(new IdCreateDirectoryRequest(this, parentPath, directoryName, callback, errorCallback));
} }
Networking::Request *IdStorage::streamFile(Common::String path, Networking::NetworkReadStreamCallback outerCallback, Networking::ErrorCallback errorCallback) {
return addRequest(new IdStreamFileRequest(this, path, outerCallback, errorCallback));
}
Networking::Request *IdStorage::download(Common::String remotePath, Common::String localPath, BoolCallback callback, Networking::ErrorCallback errorCallback) {
return addRequest(new IdDownloadRequest(this, remotePath, localPath, callback, errorCallback));
}
} // End of namespace Id } // End of namespace Id
} // End of namespace Cloud } // End of namespace Cloud

View file

@ -68,6 +68,13 @@ public:
virtual Networking::Request *createDirectory(Common::String path, BoolCallback callback, Networking::ErrorCallback errorCallback); virtual Networking::Request *createDirectory(Common::String path, BoolCallback callback, Networking::ErrorCallback errorCallback);
virtual Networking::Request *createDirectoryWithParentId(Common::String parentId, Common::String name, BoolCallback callback, Networking::ErrorCallback errorCallback) = 0; virtual Networking::Request *createDirectoryWithParentId(Common::String parentId, Common::String name, BoolCallback callback, Networking::ErrorCallback errorCallback) = 0;
/** Returns pointer to Networking::NetworkReadStream. */
virtual Networking::Request *streamFile(Common::String path, Networking::NetworkReadStreamCallback callback, Networking::ErrorCallback errorCallback);
virtual Networking::Request *streamFileById(Common::String id, Networking::NetworkReadStreamCallback callback, Networking::ErrorCallback errorCallback) = 0;
/** Calls the callback when finished. */
virtual Networking::Request *download(Common::String remotePath, Common::String localPath, BoolCallback callback, Networking::ErrorCallback errorCallback);
virtual Common::String getRootDirectoryId() = 0; virtual Common::String getRootDirectoryId() = 0;
}; };

View file

@ -0,0 +1,91 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "backends/cloud/id/idstreamfilerequest.h"
#include "backends/cloud/id/idstorage.h"
namespace Cloud {
namespace Id {
IdStreamFileRequest::IdStreamFileRequest(IdStorage *storage, Common::String path, Networking::NetworkReadStreamCallback cb, Networking::ErrorCallback ecb):
Networking::Request(nullptr, ecb), _requestedFile(path), _storage(storage), _streamCallback(cb),
_workingRequest(nullptr), _ignoreCallback(false) {
start();
}
IdStreamFileRequest::~IdStreamFileRequest() {
_ignoreCallback = true;
if (_workingRequest) _workingRequest->finish();
delete _streamCallback;
}
void IdStreamFileRequest::start() {
//cleanup
_ignoreCallback = true;
if (_workingRequest) _workingRequest->finish();
_workingRequest = nullptr;
_ignoreCallback = false;
//find file's id
Storage::UploadCallback innerCallback = new Common::Callback<IdStreamFileRequest, Storage::UploadResponse>(this, &IdStreamFileRequest::idResolvedCallback);
Networking::ErrorCallback innerErrorCallback = new Common::Callback<IdStreamFileRequest, Networking::ErrorResponse>(this, &IdStreamFileRequest::idResolveFailedCallback);
_workingRequest = _storage->resolveFileId(_requestedFile, innerCallback, innerErrorCallback);
}
void IdStreamFileRequest::idResolvedCallback(Storage::UploadResponse response) {
_workingRequest = nullptr;
if (_ignoreCallback) return;
Networking::NetworkReadStreamCallback innerCallback = new Common::Callback<IdStreamFileRequest, Networking::NetworkReadStreamResponse>(this, &IdStreamFileRequest::streamFileCallback);
Networking::ErrorCallback innerErrorCallback = new Common::Callback<IdStreamFileRequest, Networking::ErrorResponse>(this, &IdStreamFileRequest::streamFileErrorCallback);
_workingRequest = _storage->streamFileById(response.value.id(), innerCallback, innerErrorCallback);
}
void IdStreamFileRequest::idResolveFailedCallback(Networking::ErrorResponse error) {
_workingRequest = nullptr;
if (_ignoreCallback) return;
finishError(error);
}
void IdStreamFileRequest::streamFileCallback(Networking::NetworkReadStreamResponse response) {
_workingRequest = nullptr;
if (_ignoreCallback) return;
finishStream(response.value);
}
void IdStreamFileRequest::streamFileErrorCallback(Networking::ErrorResponse error) {
_workingRequest = nullptr;
if (_ignoreCallback) return;
finishError(error);
}
void IdStreamFileRequest::handle() {}
void IdStreamFileRequest::restart() { start(); }
void IdStreamFileRequest::finishStream(Networking::NetworkReadStream *stream) {
Request::finishSuccess();
if (_streamCallback) (*_streamCallback)(Networking::NetworkReadStreamResponse(this, stream));
}
} // End of namespace Id
} // End of namespace Cloud

View file

@ -0,0 +1,59 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#ifndef BACKENDS_CLOUD_ID_IDSTREAMFILEREQUEST_H
#define BACKENDS_CLOUD_ID_IDSTREAMFILEREQUEST_H
#include "backends/cloud/storage.h"
#include "backends/networking/curl/request.h"
#include "common/callback.h"
namespace Cloud {
namespace Id {
class IdStorage;
class IdStreamFileRequest: public Networking::Request {
Common::String _requestedFile;
IdStorage *_storage;
Networking::NetworkReadStreamCallback _streamCallback;
Request *_workingRequest;
bool _ignoreCallback;
void start();
void idResolvedCallback(Storage::UploadResponse response);
void idResolveFailedCallback(Networking::ErrorResponse error);
void streamFileCallback(Networking::NetworkReadStreamResponse response);
void streamFileErrorCallback(Networking::ErrorResponse error);
void finishStream(Networking::NetworkReadStream *stream);
public:
IdStreamFileRequest(IdStorage *storage, Common::String path, Networking::NetworkReadStreamCallback cb, Networking::ErrorCallback ecb);
virtual ~IdStreamFileRequest();
virtual void handle();
virtual void restart();
};
} // End of namespace Id
} // End of namespace Cloud
#endif

View file

@ -46,8 +46,10 @@ MODULE_OBJS += \
cloud/googledrive/googledriveuploadrequest.o \ cloud/googledrive/googledriveuploadrequest.o \
cloud/id/idstorage.o \ cloud/id/idstorage.o \
cloud/id/idcreatedirectoryrequest.o \ cloud/id/idcreatedirectoryrequest.o \
cloud/id/iddownloadrequest.o \
cloud/id/idlistdirectoryrequest.o \ cloud/id/idlistdirectoryrequest.o \
cloud/id/idresolveidrequest.o \ cloud/id/idresolveidrequest.o \
cloud/id/idstreamfilerequest.o \
cloud/onedrive/onedrivestorage.o \ cloud/onedrive/onedrivestorage.o \
cloud/onedrive/onedrivecreatedirectoryrequest.o \ cloud/onedrive/onedrivecreatedirectoryrequest.o \
cloud/onedrive/onedrivetokenrefresher.o \ cloud/onedrive/onedrivetokenrefresher.o \