From caaa4c5a5d0bce7582cc6611d8bde53fbdb1f2d1 Mon Sep 17 00:00:00 2001 From: Alexander Tkachev Date: Tue, 24 May 2016 12:31:27 +0600 Subject: CLOUD: Make DownloadRequest write to local file Tested with .jpg file. Transfer complete, CRC-32 is the same. --- backends/cloud/downloadrequest.cpp | 41 ++++++++++++++++++------------- backends/cloud/downloadrequest.h | 8 +++--- backends/cloud/dropbox/dropboxstorage.cpp | 15 ++++++++--- backends/cloud/dropbox/dropboxstorage.h | 2 +- backends/cloud/storage.h | 2 +- 5 files changed, 43 insertions(+), 25 deletions(-) (limited to 'backends/cloud') diff --git a/backends/cloud/downloadrequest.cpp b/backends/cloud/downloadrequest.cpp index ed84d4f0e5..8124fd67d7 100644 --- a/backends/cloud/downloadrequest.cpp +++ b/backends/cloud/downloadrequest.cpp @@ -26,38 +26,45 @@ namespace Cloud { -DownloadRequest::DownloadRequest(Storage::BoolCallback callback, Networking::NetworkReadStream *stream): - Request(0), _boolCallback(callback), _stream(stream) {} +DownloadRequest::DownloadRequest(Storage::BoolCallback callback, Networking::NetworkReadStream *stream, Common::DumpFile *dumpFile): + Request(0), _boolCallback(callback), _remoteFileStream(stream), _localFile(dumpFile) {} bool DownloadRequest::handle() { - if (!_stream) { + if (!_remoteFileStream) { warning("DownloadRequest: no stream to read"); return true; } + if (!_localFile) { + warning("DownloadRequest: no file to write"); + return true; + } + + if (!_localFile->isOpen()) { + warning("DownloadRequest: failed to open file to write"); + return true; + } + const int kBufSize = 16 * 1024; char buf[kBufSize]; - uint32 readBytes = _stream->read(buf, kBufSize); + uint32 readBytes = _remoteFileStream->read(buf, kBufSize); - //TODO: save into file - /* if (readBytes != 0) - if (_outputStream.write(buf, readBytes) != readBytes) - warning("DropboxDownloadRequest: unable to write all received bytes into output stream"); - */ - - buf[readBytes] = 0; - debug("%s", buf); //TODO: remove + if (_localFile->write(buf, readBytes) != readBytes) { + warning("DownloadRequest: unable to write all received bytes into output file"); + if (_boolCallback) (*_boolCallback)(false); + return true; + } - if (_stream->eos()) { - if (_stream->httpResponseCode() != 200) { - warning("HTTP response code is not 200 OK (it's %ld)", _stream->httpResponseCode()); + if (_remoteFileStream->eos()) { + if (_remoteFileStream->httpResponseCode() != 200) { + warning("HTTP response code is not 200 OK (it's %ld)", _remoteFileStream->httpResponseCode()); //TODO: do something about it actually } - if (_boolCallback) (*_boolCallback)(_stream->httpResponseCode() == 200); + if (_boolCallback) (*_boolCallback)(_remoteFileStream->httpResponseCode() == 200); - //TODO: close file stream + _localFile->close(); //yes, I know it's closed automatically in ~DumpFile() return true; } diff --git a/backends/cloud/downloadrequest.h b/backends/cloud/downloadrequest.h index a819910b1b..b135b15f23 100644 --- a/backends/cloud/downloadrequest.h +++ b/backends/cloud/downloadrequest.h @@ -26,16 +26,18 @@ #include "backends/networking/curl/request.h" #include "backends/networking/curl/networkreadstream.h" #include "backends/cloud/storage.h" +#include namespace Cloud { class DownloadRequest: public Networking::Request { - Networking::NetworkReadStream *_stream; Storage::BoolCallback _boolCallback; + Networking::NetworkReadStream *_remoteFileStream; + Common::DumpFile *_localFile; public: - DownloadRequest(Storage::BoolCallback callback, Networking::NetworkReadStream *stream); - virtual ~DownloadRequest() {} + DownloadRequest(Storage::BoolCallback callback, Networking::NetworkReadStream *stream, Common::DumpFile *dumpFile); + virtual ~DownloadRequest() { delete _localFile; } virtual bool handle(); }; diff --git a/backends/cloud/dropbox/dropboxstorage.cpp b/backends/cloud/dropbox/dropboxstorage.cpp index 1b6dc1b92f..38ad12d94b 100644 --- a/backends/cloud/dropbox/dropboxstorage.cpp +++ b/backends/cloud/dropbox/dropboxstorage.cpp @@ -30,6 +30,7 @@ #include "common/debug.h" #include "common/json.h" #include +#include namespace Cloud { namespace Dropbox { @@ -100,15 +101,23 @@ Networking::NetworkReadStream *DropboxStorage::streamFile(Common::String path) { return request->execute(); } -void DropboxStorage::download(Common::String path, BoolCallback callback) { - ConnMan.addRequest(new DownloadRequest(callback, streamFile(path))); +void DropboxStorage::download(Common::String remotePath, Common::String localPath, BoolCallback callback) { + Common::DumpFile *f = new Common::DumpFile(); + if (!f->open(localPath)) { + warning("DropboxStorage: unable to open file to download into"); + if (callback) (*callback)(false); + delete f; + return; + } + + ConnMan.addRequest(new DownloadRequest(callback, streamFile(remotePath), f)); } void DropboxStorage::syncSaves(BoolCallback callback) { //this is not the real syncSaves() implementation //"" is root in Dropbox, not "/" //listDirectory("", new Common::Callback >(this, &DropboxStorage::printFiles), true); - download("/notempty.txt", 0); + download("/remote/test.jpg", "local/test.jpg", 0); } void DropboxStorage::info(StorageInfoCallback outerCallback) { diff --git a/backends/cloud/dropbox/dropboxstorage.h b/backends/cloud/dropbox/dropboxstorage.h index 92c3746da5..415a3fe5fb 100644 --- a/backends/cloud/dropbox/dropboxstorage.h +++ b/backends/cloud/dropbox/dropboxstorage.h @@ -73,7 +73,7 @@ public: virtual Networking::NetworkReadStream *streamFile(Common::String path); /** Calls the callback when finished. */ - virtual void download(Common::String path, BoolCallback callback); + virtual void download(Common::String remotePath, Common::String localPath, BoolCallback callback); /** Calls the callback when finished. */ virtual void remove(Common::String path, BoolCallback callback) {} //TODO diff --git a/backends/cloud/storage.h b/backends/cloud/storage.h index 92f3bb96b2..e38c6bedd9 100644 --- a/backends/cloud/storage.h +++ b/backends/cloud/storage.h @@ -69,7 +69,7 @@ public: virtual Networking::NetworkReadStream *streamFile(Common::String path) = 0; /** Calls the callback when finished. */ - virtual void download(Common::String path, BoolCallback callback) = 0; + virtual void download(Common::String remotePath, Common::String localPath, BoolCallback callback) = 0; /** Calls the callback when finished. */ virtual void remove(Common::String path, BoolCallback callback) = 0; -- cgit v1.2.3