Ticket #9058: configure-preview-limit-by-mimetype.diff

File configure-preview-limit-by-mimetype.diff, 2.1 KB (added by nielx, 8 years ago)

Test

  • trac/mimeview/api.py

     
    633633        and a Python regexp used for matching filenames, separated by a colon
    634634        (":"). (''since 1.0'')""")
    635635
     636    preview_limit_map = ListOption('mimeviewer', 'preview_limit_map',
     637        'text/x-diff:262144,text/x-rst:262144',
     638        doc="""List of specific preview limits per MIME type. (''local extension'')""")
     639
    636640    treat_as_binary = ListOption('mimeviewer', 'treat_as_binary',
    637641        'application/octet-stream, application/pdf, application/postscript, '
    638642        'application/msword,application/rtf,',
     
    642646    def __init__(self):
    643647        self._mime_map = None
    644648        self._mime_map_patterns = None
     649        self._preview_limit_map = None
    645650
    646651    # Public API
    647652
     
    909914                        self._mime_map[keyword] = assocations[0]
    910915        return self._mime_map
    911916
     917    @property
     918    def preview_limit_map(self):
     919        # fill preview limit map from configuration
     920        if not self._preview_limit_map:
     921            self._preview_limit_map = {}
     922            for mapping in self.config['mimeviewer'].getlist('preview_limit_map'):
     923                if ':' in mapping:
     924                    values = mapping.split(':')
     925                    self._preview_limit_map[values[0]] = int(values[1])
     926        return self._preview_limit_map
     927
    912928    def get_mimetype(self, filename, content=None):
    913929        """Infer the MIME type from the `filename` or the `content`.
    914930
     
    9981014                'max_file_size_reached': False,
    9991015                'rendered': None,
    10001016                }
    1001         if length >= self.max_preview_size:
     1017        preview_limit = self.preview_limit_map.get(ct_mimetype(mimetype),
     1018                                                   self.max_preview_size)
     1019        if length >= preview_limit:
     1020            data['max_file_size'] = preview_limit
    10021021            data['max_file_size_reached'] = True
    10031022        else:
    10041023            result = self.render(context, mimetype, content, filename, url,