summaryrefslogtreecommitdiff
path: root/fbin/monkey.py
blob: c6458ad24f27e6bbc62a04bf1def00201557a418 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import tempfile

from flask import current_app
import werkzeug.formparser
import werkzeug.wrappers

def werkzeug_patch():
    global werkzeug_orig_stream_factory

    werkzeug_orig_stream_factory = werkzeug.formparser.default_stream_factory

    def custom_stream_factory(total_content_length, filename, content_type, content_length=None):
        if total_content_length > 1024 * 500:
                return tempfile.NamedTemporaryFile('wb+', prefix = 'upload_', dir = current_app.config['FILE_DIRECTORY'], delete = True)
        return werkzeug_orig_stream_factory(total_content_length, filename, content_type, content_length)

    werkzeug.formparser.default_stream_factory = custom_stream_factory
    werkzeug.wrappers.default_stream_factory = custom_stream_factory

def werkzeug_reset():
    werkzeug.formparser.default_stream_factory = werkzeug_orig_stream_factory
    werkzeug.wrappers.default_stream_factory = werkzeug_orig_stream_factory

def patch():
    werkzeug_patch()