From 493162e782b95bf5c85a9b1d0b199219ebb3d7f9 Mon Sep 17 00:00:00 2001 From: Chris Kuehl Date: Tue, 20 Aug 2024 16:18:48 -0500 Subject: [PATCH 1/3] Use same filename for TXT and HTML for pastes --- fluffy/models.py | 18 +++++++++++------- fluffy/views.py | 3 +++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/fluffy/models.py b/fluffy/models.py index 08ae583..fd0b219 100644 --- a/fluffy/models.py +++ b/fluffy/models.py @@ -87,7 +87,7 @@ def __new__(cls, *args, **kwargs): @classmethod @contextmanager - def from_http_file(cls, f): + def from_http_file(cls, f, unique_id: str | None = None): with tempfile.NamedTemporaryFile() as tf: # We don't know the file size until we start to save the file (the # client can lie about the uploaded size, and some browsers don't @@ -102,12 +102,12 @@ def from_http_file(cls, f): human_name=f.filename, num_bytes=num_bytes, open_file=tf, - unique_id=gen_unique_id(), + unique_id=unique_id or gen_unique_id(), ) @classmethod @contextmanager - def from_text(cls, text, human_name='plaintext.txt'): + def from_text(cls, text, human_name='plaintext.txt', unique_id: str | None = None): with io.BytesIO(text.encode('utf8')) as open_file: num_bytes = len(text) if num_bytes > app.config['MAX_UPLOAD_SIZE']: @@ -117,7 +117,7 @@ def from_text(cls, text, human_name='plaintext.txt'): human_name=human_name, num_bytes=num_bytes, open_file=open_file, - unique_id=gen_unique_id(), + unique_id=unique_id or gen_unique_id(), ) @cached_property @@ -191,8 +191,8 @@ class HtmlToStore( namedtuple( 'HtmlToStore', ( - 'name', 'open_file', + 'unique_id', ), ), ObjectToStore, @@ -200,11 +200,11 @@ class HtmlToStore( @classmethod @contextmanager - def from_html(cls, html): + def from_html(cls, html, unique_id: str | None = None): with io.BytesIO(html.encode('utf8')) as open_file: yield cls( - name=gen_unique_id() + '.html', open_file=open_file, + unique_id=unique_id or gen_unique_id(), ) @property @@ -216,6 +216,10 @@ def content_disposition_header(self): # inline => render as HTML as opposed to downloading the HTML return 'inline' + @cached_property + def name(self): + return f"{self.unique_id}.html" + @cached_property def url(self): return app.config['HTML_URL'].format(name=self.name) diff --git a/fluffy/views.py b/fluffy/views.py index dfced44..4f2c5bb 100644 --- a/fluffy/views.py +++ b/fluffy/views.py @@ -94,6 +94,7 @@ def upload(): raw_url=app.config['FILE_URL'].format(name=uf.name), styles=STYLES_BY_CATEGORY, ), + unique_id=uf.unique_id, ), ) objects.append(pb) @@ -219,6 +220,7 @@ def paste(): raw_url=app.config['FILE_URL'].format(name=uf.name), styles=STYLES_BY_CATEGORY, ), + unique_id=uf.unique_id, ), ) objects.append(paste_obj) @@ -232,6 +234,7 @@ def paste(): copy_and_edit_text=transformed_text, raw_url=app.config['FILE_URL'].format(name=uf.name), ), + unique_id=uf.unique_id, ), ) objects.append(paste_obj) From 7f7173726c6f14cf14e403301214d9847312cb1d Mon Sep 17 00:00:00 2001 From: Chris Kuehl Date: Wed, 21 Aug 2024 00:00:23 -0500 Subject: [PATCH 2/3] Add tests for same filenames --- Makefile | 2 +- tests/integration/paste_test.py | 4 ++++ tests/integration/upload_test.py | 5 +++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 8fe7247..2602393 100644 --- a/Makefile +++ b/Makefile @@ -48,7 +48,7 @@ dev: $(VENV) fluffy/static/app.css test: $(VENV) $(BIN)/coverage erase COVERAGE_PROCESS_START=$(CURDIR)/.coveragerc \ - $(BIN)/py.test -vv tests/ + $(BIN)/py.test --tb=native -vv tests/ $(BIN)/coverage combine $(BIN)/coverage report diff --git a/tests/integration/paste_test.py b/tests/integration/paste_test.py index a48ae41..47f0c19 100644 --- a/tests/integration/paste_test.py +++ b/tests/integration/paste_test.py @@ -91,3 +91,7 @@ def test_simple_paste_json(running_server): }, }, } + + # The paste's HTML view and raw view should have the same URL minus the extension. + details = req.json()['uploaded_files']['paste'] + assert details['raw'].rsplit('/', 1)[1] == details['paste'].replace('.html', '.txt').rsplit('/', 1)[1] diff --git a/tests/integration/upload_test.py b/tests/integration/upload_test.py index 1cb49d6..d5e97a8 100644 --- a/tests/integration/upload_test.py +++ b/tests/integration/upload_test.py @@ -6,6 +6,7 @@ from pyquery import PyQuery as pq from testing import assert_url_matches_content +from testing import raw_text_url_from_paste_html from testing import BINARY_TESTCASES from testing import FILE_CONTENT_TESTCASES from testing import paste_urls_from_details @@ -112,6 +113,10 @@ def test_plaintext_files_are_also_pasted(content, running_server): content ) + # The paste's HTML view and raw view should have the same URL minus the extension. + raw_url = raw_text_url_from_paste_html(req.text) + assert raw_url.rsplit('/', 1)[1] == url.replace('.html', '.bin').rsplit('/', 1)[1] + @pytest.mark.parametrize('content', BINARY_TESTCASES) def test_binary_files_are_not_pasted(content, running_server): From 87b05b17b77fcc383284e3fdfe22e8f6acc35289 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 21 Aug 2024 05:00:42 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/integration/upload_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/upload_test.py b/tests/integration/upload_test.py index d5e97a8..7abedb2 100644 --- a/tests/integration/upload_test.py +++ b/tests/integration/upload_test.py @@ -6,11 +6,11 @@ from pyquery import PyQuery as pq from testing import assert_url_matches_content -from testing import raw_text_url_from_paste_html from testing import BINARY_TESTCASES from testing import FILE_CONTENT_TESTCASES from testing import paste_urls_from_details from testing import PLAINTEXT_TESTCASES +from testing import raw_text_url_from_paste_html from testing import urls_from_details