[vlmc-devel] [PATCH] Added libcloudstorage based filesystem.

Paweł Wegner pawel.wegner95 at gmail.com
Tue Aug 9 18:06:55 CEST 2016


---
 Makefile.am                                | 17 +++++++
 configure.ac                               |  6 +++
 src/factory/CloudProvider.cpp              | 63 +++++++++++++++++++++++
 src/factory/CloudProvider.h                | 54 ++++++++++++++++++++
 src/filesystem/cloudprovider/Device.cpp    | 82 ++++++++++++++++++++++++++++++
 src/filesystem/cloudprovider/Device.h      | 50 ++++++++++++++++++
 src/filesystem/cloudprovider/Directory.cpp | 79 ++++++++++++++++++++++++++++
 src/filesystem/cloudprovider/Directory.h   | 53 +++++++++++++++++++
 src/filesystem/cloudprovider/File.cpp      | 40 +++++++++++++++
 src/filesystem/cloudprovider/File.h        | 43 ++++++++++++++++
 10 files changed, 487 insertions(+)
 create mode 100644 src/factory/CloudProvider.cpp
 create mode 100644 src/factory/CloudProvider.h
 create mode 100644 src/filesystem/cloudprovider/Device.cpp
 create mode 100644 src/filesystem/cloudprovider/Device.h
 create mode 100644 src/filesystem/cloudprovider/Directory.cpp
 create mode 100644 src/filesystem/cloudprovider/Directory.h
 create mode 100644 src/filesystem/cloudprovider/File.cpp
 create mode 100644 src/filesystem/cloudprovider/File.h

diff --git a/Makefile.am b/Makefile.am
index 064be20..eb4ec03 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -194,6 +194,23 @@ libmedialibrary_la_LIBADD += $(LIBJPEG_LIBS)
 endif
 endif
 
+if HAVE_LIBCLOUDSTORAGE
+libmedialibrary_la_SOURCES += \
+	src/factory/CloudProvider.cpp \
+	src/filesystem/cloudprovider/Directory.cpp \
+	src/filesystem/cloudprovider/Device.cpp \
+	src/filesystem/cloudprovider/File.cpp \
+	$(NULL)
+noinst_HEADERS += \
+	src/factory/CloudProvider.h \
+	src/filesystem/cloudprovider/Directory.h \
+	src/filesystem/cloudprovider/Device.h \
+	src/filesystem/cloudprovider/File.h \
+	$(NULL)
+libmedialibrary_la_CPPFLAGS += $(LIBCLOUDSTORAGE_CFLAGS)
+libmedialibrary_la_LIBADD += $(LIBCLOUDSTORAGE_LIBS)
+endif
+
 lib_LTLIBRARIES = libmedialibrary.la
 if HAVE_DARWIN
 lib_LTLIBRARIES += libmedialibrary_macos.la
diff --git a/configure.ac b/configure.ac
index 5ebba8a..6efb72b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -269,6 +269,12 @@ PKG_CHECK_MODULES(EVAS, evas, [
  ], [have_evas="no"])
 AM_CONDITIONAL([HAVE_EVAS], [test "${have_evas}" = "yes"])
 
