SuspiciousOperation使用sorl-thumbnail

时间:2013-02-11 21:13:46

标签: python django exception thumbnails sorl-thumbnail

我有一个Django Web应用程序,代表用户访问和操作多个服务器文件系统(例如/ fs01,/ fs02等)。我想在这些文件系统上向用户呈现图像的缩略图,并认为sorl-thumbnail是这样做的方法。

虽然图像必须在MEDIA_ROOT下,因为sorl-thumbnail可以创建缩略图。我的MEDIA_ROOT/Users/me/Dev/MyProject/myproj/media,因此可行:

path = "/Users/me/Dev/MyProject/myproj/media/pipe-img/magritte-pipe-large.jpg"
try:
  im = get_thumbnail(path, '100x100', crop='center', quality=99)
except Exception, e:
  exc_type, exc_obj, exc_tb = sys.exc_info()
  print "Failed getting thumbnail: (%s) %s" % (exc_type, e)
print "im.url = %s" % im.url

它会创建缩略图并打印im.url,正如我所期望的那样。但是当我将path更改为:

path = "/fs02/dir/ep340102/foo/2048x1024/magritte-pipe-large.jpg"

失败了:

Failed getting thumbnail: (<class 'django.core.exceptions.SuspiciousOperation'>)
Attempted access to '/fs02/dir/ep340102/foo/2048x1024/magritte-pipe-large.jpg' denied.

有没有办法解决这个问题?我是否可以使用sorl-thumbnail创建缩略图,因为我想在这些其他文件系统下(例如/ fs01,/ fs02,/ fs03等)?有更好的方法吗?

更新。以下是完整的堆栈跟踪:

Environment:


Request Method: GET
Request URL: http://localhost:8000/pipe/file_selection/

Django Version: 1.4.1
Python Version: 2.7.2
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'django.contrib.humanize',
 'django.contrib.messages',
 'pipeproj.pipe',
 'south',
 'guardian',
 'sorl.thumbnail')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  20.                 return view_func(request, *args, **kwargs)
File "/Users/dylan/Dev/Pipe/pipeproj/../pipeproj/pipe/views/data.py" in file_selection
  184.  im = get_thumbnail(path, '100x100', crop='center', quality=99)
File "/Library/Python/2.7/site-packages/sorl_thumbnail-11.12-py2.7.egg/sorl/thumbnail/shortcuts.py" in get_thumbnail
  8.     return default.backend.get_thumbnail(file_, geometry_string, **options)
File "/Library/Python/2.7/site-packages/sorl_thumbnail-11.12-py2.7.egg/sorl/thumbnail/base.py" in get_thumbnail
  56.             source_image = default.engine.get_image(source)
File "/Library/Python/2.7/site-packages/sorl_thumbnail-11.12-py2.7.egg/sorl/thumbnail/engines/pil_engine.py" in get_image
  12.         buf = StringIO(source.read())
File "/Library/Python/2.7/site-packages/sorl_thumbnail-11.12-py2.7.egg/sorl/thumbnail/images.py" in read
  121.         return self.storage.open(self.name).read()
File "/Library/Python/2.7/site-packages/django/core/files/storage.py" in open
  33.         return self._open(name, mode)
File "/Library/Python/2.7/site-packages/django/core/files/storage.py" in _open
  156.         return File(open(self.path(name), mode))
File "/Library/Python/2.7/site-packages/django/core/files/storage.py" in path
  246.             raise SuspiciousOperation("Attempted access to '%s' denied." % name)

Exception Type: SuspiciousOperation at /pipe/file_selection/
Exception Value: Attempted access to '/fs02/dir/ep340102/foo/2048x1024/bettina.jpg' denied.

3 个答案:

答案 0 :(得分:4)

SuspiciousOperation来自FileSystemStorage.path():

def path(self, name):
try:
    path = safe_join(self.location, name)
except ValueError:
    raise SuspiciousFileOperation("Attempted access to '%s' denied." % name)
return os.path.normpath(path)

它起源于safe_join(),它有这个测试:

if (not normcase(final_path).startswith(normcase(base_path + sep)) and
...

这意味着计算的文件名必须存在于配置的缩略图存储中。默认情况下,settings.THUMBNAIL_STORAGE是settings.DEFAULT_FILE_STORAGE,它是FileSystemStorage,它将文件存储在settings.MEDIA_ROOT中。

您应该能够通过定义存储类来使用不同的缩略图存储路径:

from django.core.files.storage import FileSystemStorage

class ThumbnailStorage(FileSystemStorage):
    def __init__(self, **kwargs):
        super(ThumbnailStorage, self).__init__(
            location='/fs02', base_url='/fs02')

然后在settings.py

THUMBNAIL_STORAGE = 'myproj.storage.ThumbnailStorage'

您还需要确保某个内容正在该网址上提供/ fs02:

if settings.DEBUG:
    patterns += patterns('',
        url(r'^fs02/(?P<path>.*)$', 'django.views.static.serve',
            {'document_root': '/fs02'}))

请注意,根据默认的THUMBNAIL_PREFIX

,您的缩略图将创建为/ fs02 / cache / ...

答案 1 :(得分:0)

使用以下命令可以得到什么?

ls -la /fs02/dir/ep340102/foo/2048x1024/

如果文件所有者不是好文件所有者(和/或错误的文件权限),通常会发生拒绝访问...

答案 2 :(得分:0)

我是通过提供绝对网址来实现的,如下所示:

from sorl.thumbnail import get_thumbnail
from django.contrib.staticfiles.storage import staticfiles_storage

image_url = staticfiles_storage.url('image.jpg')
thumbnail = get_thumbnail(image_url, '100x100')