CLOUD: Make DownloadRequest write to local file

Tested with .jpg file. Transfer complete, CRC-32 is the same.
This commit is contained in:
Alexander Tkachev 2016-05-24 12:31:27 +06:00
parent 826a2a921c
commit caaa4c5a5d
5 changed files with 43 additions and 25 deletions

View File

@ -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");
*/
if (_localFile->write(buf, readBytes) != readBytes) {
warning("DownloadRequest: unable to write all received bytes into output file");
if (_boolCallback) (*_boolCallback)(false);
return true;
}
buf[readBytes] = 0;
debug("%s", buf); //TODO: remove
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;
}

View File

@ -26,16 +26,18 @@
#include "backends/networking/curl/request.h"
#include "backends/networking/curl/networkreadstream.h"
#include "backends/cloud/storage.h"
#include <common/file.h>
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();
};

View File

@ -30,6 +30,7 @@
#include "common/debug.h"
#include "common/json.h"
#include <curl/curl.h>
#include <common/file.h>
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<DropboxStorage, Common::Array<StorageFile> >(this, &DropboxStorage::printFiles), true);
download("/notempty.txt", 0);
download("/remote/test.jpg", "local/test.jpg", 0);
}
void DropboxStorage::info(StorageInfoCallback outerCallback) {

View File

@ -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

View File

@ -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;