Make MimetypeHandlers take care of deleting associated files for a file attachment.
Review Request #13141 — Created July 13, 2023 and submitted
Currently we have no centralized method for deleting extra files associated
with file attachments, such as thumbnail files. This adds a
delete_associated_files
method to the baseMimetypeHandler
class, which
can be implemented by subclasses to take care of deleting any extra associated
files that are created for file attachments of certain mimetypes. This also
updates theImageMimetype
class to delete the files it creates for thumbnails.
- Tested in an upcoming change where deleting draft file attachments will
trigger deleting all files associated with the draft. - Ran unit tests.
Summary |
---|
Description | From | Last Updated |
---|---|---|
Might be worth centralizing the list of available thumbnail files using some function, and then using that in both get_thumbnail() … |
|
|
Can you show me an example of how these paths end up looking? The before/after? I feel like there's got … |
|
|
Can you add -> None for each of these, so we can do type checking as part of tests? |
|
|
Just for alignment purposes, we can prefix all these with f, even if we're not putting in any format strings. … |
|
|
Add a blank line between these two. |
|
|
Missing trailing comma. |
|
|
We can just use attachment, since we have that locally. |
|
|
lstrip() isn't safe. It doesn't remove a prefix. It removes any leading characters found in the provided string. So, any … |
|

Change Summary:
- Moved to RB6.
Commits: |
|
||||||
---|---|---|---|---|---|---|---|
Branch: |
|
||||||
Diff: |
Revision 2 (+178) |
Checks run (2 succeeded)
-
-
reviewboard/attachments/mimetypes.py (Diff revision 2) Might be worth centralizing the list of available thumbnail files using some function, and then using that in both
get_thumbnail()
and in here. Maybe something that supplies the filename and the 1x/2x/etc. This will also help us scale up thumbnails for higher-res displays down the road. -
reviewboard/attachments/mimetypes.py (Diff revision 2) Can you show me an example of how these paths end up looking? The before/after? I feel like there's got to be another way to do what we want to do here.
-
reviewboard/attachments/tests.py (Diff revision 2) Can you add
-> None
for each of these, so we can do type checking as part of tests? -
reviewboard/attachments/tests.py (Diff revision 2) Just for alignment purposes, we can prefix all these with
f
, even if we're not putting in any format strings.There's no difference to Python. A format string that doesn't format just becomes a string.
>>> import dis >>> def foo(): ... return ( ... 'test' ... f'{hi}' ... 'bar' ... ) ... >>> def bar(): ... return ( ... f'test' ... f'{hi}' ... f'bar' ... ) ... >>> dis.dis(foo) == dis.dis(bar) True

Description: |
|
|||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Commits: |
|
|||||||||||||||||||||
Diff: |
Revision 3 (+234 -4) |
Checks run (2 succeeded)

Commits: |
|
||||||
---|---|---|---|---|---|---|---|
Diff: |
Revision 4 (+244 -4) |
Checks run (2 succeeded)
-
-
-
reviewboard/attachments/mimetypes.py (Diff revision 4) We can just use
attachment
, since we have that locally. -
reviewboard/attachments/mimetypes.py (Diff revision 4) lstrip()
isn't safe. It doesn't remove a prefix. It removes any leading characters found in the provided string. So, any characters listed insite_media_url
will be removed from the beginning of the URL.Ideally we'd use
removeprefix()
, but that's only in 3.9+. So you'd instead need to do:if t.startswith(site_media_url): t = t[len(site_media_url):]
Or maybe assert that it starts with it? Or skip/log if it doesn't?

Commits: |
|
||||||
---|---|---|---|---|---|---|---|
Diff: |
Revision 5 (+284 -4) |