Jump to >

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

unicode

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 unparseable. 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

unicode

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 of MimetypeHandler.

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 of MimetypeHandler.

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 by mimeparse.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 by mimeparse.parse_mime_type().

Returns

The resulting score for the match.

Return type

float

class MimetypeHandler(attachment, mimetype)[source]

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.

Type

reviewboard.attachments.models.FileAttachment

mimetype

The mimetype for the file attachment.

Type

unicode

supported_mimetypes = [][source]

A list of mimetypes supported by this handler.

use_hd_thumbnails = True[source]

Whether HD thumbnails are provided by this handler.

Subclasses (especially in extensions) can use this to introspect what size thumbnails they should generate.

classmethod get_best_handler(mimetype)[source]

Return the handler and score that that best fit the mimetype.

Parameters

mimetype (tuple) – A parsed mimetype to find the best handler for. This is a 3-tuple of the type, subtype, and parameters as returned by mimeparse.parse_mime_type().

Returns

A tuple of (best_score, mimetype_handler). If no handler was found, this will be (0, None).

Return type

tuple

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

MimetypeHandler

get_icon_url()[source]

Return the appropriate icon URL for this mimetype.

Returns

The URL to an icon representing this mimetype.

Return type

unicode

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()[source]

Set the thumbnail data for this attachment.

This should be implemented by subclasses if they need the thumbnail to be generated client-side.

class ImageMimetype(attachment, mimetype)[source]

Handles image mimetypes.

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

class TextMimetype(attachment, mimetype)[source]

Handles text mimetypes.

Text mimetypes provide thumbnails containing the first few lines of the file, syntax-highlighted.

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

class ReStructuredTextMimetype(attachment, mimetype)[source]

Handles ReStructuredText (.rst) mimetypes.

ReST mimetypes provide thumbnails containing the first few lines of rendered content from the file.

class MarkDownMimetype(attachment, mimetype)[source]

Handle MarkDown (.md) mimetypes.

Markdown mimetypes provide thumbnails containing the first few lines of rendered content from the file.

class VideoMimetype(attachment, mimetype)[source]

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.

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