Beispiel #1
0
	def _dmeta_init(self):
		fs_path = self.data.name
		if not fs.exists(fs_path): return
		# OSError's can be raised in the default FileStorage class,
		#  though they shouldn't be with exists() check in place (races?)
		try: self._size = fs.size(fs_path)
		except (NotImplementedError, OSError): pass
		try: self._mtime = fs.modified_time(fs_path)
		except (NotImplementedError, OSError):
			try: self._mtime = fs.created_time(fs_path) # immutable storage?
			except (NotImplementedError, OSError): pass
		else:
			self._etag = hashlib.sha1('{}\0{}'.format(
				http_date(self._mtime), self._size )).hexdigest()
Beispiel #2
0
 def _dmeta_init(self):
     fs_path = self.data.name
     if not fs.exists(fs_path): return
     # OSError's can be raised in the default FileStorage class,
     #  though they shouldn't be with exists() check in place (races?)
     try:
         self._size = fs.size(fs_path)
     except (NotImplementedError, OSError):
         pass
     try:
         self._mtime = fs.modified_time(fs_path)
     except (NotImplementedError, OSError):
         try:
             self._mtime = fs.created_time(fs_path)  # immutable storage?
         except (NotImplementedError, OSError):
             pass
     else:
         self._etag = hashlib.sha1('{}\0{}'.format(http_date(self._mtime),
                                                   self._size)).hexdigest()
def storage_api(request, obj):
    fs = obj.data.storage

    if request.method in ["GET", "HEAD"]:
        if not obj.data:
            return HttpResponseNotFound("Path {!r} does not exists".format(obj.path))

            # Optimizations
        if getattr(settings, "REMOTESTORAGE_DAV_SENDFILE", False):
            try:
                real_path = fs.path(obj.data.name)
            except NotImplementedError:
                pass
            else:
                response = HttpResponse()
                response["X-SendFile"] = real_path
                return response
        accel_redir = getattr(settings, "REMOTESTORAGE_DAV_ACCEL", None)
        if accel_redir is not None:
            response = HttpResponse()
            response["X-Accel-Redirect"] = join(accel_redir, obj.path)
            # response['X-Accel-Charset'] = 'utf-8'
            return response
        if getattr(settings, "REMOTESTORAGE_DAV_REDIRECT", False) and getattr(settings, "MEDIA_URL", False):
            try:
                return HttpResponseRedirect(obj.data.url)
            except NotImplementedError:
                pass

            # Worst case - pipe through python
        content_type, encoding = mimetypes.guess_type(obj.path)
        content_type = "application/data" if content_type is None else "{}; charset={}".format(content_type, encoding)

        response = HttpResponse(fs.open(obj.data.name).read(), content_type=content_type)

        response["Date"] = http_date()
        if obj.size is not None:
            response["Content-Length"] = obj.size
        if obj.mtime is not None:
            response["Last-Modified"] = http_date(obj.mtime)
        if obj.etag is not None:
            response["ETag"] = obj.etag
        return response

    elif request.method == "PUT":
        created = not obj.data
        if not created:
            fs.delete(obj.data.name)
        obj.data.save(obj.path, ContentFile(request.body))
        return HttpResponse(status=httplib.CREATED if created else httplib.NO_CONTENT)

    elif request.method == "DELETE":
        if not obj.data:
            return HttpResponseNotFound()
        with transaction.commit_on_success():
            obj.delete()
            fs.delete(obj.data.name)
        return HttpResponse(status=httplib.NO_CONTENT)

    elif request.method == "OPTIONS":
        response = HttpResponse(mimetype="httpd/unix-directory")
        for k, v in {
            "DAV": "1,2",
            "MS-Author-Via": "DAV",
            "Date": http_date(),
            "Allow": methods(obj.path, exists=obj.data, can_be_created=obj.can_be_created),
        }.viewitems():
            response[k] = v
        return response

    elif request.method in ["POST", "TRACE", "MKCOL", "PROPFIND", "PROPPATCH", "COPY", "MOVE", "LOCK", "UNLOCK"]:
        raise NotImplementedError("Method {}.".format(request.method))

    return HttpResponseNotAllowed(methods(obj.path, exists=obj.data, can_be_created=obj.can_be_created))