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-04-16 17:12:10 -05:00
|
|
|
#include <set>
|
2016-02-19 15:19:15 -06:00
|
|
|
|
2016-04-06 23:32:28 -05:00
|
|
|
#include <Poco/Util/Application.h>
|
2016-03-31 01:48:34 -05:00
|
|
|
#include <Poco/URI.h>
|
2016-03-03 20:05:30 -06:00
|
|
|
|
2016-02-19 15:19:15 -06:00
|
|
|
#include "Auth.hpp"
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include "Log.hpp"
|
2016-02-19 15:19:15 -06:00
|
|
|
#include "Util.hpp"
|
|
|
|
|
|
|
|
/// Base class of all Storage abstractions.
|
|
|
|
class StorageBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2016-08-13 23:01:13 -05:00
|
|
|
/// Represents a file's attributes.
|
|
|
|
/// Used for local and network files.
|
2016-03-21 18:12:00 -05:00
|
|
|
class FileInfo
|
|
|
|
{
|
|
|
|
public:
|
2016-09-26 01:44:40 -05:00
|
|
|
FileInfo(const std::string& filename,
|
|
|
|
const Poco::Timestamp& modifiedTime,
|
|
|
|
size_t size,
|
|
|
|
const std::string& userId,
|
|
|
|
const std::string& userName)
|
|
|
|
: _filename(filename),
|
|
|
|
_modifiedTime(modifiedTime),
|
|
|
|
_size(size),
|
|
|
|
_userId(userId),
|
|
|
|
_userName(userName)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-04-06 23:32:28 -05:00
|
|
|
bool isValid() const
|
|
|
|
{
|
2016-04-19 02:10:07 -05:00
|
|
|
return !_filename.empty() && _size > 0;
|
2016-04-06 23:32:28 -05:00
|
|
|
}
|
|
|
|
|
2016-04-19 02:10:07 -05:00
|
|
|
std::string _filename;
|
|
|
|
Poco::Timestamp _modifiedTime;
|
|
|
|
size_t _size;
|
2016-08-29 08:41:37 -05:00
|
|
|
std::string _userId;
|
|
|
|
std::string _userName;
|
2016-03-21 18:12:00 -05:00
|
|
|
};
|
|
|
|
|
2016-03-05 09:27:30 -06:00
|
|
|
/// localStorePath the absolute root path of the chroot.
|
|
|
|
/// jailPath the path within the jail that the child uses.
|
|
|
|
StorageBase(const std::string& localStorePath,
|
2016-03-09 17:36:25 -06:00
|
|
|
const std::string& jailPath,
|
|
|
|
const std::string& uri) :
|
2016-03-05 09:27:30 -06:00
|
|
|
_localStorePath(localStorePath),
|
2016-03-09 17:36:25 -06:00
|
|
|
_jailPath(jailPath),
|
2016-09-26 01:44:40 -05:00
|
|
|
_uri(uri),
|
|
|
|
_fileInfo("", Poco::Timestamp(), 0, "", "")
|
2016-03-03 20:05:30 -06:00
|
|
|
{
|
2016-03-21 07:28:18 -05:00
|
|
|
Log::debug("Storage ctor: " + uri);
|
2016-03-03 20:05:30 -06:00
|
|
|
}
|
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
std::string getLocalRootPath() const;
|
2016-03-08 18:21:16 -06:00
|
|
|
|
2016-03-09 17:36:25 -06:00
|
|
|
const std::string& getUri() const { return _uri; }
|
|
|
|
|
2016-03-21 18:12:00 -05:00
|
|
|
/// Returns information about the file.
|
2016-03-22 17:46:04 -05:00
|
|
|
virtual FileInfo getFileInfo(const Poco::URI& uri) = 0;
|
2016-03-21 18:12:00 -05:00
|
|
|
|
|
|
|
/// Returns a local file path for the given URI.
|
2016-02-19 15:19:15 -06:00
|
|
|
/// If necessary copies the file locally first.
|
2016-03-10 21:01:34 -06:00
|
|
|
virtual std::string loadStorageFileToLocal() = 0;
|
2016-02-19 15:19:15 -06:00
|
|
|
|
2016-03-03 20:05:30 -06:00
|
|
|
/// Writes the contents of the file back to the source.
|
2016-03-21 18:12:00 -05:00
|
|
|
/// TODO: Should we save to the specific client's URI?
|
|
|
|
/// The advantage is that subseqent views (to the first)
|
|
|
|
/// will not depend on the token of the first.
|
2016-03-10 21:01:34 -06:00
|
|
|
virtual bool saveLocalFileToStorage() = 0;
|
2016-02-19 15:19:15 -06:00
|
|
|
|
2016-03-11 12:45:13 -06:00
|
|
|
static
|
2016-03-31 01:48:34 -05:00
|
|
|
size_t getFileSize(const std::string& filename);
|
2016-03-11 12:45:13 -06:00
|
|
|
|
2016-04-16 07:13:59 -05:00
|
|
|
/// Must be called at startup to configure.
|
|
|
|
static void initialize();
|
|
|
|
|
|
|
|
/// Storage object creation factory.
|
2016-04-07 15:59:27 -05:00
|
|
|
static std::unique_ptr<StorageBase> create(const std::string& jailRoot,
|
|
|
|
const std::string& jailPath,
|
|
|
|
const Poco::URI& uri);
|
|
|
|
|
2016-03-03 20:05:30 -06:00
|
|
|
protected:
|
|
|
|
const std::string _localStorePath;
|
2016-03-05 09:27:30 -06:00
|
|
|
const std::string _jailPath;
|
2016-03-09 17:36:25 -06:00
|
|
|
const std::string _uri;
|
|
|
|
std::string _jailedFilePath;
|
2016-03-21 18:12:00 -05:00
|
|
|
FileInfo _fileInfo;
|
2016-04-16 07:13:59 -05:00
|
|
|
|
2016-06-07 02:18:49 -05:00
|
|
|
static bool FilesystemEnabled;
|
|
|
|
static bool WopiEnabled;
|
2016-04-18 20:13:17 -05:00
|
|
|
/// Allowed/denied WOPI hosts, if any and if WOPI is enabled.
|
2016-06-07 02:18:49 -05:00
|
|
|
static Util::RegexListMatcher WopiHosts;
|
2016-02-19 15:19:15 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Trivial implementation of local storage that does not need do anything.
|
|
|
|
class LocalStorage : public StorageBase
|
|
|
|
{
|
|
|
|
public:
|
2016-03-05 09:27:30 -06:00
|
|
|
LocalStorage(const std::string& localStorePath,
|
2016-03-09 17:36:25 -06:00
|
|
|
const std::string& jailPath,
|
|
|
|
const std::string& uri) :
|
|
|
|
StorageBase(localStorePath, jailPath, uri),
|
|
|
|
_isCopy(false)
|
2016-03-05 09:27:30 -06:00
|
|
|
{
|
2016-04-16 11:53:42 -05:00
|
|
|
Log::info("LocalStorage ctor with localStorePath: [" + localStorePath +
|
|
|
|
"], jailPath: [" + jailPath + "], uri: [" + uri + "].");
|
2016-03-05 09:27:30 -06:00
|
|
|
}
|
2016-02-19 15:19:15 -06:00
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
FileInfo getFileInfo(const Poco::URI& uri) override;
|
2016-03-05 09:27:30 -06:00
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
std::string loadStorageFileToLocal() override;
|
2016-03-05 09:27:30 -06:00
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
bool saveLocalFileToStorage() override;
|
2016-03-09 17:36:25 -06:00
|
|
|
|
|
|
|
private:
|
|
|
|
/// True if the jailed file is not linked but copied.
|
|
|
|
bool _isCopy;
|
2016-02-19 15:19:15 -06:00
|
|
|
};
|
|
|
|
|
2016-08-13 23:01:13 -05:00
|
|
|
/// WOPI protocol backed storage.
|
2016-03-03 20:05:30 -06:00
|
|
|
class WopiStorage : public StorageBase
|
|
|
|
{
|
|
|
|
public:
|
2016-03-05 09:27:30 -06:00
|
|
|
WopiStorage(const std::string& localStorePath,
|
2016-03-09 17:36:25 -06:00
|
|
|
const std::string& jailPath,
|
|
|
|
const std::string& uri) :
|
|
|
|
StorageBase(localStorePath, jailPath, uri)
|
2016-03-03 20:05:30 -06:00
|
|
|
{
|
2016-04-16 11:53:42 -05:00
|
|
|
Log::info("WopiStorage ctor with localStorePath: [" + localStorePath +
|
|
|
|
"], jailPath: [" + jailPath + "], uri: [" + uri + "].");
|
2016-03-03 20:05:30 -06:00
|
|
|
}
|
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
FileInfo getFileInfo(const Poco::URI& uri) override;
|
2016-03-21 18:12:00 -05:00
|
|
|
|
2016-03-03 20:05:30 -06:00
|
|
|
/// uri format: http://server/<...>/wopi*/files/<id>/content
|
2016-03-31 01:48:34 -05:00
|
|
|
std::string loadStorageFileToLocal() override;
|
2016-03-11 12:45:13 -06:00
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
bool saveLocalFileToStorage() override;
|
2016-03-03 20:05:30 -06:00
|
|
|
};
|
|
|
|
|
2016-08-13 23:01:13 -05:00
|
|
|
/// WebDAV protocol backed storage.
|
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,
|
2016-03-05 09:27:30 -06:00
|
|
|
const std::string& jailPath,
|
2016-03-09 17:36:25 -06:00
|
|
|
const std::string& uri,
|
2016-03-03 20:05:30 -06:00
|
|
|
std::unique_ptr<AuthBase> authAgent) :
|
2016-03-09 17:36:25 -06:00
|
|
|
StorageBase(localStorePath, jailPath, uri),
|
2016-02-19 15:19:15 -06:00
|
|
|
_authAgent(std::move(authAgent))
|
|
|
|
{
|
2016-04-16 11:53:42 -05:00
|
|
|
Log::info("WebDAVStorage ctor with localStorePath: [" + localStorePath +
|
|
|
|
"], jailPath: [" + jailPath + "], uri: [" + uri + "].");
|
2016-02-19 15:19:15 -06:00
|
|
|
}
|
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
FileInfo getFileInfo(const Poco::URI& uri) override;
|
2016-03-21 18:12:00 -05:00
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
std::string loadStorageFileToLocal() override;
|
2016-02-19 15:19:15 -06:00
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
bool saveLocalFileToStorage() override;
|
2016-04-16 11:53:42 -05:00
|
|
|
|
2016-02-19 15:19:15 -06:00
|
|
|
private:
|
|
|
|
std::unique_ptr<AuthBase> _authAgent;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
2016-04-08 03:08:41 -05:00
|
|
|
|
2016-02-19 15:19:15 -06:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|