diff --git a/e2e/e2e_namespace_file.py b/e2e/e2e_namespace_file.py index 5c94a20..ff8591d 100644 --- a/e2e/e2e_namespace_file.py +++ b/e2e/e2e_namespace_file.py @@ -221,6 +221,22 @@ def test_file_upload_png(page: Page, login): expect(page.locator("text=Filesize: 2.81 KiB")).to_be_visible() +def test_file_upload_too_large(page: Page, login, tmp_path): + """Check that uploading a file above the size limit gives a clear error.""" + large_file = tmp_path / "large.png" + large_file.write_bytes(b"\0" * (5 * 1024 * 1024)) + + page.goto("http://localhost:8080/edit/File/en/Test.png") + expect(page.locator("text=Upload new file (max 4 MiB)")).to_be_visible() + + page.locator("[name=file]").set_input_files(str(large_file)) + with page.expect_navigation(): + page.locator("[name=save]").click() + page.wait_for_url("http://localhost:8080/edit/File/en/Test.png") + + expect(page.locator("text=The file you tried to upload is bigger than 4 MiB.")).to_be_visible() + + def test_file_upload_wrong_png(page: Page, login): """Check if uploading a PNG on a .gif doesn't actually work.""" page.goto("http://localhost:8080/File/en/Test.gif") diff --git a/truewiki/__main__.py b/truewiki/__main__.py index 3076397..eab56a2 100644 --- a/truewiki/__main__.py +++ b/truewiki/__main__.py @@ -49,8 +49,6 @@ log = logging.getLogger(__name__) -# Don't allow any file above 4 MiB. -MAX_UPLOAD_SIZE = (1024**2) * 4 # Cache static files for 24 hours. CACHE_TIME_STATIC = 3600 * 24 # Cache uploads for 5 minutes by default. These files can be overwritten by @@ -188,7 +186,7 @@ def main(bind, port, storage, frontend_url, cache_time, remote_ip_header, valida print(json.dumps(errors, indent=4)) return - webapp = web.Application(client_max_size=MAX_UPLOAD_SIZE, middlewares=[remove_cookie_middleware]) + webapp = web.Application(client_max_size=config.MAX_UPLOAD_SIZE, middlewares=[remove_cookie_middleware]) webapp.on_response_prepare.append(cache_on_prepare) if remote_ip_header: global REMOTE_IP_HEADER diff --git a/truewiki/config.py b/truewiki/config.py index fb6efbf..0f70f4f 100644 --- a/truewiki/config.py +++ b/truewiki/config.py @@ -12,6 +12,7 @@ } JAVASCRIPT = [] LICENSE = "Unlicensed" +MAX_UPLOAD_SIZE = (1024**2) * 4 PRIMARY_LANGUAGE = "en" PROJECT_NAME = "Unnamed" diff --git a/truewiki/namespaces/file/namespace.py b/truewiki/namespaces/file/namespace.py index c559514..17ea065 100644 --- a/truewiki/namespaces/file/namespace.py +++ b/truewiki/namespaces/file/namespace.py @@ -10,6 +10,7 @@ footer as folder_footer, ) from ... import ( + config, metadata, singleton, wiki_page, @@ -177,9 +178,9 @@ def file_get_img(url: str, thumb: Optional[int] = None) -> str: @staticmethod def add_edit_content() -> str: - return """ + return f"""
Please ensure the upload has a license that is compatible with the license of this wiki.
Any new upload will overwrite the existing upload at this location. Preview won't work for new uploads.