gorodechnyj avatar gorodechnyj committed 875fa8b

[fix] Error with read function when num_bytes (which is None) is being added to int
[chg] RackSpace is not require to push directories to CDN, so don't do it
[fix] All files are pushed to CDN with unknown content_type, so I added mimetypes.guess_type() call to them.

Comments (0)

Files changed (1)


 from django.core.files import File
 from django.core.files.storage import Storage
 from django.utils.text import get_valid_filename
+from datetime import datetime
+import mimetypes
     from cStringIO import StringIO
     import cloudfiles
-    from cloudfiles.errors import NoSuchObject
+    from cloudfiles.errors import NoSuchObject, ResponseError
 except ImportError:
     raise ImproperlyConfigured("Could not load cloudfiles dependency. See "
         Use the Cloud Files service to write ``content`` to a remote file
         (called ``name``).
-        (path, last) = os.path.split(name)
-        if path:
-            try:
-                self.container.get_object(path)
-            except NoSuchObject:
-                self._save(path, CloudStorageDirectory(path))
         cloud_obj = self.container.create_object(name)
         cloud_obj.size = content.size
         # getting the cloud object to try to guess.
         if hasattr(content.file, 'content_type'):
             cloud_obj.content_type = content.file.content_type
+        else:
+            content_type = mimetypes.guess_type(name)[0] or "application/x-octet-stream"
+            cloud_obj.content_type = content_type
         return name
         # If the file exists, delete it.
         if self.exists(name):
-            self.container.delete_object(name)
+            try:
+                self.container.delete_object(name)
+            except ResponseError:
+                pass # Not found, maybe, so skip it. 404 is very often raised here.
     def exists(self, name):
         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):
+    def read(self, num_bytes=0):
         if self._pos == self._get_size():
             return None
         if self._pos + num_bytes > self._get_size():
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.