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