aboutsummaryrefslogtreecommitdiff
path: root/backends/cloud/onedrive
diff options
context:
space:
mode:
Diffstat (limited to 'backends/cloud/onedrive')
-rw-r--r--backends/cloud/onedrive/onedrivestorage.cpp37
-rw-r--r--backends/cloud/onedrive/onedrivestorage.h4
-rw-r--r--backends/cloud/onedrive/onedrivetokenrefresher.cpp2
-rw-r--r--backends/cloud/onedrive/onedrivetokenrefresher.h2
4 files changed, 34 insertions, 11 deletions
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