reviewboard.attachments.mimetypes¶
File attachment mimetype registration and scoring.
- guess_mimetype(uploaded_file)[source]¶
Guess the mimetype of an uploaded file.
Uploaded files don’t necessarily have valid mimetypes provided, so attempt to guess them when they’re blank.
This only works if file is in the path. If it’s not, or guessing fails, we fall back to a mimetype of application/octet-stream.
- Parameters:
uploaded_file (
django.core.files.File
) – The uploaded file object.- Returns:
The guessed mimetype.
- Return type:
- get_uploaded_file_mimetype(uploaded_file)[source]¶
Return the mimetype of a file that was uploaded.
There are several things that can go wrong with browser-provided mimetypes. In one case (bug 3427), Firefox on Linux Mint was providing a mimetype that looked like
text/text/application/pdf
, which is unparsable. IE also has a habit of setting any unknown file type to application/octet-stream, rather than just choosing not to provide a mimetype. In the case where what we get from the browser is obviously wrong, try to guess.- Parameters:
uploaded_file (
django.core.files.File
) – The uploaded file object.- Returns:
The guessed mimetype.
- Return type:
- register_mimetype_handler(handler)[source]¶
Register a MimetypeHandler class.
This will register a mimetype Handler used by Review Board to render thumbnails for the file attachments across different mimetypes.
- Parameters:
handler (
type
) – The mimetype handler to register. This must be a subclass ofMimetypeHandler
.- Raises:
TypeError – The provided class is not of the correct type.
- unregister_mimetype_handler(handler)[source]¶
Unregister a MimetypeHandler class.
This will unregister a previously registered mimetype handler.
- Parameters:
handler (
type
) – The mimetype handler to unregister. This must be a subclass ofMimetypeHandler
.- Raises:
TypeError – The provided class is not of the correct type.
ValueError – The mimetype handler was not previously registered.
- score_match(pattern, test)[source]¶
Return a score for how well the pattern matches the mimetype.
This is an ordered list of precedence (
_
indicates non-match):Format
Precedence
Type/Vendor+Subtype
2.0
Type/Subtype
1.9
Type/*
1.8
*/Vendor+Subtype
1.7
*/_ +Subtype
1.6
*/*
1.5
_
0
- Parameters:
pattern (
tuple
) – A parsed mimetype pattern to score. This is a 3-tuple of the type, subtype, and parameters as returned bymimeparse.parse_mime_type()
. This may include*
wildcards.test (
tuple
) – A parsed mimetype to match against the pattern. This is a 3-tuple of the type, subtype, and parameters as returned bymimeparse.parse_mime_type()
.
- Returns:
The resulting score for the match.
- Return type:
- class MimetypeHandler(attachment, mimetype)[source]¶
Bases:
object
Handles mimetype-specific properties.
This class also acts as a generic handler for mimetypes not matched explicitly by any handler. Note that this is not the same as
*/*
.- attachment¶
The file attachment being handled.
- THUMBNAIL_IMAGE_SCALES: Final[tuple[int, ...]] = (1, 2, 3)¶
All scaling factors for multi-DPI thumbnail images.
This is only used for mimetype handlers that support thumbnail images.
New in version 7.0.
- BASE_THUMBNAIL_IMAGE_WIDTH: Final[int] = 300¶
The base size for thumbnail images.
This is only used for mimetype handlers that support thumbnail images.
New in version 7.0.
- use_hd_thumbnails: ClassVar[bool] = True¶
Whether HD thumbnails are provided by this handler.
Subclasses (especially in extensions) can use this to introspect what size thumbnails they should generate.
- __init__(attachment, mimetype)[source]¶
Initialize the handler.
- Parameters:
attachment (
reviewboard.attachments.models.FileAttachment
) – The file attachment being handled.mimetype (
unicode
) – The mimetype for the file attachment.
- classmethod get_best_handler(mimetype)[source]¶
Return the handler and score that that best fit the mimetype.
- classmethod for_type(attachment)[source]¶
Return the handler that is the best fit for provided mimetype.
- Parameters:
attachment (
reviewboard.attachments.models.FileAttachment
) – The file attachment to find the best handler for.- Returns:
The best mimetype handler for the attachment, or
None
if one could not be found.- Return type:
- get_icon_url()[source]¶
Return the appropriate icon URL for this mimetype.
- Returns:
The URL to an icon representing this mimetype.
- Return type:
- get_thumbnail()[source]¶
Return HTML that represents a preview of the attachment.
Subclasses can override this to provide a suitable thumbnail. The outer element of the thumbnail should have a
file-thumbnail
CSS class.By default, this returns an empty thumbnail.
- Returns:
The HTML for the thumbnail for the associated attachment.
- Return type:
django.utils.safestring.SafeText
- set_thumbnail(data)[source]¶
Set the thumbnail data for this attachment.
This should be implemented by subclasses if they need the thumbnail to be generated client-side.
- Parameters:
data (
bytes
) – The contents of the thumbnail data.
- get_raw_thumbnail_image_url(*, width: Optional[int] = None, height: Optional[int] = None) Optional[str] [source]¶
Return the URL to a thumbnail of a given size.
For mimetype handlers that support image-based thumbnails, this will compute a thumbnail matching the given size requirements, and then return the URL to that thumbnail in file storage.
The caller must provide a width or a height at minimum, and may provide both. If providing only a width or a height, the thumbnail size is expected to be constrained only to that dimension.
Subclasses must override
generate_thumbnail_image()
to provide the logic for creating a thumbnail.New in version 7.0.
- Parameters:
- Raises:
NotImplementedError – The mimetype handler does not support thumbnail images.
ValueError – Neither a width nor a height was provided.
- Returns:
The URL to the thumbnail, or
None
if one could not be generated.- Return type:
- generate_thumbnail_image(*, width: Optional[int] = None, height: Optional[int] = None, create_if_missing: bool = True) Optional[str] [source]¶
Generate a thumbnail of a given size.
For mimetype handlers that support image-based thumbnails, this must generate a thumbnail matching the given size requirements, and then return the URL to that thumbnail.
The caller must provide a width or a height at minimum, and may provide both. If providing only a width or a height, the thumbnail size is expected to be constrained only to that dimension.
New in version 7.0.
- Parameters:
width (
int
, optional) – The width to constrain the thumbnail image to.height (
int
, optional) – The height to constrain the thumbnail image to.create_if_missing (
bool
, optional) –Whether to create the thumbnail if one does not already exist.
If
False
, the existing thumbnail URL will be returned if it exists, but a new one will not otherwise be created.
- Raises:
NotImplementedError – The mimetype handler does not support thumbnail images.
- Returns:
The URL to the thumbnail, or
None
if one could not be generated.- Return type:
- delete_associated_files() None [source]¶
Delete any extra files associated with this attachment.
This should be implemented by subclasses who create and store extra files for file attachments, such as handlers that create and store thumbnail files. This should not delete the main file of the file attachment.
New in version 6.0.
- __annotations__ = {'BASE_THUMBNAIL_IMAGE_WIDTH': 'Final[int]', 'MIMETYPES_DIR': 'Final[str]', 'THUMBNAIL_IMAGE_SCALES': 'Final[tuple[int, ...]]', 'supported_mimetypes': 'ClassVar[list[str]]', 'use_hd_thumbnails': 'ClassVar[bool]'}¶
- class ImageMimetype(attachment, mimetype)[source]¶
Bases:
MimetypeHandler
Handles image mimetypes.
- supported_mimetypes: ClassVar[list[str]] = ['image/*'][source]¶
A list of mimetypes supported by this handler.
- get_thumbnail()[source]¶
Return a thumbnail of the image.
- Returns:
The HTML for the thumbnail for the associated attachment.
- Return type:
django.utils.safestring.SafeText
- generate_thumbnail_image(*, width: Optional[int] = None, height: Optional[int] = None, create_if_missing: bool = True) Optional[str] [source]¶
Generate a thumbnail of a given size.
For mimetype handlers that support image-based thumbnails, this must generate a thumbnail matching the given size requirements, and then return the URL to that thumbnail.
The caller must provide a width or a height at minimum, and may provide both. If providing only a width or a height, the thumbnail size is expected to be constrained only to that dimension.
New in version 7.0.
- Parameters:
width (
int
, optional) – The width to constrain the thumbnail image to.height (
int
, optional) – The height to constrain the thumbnail image to.create_if_missing (
bool
, optional) –Whether to create the thumbnail if one does not already exist.
If
False
, the existing thumbnail URL will be returned if it exists, but a new one will not otherwise be created.
- Raises:
NotImplementedError – The mimetype handler does not support thumbnail images.
- Returns:
The URL to the thumbnail, or
None
if one could not be generated.- Return type:
- delete_associated_files() None [source]¶
Delete the thumbnail files for this attachment.
New in version 6.0.
- __annotations__ = {'BASE_THUMBNAIL_IMAGE_WIDTH': 'Final[int]', 'MIMETYPES_DIR': 'Final[str]', 'THUMBNAIL_IMAGE_SCALES': 'Final[tuple[int, ...]]', 'supported_mimetypes': 'ClassVar[list[str]]', 'use_hd_thumbnails': 'ClassVar[bool]'}¶
- class TextMimetype(attachment, mimetype)[source]¶
Bases:
MimetypeHandler
Handles text mimetypes.
Text mimetypes provide thumbnails containing the first few lines of the file, syntax-highlighted.
- supported_mimetypes: ClassVar[list[str]] = ['text/*', 'application/javascript', 'application/json', 'application/x-javascript', 'application/x-json', 'application/x-yaml'][source]¶
A list of mimetypes supported by this handler.
- get_thumbnail()[source]¶
Return the thumbnail of the text file as rendered as html.
The content will be generated and then cached for future requests.
- Returns:
The resulting HTML-safe thumbnail content.
- Return type:
django.utils.safestring.SafeText
- __annotations__ = {'BASE_THUMBNAIL_IMAGE_WIDTH': 'Final[int]', 'MIMETYPES_DIR': 'Final[str]', 'THUMBNAIL_IMAGE_SCALES': 'Final[tuple[int, ...]]', 'supported_mimetypes': 'ClassVar[list[str]]', 'use_hd_thumbnails': 'ClassVar[bool]'}¶
- class ReStructuredTextMimetype(attachment, mimetype)[source]¶
Bases:
TextMimetype
Handles ReStructuredText (.rst) mimetypes.
ReST mimetypes provide thumbnails containing the first few lines of rendered content from the file.
- supported_mimetypes: ClassVar[list[str]] = ['text/x-rst', 'text/rst'][source]¶
A list of mimetypes supported by this handler.
- __annotations__ = {'BASE_THUMBNAIL_IMAGE_WIDTH': 'Final[int]', 'MIMETYPES_DIR': 'Final[str]', 'THUMBNAIL_IMAGE_SCALES': 'Final[tuple[int, ...]]', 'supported_mimetypes': 'ClassVar[list[str]]', 'use_hd_thumbnails': 'ClassVar[bool]'}¶
- class MarkDownMimetype(attachment, mimetype)[source]¶
Bases:
TextMimetype
Handle MarkDown (.md) mimetypes.
Markdown mimetypes provide thumbnails containing the first few lines of rendered content from the file.
- supported_mimetypes: ClassVar[list[str]] = ['text/x-markdown', 'text/markdown'][source]¶
A list of mimetypes supported by this handler.
- __annotations__ = {'BASE_THUMBNAIL_IMAGE_WIDTH': 'Final[int]', 'MIMETYPES_DIR': 'Final[str]', 'THUMBNAIL_IMAGE_SCALES': 'Final[tuple[int, ...]]', 'supported_mimetypes': 'ClassVar[list[str]]', 'use_hd_thumbnails': 'ClassVar[bool]'}¶
- class VideoMimetype(attachment, mimetype)[source]¶
Bases:
MimetypeHandler
Handles video mimetypes.
This will display a thumbnail utilizing the
<video>
tag, allowing a frame of video to be shown for browsers that support the video format.- supported_mimetypes: ClassVar[list[str]] = ['video/*'][source]¶
A list of mimetypes supported by this handler.
- get_thumbnail()[source]¶
Return HTML that represents a preview of the attachment.
This will create a
<video>
tag that starts half a second into the video, giving the browser a spot in which to load a frame for use in the thumbnail. The browser will fetch only what’s needed in order to show this.- Returns:
The HTML for the video thumbnail.
- Return type:
django.utils.safestring.SafeText
- __annotations__ = {'BASE_THUMBNAIL_IMAGE_WIDTH': 'Final[int]', 'MIMETYPES_DIR': 'Final[str]', 'THUMBNAIL_IMAGE_SCALES': 'Final[tuple[int, ...]]', 'supported_mimetypes': 'ClassVar[list[str]]', 'use_hd_thumbnails': 'ClassVar[bool]'}¶