Commit e08ccc42 authored by Paweł Wegner's avatar Paweł Wegner

Added convenience methods.

parent 5a8f2290
......@@ -36,21 +36,6 @@ namespace cloudstorage {
namespace {
class ListDirectoryCallback : public IListDirectoryCallback {
public:
ListDirectoryCallback(Request<bool>::Semaphore* semaphore)
: semaphore_(semaphore) {}
void receivedItem(IItem::Pointer) override {}
void done(const std::vector<IItem::Pointer>&) override {
semaphore_->notify();
}
void error(const std::string&) override { semaphore_->notify(); }
private:
Request<bool>::Semaphore* semaphore_;
};
std::string escapePath(IHttp* http, const std::string& str) {
std::string data = http->escape(str);
std::string slash = http->escape("/");
......@@ -92,7 +77,9 @@ bool rename(Request<bool>* r, std::string dest_id, std::string source_id,
->result();
Request<bool>::Semaphore semaphore(r);
auto children_request = r->provider()->listDirectoryAsync(
directory, make_unique<ListDirectoryCallback>(&semaphore));
directory, [&semaphore](const std::vector<IItem::Pointer>&) {
semaphore.notify();
});
semaphore.wait();
if (r->is_cancelled()) {
children_request->cancel();
......@@ -258,7 +245,9 @@ ICloudProvider::DeleteItemRequest::Pointer AmazonS3::deleteItemAsync(
if (item->type() == IItem::FileType::Directory) {
Request<bool>::Semaphore semaphore(r);
auto children_request = r->provider()->listDirectoryAsync(
item, make_unique<ListDirectoryCallback>(&semaphore));
item, [&semaphore](const std::vector<IItem::Pointer>&) {
semaphore.notify();
});
semaphore.wait();
if (r->is_cancelled()) {
children_request->cancel();
......
......@@ -25,6 +25,7 @@
#include <json/json.h>
#include <cstring>
#include <fstream>
#include <sstream>
#include "Utility/Item.h"
......@@ -50,6 +51,90 @@
using namespace std::placeholders;
namespace {
class ListDirectoryCallback : public cloudstorage::IListDirectoryCallback {
public:
ListDirectoryCallback(cloudstorage::ListDirectoryCallback callback)
: callback_(callback) {}
void receivedItem(cloudstorage::IItem::Pointer) override {}
void done(const std::vector<cloudstorage::IItem::Pointer>& result) override {
callback_(result);
}
void error(const std::string&) override { callback_({}); }
private:
cloudstorage::ListDirectoryCallback callback_;
};
class DownloadFileCallback : public cloudstorage::IDownloadFileCallback {
public:
DownloadFileCallback(const std::string& filename,
cloudstorage::DownloadFileCallback callback)
: file_(filename, std::ios_base::out | std::ios_base::binary),
callback_(callback) {}
void receivedData(const char* data, uint32_t length) override {
file_.write(data, length);
}
void done() override {
file_.close();
callback_(true);
}
void error(const std::string&) override {
file_.close();
callback_(false);
}
void progress(uint32_t, uint32_t) override {}
private:
std::fstream file_;
cloudstorage::DownloadFileCallback callback_;
};
class UploadFileCallback : public cloudstorage::IUploadFileCallback {
public:
UploadFileCallback(const std::string& filename,
cloudstorage::UploadFileCallback callback)
: file_(filename, std::ios_base::in | std::ios_base::binary),
callback_(callback) {
file_.seekg(0, std::ios::end);
size_ = file_.tellg();
file_.seekg(std::ios::beg);
}
void reset() override {
file_.clear();
file_.seekg(std::ios::beg);
}
uint32_t putData(char* data, uint32_t maxlength) override {
file_.read(data, maxlength);
return file_.gcount();
}
uint64_t size() override { return size_; }
void done() override { callback_(true); }
void error(const std::string&) override { callback_(false); }
void progress(uint32_t, uint32_t) override {}
private:
std::fstream file_;
cloudstorage::UploadFileCallback callback_;
uint64_t size_;
};
} // namespace
namespace cloudstorage {
CloudProvider::CloudProvider(IAuth::Pointer auth)
......@@ -257,6 +342,26 @@ ICloudProvider::RenameItemRequest::Pointer CloudProvider::renameItemAsync(
name, callback);
}
ICloudProvider::ListDirectoryRequest::Pointer CloudProvider::listDirectoryAsync(
IItem::Pointer item, ListDirectoryCallback callback) {
return listDirectoryAsync(item,
make_unique<::ListDirectoryCallback>(callback));
}
ICloudProvider::DownloadFileRequest::Pointer CloudProvider::downloadFileAsync(
IItem::Pointer item, const std::string& filename,
DownloadFileCallback callback) {
return downloadFileAsync(
item, make_unique<::DownloadFileCallback>(filename, callback));
}
ICloudProvider::UploadFileRequest::Pointer CloudProvider::uploadFileAsync(
IItem::Pointer parent, const std::string& filename,
UploadFileCallback callback) {
return uploadFileAsync(parent, filename,
make_unique<::UploadFileCallback>(filename, callback));
}
IHttpRequest::Pointer CloudProvider::getItemDataRequest(const std::string&,
std::ostream&) const {
return nullptr;
......
......@@ -82,6 +82,14 @@ class CloudProvider : public ICloudProvider,
RenameItemRequest::Pointer renameItemAsync(IItem::Pointer item,
const std::string&,
RenameItemCallback) override;
ListDirectoryRequest::Pointer listDirectoryAsync(
IItem::Pointer item, ListDirectoryCallback callback) override;
DownloadFileRequest::Pointer downloadFileAsync(IItem::Pointer item,
const std::string& filename,
DownloadFileCallback) override;
UploadFileRequest::Pointer uploadFileAsync(IItem::Pointer parent,
const std::string& filename,
UploadFileCallback) override;
/**
* Used by default implementation of getItemDataAsync.
......
......@@ -309,6 +309,48 @@ class ICloudProvider {
virtual RenameItemRequest::Pointer renameItemAsync(
IItem::Pointer item, const std::string& name,
RenameItemCallback callback) = 0;
/**
* Simplified version of listDirectoryAsync.
*
* @param item directory to be listed
* @param callback called when the request is finished
* @return object representing the pending request
*/
virtual ListDirectoryRequest::Pointer listDirectoryAsync(
IItem::Pointer item, ListDirectoryCallback callback) = 0;
/**
* Simplified version of downloadFileAsync.
*
* @param item item to be downloaded
*
* @param filename name at which the downloaded file will be saved
*
* @param callback called when done; if successful, it gets true, otherwise
* false
*
* @return object representing the pending request
*/
virtual DownloadFileRequest::Pointer downloadFileAsync(
IItem::Pointer item, const std::string& filename,
DownloadFileCallback callback) = 0;
/**
* Simplified version of uploadFileAsync.
*
* @param parent parent of the uploaded file
*
* @param filename name of file to upload
*
* @param callback called when done, if successful, it gets true, otherwise
* false
*
* @return object representing the pending request
*/
virtual UploadFileRequest::Pointer uploadFileAsync(
IItem::Pointer parent, const std::string& filename,
UploadFileCallback callback) = 0;
};
} // namespace cloudstorage
......
......@@ -175,6 +175,10 @@ using DeleteItemCallback = std::function<void(bool)>;
using CreateDirectoryCallback = std::function<void(IItem::Pointer)>;
using MoveItemCallback = std::function<void(bool)>;
using RenameItemCallback = std::function<void(bool)>;
using ListDirectoryCallback =
std::function<void(const std::vector<IItem::Pointer>&)>;
using DownloadFileCallback = std::function<void(bool)>;
using UploadFileCallback = std::function<void(bool)>;
} // namespace cloudstorage
......
......@@ -46,8 +46,8 @@ GetItemRequest::GetItemRequest(std::shared_ptr<CloudProvider> p,
{
std::lock_guard<std::mutex> lock(mutex_);
if (is_cancelled()) return nullptr;
current_request_ =
provider()->listDirectoryAsync(std::move(node), nullptr);
current_request_ = provider()->listDirectoryAsync(
std::move(node), [](const std::vector<IItem::Pointer>&) {});
}
node = getItem(current_request_->result(), token);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment