2016-02-19 15:19:15 -06:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Storage abstraction.
|
|
|
|
#ifndef INCLUDED_STORAGE_HPP
|
|
|
|
#define INCLUDED_STORAGE_HPP
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2016-03-03 20:05:30 -06:00
|
|
|
#include <Poco/Net/HTTPResponse.h>
|
|
|
|
|
2016-02-19 15:19:15 -06:00
|
|
|
#include "Auth.hpp"
|
|
|
|
#include "Util.hpp"
|
|
|
|
|
|
|
|
/// Base class of all Storage abstractions.
|
|
|
|
class StorageBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2016-03-03 20:05:30 -06:00
|
|
|
StorageBase(const std::string& localStorePath) :
|
|
|
|
_localStorePath(localStorePath)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a local file path given a URI or ID.
|
2016-02-19 15:19:15 -06:00
|
|
|
/// If necessary copies the file locally first.
|
|
|
|
virtual std::string getFilePathFromURI(const std::string& uri) = 0;
|
|
|
|
|
2016-03-03 20:05:30 -06:00
|
|
|
/// Writes the contents of the file back to the source.
|
2016-02-19 15:19:15 -06:00
|
|
|
virtual bool restoreFileToURI(const std::string& path, const std::string& uri) = 0;
|
|
|
|
|
2016-03-03 20:05:30 -06:00
|
|
|
protected:
|
|
|
|
const std::string _localStorePath;
|
2016-02-19 15:19:15 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Trivial implementation of local storage that does not need do anything.
|
|
|
|
class LocalStorage : public StorageBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
std::string getFilePathFromURI(const std::string& uri) override
|
|
|
|
{
|
|
|
|
// It's local already.
|
|
|
|
// TODO: Validate access?
|
|
|
|
return uri;
|
|
|
|
}
|
|
|
|
|
2016-02-23 02:32:25 -06:00
|
|
|
bool restoreFileToURI(const std::string& path, const std::string& uri) override
|
2016-02-19 15:19:15 -06:00
|
|
|
{
|
|
|
|
// Nothing to do.
|
|
|
|
(void)path;
|
|
|
|
(void)uri;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-03-03 20:05:30 -06:00
|
|
|
class WopiStorage : public StorageBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
WopiStorage(const std::string& localStorePath) :
|
|
|
|
StorageBase(localStorePath)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// uri format: http://server/<...>/wopi*/files/<id>/content
|
|
|
|
std::string getFilePathFromURI(const std::string& uri) override
|
|
|
|
{
|
|
|
|
Poco::URI uriObject(uri);
|
|
|
|
Poco::Net::HTTPClientSession session(uriObject.getHost(), uriObject.getPort());
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, uri, Poco::Net::HTTPMessage::HTTP_1_1);
|
|
|
|
Poco::Net::HTTPResponse response;
|
|
|
|
session.sendRequest(request);
|
|
|
|
std::istream& rs = session.receiveResponse(response);
|
|
|
|
Log::info() << "WOPI::GetFile Status: " << response.getStatus() << " " << response.getReason() << Log::end;
|
|
|
|
|
|
|
|
//TODO: Get proper filename.
|
|
|
|
const std::string local_filename = _localStorePath + "/filename";
|
|
|
|
std::ofstream ofs(local_filename);
|
|
|
|
std::copy(std::istreambuf_iterator<char>(rs),
|
|
|
|
std::istreambuf_iterator<char>(),
|
|
|
|
std::ostreambuf_iterator<char>(ofs));
|
|
|
|
return local_filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool restoreFileToURI(const std::string& path, const std::string& uri) override
|
|
|
|
{
|
|
|
|
Poco::URI uriObject(uri);
|
|
|
|
Poco::Net::HTTPClientSession session(uriObject.getHost(), uriObject.getPort());
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, uri, Poco::Net::HTTPMessage::HTTP_1_1);
|
|
|
|
|
|
|
|
std::ifstream ifs(path);
|
|
|
|
request.read(ifs);
|
|
|
|
|
|
|
|
Poco::Net::HTTPResponse response;
|
|
|
|
session.sendRequest(request);
|
|
|
|
Log::info() << "WOPI::PutFile Status: " << response.getStatus() << " " << response.getReason() << Log::end;
|
|
|
|
|
|
|
|
return (response.getStatus() == Poco::Net::HTTPResponse::HTTP_OK);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-19 15:19:15 -06:00
|
|
|
class WebDAVStorage : public StorageBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2016-03-03 20:05:30 -06:00
|
|
|
WebDAVStorage(const std::string& localStorePath,
|
|
|
|
const std::string& url,
|
|
|
|
std::unique_ptr<AuthBase> authAgent) :
|
|
|
|
StorageBase(localStorePath),
|
2016-02-19 15:19:15 -06:00
|
|
|
_url(url),
|
|
|
|
_authAgent(std::move(authAgent))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getFilePathFromURI(const std::string& uri) override
|
|
|
|
{
|
|
|
|
// TODO: implement webdav GET.
|
|
|
|
return uri;
|
|
|
|
}
|
|
|
|
|
2016-02-23 02:32:25 -06:00
|
|
|
bool restoreFileToURI(const std::string& path, const std::string& uri) override
|
2016-02-19 15:19:15 -06:00
|
|
|
{
|
|
|
|
// TODO: implement webdav PUT.
|
|
|
|
(void)path;
|
|
|
|
(void)uri;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const std::string _url;
|
|
|
|
std::unique_ptr<AuthBase> _authAgent;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|