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

Move: provide new handle to moved file.

parent d9172832
...@@ -34,8 +34,8 @@ namespace cloudstorage { ...@@ -34,8 +34,8 @@ namespace cloudstorage {
namespace { namespace {
void move(Request<EitherError<void>>::Pointer r, IHttp* http, template <class T>
std::string metadata_url, void move(typename Request<T>::Pointer r, IHttp* http, std::string metadata_url,
std::shared_ptr<std::vector<std::string>> lst, IItem::Pointer source, std::shared_ptr<std::vector<std::string>> lst, IItem::Pointer source,
IItem::Pointer destination, IItem::Pointer destination,
std::function<void(EitherError<void>)> complete) { std::function<void(EitherError<void>)> complete) {
...@@ -58,7 +58,7 @@ void move(Request<EitherError<void>>::Pointer r, IHttp* http, ...@@ -58,7 +58,7 @@ void move(Request<EitherError<void>>::Pointer r, IHttp* http,
if (e.left()) if (e.left())
complete(e.left()); complete(e.left());
else else
move(r, http, metadata_url, lst, source, destination, complete); move<T>(r, http, metadata_url, lst, source, destination, complete);
}, },
output); output);
} }
...@@ -99,13 +99,16 @@ IItem::Pointer AmazonDrive::rootDirectory() const { ...@@ -99,13 +99,16 @@ IItem::Pointer AmazonDrive::rootDirectory() const {
ICloudProvider::MoveItemRequest::Pointer AmazonDrive::moveItemAsync( ICloudProvider::MoveItemRequest::Pointer AmazonDrive::moveItemAsync(
IItem::Pointer source, IItem::Pointer destination, IItem::Pointer source, IItem::Pointer destination,
MoveItemCallback callback) { MoveItemCallback callback) {
auto r = std::make_shared<Request<EitherError<void>>>(shared_from_this()); auto r = std::make_shared<Request<EitherError<IItem>>>(shared_from_this());
r->set( r->set(
[=](Request<EitherError<void>>::Pointer r) { [=](Request<EitherError<IItem>>::Pointer r) {
move(r, http(), metadata_url(), move<EitherError<IItem>>(
std::make_shared<std::vector<std::string>>( r, http(), metadata_url(),
static_cast<Item*>(source.get())->parents()), std::make_shared<std::vector<std::string>>(
source, destination, callback); static_cast<Item*>(source.get())->parents()),
source, destination, [=](EitherError<void>) {
});
}, },
callback); callback);
return r->run(); return r->run();
......
...@@ -218,14 +218,20 @@ AuthorizeRequest::Pointer AmazonS3::authorizeAsync() { ...@@ -218,14 +218,20 @@ AuthorizeRequest::Pointer AmazonS3::authorizeAsync() {
ICloudProvider::MoveItemRequest::Pointer AmazonS3::moveItemAsync( ICloudProvider::MoveItemRequest::Pointer AmazonS3::moveItemAsync(
IItem::Pointer source, IItem::Pointer destination, IItem::Pointer source, IItem::Pointer destination,
MoveItemCallback callback) { MoveItemCallback callback) {
auto r = std::make_shared<Request<EitherError<void>>>(shared_from_this()); auto r = std::make_shared<Request<EitherError<IItem>>>(shared_from_this());
r->set( r->set(
[=](Request<EitherError<void>>::Pointer r) { [=](Request<EitherError<IItem>>::Pointer r) {
auto data = AmazonS3::extract(destination->id()); auto data = AmazonS3::extract(destination->id());
rename<EitherError<void>>( rename<EitherError<IItem>>(
r, http(), region(), {data.first, data.second + source->filename()}, r, http(), region(), {data.first, data.second + source->filename()},
AmazonS3::extract(source->id()), AmazonS3::extract(source->id()), [=](EitherError<void> e) {
[=](EitherError<void> e) { r->done(e); }); if (e.left()) return r->done(e.left());
IItem::Pointer nitem = util::make_unique<Item>(
source->filename(),
to_string({data.first, data.second + source->filename()}),
source->size(), source->timestamp(), source->type());
r->done(nitem);
});
}, },
callback); callback);
return r->run(); return r->run();
...@@ -248,8 +254,8 @@ ICloudProvider::RenameItemRequest::Pointer AmazonS3::renameItemAsync( ...@@ -248,8 +254,8 @@ ICloudProvider::RenameItemRequest::Pointer AmazonS3::renameItemAsync(
[=](EitherError<void> e) { [=](EitherError<void> e) {
if (e.left()) return r->done(e.left()); if (e.left()) return r->done(e.left());
IItem::Pointer nitem = util::make_unique<Item>( IItem::Pointer nitem = util::make_unique<Item>(
item->filename(), item->id(), item->size(), item->timestamp(), name, to_string({data.first, path + name}), item->size(),
item->type()); item->timestamp(), item->type());
r->done(nitem); r->done(nitem);
}); });
}, },
......
...@@ -517,6 +517,11 @@ IItem::Pointer CloudProvider::renameItemResponse(const IItem&, ...@@ -517,6 +517,11 @@ IItem::Pointer CloudProvider::renameItemResponse(const IItem&,
return getItemDataResponse(response); return getItemDataResponse(response);
} }
IItem::Pointer CloudProvider::moveItemResponse(const IItem&, const IItem&,
std::istream& response) const {
return getItemDataResponse(response);
}
std::string CloudProvider::getItemUrlResponse(const IItem&, std::string CloudProvider::getItemUrlResponse(const IItem&,
std::istream&) const { std::istream&) const {
return ""; return "";
......
...@@ -232,6 +232,9 @@ class CloudProvider : public ICloudProvider, ...@@ -232,6 +232,9 @@ class CloudProvider : public ICloudProvider,
const std::string& name, const std::string& name,
std::istream& response) const; std::istream& response) const;
virtual IItem::Pointer moveItemResponse(const IItem&, const IItem&,
std::istream&) const;
/** /**
* Used by default implementation of createDirectoryAsync, should translate * Used by default implementation of createDirectoryAsync, should translate
* response into new directory's item object. * response into new directory's item object.
......
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#include <condition_variable> #include <condition_variable>
#include <cstring> #include <cstring>
#include <fstream> #include <fstream>
#include <iostream>
#include <queue> #include <queue>
using namespace mega; using namespace mega;
...@@ -718,17 +719,23 @@ ICloudProvider::CreateDirectoryRequest::Pointer MegaNz::createDirectoryAsync( ...@@ -718,17 +719,23 @@ ICloudProvider::CreateDirectoryRequest::Pointer MegaNz::createDirectoryAsync(
ICloudProvider::MoveItemRequest::Pointer MegaNz::moveItemAsync( ICloudProvider::MoveItemRequest::Pointer MegaNz::moveItemAsync(
IItem::Pointer source, IItem::Pointer destination, IItem::Pointer source, IItem::Pointer destination,
MoveItemCallback callback) { MoveItemCallback callback) {
auto r = std::make_shared<Request<EitherError<void>>>(shared_from_this()); auto r = std::make_shared<Request<EitherError<IItem>>>(shared_from_this());
r->set( r->set(
[=](Request<EitherError<void>>::Pointer r) { [=](Request<EitherError<IItem>>::Pointer r) {
ensureAuthorized<EitherError<void>>(r, [=] { ensureAuthorized<EitherError<IItem>>(r, [=] {
std::unique_ptr<mega::MegaNode> source_node( std::unique_ptr<mega::MegaNode> source_node(
mega_->getNodeByPath(source->id().c_str())); mega_->getNodeByPath(source->id().c_str()));
std::unique_ptr<mega::MegaNode> destination_node( std::unique_ptr<mega::MegaNode> destination_node(
mega_->getNodeByPath(destination->id().c_str())); mega_->getNodeByPath(destination->id().c_str()));
if (source_node && destination_node) { if (source_node && destination_node) {
auto listener = Listener::make<RequestListener>( auto listener = Listener::make<RequestListener>(
[=](EitherError<void> e, Listener*) { r->done(e); }, this); [=](EitherError<void> e, Listener* listener) {
if (e.left()) return r->done(e.left());
std::unique_ptr<mega::MegaNode> node(mega_->getNodeByHandle(
static_cast<RequestListener*>(listener)->node_));
r->done(toItem(node.get()));
},
this);
r->subrequest(listener); r->subrequest(listener);
mega_->moveNode(source_node.get(), destination_node.get(), mega_->moveNode(source_node.get(), destination_node.get(),
listener.get()); listener.get());
...@@ -752,10 +759,10 @@ ICloudProvider::RenameItemRequest::Pointer MegaNz::renameItemAsync( ...@@ -752,10 +759,10 @@ ICloudProvider::RenameItemRequest::Pointer MegaNz::renameItemAsync(
mega_->getNodeByPath(item->id().c_str())); mega_->getNodeByPath(item->id().c_str()));
if (node) { if (node) {
auto listener = Listener::make<RequestListener>( auto listener = Listener::make<RequestListener>(
[=](EitherError<void> e, Listener*) { [=](EitherError<void> e, Listener* listener) {
if (e.left()) return r->done(e.left()); if (e.left()) return r->done(e.left());
std::unique_ptr<mega::MegaNode> node(mega_->getNodeByPath( std::unique_ptr<mega::MegaNode> node(mega_->getNodeByHandle(
(getPath(item->id()) + "/" + name).c_str())); static_cast<RequestListener*>(listener)->node_));
r->done(toItem(node.get())); r->done(toItem(node.get()));
}, },
this); this);
......
...@@ -145,9 +145,9 @@ IHttpRequest::Pointer WebDav::moveItemRequest(const IItem& source, ...@@ -145,9 +145,9 @@ IHttpRequest::Pointer WebDav::moveItemRequest(const IItem& source,
const IItem& destination, const IItem& destination,
std::ostream&) const { std::ostream&) const {
auto request = http()->create(endpoint() + source.id(), "MOVE"); auto request = http()->create(endpoint() + source.id(), "MOVE");
request->setHeaderParameter("Destination", request->setHeaderParameter(
util::Url(endpoint()).path() + destination.id() + "Destination",
"/" + source.filename()); util::Url(endpoint()).path() + destination.id() + source.filename());
return request; return request;
} }
...@@ -176,7 +176,22 @@ IItem::Pointer WebDav::renameItemResponse(const IItem& item, ...@@ -176,7 +176,22 @@ IItem::Pointer WebDav::renameItemResponse(const IItem& item,
std::istream&) const { std::istream&) const {
auto i = util::make_unique<Item>(name, getPath(item.id()) + "/" + name, auto i = util::make_unique<Item>(name, getPath(item.id()) + "/" + name,
item.size(), item.timestamp(), item.type()); item.size(), item.timestamp(), item.type());
i->set_url(static_cast<const Item&>(item).url()); auto lock = auth_lock();
auto url = util::Url(webdav_url_);
i->set_url(url.protocol() + "://" + user_ + ":" + password_ + "@" +
url.host() + url.path() + i->id() + url.query());
return i;
}
IItem::Pointer WebDav::moveItemResponse(const IItem& source, const IItem& dest,
std::istream&) const {
auto i =
util::make_unique<Item>(source.filename(), dest.id() + source.filename(),
source.size(), source.timestamp(), source.type());
auto lock = auth_lock();
auto url = util::Url(webdav_url_);
i->set_url(url.protocol() + "://" + user_ + ":" + password_ + "@" +
url.host() + url.path() + i->id() + url.query());
return i; return i;
} }
......
...@@ -78,6 +78,8 @@ class WebDav : public CloudProvider { ...@@ -78,6 +78,8 @@ class WebDav : public CloudProvider {
IItem::Pointer renameItemResponse(const IItem& old_item, IItem::Pointer renameItemResponse(const IItem& old_item,
const std::string& name, const std::string& name,
std::istream& response) const override; std::istream& response) const override;
IItem::Pointer moveItemResponse(const IItem&, const IItem&,
std::istream&) const override;
IItem::Pointer toItem(const tinyxml2::XMLNode*) const; IItem::Pointer toItem(const tinyxml2::XMLNode*) const;
......
...@@ -220,6 +220,14 @@ IItem::Pointer YandexDisk::renameItemResponse(const IItem& item, ...@@ -220,6 +220,14 @@ IItem::Pointer YandexDisk::renameItemResponse(const IItem& item,
item.size(), item.timestamp(), item.type()); item.size(), item.timestamp(), item.type());
} }
IItem::Pointer YandexDisk::moveItemResponse(const IItem& source,
const IItem& dest,
std::istream&) const {
return util::make_unique<Item>(
source.filename(), dest.id() + "/" + source.filename(), source.size(),
source.timestamp(), source.type());
}
std::vector<IItem::Pointer> YandexDisk::listDirectoryResponse( std::vector<IItem::Pointer> YandexDisk::listDirectoryResponse(
const IItem&, std::istream& stream, std::string& next_page_token) const { const IItem&, std::istream& stream, std::string& next_page_token) const {
Json::Value response; Json::Value response;
......
...@@ -69,6 +69,8 @@ class YandexDisk : public CloudProvider { ...@@ -69,6 +69,8 @@ class YandexDisk : public CloudProvider {
IItem::Pointer renameItemResponse(const IItem& old_item, IItem::Pointer renameItemResponse(const IItem& old_item,
const std::string& name, const std::string& name,
std::istream& response) const override; std::istream& response) const override;
IItem::Pointer moveItemResponse(const IItem&, const IItem&,
std::istream&) const override;
IItem::Pointer toItem(const Json::Value&) const; IItem::Pointer toItem(const Json::Value&) const;
void authorizeRequest(IHttpRequest&) const override; void authorizeRequest(IHttpRequest&) const override;
......
...@@ -53,7 +53,7 @@ class ICloudProvider { ...@@ -53,7 +53,7 @@ class ICloudProvider {
using GetItemDataRequest = IRequest<EitherError<IItem>>; using GetItemDataRequest = IRequest<EitherError<IItem>>;
using DeleteItemRequest = IRequest<EitherError<void>>; using DeleteItemRequest = IRequest<EitherError<void>>;
using CreateDirectoryRequest = IRequest<EitherError<IItem>>; using CreateDirectoryRequest = IRequest<EitherError<IItem>>;
using MoveItemRequest = IRequest<EitherError<void>>; using MoveItemRequest = IRequest<EitherError<IItem>>;
using RenameItemRequest = IRequest<EitherError<IItem>>; using RenameItemRequest = IRequest<EitherError<IItem>>;
class IAuthCallback { class IAuthCallback {
...@@ -331,7 +331,7 @@ class ICloudProvider { ...@@ -331,7 +331,7 @@ class ICloudProvider {
*/ */
virtual MoveItemRequest::Pointer moveItemAsync( virtual MoveItemRequest::Pointer moveItemAsync(
IItem::Pointer source, IItem::Pointer destination, IItem::Pointer source, IItem::Pointer destination,
MoveItemCallback callback = [](EitherError<void>) {}) = 0; MoveItemCallback callback = [](EitherError<IItem>) {}) = 0;
/** /**
* Renames item. * Renames item.
......
...@@ -229,7 +229,7 @@ using GetItemCallback = std::function<void(EitherError<IItem>)>; ...@@ -229,7 +229,7 @@ using GetItemCallback = std::function<void(EitherError<IItem>)>;
using GetItemDataCallback = std::function<void(EitherError<IItem>)>; using GetItemDataCallback = std::function<void(EitherError<IItem>)>;
using DeleteItemCallback = std::function<void(EitherError<void>)>; using DeleteItemCallback = std::function<void(EitherError<void>)>;
using CreateDirectoryCallback = std::function<void(EitherError<IItem>)>; using CreateDirectoryCallback = std::function<void(EitherError<IItem>)>;
using MoveItemCallback = std::function<void(EitherError<void>)>; using MoveItemCallback = std::function<void(EitherError<IItem>)>;
using RenameItemCallback = std::function<void(EitherError<IItem>)>; using RenameItemCallback = std::function<void(EitherError<IItem>)>;
using ListDirectoryPageCallback = std::function<void(EitherError<PageData>)>; using ListDirectoryPageCallback = std::function<void(EitherError<PageData>)>;
using ListDirectoryCallback = using ListDirectoryCallback =
......
...@@ -43,10 +43,13 @@ MoveItemRequest::MoveItemRequest(std::shared_ptr<CloudProvider> p, ...@@ -43,10 +43,13 @@ MoveItemRequest::MoveItemRequest(std::shared_ptr<CloudProvider> p,
return p->moveItemRequest(*source, *destination, *stream); return p->moveItemRequest(*source, *destination, *stream);
}, },
[=](EitherError<util::Output> e) { [=](EitherError<util::Output> e) {
if (e.left()) if (e.left()) return request->done(e.left());
request->done(e.left()); try {
else request->done(
request->done(nullptr); p->moveItemResponse(*source, *destination, *output));
} catch (std::exception) {
request->done(Error{IHttpRequest::Failure, output->str()});
}
}, },
output); output);
}, },
......
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
namespace cloudstorage { namespace cloudstorage {
class MoveItemRequest : public Request<EitherError<void>> { class MoveItemRequest : public Request<EitherError<IItem>> {
public: public:
MoveItemRequest(std::shared_ptr<CloudProvider>, IItem::Pointer source, MoveItemRequest(std::shared_ptr<CloudProvider>, IItem::Pointer source,
IItem::Pointer destination, MoveItemCallback); IItem::Pointer destination, MoveItemCallback);
......
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