ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

boxuploadrequest.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "backends/cloud/box/boxuploadrequest.h"
00024 #include "backends/cloud/box/boxstorage.h"
00025 #include "backends/cloud/box/boxtokenrefresher.h"
00026 #include "backends/cloud/iso8601.h"
00027 #include "backends/cloud/storage.h"
00028 #include "backends/networking/curl/connectionmanager.h"
00029 #include "backends/networking/curl/curljsonrequest.h"
00030 #include "backends/networking/curl/networkreadstream.h"
00031 #include "common/json.h"
00032 
00033 namespace Cloud {
00034 namespace Box {
00035 
00036 #define BOX_API_FILES "https://upload.box.com/api/2.0/files"
00037 
00038 BoxUploadRequest::BoxUploadRequest(BoxStorage *storage, Common::String path, Common::String localPath, Storage::UploadCallback callback, Networking::ErrorCallback ecb):
00039     Networking::Request(nullptr, ecb), _storage(storage), _savePath(path), _localPath(localPath), _uploadCallback(callback),
00040     _workingRequest(nullptr), _ignoreCallback(false) {
00041     start();
00042 }
00043 
00044 BoxUploadRequest::~BoxUploadRequest() {
00045     _ignoreCallback = true;
00046     if (_workingRequest)
00047         _workingRequest->finish();
00048     delete _uploadCallback;
00049 }
00050 
00051 void BoxUploadRequest::start() {
00052     _ignoreCallback = true;
00053     if (_workingRequest)
00054         _workingRequest->finish();
00055     _resolvedId = ""; //used to update file contents
00056     _parentId = ""; //used to create file within parent directory
00057     _ignoreCallback = false;
00058 
00059     resolveId();
00060 }
00061 
00062 void BoxUploadRequest::resolveId() {
00063     //check whether such file already exists
00064     Storage::UploadCallback innerCallback = new Common::Callback<BoxUploadRequest, Storage::UploadResponse>(this, &BoxUploadRequest::idResolvedCallback);
00065     Networking::ErrorCallback innerErrorCallback = new Common::Callback<BoxUploadRequest, Networking::ErrorResponse>(this, &BoxUploadRequest::idResolveFailedCallback);
00066     _workingRequest = _storage->resolveFileId(_savePath, innerCallback, innerErrorCallback);
00067 }
00068 
00069 void BoxUploadRequest::idResolvedCallback(Storage::UploadResponse response) {
00070     _workingRequest = nullptr;
00071     if (_ignoreCallback) return;
00072     _resolvedId = response.value.id();
00073     upload();
00074 }
00075 
00076 void BoxUploadRequest::idResolveFailedCallback(Networking::ErrorResponse error) {
00077     _workingRequest = nullptr;
00078     if (_ignoreCallback) return;
00079 
00080     //not resolved => error or no such file
00081     if (error.response.contains("no such file found in its parent directory")) {
00082         //parent's id after the '\n'
00083         Common::String parentId = error.response;
00084         for (uint32 i = 0; i < parentId.size(); ++i)
00085             if (parentId[i] == '\n') {
00086                 parentId.erase(0, i + 1);
00087                 break;
00088             }
00089 
00090         _parentId = parentId;
00091         upload();
00092         return;
00093     }
00094 
00095     finishError(error);
00096 }
00097 
00098 void BoxUploadRequest::upload() {
00099     Common::String name = _savePath;
00100     for (uint32 i = name.size(); i > 0; --i) {
00101         if (name[i - 1] == '/' || name[i - 1] == '\\') {
00102             name.erase(0, i);
00103             break;
00104         }
00105     }
00106 
00107     Common::String url = BOX_API_FILES;
00108     if (_resolvedId != "")
00109         url += "/" + _resolvedId;
00110     url += "/content";
00111     Networking::JsonCallback callback = new Common::Callback<BoxUploadRequest, Networking::JsonResponse>(this, &BoxUploadRequest::uploadedCallback);
00112     Networking::ErrorCallback failureCallback = new Common::Callback<BoxUploadRequest, Networking::ErrorResponse>(this, &BoxUploadRequest::notUploadedCallback);
00113     Networking::CurlJsonRequest *request = new BoxTokenRefresher(_storage, callback, failureCallback, url.c_str());
00114     request->addHeader("Authorization: Bearer " + _storage->accessToken());
00115 
00116     Common::JSONObject jsonRequestParameters;
00117     if (_resolvedId == "") {
00118         Common::JSONObject parentObject;
00119         parentObject.setVal("id", new Common::JSONValue(_parentId));
00120         jsonRequestParameters.setVal("parent", new Common::JSONValue(parentObject));
00121         jsonRequestParameters.setVal("name", new Common::JSONValue(name));
00122     }
00123 
00124     Common::JSONValue value(jsonRequestParameters);
00125     request->addFormField("attributes", Common::JSON::stringify(&value));
00126     request->addFormFile("file", _localPath);
00127 
00128     _workingRequest = ConnMan.addRequest(request);
00129 }
00130 
00131 void BoxUploadRequest::uploadedCallback(Networking::JsonResponse response) {
00132     _workingRequest = nullptr;
00133     if (_ignoreCallback) return;
00134 
00135     Networking::ErrorResponse error(this, false, true, "", -1);
00136     Networking::CurlJsonRequest *rq = (Networking::CurlJsonRequest *)response.request;
00137     if (rq) {
00138         const Networking::NetworkReadStream *stream = rq->getNetworkReadStream();
00139         if (stream) {
00140             long code = stream->httpResponseCode();
00141             error.httpResponseCode = code;
00142         }
00143     }
00144 
00145     if (error.httpResponseCode != 200 && error.httpResponseCode != 201)
00146         warning("BoxUploadRequest: looks like an error (bad HTTP code)");
00147 
00148     //check JSON and show warnings if it's malformed
00149     Common::JSONValue *json = response.value;
00150     if (json == nullptr) {
00151         error.response = "Failed to parse JSON, null passed!";
00152         finishError(error);
00153         return;
00154     }
00155 
00156     if (!json->isObject()) {
00157         error.response = "Passed JSON is not an object!";
00158         finishError(error);
00159         delete json;
00160         return;
00161     }
00162 
00163     Common::JSONObject object = json->asObject();
00164     if (Networking::CurlJsonRequest::jsonContainsArray(object, "entries", "BoxUploadRequest")) {
00165         Common::JSONArray entries = object.getVal("entries")->asArray();
00166         if (entries.size() == 0) {
00167             warning("BoxUploadRequest: 'entries' found, but it's empty");
00168         } else if (!Networking::CurlJsonRequest::jsonIsObject(entries[0], "BoxUploadRequest")) {
00169             warning("BoxUploadRequest: 'entries' first item is not an object");
00170         } else {
00171             Common::JSONObject item = entries[0]->asObject();
00172 
00173             if (Networking::CurlJsonRequest::jsonContainsString(item, "id", "BoxUploadRequest") &&
00174                 Networking::CurlJsonRequest::jsonContainsString(item, "name", "BoxUploadRequest") &&
00175                 Networking::CurlJsonRequest::jsonContainsString(item, "type", "BoxUploadRequest") &&
00176                 Networking::CurlJsonRequest::jsonContainsString(item, "modified_at", "BoxUploadRequest") &&
00177                 Networking::CurlJsonRequest::jsonContainsStringOrIntegerNumber(item, "size", "BoxUploadRequest")) {
00178 
00179                 //finished
00180                 Common::String id = item.getVal("id")->asString();
00181                 Common::String name = item.getVal("name")->asString();
00182                 bool isDirectory = (item.getVal("type")->asString() == "folder");
00183                 uint32 size;
00184                 if (item.getVal("size")->isString()) {
00185                     size = item.getVal("size")->asString().asUint64();
00186                 } else {
00187                     size = item.getVal("size")->asIntegerNumber();
00188                 }
00189                 uint32 timestamp = ISO8601::convertToTimestamp(item.getVal("modified_at")->asString());
00190 
00191                 finishUpload(StorageFile(id, _savePath, name, size, timestamp, isDirectory));
00192                 delete json;
00193                 return;
00194             }
00195         }
00196     }
00197 
00198     //TODO: check errors
00199     /*
00200     if (object.contains("error")) {
00201         warning("Box returned error: %s", json->stringify(true).c_str());
00202         delete json;
00203         error.response = json->stringify(true);
00204         finishError(error);
00205         return;
00206     }
00207     */
00208 
00209     warning("BoxUploadRequest: no file info to return");
00210     finishUpload(StorageFile(_savePath, 0, 0, false));
00211 
00212     delete json;
00213 }
00214 
00215 void BoxUploadRequest::notUploadedCallback(Networking::ErrorResponse error) {
00216     _workingRequest = nullptr;
00217     if (_ignoreCallback) return;
00218     finishError(error);
00219 }
00220 
00221 void BoxUploadRequest::handle() {}
00222 
00223 void BoxUploadRequest::restart() { start(); }
00224 
00225 void BoxUploadRequest::finishUpload(StorageFile file) {
00226     Request::finishSuccess();
00227     if (_uploadCallback)
00228         (*_uploadCallback)(Storage::UploadResponse(this, file));
00229 }
00230 
00231 } // End of namespace Box
00232 } // End of namespace Cloud


Generated on Sat May 18 2019 05:00:56 for ResidualVM by doxygen 1.7.1
curved edge   curved edge