Skip to content

Commit

Permalink
Use same filename for TXT and HTML for pastes
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskuehl committed Aug 20, 2024
1 parent b7f0025 commit 493162e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
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

0 comments on commit 493162e

Please sign in to comment.