2016-03-10 20:42:33 -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/.
|
|
|
|
*/
|
|
|
|
|
2016-03-12 18:29:17 -06:00
|
|
|
#ifndef INCLUDED_DOCUMENTBROKER_HPP
|
|
|
|
#define INCLUDED_DOCUMENTBROKER_HPP
|
2016-03-10 20:42:33 -06:00
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <mutex>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <Poco/Path.h>
|
|
|
|
|
|
|
|
#include "Storage.hpp"
|
|
|
|
|
2016-03-12 18:29:17 -06:00
|
|
|
/// DocumentBroker is responsible for setting up a document
|
|
|
|
/// in jail and brokering loading it from Storage
|
|
|
|
/// and saving it back.
|
2016-03-10 20:42:33 -06:00
|
|
|
/// Contains URI, physical path, etc.
|
2016-03-12 18:29:17 -06:00
|
|
|
class DocumentBroker
|
2016-03-10 20:42:33 -06:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
static
|
|
|
|
Poco::URI getUri(std::string uri)
|
|
|
|
{
|
|
|
|
// The URI of the document is url-encoded
|
|
|
|
// and passed in our URL.
|
|
|
|
if (uri.size() > 1 && uri[0] == '/')
|
|
|
|
{
|
|
|
|
// Remove leading '/'.
|
|
|
|
uri.erase(0, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string decodedUri;
|
|
|
|
Poco::URI::decode(uri, decodedUri);
|
|
|
|
auto uriPublic = Poco::URI(decodedUri);
|
|
|
|
|
|
|
|
if (uriPublic.isRelative() || uriPublic.getScheme() == "file")
|
|
|
|
{
|
|
|
|
// TODO: Validate and limit access to local paths!
|
|
|
|
uriPublic.normalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
Log::info("Public URI [" + uriPublic.toString() + "].");
|
|
|
|
if (uriPublic.getPath().empty())
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Invalid URI.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return uriPublic;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
2016-03-12 18:29:17 -06:00
|
|
|
std::shared_ptr<DocumentBroker> create(const std::string& uri)
|
2016-03-10 20:42:33 -06:00
|
|
|
{
|
|
|
|
std::string decodedUri;
|
|
|
|
Poco::URI::decode(uri, decodedUri);
|
|
|
|
auto uriPublic = Poco::URI(decodedUri);
|
|
|
|
|
|
|
|
if (uriPublic.isRelative() || uriPublic.getScheme() == "file")
|
|
|
|
{
|
|
|
|
// TODO: Validate and limit access to local paths!
|
|
|
|
uriPublic.normalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
Log::info("Public URI [" + uriPublic.toString() + "].");
|
|
|
|
if (uriPublic.getPath().empty())
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Invalid URI.");
|
|
|
|
}
|
|
|
|
|
2016-03-12 18:29:17 -06:00
|
|
|
return create(uriPublic);
|
2016-03-10 20:42:33 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
2016-03-12 18:29:17 -06:00
|
|
|
std::shared_ptr<DocumentBroker> create(const Poco::URI& uriPublic)
|
2016-03-10 20:42:33 -06:00
|
|
|
{
|
2016-03-12 18:29:17 -06:00
|
|
|
Log::info("Creating DocumentBroker for uri: " + uriPublic.toString());
|
|
|
|
|
|
|
|
std::string docKey;
|
|
|
|
Poco::URI::encode(uriPublic.getPath(), "", docKey);
|
|
|
|
|
|
|
|
return std::shared_ptr<DocumentBroker>(new DocumentBroker(uriPublic, docKey));
|
|
|
|
}
|
|
|
|
|
|
|
|
~DocumentBroker()
|
|
|
|
{
|
|
|
|
Log::info("~DocumentBroker [" + _uriPublic.toString() + "] destroyed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Loads a document from the public URI into the jail.
|
|
|
|
bool load(const std::string& jailRoot, const std::string& jailId)
|
|
|
|
{
|
|
|
|
Log::debug("Loading from URI: " + _uriPublic.toString());
|
|
|
|
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
|
|
|
if (_storage)
|
|
|
|
{
|
|
|
|
// Already loaded. Just return.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
_jailId = jailId;
|
2016-03-10 20:42:33 -06:00
|
|
|
|
|
|
|
// The URL is the publicly visible one, not visible in the chroot jail.
|
|
|
|
// We need to map it to a jailed path and copy the file there.
|
|
|
|
|
2016-03-12 17:10:53 -06:00
|
|
|
// user/doc/jailId
|
|
|
|
const auto jailPath = Poco::Path(JailedDocumentRoot, jailId);
|
2016-03-10 20:42:33 -06:00
|
|
|
|
|
|
|
Log::info("jailPath: " + jailPath.toString() + ", jailRoot: " + jailRoot);
|
|
|
|
|
2016-03-12 18:29:17 -06:00
|
|
|
if (_uriPublic.isRelative() || _uriPublic.getScheme() == "file")
|
2016-03-10 20:42:33 -06:00
|
|
|
{
|
2016-03-12 18:29:17 -06:00
|
|
|
Log::info("Public URI [" + _uriPublic.toString() + "] is a file.");
|
|
|
|
_storage.reset(new LocalStorage(jailRoot, jailPath.toString(), _uriPublic.getPath()));
|
2016-03-10 20:42:33 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-12 18:29:17 -06:00
|
|
|
Log::info("Public URI [" + _uriPublic.toString() +
|
2016-03-10 20:42:33 -06:00
|
|
|
"] assuming cloud storage.");
|
|
|
|
//TODO: Configure the storage to use. For now, assume it's WOPI.
|
2016-03-12 18:29:17 -06:00
|
|
|
_storage.reset(new WopiStorage(jailRoot, jailPath.toString(), _uriPublic.toString()));
|
2016-03-10 20:42:33 -06:00
|
|
|
}
|
|
|
|
|
2016-03-12 18:29:17 -06:00
|
|
|
const auto localPath = _storage->loadStorageFileToLocal();
|
|
|
|
_uriJailed = Poco::URI(Poco::URI("file://"), localPath);
|
|
|
|
return true;
|
2016-03-10 20:42:33 -06:00
|
|
|
}
|
|
|
|
|
2016-03-10 21:01:34 -06:00
|
|
|
bool save()
|
|
|
|
{
|
2016-03-12 18:29:17 -06:00
|
|
|
Log::debug("Saving to URI: " + _uriPublic.toString());
|
|
|
|
|
2016-03-11 10:26:19 -06:00
|
|
|
assert(_storage);
|
2016-03-10 21:01:34 -06:00
|
|
|
return _storage->saveLocalFileToStorage();
|
|
|
|
}
|
|
|
|
|
2016-03-10 20:42:33 -06:00
|
|
|
Poco::URI getPublicUri() const { return _uriPublic; }
|
|
|
|
Poco::URI getJailedUri() const { return _uriJailed; }
|
2016-03-12 18:29:17 -06:00
|
|
|
const std::string& getJailId() const { return _jailId; }
|
|
|
|
const std::string& getDocKey() const { return _docKey; }
|
2016-03-10 20:42:33 -06:00
|
|
|
|
|
|
|
private:
|
2016-03-12 18:29:17 -06:00
|
|
|
DocumentBroker(const Poco::URI& uriPublic,
|
|
|
|
const std::string& docKey) :
|
2016-03-10 20:42:33 -06:00
|
|
|
_uriPublic(uriPublic),
|
2016-03-12 18:29:17 -06:00
|
|
|
_docKey(docKey)
|
2016-03-10 20:42:33 -06:00
|
|
|
{
|
2016-03-12 18:29:17 -06:00
|
|
|
Log::info("DocumentBroker [" + _uriPublic.toString() + "] created.");
|
2016-03-10 20:42:33 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Poco::URI _uriPublic;
|
2016-03-12 18:29:17 -06:00
|
|
|
const std::string _docKey;
|
|
|
|
Poco::URI _uriJailed;
|
|
|
|
std::string _jailId;
|
2016-03-10 21:01:34 -06:00
|
|
|
std::unique_ptr<StorageBase> _storage;
|
2016-03-12 18:29:17 -06:00
|
|
|
std::mutex _mutex;
|
2016-03-10 20:42:33 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|