PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/storages/backends/mosso.py

https://bitbucket.org/btimby/django-storages
Python | 343 lines | 298 code | 8 blank | 37 comment | 8 complexity | 10152421e5e7f2c64768a87f8c3c078a MD5 | raw file
Possible License(s): BSD-3-Clause
  1. """
  2. Custom storage for django with Mosso Cloud Files backend.
  3. Created by Rich Leland <rich@richleland.com>.
  4. """
  5. import os
  6. import warnings
  7. warnings.simplefilter('always', PendingDeprecationWarning)
  8. warnings.warn("The mosso module will be deprecated in version 1.2 of "
  9. "django-storages. The CloudFiles code has been moved into"
  10. "django-cumulus at http://github.com/richleland/django-cumulus.",
  11. PendingDeprecationWarning)
  12. from django.conf import settings
  13. from django.core.exceptions import ImproperlyConfigured
  14. from django.core.files import File
  15. from django.core.files.storage import Storage
  16. from django.utils.text import get_valid_filename
  17. try:
  18. from cStringIO import StringIO
  19. except:
  20. from StringIO import StringIO
  21. try:
  22. import cloudfiles
  23. from cloudfiles.errors import NoSuchObject
  24. except ImportError:
  25. raise ImproperlyConfigured("Could not load cloudfiles dependency. See "
  26. "http://www.mosso.com/cloudfiles.jsp.")
  27. # TODO: implement TTL into cloudfiles methods
  28. TTL = getattr(settings, 'CLOUDFILES_TTL', 600)
  29. CONNECTION_KWARGS = getattr(settings, 'CLOUDFILES_CONNECTION_KWARGS', {})
  30. def cloudfiles_upload_to(self, filename):
  31. """
  32. Simple, custom upload_to because Cloud Files doesn't support
  33. nested containers (directories).
  34. Actually found this out from @minter:
  35. @richleland The Cloud Files APIs do support pseudo-subdirectories, by
  36. creating zero-byte files with type application/directory.
  37. May implement in a future version.
  38. """
  39. return get_valid_filename(filename)
  40. class CloudFilesStorage(Storage):
  41. """
  42. Custom storage for Mosso Cloud Files.
  43. """
  44. default_quick_listdir = True
  45. def __init__(self,
  46. username=settings.CLOUDFILES_USERNAME,
  47. api_key=settings.CLOUDFILES_API_KEY,
  48. container=settings.CLOUDFILES_CONTAINER,
  49. connection_kwargs=CONNECTION_KWARGS):
  50. """
  51. Initialize the settings for the connection and container.
  52. """
  53. self.username = username
  54. self.api_key = api_key
  55. self.container_name = container
  56. self.connection_kwargs = connection_kwargs
  57. def __getstate__(self):
  58. """
  59. Return a picklable representation of the storage.
  60. """
  61. return dict(username=self.username,
  62. api_key=self.api_key,
  63. container_name=self.container_name,
  64. connection_kwargs=self.connection_kwargs)
  65. def _get_connection(self):
  66. if not hasattr(self, '_connection'):
  67. self._connection = cloudfiles.get_connection(self.username,
  68. self.api_key, **self.connection_kwargs)
  69. return self._connection
  70. def _set_connection(self, value):
  71. self._connection = value
  72. connection = property(_get_connection, _set_connection)
  73. def _get_container(self):
  74. if not hasattr(self, '_container'):
  75. self.container = self.connection.get_container(
  76. self.container_name)
  77. return self._container
  78. def _set_container(self, container):
  79. """
  80. Set the container, making it publicly available (on Limelight CDN) if
  81. it is not already.
  82. """
  83. if not container.is_public():
  84. container.make_public()
  85. if hasattr(self, '_container_public_uri'):
  86. delattr(self, '_container_public_uri')
  87. self._container = container
  88. container = property(_get_container, _set_container)
  89. def _get_container_url(self):
  90. if not hasattr(self, '_container_public_uri'):
  91. self._container_public_uri = self.container.public_uri()
  92. return self._container_public_uri
  93. container_url = property(_get_container_url)
  94. def _get_cloud_obj(self, name):
  95. """
  96. Helper function to get retrieve the requested Cloud Files Object.
  97. """
  98. return self.container.get_object(name)
  99. def _open(self, name, mode='rb'):
  100. """
  101. Return the CloudFilesStorageFile.
  102. """
  103. return CloudFilesStorageFile(storage=self, name=name)
  104. def _save(self, name, content):
  105. """
  106. Use the Cloud Files service to write ``content`` to a remote file
  107. (called ``name``).
  108. """
  109. (path, last) = os.path.split(name)
  110. if path:
  111. try:
  112. self.container.get_object(path)
  113. except NoSuchObject:
  114. self._save(path, CloudStorageDirectory(path))
  115. cloud_obj = self.container.create_object(name)
  116. cloud_obj.size = content.size
  117. content.open()
  118. # If the content type is available, pass it in directly rather than
  119. # getting the cloud object to try to guess.
  120. if hasattr(content.file, 'content_type'):
  121. cloud_obj.content_type = content.file.content_type
  122. cloud_obj.send(content)
  123. content.close()
  124. return name
  125. def delete(self, name):
  126. """
  127. Deletes the specified file from the storage system.
  128. """
  129. # If the file exists, delete it.
  130. if self.exists(name):
  131. self.container.delete_object(name)
  132. def exists(self, name):
  133. """
  134. Returns True if a file referenced by the given name already exists in
  135. the storage system, or False if the name is available for a new file.
  136. """
  137. try:
  138. self._get_cloud_obj(name)
  139. return True
  140. except NoSuchObject:
  141. return False
  142. def listdir(self, path):
  143. """
  144. Lists the contents of the specified path, returning a 2-tuple; the
  145. first being an empty list of directories (not available for quick-
  146. listing), the second being a list of filenames.
  147. If the list of directories is required, use the full_listdir method.
  148. """
  149. files = []
  150. if path and not path.endswith('/'):
  151. path = '%s/' % path
  152. path_len = len(path)
  153. for name in self.container.list_objects(path=path):
  154. files.append(name[path_len:])
  155. return ([], files)
  156. def full_listdir(self, path):
  157. """
  158. Lists the contents of the specified path, returning a 2-tuple of lists;
  159. the first item being directories, the second item being files.
  160. On large containers, this may be a slow operation for root containers
  161. because every single object must be returned (cloudfiles does not
  162. provide an explicit way of listing directories).
  163. """
  164. dirs = set()
  165. files = []
  166. if path and not path.endswith('/'):
  167. path = '%s/' % path
  168. path_len = len(path)
  169. for name in self.container.list_objects(prefix=path):
  170. name = name[path_len:]
  171. slash = name[1:-1].find('/') + 1
  172. if slash:
  173. dirs.add(name[:slash])
  174. elif name:
  175. files.append(name)
  176. dirs = list(dirs)
  177. dirs.sort()
  178. return (dirs, files)
  179. def size(self, name):
  180. """
  181. Returns the total size, in bytes, of the file specified by name.
  182. """
  183. return self._get_cloud_obj(name).size
  184. def url(self, name):
  185. """
  186. Returns an absolute URL where the file's contents can be accessed
  187. directly by a web browser.
  188. """
  189. return '%s/%s' % (self.container_url, name)
  190. class CloudStorageDirectory(File):
  191. """
  192. A File-like object that creates a directory at cloudfiles
  193. """
  194. def __init__(self, name):
  195. super(CloudStorageDirectory, self).__init__(StringIO(), name=name)
  196. self.file.content_type = 'application/directory'
  197. self.size = 0
  198. def __str__(self):
  199. return 'directory'
  200. def __nonzero__(self):
  201. return True
  202. def open(self, mode=None):
  203. self.seek(0)
  204. def close(self):
  205. pass
  206. class CloudFilesStorageFile(File):
  207. closed = False
  208. def __init__(self, storage, name, *args, **kwargs):
  209. self._storage = storage
  210. super(CloudFilesStorageFile, self).__init__(file=None, name=name,
  211. *args, **kwargs)
  212. self._pos = 0
  213. def _get_size(self):
  214. if not hasattr(self, '_size'):
  215. self._size = self._storage.size(self.name)
  216. return self._size
  217. def _set_size(self, size):
  218. self._size = size
  219. size = property(_get_size, _set_size)
  220. def _get_file(self):
  221. if not hasattr(self, '_file'):
  222. self._file = self._storage._get_cloud_obj(self.name)
  223. return self._file
  224. def _set_file(self, value):
  225. if value is None:
  226. if hasattr(self, '_file'):
  227. del self._file
  228. else:
  229. self._file = value
  230. file = property(_get_file, _set_file)
  231. def read(self, num_bytes=None):
  232. if self._pos == self._get_size():
  233. return None
  234. if self._pos + num_bytes > self._get_size():
  235. num_bytes = self._get_size() - self._pos
  236. data = self.file.read(size=num_bytes or -1, offset=self._pos)
  237. self._pos += len(data)
  238. return data
  239. def open(self, *args, **kwargs):
  240. """
  241. Open the cloud file object.
  242. """
  243. self.file
  244. self._pos = 0
  245. def close(self, *args, **kwargs):
  246. self._pos = 0
  247. @property
  248. def closed(self):
  249. return not hasattr(self, '_file')
  250. def seek(self, pos):
  251. self._pos = pos
  252. class ThreadSafeCloudFilesStorage(CloudFilesStorage):
  253. """
  254. Extends CloudFilesStorage to make it thread safer.
  255. As long as you don't pass container or cloud objects
  256. between threads, you'll be thread safe.
  257. Uses one cloudfiles connection per thread.
  258. """
  259. def __init__(self, *args, **kwargs):
  260. super(ThreadSafeCloudFilesStorage, self).__init__(*args, **kwargs)
  261. import threading
  262. self.local_cache = threading.local()
  263. def _get_connection(self):
  264. if not hasattr(self.local_cache, 'connection'):
  265. connection = cloudfiles.get_connection(self.username,
  266. self.api_key, **self.connection_kwargs)
  267. self.local_cache.connection = connection
  268. return self.local_cache.connection
  269. connection = property(_get_connection, CloudFilesStorage._set_connection)
  270. def _get_container(self):
  271. if not hasattr(self.local_cache, 'container'):
  272. container = self.connection.get_container(self.container_name)
  273. self.local_cache.container = container
  274. return self.local_cache.container
  275. container = property(_get_container, CloudFilesStorage._set_container)