Add a method to check if a mimetype is supported and improve guess_mimetype.

Review Request #14310 — Created Jan. 27, 2025 and updated — Latest diff uploaded

Information

Review Board
release-7.x

Reviewers

This adds a method for checking whether a mimetype is supported or not.
This is useful for code that deals with uploading binary files, to help
decide which files should be uploaded. We have similar logic for this in
RBTools. Valid and invalid mimetypes are saved to the cache and are
checked against when available, which should help speed things up when
the caller is checking a sequence of mimetypes.

This change also fixes a fault with guess_mimetype. The function runs
the file command in a subprocess to determine the given file's mimetype.
The function doesn't properly close and clean up the subprocess's resources
(the file descriptors for stdin and stdout). In the Review Board server
process which runs for a long time, we're relying on Python's garbage
collector to close everything. The garbage collector is not deterministic
and it could take a while before the resources are cleaned up. It's better
to deal with these ourselves to avoid any unexpected side effects. This
change makes sure the resources get cleaned up when they're no longer used.

  • Ran unit tests.
  • Used in the Bitbucket Server Pull Request integration code in Power
    Pack.
  • Saw the ResourceWarning: unclosed file coming from guess_mimetype
    go away in the logs.

Diff Revision 2 (Latest)

orig
1
2

Commits

First Last Summary ID Author
Add a method to check if a mimetype is supported and improve guess_mimetype.
This adds a method for checking whether a mimetype is supported or not. This is useful for code that deals with uploading binary files, to help decide which files should be uploaded. We have similar logic for this in RBTools. Valid and invalid mimetypes are saved to the cache and are checked against when available, which should help speed things up when the caller is checking a sequence of mimetypes. This change also fixes a fault with `guess_mimetype`. The function runs the `file` command in a subprocess to determine the given file's mimetype. The function doesn't properly close and clean up the subprocess's resources (the file descriptors for stdin and stdout). In the Review Board server process which runs for a long time, we're relying on Python's garbage collector to close everything. The garbage collector is not deterministic and it could take a while before the resources are cleaned up. It's better to deal with these ourselves to avoid any unexpected side effects. This change makes sure the resources get cleaned up when they're no longer used.
1e49515439845acb8737028c2d9d4f7cc517133b Michelle Aubin
reviewboard/attachments/mimetypes.py
reviewboard/attachments/tests/test_mimetypes.py
Loading...