+PKG_CHECK_MODULES(LIBCLOUDSTORAGE, libcloudstorage, [
+     have_libcloudstorage="yes"
+     AC_DEFINE([HAVE_LIBCLOUDSTORAGE], 1, [Define to 1 if libcloudstorage is available])
+  ])
+AM_CONDITIONAL([HAVE_LIBCLOUDSTORAGE], [test "${have_libcloudstorage}" = "yes"])
+
 AC_ARG_ENABLE(tests,AC_HELP_STRING([--disable-tests], [Disable build of automated tests suites]))
 AM_CONDITIONAL([HAVE_TESTS], [test "${enable_tests}" = "yes"])
 AS_IF([test "${enable_tests}" = "yes"], [
diff --git a/src/factory/CloudProvider.cpp b/src/factory/CloudProvider.cpp
new file mode 100644
index 0000000..081c535
--- /dev/null
+++ b/src/factory/CloudProvider.cpp
@@ -0,0 +1,63 @@
+/*****************************************************************************
+ * Media Library
+ *****************************************************************************
+ * Copyright (C) 2016 Paweł Wegner
+ *
+ * Authors: Paweł Wegner<pawel.wegner95 at gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#include "CloudProvider.h"
+
+#include <ICloudStorage.h>
+
+#include "filesystem/cloudprovider/Device.h"
+#include "filesystem/cloudprovider/Directory.h"
+#include "logging/Logger.h"
+
+namespace medialibrary {
+
+namespace factory {
+
+CloudProvider::CloudProvider( cloudstorage::ICloudProvider::Pointer provider )
+    : m_device( std::make_shared<cp::Device>( provider ) ) {
+}
+
+std::shared_ptr<fs::IDirectory> CloudProvider::createDirectory(
+    const std::string& path ) {
+    std::lock_guard<std::mutex> lock( m_mutex );
+    const auto it = m_directories.find( path );
+    if ( it != end( m_directories ) ) return it->second;
+    try {
+        auto dir = std::make_shared<cp::Directory>( m_device, nullptr, path );
+        m_directories[path] = dir;
+        return dir;
+    } catch ( const std::exception& ex ) {
+        LOG_ERROR( "Failed to create cp::IDirectory for ", path, ": ", ex.what() );
+        return nullptr;
+    }
+}
+
+std::shared_ptr<fs::IDevice> CloudProvider::createDevice( const std::string& ) {
+    return m_device;
+}
+
+void CloudProvider::refresh() {
+    std::lock_guard<std::mutex> lock( m_mutex );
+    m_directories.clear();
+}
+}
+}
diff --git a/src/factory/CloudProvider.h b/src/factory/CloudProvider.h
new file mode 100644
index 0000000..0b281fb
--- /dev/null
+++ b/src/factory/CloudProvider.h
@@ -0,0 +1,54 @@
+/*****************************************************************************
+ * Media Library
+ *****************************************************************************
+ * Copyright (C) 2016 Paweł Wegner
+ *
+ * Authors: Paweł Wegner<pawel.wegner95 at gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#pragma once
+
+#include "factory/IFileSystem.h"
+#include "filesystem/cloudprovider/Device.h"
+
+#include <ICloudProvider.h>
+#include <mutex>
+#include <unordered_map>
+
+namespace medialibrary {
+
+namespace factory {
+
+class CloudProvider : public IFileSystem {
+public:
+    CloudProvider( cloudstorage::ICloudProvider::Pointer provider );
+
+    std::shared_ptr<fs::IDirectory> createDirectory(
+        const std::string& path ) override;
+
+    std::shared_ptr<fs::IDevice> createDevice( const std::string& uuid ) override;
+
+    void refresh() override;
+
+private:
+    std::mutex m_mutex;
+    std::unordered_map<std::string, std::shared_ptr<fs::IDirectory>>
+            m_directories;
+    std::shared_ptr<cp::Device> m_device;
+};
+}
+}
diff --git a/src/filesystem/cloudprovider/Device.cpp b/src/filesystem/cloudprovider/Device.cpp
new file mode 100644
index 0000000..54e9eb9
--- /dev/null
+++ b/src/filesystem/cloudprovider/Device.cpp
@@ -0,0 +1,82 @@
+/*****************************************************************************
+ * Media Library
+ *****************************************************************************
+ * Copyright (C) 2016 Paweł Wegner
+ *
+ * Authors: Paweł Wegner<pawel.wegner95 at gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#include "Device.h"
+
+#include "logging/Logger.h"
+
+namespace medialibrary {
+
+namespace cp {
+
+namespace {
+class AuthorizationCallback : public cloudstorage::ICloudProvider::ICallback {
+public:
+    Status userConsentRequired( const cloudstorage::ICloudProvider& p ) override {
+        LOG_INFO( "User consent required at " + p.authorizeLibraryUrl() );
+        return Status::WaitForAuthorizationCode;
+    }
+
+    void accepted( const cloudstorage::ICloudProvider& ) override {}
+
+    void declined( const cloudstorage::ICloudProvider& ) override {}
+
+    void error( const cloudstorage::ICloudProvider&,
+                const std::string& description ) override {
+        LOG_ERROR( description );
+    }
+};
+}
+
+Device::Device( cloudstorage::ICloudProvider::Pointer provider )
+    : m_cloudprovider( provider ),
+      m_uuid( "cloudprovider-" + provider->name() ),
+      m_mountpoint( provider->endpoint() ) {
+    // TODO reuse refresh token
+    // TODO provide ICryptoPP implementation
+    // TODO provide IHttp implementation
+    provider->initialize(
+    {"", std::make_shared<AuthorizationCallback>(), nullptr, nullptr, {}} );
+}
+
+const std::string& Device::uuid() const {
+    return m_uuid;
+}
+
+bool Device::isRemovable() const {
+    return true;
+}
+
+bool Device::isPresent() const {
+    // TODO check if connected to the internet
+    return true;
+}
+
+const std::string& Device::mountpoint() const {
+    return m_mountpoint;
+}
+
+cloudstorage::ICloudProvider::Pointer Device::provider() const {
+    return m_cloudprovider;
+}
+}
+}
diff --git a/src/filesystem/cloudprovider/Device.h b/src/filesystem/cloudprovider/Device.h
new file mode 100644
index 0000000..32defd3
--- /dev/null
+++ b/src/filesystem/cloudprovider/Device.h
@@ -0,0 +1,50 @@
+/*****************************************************************************
+ * Media Library
+ *****************************************************************************
+ * Copyright (C) 2016 Paweł Wegner
+ *
+ * Authors: Paweł Wegner<pawel.wegner95 at gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#pragma once
+
+#include <ICloudProvider.h>
+
+#include "filesystem/IDevice.h"
+
+namespace medialibrary {
+
+namespace cp {
+
+class Device : public fs::IDevice {
+public:
+    Device( cloudstorage::ICloudProvider::Pointer );
+
+    const std::string& uuid() const override;
+    bool isRemovable() const override;
+    bool isPresent() const override;
+    const std::string& mountpoint() const override;
+
+    cloudstorage::ICloudProvider::Pointer provider() const;
+
+private:
+    cloudstorage::ICloudProvider::Pointer m_cloudprovider;
+    std::string m_uuid;
+    std::string m_mountpoint;
+};
+}
+}
diff --git a/src/filesystem/cloudprovider/Directory.cpp b/src/filesystem/cloudprovider/Directory.cpp
new file mode 100644
index 0000000..fadf19f
--- /dev/null
+++ b/src/filesystem/cloudprovider/Directory.cpp
@@ -0,0 +1,79 @@
+/*****************************************************************************
+ * Media Library
+ *****************************************************************************
+ * Copyright (C) 2016 Paweł Wegner
+ *
+ * Authors: Paweł Wegner<pawel.wegner95 at gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#include "Directory.h"
+
+#include "filesystem/cloudprovider/File.h"
+
+namespace medialibrary {
+
+namespace cp {
+
+Directory::Directory( std::shared_ptr<Device> device,
+                      cloudstorage::IItem::Pointer item, const std::string& path )
+    : m_device( device ), m_path( path ), m_item( item ) {}
+
+const std::string& Directory::path() const {
+    return m_path;
+}
+
+const std::vector<std::shared_ptr<fs::IFile>>& Directory::files() const {
+    if ( m_files.size() == 0 && m_directories.size() == 0 ) read();
+    return m_files;
+}
+
+const std::vector<std::shared_ptr<fs::IDirectory>>& Directory::dirs() const {
+    if ( m_files.size() == 0 && m_directories.size() == 0 ) read();
+    return m_directories;
+}
+
+std::shared_ptr<fs::IDevice> Directory::device() const {
+    return m_device;
+}
+
+void Directory::read() const {
+    if ( !m_item ) {
+        m_item = m_device->provider()
+        ->getItemAsync( path(), []( cloudstorage::IItem::Pointer ) {} )
+        ->result();
+    }
+    auto t =
+        m_device->provider()
+        ->listDirectoryAsync(
+    m_item, []( const std::vector<cloudstorage::IItem::Pointer>& ) {} )
+    ->result();
+    for ( auto item : t ) {
+        if ( item->type() == cloudstorage::IItem::FileType::Directory ) {
+            m_directories.emplace_back( std::make_shared<Directory>(
+                                            m_device, item, path() + "/" + item->filename() ) );
+        } else {
+            auto i = m_device->provider()
+                     ->getItemDataAsync( item->id(),
+            []( cloudstorage::IItem::Pointer ) {} )
+            ->result();
+            m_files.emplace_back(
+                std::make_shared<File>( i->url(), path() + "/" + item->filename() ) );
+        }
+    }
+}
+}
+}
diff --git a/src/filesystem/cloudprovider/Directory.h b/src/filesystem/cloudprovider/Directory.h
new file mode 100644
index 0000000..6a9615e
--- /dev/null
+++ b/src/filesystem/cloudprovider/Directory.h
@@ -0,0 +1,53 @@
+/*****************************************************************************
+ * Media Library
+ *****************************************************************************
+ * Copyright (C) 2016 Paweł Wegner
+ *
+ * Authors: Paweł Wegner<pawel.wegner95 at gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#pragma once
+
+#include "filesystem/IDirectory.h"
+#include "filesystem/cloudprovider/Device.h"
+
+namespace medialibrary {
+
+namespace cp {
+
+class Directory : public fs::IDirectory {
+public:
+    Directory( std::shared_ptr<Device> device, cloudstorage::IItem::Pointer item,
+               const std::string& path );
+
+    const std::string& path() const override;
+
+    const std::vector<std::shared_ptr<fs::IFile>>& files() const override;
+    const std::vector<std::shared_ptr<IDirectory>>& dirs() const override;
+    std::shared_ptr<fs::IDevice> device() const override;
+
+private:
+    void read() const;
+
+    std::shared_ptr<Device> m_device;
+    std::string m_path;
+    mutable std::vector<std::shared_ptr<fs::IFile>> m_files;
+    mutable std::vector<std::shared_ptr<fs::IDirectory>> m_directories;
+    mutable cloudstorage::IItem::Pointer m_item;
+};
+}
+}
diff --git a/src/filesystem/cloudprovider/File.cpp b/src/filesystem/cloudprovider/File.cpp
new file mode 100644
index 0000000..bd4a8a2
--- /dev/null
+++ b/src/filesystem/cloudprovider/File.cpp
@@ -0,0 +1,40 @@
+/*****************************************************************************
+ * Media Library
+ *****************************************************************************
+ * Copyright (C) 2016 Paweł Wegner
+ *
+ * Authors: Paweł Wegner<pawel.wegner95 at gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#include "File.h"
+
+namespace medialibrary {
+namespace cp {
+
+File::File( const std::string& url, const std::string& path )
+    : fs::CommonFile( path ), m_url( url ) {}
+
+unsigned int File::lastModificationDate() const {
+    // TODO
+    return 0;
+}
+
+const std::string& File::fullPath() const {
+    return m_url;
+}
+}
+}
diff --git a/src/filesystem/cloudprovider/File.h b/src/filesystem/cloudprovider/File.h
new file mode 100644
index 0000000..5901225
--- /dev/null
+++ b/src/filesystem/cloudprovider/File.h
@@ -0,0 +1,43 @@
+/*****************************************************************************
+ * Media Library
+ *****************************************************************************
+ * Copyright (C) 2016 Paweł Wegner
+ *
+ * Authors: Paweł Wegner<pawel.wegner95 at gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#pragma once
+
+#include "filesystem/common/CommonFile.h"
+
+#include <IItem.h>
+
+namespace medialibrary {
+namespace cp {
+
+class File : public fs::CommonFile {
+public:
+    File( const std::string& url, const std::string& path );
+
+    unsigned int lastModificationDate() const override;
+    const std::string& fullPath() const override;
+
+private:
+    std::string m_url;
+};
+}
+}
-- 
2.7.4



More information about the Vlmc-devel mailing list