Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
synapse
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Maunium
synapse
Commits
47ca5eb8
Commit
47ca5eb8
authored
7 years ago
by
Erik Johnston
Browse files
Options
Downloads
Patches
Plain Diff
Split out add_file_headers
parent
ce3a726f
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
synapse/rest/media/v1/_base.py
+42
-28
42 additions, 28 deletions
synapse/rest/media/v1/_base.py
with
42 additions
and
28 deletions
synapse/rest/media/v1/_base.py
+
42
−
28
View file @
47ca5eb8
...
@@ -70,38 +70,11 @@ def respond_with_file(request, media_type, file_path,
...
@@ -70,38 +70,11 @@ def respond_with_file(request, media_type, file_path,
logger
.
debug
(
"
Responding with %r
"
,
file_path
)
logger
.
debug
(
"
Responding with %r
"
,
file_path
)
if
os
.
path
.
isfile
(
file_path
):
if
os
.
path
.
isfile
(
file_path
):
request
.
setHeader
(
b
"
Content-Type
"
,
media_type
.
encode
(
"
UTF-8
"
))
if
upload_name
:
if
is_ascii
(
upload_name
):
request
.
setHeader
(
b
"
Content-Disposition
"
,
b
"
inline; filename=%s
"
%
(
urllib
.
quote
(
upload_name
.
encode
(
"
utf-8
"
)),
),
)
else
:
request
.
setHeader
(
b
"
Content-Disposition
"
,
b
"
inline; filename*=utf-8
''
%s
"
%
(
urllib
.
quote
(
upload_name
.
encode
(
"
utf-8
"
)),
),
)
# cache for at least a day.
# XXX: we might want to turn this off for data we don't want to
# recommend caching as it's sensitive or private - or at least
# select private. don't bother setting Expires as all our
# clients are smart enough to be happy with Cache-Control
request
.
setHeader
(
b
"
Cache-Control
"
,
b
"
public,max-age=86400,s-maxage=86400
"
)
if
file_size
is
None
:
if
file_size
is
None
:
stat
=
os
.
stat
(
file_path
)
stat
=
os
.
stat
(
file_path
)
file_size
=
stat
.
st_size
file_size
=
stat
.
st_size
request
.
setHeader
(
add_file_headers
(
request
,
media_type
,
file_size
,
upload_name
)
b
"
Content-Length
"
,
b
"
%d
"
%
(
file_size
,)
)
with
open
(
file_path
,
"
rb
"
)
as
f
:
with
open
(
file_path
,
"
rb
"
)
as
f
:
yield
logcontext
.
make_deferred_yieldable
(
yield
logcontext
.
make_deferred_yieldable
(
...
@@ -111,3 +84,44 @@ def respond_with_file(request, media_type, file_path,
...
@@ -111,3 +84,44 @@ def respond_with_file(request, media_type, file_path,
finish_request
(
request
)
finish_request
(
request
)
else
:
else
:
respond_404
(
request
)
respond_404
(
request
)
def
add_file_headers
(
request
,
media_type
,
file_size
,
upload_name
):
"""
Adds the correct response headers in preparation for responding with the
media.
Args:
request (twisted.web.http.Request)
media_type (str): The media/content type.
file_size (int): Size in bytes of the media, if known.
upload_name (str): The name of the requested file, if any.
"""
request
.
setHeader
(
b
"
Content-Type
"
,
media_type
.
encode
(
"
UTF-8
"
))
if
upload_name
:
if
is_ascii
(
upload_name
):
request
.
setHeader
(
b
"
Content-Disposition
"
,
b
"
inline; filename=%s
"
%
(
urllib
.
quote
(
upload_name
.
encode
(
"
utf-8
"
)),
),
)
else
:
request
.
setHeader
(
b
"
Content-Disposition
"
,
b
"
inline; filename*=utf-8
''
%s
"
%
(
urllib
.
quote
(
upload_name
.
encode
(
"
utf-8
"
)),
),
)
# cache for at least a day.
# XXX: we might want to turn this off for data we don't want to
# recommend caching as it's sensitive or private - or at least
# select private. don't bother setting Expires as all our
# clients are smart enough to be happy with Cache-Control
request
.
setHeader
(
b
"
Cache-Control
"
,
b
"
public,max-age=86400,s-maxage=86400
"
)
request
.
setHeader
(
b
"
Content-Length
"
,
b
"
%d
"
%
(
file_size
,)
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment