def _open(self, name, mode): if 'w' in mode or '+' in mode or 'a' in mode: raise ValueError('FiletrackerStorage.open does not support ' 'writing. Use FiletrackerStorage.save.') path = self._make_filetracker_path(name) reader, _version = self.client.get_stream(path) return File(reader, FiletrackerFilename(name))
def _save(self, name, content): path = self._make_filetracker_path(name) if hasattr(content, 'temporary_file_path'): filename = content.temporary_file_path() elif getattr(content, 'file', None) \ and hasattr(content.file, 'name') \ and os.path.isfile(content.file.name): filename = content.file.name elif DJANGO_VERSION < (1, 11) and isinstance(getattr(content, 'file', None), FileInFiletracker): # This happens when used with field assignment # We are ignoring suggested name, as copying files in filetracker # isn't implemented return content.file.name elif isinstance(content, FileInFiletracker): # This happens when file_field.save(path, file) is called # explicitly if DJANGO_VERSION < (1, 11): raise NotImplementedError("Filename cannot be changed") else: return content.name else: f = tempfile.NamedTemporaryFile() for chunk in content.chunks(): f.write(chunk) f.flush() filename = f.name # If there will be only local store, filetracker will ignore # 'to_local_store' argument. name = self._cut_prefix(self.client.put_file(path, filename, to_local_store=False)) name = FiletrackerFilename(name) content.close() return name
def filetracker_to_django_file(filetracker_path, storage=None): """Returns a :class:`~django.core.files.File` representing an existing Filetracker file (usable only for assigning to a :class:`~django.db.models.FileField`)""" if storage is None: storage = default_storage prefix_len = len(storage.prefix.rstrip('/')) if not filetracker_path.startswith(storage.prefix) or \ filetracker_path[prefix_len:prefix_len + 1] != '/': raise ValueError('Path %s is outside of storage prefix %s' % (filetracker_path, storage.prefix)) return FileInFiletracker( storage, FiletrackerFilename(filetracker_path[prefix_len + 1:]))
def read_using_cache(self, name): """Opens a file using a cache (if it's possible)""" path = self._make_filetracker_path(name) reader, _version = self.client.get_stream(path, serve_from_cache=True) return File(reader, FiletrackerFilename(name))
def __init__(self, instance, field, name): if name is not None: name = FiletrackerFilename(name) super(FieldFile, self).__init__(instance, field, name)