Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Websites UFRPE
Django Private Storage
Commits
64f1848d
Unverified
Commit
64f1848d
authored
May 07, 2018
by
Diederik van der Boor
Browse files
Fixed python 3 encoding issues with content-disposition
parent
5044b804
Changes
2
Hide whitespace changes
Inline
Side-by-side
private_storage/tests/test_views.py
View file @
64f1848d
# encoding: utf-8
from
django.contrib.auth.models
import
User
from
django.core.files.uploadedfile
import
SimpleUploadedFile
from
django.test
import
RequestFactory
...
...
@@ -68,3 +69,40 @@ class ViewTests(PrivateFileTestCase):
self
.
assertEqual
(
response
[
'Content-Length'
],
'5'
)
self
.
assertEqual
(
response
[
'Content-Disposition'
],
"attachment; filename*=UTF-8''test5.txt"
)
self
.
assertIn
(
'Last-Modified'
,
response
)
def
test_private_file_view_utf8
(
self
):
"""
Test the detail view that returns the object
"""
obj
=
CustomerDossier
.
objects
.
create
(
customer
=
'cust2'
,
file
=
SimpleUploadedFile
(
u
'Heizölrückstoßabdämpfung.txt'
,
b
'test5'
)
)
self
.
assertExists
(
'CustomerDossier'
,
'cust2'
,
u
'Heizölrückstoßabdämpfung.txt'
)
# Initialize locally, no need for urls.py etc..
# This behaves like a standard DetailView
view
=
PrivateStorageView
.
as_view
(
content_disposition
=
'attachment'
,
)
admin
=
User
.
objects
.
create_superuser
(
'admin'
,
'admin@example.com'
,
'admin'
)
for
user_agent
,
expect_header
in
[
(
'Firefox'
,
"attachment; filename*=UTF-8''Heiz%C3%B6lr%C3%BCcksto%C3%9Fabd%C3%A4mpfung.txt"
),
(
'WebKit'
,
'attachment; filename=Heiz
\xc3\xb6
lr
\xc3\xbc
cksto
\xc3\x9f
abd
\xc3\xa4
mpfung.txt'
),
(
'MSIE'
,
'attachment; '
),
]:
request
=
RequestFactory
().
get
(
'/cust1/file/'
)
request
.
user
=
admin
request
.
META
[
'HTTP_USER_AGENT'
]
=
user_agent
response
=
view
(
request
,
path
=
u
'CustomerDossier/cust2/Heizölrückstoßabdämpfung.txt'
)
self
.
assertEqual
(
list
(
response
.
streaming_content
),
[
b
'test5'
])
self
.
assertEqual
(
response
[
'Content-Type'
],
'text/plain'
)
self
.
assertEqual
(
response
[
'Content-Length'
],
'5'
)
self
.
assertEqual
(
response
[
'Content-Disposition'
],
expect_header
,
user_agent
)
self
.
assertIn
(
'Last-Modified'
,
response
)
private_storage/views.py
View file @
64f1848d
...
...
@@ -96,10 +96,12 @@ class PrivateStorageView(View):
response
=
self
.
server_class
().
serve
(
private_file
)
if
self
.
content_disposition
:
# Python 3 doesn't support b'..'.format(), and % formatting
# was added in 3.4: # https://bugs.python.org/issue3982
filename
=
self
.
get_content_disposition_filename
(
private_file
)
response
[
'Content-Disposition'
]
=
'{}; {}'
.
format
(
self
.
content_disposition
,
self
.
_encode_filename_header
(
filename
)
)
response
[
'Content-Disposition'
]
=
b
'; '
.
join
([
self
.
content_disposition
.
encode
()
,
self
.
_encode_filename_header
(
filename
)
]
)
return
response
...
...
@@ -117,16 +119,15 @@ class PrivateStorageView(View):
user_agent
=
self
.
request
.
META
.
get
(
'HTTP_USER_AGENT'
,
None
)
if
'WebKit'
in
user_agent
:
# Support available for UTF-8 encoded strings.
utf8_filename
=
filename
.
encode
(
"utf-8"
)
return
'filename={}'
.
format
(
utf8_filename
)
return
u
'filename={}'
.
format
(
filename
).
encode
(
"utf-8"
)
elif
'MSIE'
in
user_agent
:
# IE does not support internationalized filename at all.
# It can only recognize internationalized URL, so we should perform a trick via URL names.
return
''
return
b
''
else
:
# For others like Firefox, we follow RFC2231 (encoding extension in HTTP headers).
rfc2231_filename
=
quote
(
filename
.
encode
(
"utf-8"
))
return
"filename*=UTF-8''{}"
.
format
(
rfc2231_filename
)
return
"filename*=UTF-8''{}"
.
format
(
rfc2231_filename
)
.
encode
(
"utf-8"
)
class
PrivateStorageDetailView
(
SingleObjectMixin
,
PrivateStorageView
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment