SYN-229: Include Content-Length when downloading files

This commit is contained in:
Mark Haines 2015-01-06 11:32:36 +00:00
parent 0e93e01fcb
commit 3c8c3bf3b7
3 changed files with 23 additions and 7 deletions

View File

@ -201,7 +201,8 @@ class BaseMediaResource(Resource):
defer.returnValue(media_info) defer.returnValue(media_info)
@defer.inlineCallbacks @defer.inlineCallbacks
def _respond_with_file(self, request, media_type, file_path): def _respond_with_file(self, request, media_type, file_path,
file_size=None):
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):
@ -215,13 +216,20 @@ class BaseMediaResource(Resource):
request.setHeader( request.setHeader(
b"Cache-Control", b"public,max-age=86400,s-maxage=86400" b"Cache-Control", b"public,max-age=86400,s-maxage=86400"
) )
if file_size is None:
stat = os.stat(file_path)
file_size = stat.st_size
request.setHeader(
b"Content-Length", b"%d" % (file_size,)
)
with open(file_path, "rb") as f: with open(file_path, "rb") as f:
yield FileSender().beginFileTransfer(f, request) yield FileSender().beginFileTransfer(f, request)
request.finish() request.finish()
else: else:
self._respond_404() self._respond_404(request)
def _get_thumbnail_requirements(self, media_type): def _get_thumbnail_requirements(self, media_type):
if media_type == "image/jpeg": if media_type == "image/jpeg":

View File

@ -46,23 +46,29 @@ class DownloadResource(BaseMediaResource):
def _respond_local_file(self, request, media_id): def _respond_local_file(self, request, media_id):
media_info = yield self.store.get_local_media(media_id) media_info = yield self.store.get_local_media(media_id)
if not media_info: if not media_info:
self._respond_404() self._respond_404(request)
return return
media_type = media_info["media_type"] media_type = media_info["media_type"]
media_length = media_info["media_length"]
file_path = self.filepaths.local_media_filepath(media_id) file_path = self.filepaths.local_media_filepath(media_id)
yield self._respond_with_file(request, media_type, file_path) yield self._respond_with_file(
request, media_type, file_path, media_length
)
@defer.inlineCallbacks @defer.inlineCallbacks
def _respond_remote_file(self, request, server_name, media_id): def _respond_remote_file(self, request, server_name, media_id):
media_info = yield self._get_remote_media(server_name, media_id) media_info = yield self._get_remote_media(server_name, media_id)
media_type = media_info["media_type"] media_type = media_info["media_type"]
media_length = media_info["media_length"]
filesystem_id = media_info["filesystem_id"] filesystem_id = media_info["filesystem_id"]
file_path = self.filepaths.remote_media_filepath( file_path = self.filepaths.remote_media_filepath(
server_name, filesystem_id server_name, filesystem_id
) )
yield self._respond_with_file(request, media_type, file_path) yield self._respond_with_file(
request, media_type, file_path, media_length
)

View File

@ -100,11 +100,12 @@ class ThumbnailResource(BaseMediaResource):
t_type = thumbnail_info["thumbnail_type"] t_type = thumbnail_info["thumbnail_type"]
t_method = thumbnail_info["thumbnail_method"] t_method = thumbnail_info["thumbnail_method"]
file_id = thumbnail_info["filesystem_id"] file_id = thumbnail_info["filesystem_id"]
t_length = thumbnail_info["thumbnail_length"]
file_path = self.filepaths.remote_media_thumbnail( file_path = self.filepaths.remote_media_thumbnail(
server_name, file_id, t_width, t_height, t_type, t_method, server_name, file_id, t_width, t_height, t_type, t_method,
) )
yield self._respond_with_file(request, t_type, file_path) yield self._respond_with_file(request, t_type, file_path, t_length)
else: else:
yield self._respond_default_thumbnail( yield self._respond_default_thumbnail(
request, media_info, width, height, method, m_type, request, media_info, width, height, method, m_type,
@ -139,11 +140,12 @@ class ThumbnailResource(BaseMediaResource):
t_height = thumbnail_info["thumbnail_height"] t_height = thumbnail_info["thumbnail_height"]
t_type = thumbnail_info["thumbnail_type"] t_type = thumbnail_info["thumbnail_type"]
t_method = thumbnail_info["thumbnail_method"] t_method = thumbnail_info["thumbnail_method"]
t_length = thumbnail_info["thumbnail_length"]
file_path = self.filepaths.default_thumbnail( file_path = self.filepaths.default_thumbnail(
top_level_type, sub_type, t_width, t_height, t_type, t_method, top_level_type, sub_type, t_width, t_height, t_type, t_method,
) )
yield self.respond_with_file(request, t_type, file_path) yield self.respond_with_file(request, t_type, file_path, t_length)
def _select_thumbnail(self, desired_width, desired_height, desired_method, def _select_thumbnail(self, desired_width, desired_height, desired_method,
desired_type, thumbnail_infos): desired_type, thumbnail_infos):