Skip to content

Commit

Permalink
Implement workaround for axios not passing Content-Length headers in …
Browse files Browse the repository at this point in the history
…multipart fields

Co-authored-by: Hugo Herter <[email protected]>
  • Loading branch information
MHHukiewitz and hoh committed Jan 22, 2024
1 parent 882aea2 commit 3a9bb34
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/aleph/web/controllers/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,11 @@ def content(self) -> Union[str, bytes]:

class MultipartUploadedFile:
_content: Optional[bytes]
size: int

def __init__(self, file_field: FileField):
def __init__(self, file_field: FileField, size: int):
self.file_field = file_field

try:
content_length_str = file_field.headers["Content-Length"]
self.size = int(content_length_str)
except (KeyError, ValueError):
raise web.HTTPUnprocessableEntity(
reason="Invalid/missing Content-Length header."
)
self.size = size
self._content = None

@property
Expand Down Expand Up @@ -243,13 +237,18 @@ async def storage_add_file(request: web.Request):
grace_period = config.storage.grace_period.value

post = await request.post()
headers = request.headers
try:
file_field = post["file"]
except KeyError:
raise web.HTTPUnprocessableEntity(reason="Missing 'file' in multipart form.")

if isinstance(file_field, FileField):
uploaded_file: UploadedFile = MultipartUploadedFile(file_field)
try:
content_length = int(headers.get("Content-Length", file_field.headers["Content-Length"]))
uploaded_file: UploadedFile = MultipartUploadedFile(file_field, content_length)
except (KeyError, ValueError):
raise web.HTTPUnprocessableEntity(reason="Invalid/missing Content-Length header.")
else:
uploaded_file = RawUploadedFile(file_field)

Expand Down

0 comments on commit 3a9bb34

Please sign in to comment.