Commits

Sebastian Annies  committed bcba288

Do not read beyond the end of the file. Server will respond with 416 unsatisfied range.

  • Participants
  • Parent commits 629607f

Comments (0)

Files changed (1)

File storages/backends/mosso.py

 Custom storage for django with Mosso Cloud Files backend.
 Created by Rich Leland <rich@richleland.com>.
 """
+from StringIO import StringIO
 from django.conf import settings
 from django.core.exceptions import ImproperlyConfigured
 from django.core.files import File
                                "http://www.mosso.com/cloudfiles.jsp.")
 
 # TODO: implement TTL into cloudfiles methods
-CLOUDFILES_TTL = getattr(settings, 'CLOUDFILES_TTL', 600)
+#CLOUDFILES_TTL = getattr(settings, 'CLOUDFILES_TTL', 600)
 
 
 def cloudfiles_upload_to(self, filename):
     """
     default_quick_listdir = True
 
-    def __init__(self, username=None, api_key=None, container=None,
-                 connection_kwargs=None):
+    def __init__(self,
+                 username=settings.CLOUDFILES_USERNAME,
+                 api_key=settings.CLOUDFILES_API_KEY,
+                 container=settings.CLOUDFILES_CONTAINER,
+                 connection_kwargs=settings.CLOUDFILES_CONNECTION_KWARGS):
         """
         Initialize the settings for the connection and container.
         """
-        self.username = username or settings.CLOUDFILES_USERNAME
-        self.api_key = api_key or settings.CLOUDFILES_API_KEY
-        self.container_name = container or settings.CLOUDFILES_CONTAINER
-        self.connection_kwargs = connection_kwargs or settings.CLOUDFILES_CONNECTION_KWARGS or {}
+        self.username = username
+        self.api_key = api_key
+        self.container_name = container
+        self.connection_kwargs = connection_kwargs
 
     def __getstate__(self):
         """
     def _get_connection(self):
         if not hasattr(self, '_connection'):
             self._connection = cloudfiles.get_connection(self.username,
-                                    self.api_key, **self.connection_kwargs)
+                                                         self.api_key, **self.connection_kwargs)
         return self._connection
 
     def _set_connection(self, value):
     def _get_container(self):
         if not hasattr(self, '_container'):
             self.container = self.connection.get_container(
-                                                        self.container_name)
+                self.container_name)
         return self._container
 
     def _set_container(self, container):
         cloud_obj = self.container.create_object(name)
         cloud_obj.size = content.size
 
-        content.open()        
+        content.open()
         # If the content type is available, pass it in directly rather than
         # getting the cloud object to try to guess.
         if hasattr(content.file, 'content_type'):
         return '%s/%s' % (self.container_url, name)
 
 
+class CloudStorageDirectory(File):
+    """
+    A File-like object that creates a directory at cloudfiles
+    """
+
+    def __init__(self, name):
+        super(CloudStorageDirectory, self).__init__(StringIO(), name=name)
+        self.file.content_type = 'application/directory'
+        self.size = 0
+
+    def __str__(self):
+        return 'directory'
+
+    def __nonzero__(self):
+        return True
+
+    def open(self, mode=None):
+        self.seek(0)
+
+    def close(self):
+        pass
+
+
 class CloudFilesStorageFile(File):
     closed = False
 
     file = property(_get_file, _set_file)
 
     def read(self, num_bytes=None):
+        if self._pos == self._get_size():
+            return None
+        if self._pos + num_bytes > self._get_size():
+            num_bytes = self._get_size() - self._pos
         data = self.file.read(size=num_bytes or -1, offset=self._pos)
         self._pos += len(data)
         return data
         super(ThreadSafeCloudFilesStorage, self).__init__(*args, **kwargs)
 
         import threading
+
         self.local_cache = threading.local()
 
     def _get_connection(self):
         if not hasattr(self.local_cache, 'connection'):
             connection = cloudfiles.get_connection(self.username,
-                                    self.api_key, **self.connection_kwargs)
+                                                   self.api_key, **self.connection_kwargs)
             self.local_cache.connection = connection
 
         return self.local_cache.connection