我有一个 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.