Skip to content

Commit

Permalink
Merge pull request #177 from chriskuehl/use-same-filename-for-pastes
Browse files Browse the repository at this point in the history
Use same filename for TXT and HTML for pastes
  • Loading branch information
chriskuehl authored Aug 21, 2024
2 parents b7f0025 + 87b05b1 commit 5efcada
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 11 additions & 7 deletions fluffy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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']:
Expand All @@ -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
Expand Down Expand Up @@ -191,20 +191,20 @@ class HtmlToStore(
namedtuple(
'HtmlToStore',
(
'name',
'open_file',
'unique_id',
),
),
ObjectToStore,
):

@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
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions fluffy/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions tests/integration/paste_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
5 changes: 5 additions & 0 deletions tests/integration/upload_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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


Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 5efcada

Please sign in to comment.