Skip to content

Commit

Permalink
fix: file upload >100MB
Browse files Browse the repository at this point in the history
* flask uses now the MAX_CONTENT_LENGTH to restrict file upload. it is
  possible to set the max_content_length per request. so for this request
  it is set to the configured max file size
  • Loading branch information
utnapischtim committed Feb 5, 2025
1 parent db94ec8 commit d28ac8d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
Changes
=======

Version unreleased

- fix: file upload >100MB

Version 7.0.0 (release 2024-12-09)

- setup: change to reusable workflows
Expand Down
23 changes: 22 additions & 1 deletion invenio_records_resources/resources/files/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Copyright (C) 2020 CERN.
# Copyright (C) 2020 Northwestern University.
# Copyright (C) 2023 TU Wien.
# Copyright (C) 2025 Graz University of Technology.
#
# Invenio-Records-Resources is free software; you can redistribute it and/or
# modify it under the terms of the MIT License; see LICENSE file for more
Expand All @@ -13,7 +14,7 @@
from contextlib import ExitStack

import marshmallow as ma
from flask import Response, abort, current_app, g, stream_with_context
from flask import Response, abort, current_app, g, request, stream_with_context
from flask_resources import (
JSONDeserializer,
RequestBodyParser,
Expand Down Expand Up @@ -51,6 +52,25 @@
)


def set_max_content_length(func):
"""Set max content length."""

def _wrapper(*args, **kwargs):
# flask >= 3.1.0 changed the behavior of MAX_CONTENT_LENGTH
# configuration variable. this is applied now to all requests
# including file upload. File uploads have much higher file
# size as form POST's. To keep request.max_content_length for
# form posts on a small value the request.max_content_length
# for file uploads is set here to the
# FILES_REST_DEFAULT_MAX_FILE_SIZE
request.max_content_length = current_app.config.get(
"FILES_REST_DEFAULT_MAX_FILE_SIZE", 10**10
)
return func(*args, **kwargs)

return _wrapper


#
# Resource
#
Expand Down Expand Up @@ -215,6 +235,7 @@ def _gen_zipstream():
},
)

@set_max_content_length
@request_view_args
@request_stream
@response_handler()
Expand Down

0 comments on commit d28ac8d

Please sign in to comment.