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, mimetype)[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
Type/_     +Subtype 1.9
Type/* 1.8
*/Vendor+Subtype 1.7
*/_     +Subtype 1.6
Type/_ 1
*/_ 0.7
Parameters:
  • pattern (unicode) – The pattern to match.
  • mimetype (unicode) – The mimetype to check for the pattern.
Returns:

The resulting score for the match.

Return type:

float

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

reviewboard.attachments.models.FileAttachment – The file attachment being handled.

mimetype

unicode – The mimetype for the file attachment.

MIMETYPES_DIR = u'rb/images/mimetypes'[source]
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.

__init__(attachment, mimetype)[source]

Initialize the handler.

Parameters:
classmethod get_best_handler(mimetype)[source]

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

Parameters:mimetype (unicode) – The mimetype to find the best handler for.
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]

Bases: reviewboard.attachments.mimetypes.MimetypeHandler

Handles image mimetypes.

supported_mimetypes = [u'image/*'][source]
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]

Bases: reviewboard.attachments.mimetypes.MimetypeHandler

Handles text mimetypes.

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

supported_mimetypes = [u'text/*'][source]
FILE_CROP_CHAR_LIMIT = 1000[source]
TEXT_CROP_NUM_HEIGHT = 50[source]
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]

Bases: reviewboard.attachments.mimetypes.TextMimetype

Handles ReStructuredText (.rst) mimetypes.

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

supported_mimetypes = [u'text/x-rst', u'text/rst'][source]
class MarkDownMimetype(attachment, mimetype)[source]

Bases: reviewboard.attachments.mimetypes.TextMimetype

Handle MarkDown (.md) mimetypes.

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

supported_mimetypes = [u'text/x-markdown', u'text/markdown'][source]