diff options
author | Alexander Tkachev | 2016-05-27 01:09:10 +0600 |
---|---|---|
committer | Alexander Tkachev | 2016-08-24 16:07:55 +0600 |
commit | 83b349a033d71e92e292d1f1da0578d557ec6411 (patch) | |
tree | abd13bd11ddb7ffd6640d37afe137868703e04f8 /backends/cloud | |
parent | 24007c029b53a5f4502ee1c48c5244b8cf8099ce (diff) | |
download | scummvm-rg350-83b349a033d71e92e292d1f1da0578d557ec6411.tar.gz scummvm-rg350-83b349a033d71e92e292d1f1da0578d557ec6411.tar.bz2 scummvm-rg350-83b349a033d71e92e292d1f1da0578d557ec6411.zip |
CLOUD: Make OneDriveStorage::download() work fine
Well, it takes two API calls instead of one now, but there are no
problems with expired token because of it.
This commit changes Storage::streamFile() to pass NetworkReadStream *
through callback.
Diffstat (limited to 'backends/cloud')
-rw-r--r-- | backends/cloud/downloadrequest.cpp | 24 | ||||
-rw-r--r-- | backends/cloud/downloadrequest.h | 3 | ||||
-rw-r--r-- | backends/cloud/dropbox/dropboxstorage.cpp | 8 | ||||
-rw-r--r-- | backends/cloud/dropbox/dropboxstorage.h | 2 | ||||
-rw-r--r-- | backends/cloud/onedrive/onedrivestorage.cpp | 37 | ||||
-rw-r--r-- | backends/cloud/onedrive/onedrivestorage.h | 4 | ||||
-rw-r--r-- | backends/cloud/onedrive/onedrivetokenrefresher.cpp | 2 | ||||
-rw-r--r-- | backends/cloud/onedrive/onedrivetokenrefresher.h | 2 | ||||
-rw-r--r-- | backends/cloud/storage.h | 4 |
9 files changed, 63 insertions, 23 deletions
diff --git a/backends/cloud/downloadrequest.cpp b/backends/cloud/downloadrequest.cpp index 756a904c74..661d6fd56b 100644 --- a/backends/cloud/downloadrequest.cpp +++ b/backends/cloud/downloadrequest.cpp @@ -27,25 +27,39 @@ namespace Cloud { -DownloadRequest::DownloadRequest(Storage::BoolCallback callback, Networking::NetworkReadStream *stream, Common::DumpFile *dumpFile): - Request(0), _boolCallback(callback), _remoteFileStream(stream), _localFile(dumpFile) {} +DownloadRequest::DownloadRequest(Storage *storage, Storage::BoolCallback callback, Common::String remoteFile, Common::DumpFile *dumpFile): + Request(0), _boolCallback(callback), _remoteFileStream(0), _localFile(dumpFile) { + storage->streamFile(remoteFile, new Common::Callback<DownloadRequest, Storage::RequestReadStreamPair>(this, &DownloadRequest::streamCallback)); +} -void DownloadRequest::handle() { - if (!_remoteFileStream) { - warning("DownloadRequest: no stream to read"); +void DownloadRequest::streamCallback(Storage::RequestReadStreamPair pair) { + if (!pair.value) { + warning("DownloadRequest: no ReadStream passed"); ConnMan.getRequestInfo(_id).state = Networking::FINISHED; + if (_boolCallback) (*_boolCallback)(Storage::RequestBoolPair(_id, false)); return; } + _remoteFileStream = (Networking::NetworkReadStream *)pair.value; +} + +void DownloadRequest::handle() { if (!_localFile) { warning("DownloadRequest: no file to write"); ConnMan.getRequestInfo(_id).state = Networking::FINISHED; + if (_boolCallback) (*_boolCallback)(Storage::RequestBoolPair(_id, false)); return; } if (!_localFile->isOpen()) { warning("DownloadRequest: failed to open file to write"); ConnMan.getRequestInfo(_id).state = Networking::FINISHED; + if (_boolCallback) (*_boolCallback)(Storage::RequestBoolPair(_id, false)); + return; + } + + if (!_remoteFileStream) { + //waiting for callback return; } diff --git a/backends/cloud/downloadrequest.h b/backends/cloud/downloadrequest.h index 724cf19d89..181536beba 100644 --- a/backends/cloud/downloadrequest.h +++ b/backends/cloud/downloadrequest.h @@ -35,8 +35,9 @@ class DownloadRequest: public Networking::Request { Networking::NetworkReadStream *_remoteFileStream; Common::DumpFile *_localFile; + void streamCallback(Storage::RequestReadStreamPair pair); public: - DownloadRequest(Storage::BoolCallback callback, Networking::NetworkReadStream *stream, Common::DumpFile *dumpFile); + DownloadRequest(Storage *storage, Storage::BoolCallback callback, Common::String remoteFile, Common::DumpFile *dumpFile); virtual ~DownloadRequest() { delete _localFile; } virtual void handle(); diff --git a/backends/cloud/dropbox/dropboxstorage.cpp b/backends/cloud/dropbox/dropboxstorage.cpp index 47576d7cda..b5292c83c7 100644 --- a/backends/cloud/dropbox/dropboxstorage.cpp +++ b/backends/cloud/dropbox/dropboxstorage.cpp @@ -88,7 +88,7 @@ int32 DropboxStorage::listDirectory(Common::String path, FileArrayCallback outer return ConnMan.addRequest(new DropboxListDirectoryRequest(_token, path, outerCallback, recursive)); } -Networking::NetworkReadStream *DropboxStorage::streamFile(Common::String path) { +int32 DropboxStorage::streamFile(Common::String path, ReadStreamCallback callback) { Common::JSONObject jsonRequestParameters; jsonRequestParameters.setVal("path", new Common::JSONValue(path)); Common::JSONValue value(jsonRequestParameters); @@ -98,7 +98,9 @@ Networking::NetworkReadStream *DropboxStorage::streamFile(Common::String path) { 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) - return request->execute(); + RequestReadStreamPair pair = request->execute(); + if (callback) (*callback)(pair); + return pair.id; } int32 DropboxStorage::download(Common::String remotePath, Common::String localPath, BoolCallback callback) { @@ -110,7 +112,7 @@ int32 DropboxStorage::download(Common::String remotePath, Common::String localPa return -1; } - return ConnMan.addRequest(new DownloadRequest(callback, streamFile(remotePath), f)); + return ConnMan.addRequest(new DownloadRequest(this, callback, remotePath, f)); } int32 DropboxStorage::syncSaves(BoolCallback callback) { diff --git a/backends/cloud/dropbox/dropboxstorage.h b/backends/cloud/dropbox/dropboxstorage.h index 4f1e6cd48a..dd082b25f1 100644 --- a/backends/cloud/dropbox/dropboxstorage.h +++ b/backends/cloud/dropbox/dropboxstorage.h @@ -71,7 +71,7 @@ public: virtual int32 upload(Common::String path, Common::ReadStream *contents, BoolCallback callback) { return -1; } //TODO /** Returns pointer to Networking::NetworkReadStream. */ - virtual Networking::NetworkReadStream *streamFile(Common::String path); + virtual int32 streamFile(Common::String path, ReadStreamCallback callback); /** Calls the callback when finished. */ virtual int32 download(Common::String remotePath, Common::String localPath, BoolCallback callback); diff --git a/backends/cloud/onedrive/onedrivestorage.cpp b/backends/cloud/onedrive/onedrivestorage.cpp index 877a1d27d1..237557df59 100644 --- a/backends/cloud/onedrive/onedrivestorage.cpp +++ b/backends/cloud/onedrive/onedrivestorage.cpp @@ -126,14 +126,35 @@ void OneDriveStorage::printJson(Networking::RequestJsonPair pair) { delete json; } -Networking::NetworkReadStream *OneDriveStorage::streamFile(Common::String path) { - Common::String url = "https://api.onedrive.com/v1.0/drive/special/approot:/" + path + ":/content"; - //NOT USING OneDriveTokenRefresher, because it's CurlJsonRequest, which saves all contents in memory to parse as JSON - //we actually don't even need a token if the download is "pre-authenticated" (whatever it means) - //still, we'd have to know direct URL (might be found in Item's "@content.downloadUrl", received from the server) - Networking::CurlRequest *request = new Networking::CurlRequest(0, url.c_str()); +void OneDriveStorage::fileInfoCallback(ReadStreamCallback outerCallback, Networking::RequestJsonPair pair) { + if (!pair.value) { + warning("fileInfoCallback: NULL"); + if (outerCallback) (*outerCallback)(RequestReadStreamPair(pair.id, 0)); + return; + } + + Common::JSONObject result = pair.value->asObject(); + if (result.contains("@content.downloadUrl")) { + const char *url = result.getVal("@content.downloadUrl")->asString().c_str(); + if (outerCallback) + (*outerCallback)(RequestReadStreamPair( + pair.id, + new Networking::NetworkReadStream(url, 0, "") + )); + } else { + warning("downloadUrl not found in passed JSON"); + debug("%s", pair.value->stringify().c_str()); + if (outerCallback) (*outerCallback)(RequestReadStreamPair(pair.id, 0)); + } + delete pair.value; +} + +int32 OneDriveStorage::streamFile(Common::String path, ReadStreamCallback outerCallback) { + Common::String url = "https://api.onedrive.com/v1.0/drive/special/approot:/" + path + ":/"; + Networking::JsonCallback innerCallback = new Common::CallbackBridge<OneDriveStorage, RequestReadStreamPair, Networking::RequestJsonPair>(this, &OneDriveStorage::fileInfoCallback, outerCallback); + Networking::CurlJsonRequest *request = new OneDriveTokenRefresher(this, innerCallback, url.c_str()); request->addHeader("Authorization: Bearer " + _token); - return request->execute(); + return ConnMan.addRequest(request); } int32 OneDriveStorage::download(Common::String remotePath, Common::String localPath, BoolCallback callback) { @@ -145,7 +166,7 @@ int32 OneDriveStorage::download(Common::String remotePath, Common::String localP return -1; } - return ConnMan.addRequest(new DownloadRequest(callback, streamFile(remotePath), f)); + return ConnMan.addRequest(new DownloadRequest(this, callback, remotePath, f)); } void OneDriveStorage::fileDownloaded(RequestBoolPair pair) { diff --git a/backends/cloud/onedrive/onedrivestorage.h b/backends/cloud/onedrive/onedrivestorage.h index 391cabe02a..be2bcdf04c 100644 --- a/backends/cloud/onedrive/onedrivestorage.h +++ b/backends/cloud/onedrive/onedrivestorage.h @@ -49,6 +49,8 @@ class OneDriveStorage: public Cloud::Storage { void printJson(Networking::RequestJsonPair pair); void fileDownloaded(RequestBoolPair pair); + + void fileInfoCallback(ReadStreamCallback outerCallback, Networking::RequestJsonPair pair); public: virtual ~OneDriveStorage(); @@ -75,7 +77,7 @@ public: virtual int32 upload(Common::String path, Common::ReadStream *contents, BoolCallback callback) { return -1; } //TODO /** Returns pointer to Networking::NetworkReadStream. */ - virtual int32 streamFile(Common::String path); + virtual int32 streamFile(Common::String path, ReadStreamCallback callback); /** Calls the callback when finished. */ virtual int32 download(Common::String remotePath, Common::String localPath, BoolCallback callback); diff --git a/backends/cloud/onedrive/onedrivetokenrefresher.cpp b/backends/cloud/onedrive/onedrivetokenrefresher.cpp index 5e72717740..d3ec0cc668 100644 --- a/backends/cloud/onedrive/onedrivetokenrefresher.cpp +++ b/backends/cloud/onedrive/onedrivetokenrefresher.cpp @@ -109,7 +109,7 @@ void OneDriveTokenRefresher::restart() { if (_jsonCallback) (*_jsonCallback)(Networking::RequestJsonPair(_id, 0)); } -Networking::NetworkReadStream *OneDriveTokenRefresher::execute() { +Cloud::Storage::RequestReadStreamPair OneDriveTokenRefresher::execute() { if (!_started) { for (uint32 i = 0; i < _headers.size(); ++i) _innerRequest->addHeader(_headers[i]); diff --git a/backends/cloud/onedrive/onedrivetokenrefresher.h b/backends/cloud/onedrive/onedrivetokenrefresher.h index 976851282e..58b7dcedb4 100644 --- a/backends/cloud/onedrive/onedrivetokenrefresher.h +++ b/backends/cloud/onedrive/onedrivetokenrefresher.h @@ -52,7 +52,7 @@ public: virtual void setHeaders(Common::Array<Common::String> &headers) { _headers = headers; } virtual void addHeader(Common::String header) { _headers.push_back(header); } virtual void addPostField(Common::String field) { _innerRequest->addPostField(field); } - virtual Networking::NetworkReadStream *execute(); + virtual Cloud::Storage::RequestReadStreamPair execute(); }; } //end of namespace OneDrive diff --git a/backends/cloud/storage.h b/backends/cloud/storage.h index 325d57d02c..0114b46dee 100644 --- a/backends/cloud/storage.h +++ b/backends/cloud/storage.h @@ -37,7 +37,7 @@ namespace Cloud { class Storage { public: typedef Networking::RequestIdPair<Common::Array<StorageFile>&> RequestFileArrayPair; - typedef Networking::RequestIdPair<Common::ReadStream *> RequestReadStreamPair; + typedef Networking::RequestIdPair<Networking::NetworkReadStream *> RequestReadStreamPair; typedef Networking::RequestIdPair<StorageInfo> RequestStorageInfoPair; typedef Networking::RequestIdPair<bool> RequestBoolPair; @@ -78,7 +78,7 @@ public: virtual int32 upload(Common::String path, Common::ReadStream *contents, BoolCallback callback) = 0; /** Returns pointer to Networking::NetworkReadStream. */ - virtual Networking::NetworkReadStream *streamFile(Common::String path) = 0; //TODO: return int32 somehow + virtual int32 streamFile(Common::String path, ReadStreamCallback callback) = 0; /** Calls the callback when finished. */ virtual int32 download(Common::String remotePath, Common::String localPath, BoolCallback callback) = 0; |