diff --git a/CHANGELOG b/CHANGELOG index 34c57d1..f6736d6 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,13 +1,19 @@ +0.4.0 + - Added: provider_name, container_name, and local_path, object_path to object + 0.3.1 - Added the config 'CLOUDSTORAGE_SERVE_FILES_URL_SECURE' in flask to serve files over https + 0.3.0 - Serve local files through Python - When flask FileStorage type is being uploaded, use the stream method - Object:get_url() will now return the appropriate url + 0.2.0 - Add extension of original file on upload if object_name doesn't have an extension - Use the original file name as object name, if object name is None + 0.1.0 - First \ No newline at end of file diff --git a/flask_cloudstorage.py b/flask_cloudstorage.py index 22d8bf8..71bc561 100644 --- a/flask_cloudstorage.py +++ b/flask_cloudstorage.py @@ -10,7 +10,7 @@ from importlib import import_module from flask import send_file, abort, url_for import shortuuid from libcloud.storage.types import Provider, ObjectDoesNotExistError -from libcloud.storage.providers import get_driver +from libcloud.storage.providers import DRIVERS, get_driver from libcloud.storage.base import Object as BaseObject, StorageDriver from libcloud.storage.drivers import local from six.moves.urllib.parse import urlparse, urlunparse, urljoin @@ -84,6 +84,19 @@ def get_driver_class(provider): driver = getattr(Provider, provider.upper()) return get_driver(driver) +def get_provider_name(driver): + """ + Return the provider name from the driver class + :param driver: obj + :return: str + """ + kls = driver.__class__.__name__ + for d, prop in DRIVERS.items(): + if prop[1] == kls: + return d + return None + + class InvalidExtensionError(Exception): pass @@ -144,6 +157,8 @@ class Storage(object): if container: self.container = container + self.local_path = local_path + def init_app(self, app): """ To initiate with Flask @@ -204,7 +219,9 @@ class Storage(object): :return: generator """ for obj in self.container.iterate_objects(): - yield Object(obj=obj, secure_url=self.secure_url) + yield Object(obj=obj, + secure_url=self.secure_url, + local_path=self.local_path) def get_object(self, object_name, secure_url=None, validate=True, **kwargs): """ @@ -228,7 +245,9 @@ class Storage(object): "meta_data": kwargs.get("meta_data", None) } obj = BaseObject(container=self.container, driver=self.driver, **params) - return Object(obj=obj, secure_url=secure_url or self.secure_url) + return Object(obj=obj, + secure_url=secure_url or self.secure_url, + local_path=self.local_path) def upload(self, file, @@ -287,7 +306,9 @@ class Storage(object): obj = self.container.upload_object(file_path=file, object_name=name, extra=extra) - return Object(obj=obj, secure_url=self.secure_url) + return Object(obj=obj, + secure_url=self.secure_url, + local_path=self.local_path) def object_exists(self, name): """ @@ -350,6 +371,7 @@ class Object(object): hash extra meta_data + driver container @@ -431,5 +453,35 @@ class Object(object): """ return get_file_extension_type(self.name) + @property + def provider_name(self): + """ + Return the provider name + :return: str + """ + return get_provider_name(self.driver) + @property + def container_name(self): + """ + Return the container name + :return: str + """ + return self.container.name + + @property + def local_path(self): + """ + Return the local path for Local storage + :return: + """ + return self._kwargs.get("local_path", None) + + @property + def object_path(self): + """ + Return the object path + :return: str + """ + return '%s/%s' % (self.container.name, self.name) diff --git a/setup.py b/setup.py index 8a382cc..04a75f1 100644 --- a/setup.py +++ b/setup.py @@ -20,7 +20,7 @@ from setuptools import setup, find_packages __NAME__ = "flask-CloudStorage" -__version__ = "0.3.1" +__version__ = "0.4.0" __author__ = "Mardix" __license__ = "MIT" __copyright__ = "2015" diff --git a/tests/__init__.py b/tests/__init__.py index 4e5980e..e69de29 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1 +0,0 @@ -__author__ = 'mardochee.macxis' diff --git a/tests/test_cloudstorage.py b/tests/test_cloudstorage.py index 6fb6784..6b2cdf6 100644 --- a/tests/test_cloudstorage.py +++ b/tests/test_cloudstorage.py @@ -8,6 +8,7 @@ from flask_cloudstorage import (get_file_extension, get_file_extension_type, get_file_name, get_driver_class, + get_provider_name, Storage, Object, InvalidExtensionError) @@ -42,6 +43,12 @@ def test_get_file_name(): filename = "/dir1/dir2/dir3/hello.jpg" assert get_file_name(filename) == "hello.jpg" +def test_get_provider_name(): + class GoogleStorageDriver(object): + pass + driver = GoogleStorageDriver() + assert get_provider_name(driver) == "google_storage" + #--- @@ -99,6 +106,32 @@ def test_object_not_exists(): storage = app_storage() assert storage.object_exists(object_name) is False +def test_object_provider_name(): + object_name = "hello.jpg" + storage = app_storage() + o = storage.get_object(object_name, validate=False) + assert o.provider_name == config.PROVIDER.lower() + +def test_object_container_name(): + object_name = "hello.jpg" + storage = app_storage() + o = storage.get_object(object_name, validate=False) + assert o.container_name == config.CONTAINER + +def test_object_object_path(): + object_name = "hello.jpg" + storage = app_storage() + o = storage.get_object(object_name, validate=False) + p = "%s/%s" % (o.container.name, o.name) + assert o.object_path == p + +def test_object_local_path(): + object_name = "hello.jpg" + storage = app_storage() + o = storage.get_object(object_name, validate=False) + if "local" in o.container.name.lower(): + assert o.local_path == CWD + def test_storage_upload_invalid(): storage = app_storage() object_name = "my-js/hello.js" @@ -139,4 +172,4 @@ def test_storage_upload_with_prefix(): full_name = "%s/%s.%s" % (prefix, object_name, "txt") o = storage.upload(CWD + "/data/hello.txt", name=object_name, prefix=prefix, overwrite=True) assert storage.object_exists(full_name) is True - assert o.name == full_name \ No newline at end of file + assert o.name == full_name