Fix content disposition parsing

master
Markus Bauer 2021-12-13 15:04:27 +01:00
parent 05192aa32c
commit b87705fffc
1 changed files with 8 additions and 2 deletions

View File

@ -251,6 +251,12 @@ class CakeCMS:
"""
return self.get('material_categories/index', course=course).json()
def __get_content_disposition_filename(self, response):
filename = response.headers['Content-Disposition'].split('filename=')[1]
if filename.startswith('"'):
return filename[1:-1]
return filename
def material_download(self, id, filename=None, course=None):
"""
Download a material file. If filename is given, the result is a pair (filename, file-content).
@ -262,10 +268,10 @@ class CakeCMS:
"""
if not filename:
response = self.get('material_files/download/' + str(id), course=course)
return response.headers['Content-Disposition'].split('filename="')[1][:-1], response.content
return self.__get_content_disposition_filename(response), response.content
else:
response = self.get('material_files/download/' + str(id), course=course, stream=True)
with open(filename, 'wb') as fd:
for chunk in response.iter_content(chunk_size=4096):
fd.write(chunk)
return response.headers['Content-Disposition'].split('filename="')[1][:-1]
return self.__get_content_disposition_filename(response)