Exemplo n.º 1
0
def testfile_headers():
    test_headers = [
        # test_file, content_type
        ('imagefile.gif', 'image/gif'),
        ('testfile.txt', 'text/plain'),
        ('pdffile.pdf', 'application/pdf'),
        ('docfile.doc', 'application/msword'),
        (None, 'application/octet-stream'),
    ]

    for test_file, content_type in test_headers:
        result = wikiutil.file_headers(test_file, None, 10)
        expected = [('Content-Type', content_type), ('Content-Length', '10')]
        assert result == expected

    # filename is none and content type has a value
    result = wikiutil.file_headers(None, 'text/plain')
    expected = [('Content-Type', 'text/plain')]
    assert result == expected
Exemplo n.º 2
0
 def _do_get_modified(self, hash, force_attachment=False, mimetype=None):
     try:
         width = int(request.values.get('w'))
     except (TypeError, ValueError):
         width = None
     try:
         height = int(request.values.get('h'))
     except (TypeError, ValueError):
         height = None
     try:
         transpose = int(request.values.get('t'))
         assert 1 <= transpose <= 8
     except (TypeError, ValueError, AssertionError):
         transpose = 1
     if width or height or transpose != 1:
         # resize requested, XXX check ACL behaviour! XXX
         hash_name = HASH_ALGORITHM
         hash_hexdigest = self.rev.meta[hash_name]
         cid = cache_key(usage="ImageTransform",
                         hash_name=hash_name,
                         hash_hexdigest=hash_hexdigest,
                         width=width,
                         height=height,
                         transpose=transpose)
         c = app.cache.get(cid)
         if c is None:
             if mimetype:
                 content_type = mimetype
             else:
                 content_type = self.rev.meta[CONTENTTYPE]
             size = (width or 99999, height or 99999)
             content_type, data = self._transform(content_type,
                                                  size=size,
                                                  transpose_op=transpose)
             headers = wikiutil.file_headers(content_type=content_type,
                                             content_length=len(data))
             app.cache.set(cid, (headers, data))
         else:
             # XXX TODO check ACL behaviour
             headers, data = c
         return Response(data, headers=headers)
     else:
         return self._do_get(hash,
                             force_attachment=force_attachment,
                             mimetype=mimetype)
Exemplo n.º 3
0
    def _render_data_diff_raw(self, oldrev, newrev):
        hash_name = HASH_ALGORITHM
        cid = cache_key(usage="ImageDiff",
                        hash_name=hash_name,
                        hash_old=oldrev.meta[hash_name],
                        hash_new=newrev.meta[hash_name])
        c = app.cache.get(cid)
        if c is None:
            if PIL is None:
                abort(404)  # TODO render user friendly error image

            content_type = newrev.meta[CONTENTTYPE]
            if content_type == 'image/jpeg':
                output_type = 'JPEG'
            elif content_type == 'image/png':
                output_type = 'PNG'
            elif content_type == 'image/gif':
                output_type = 'GIF'
            else:
                raise ValueError(
                    "content_type {0!r} not supported".format(content_type))

            try:
                oldimage = PILImage.open(oldrev.data)
                newimage = PILImage.open(newrev.data)
                oldimage.load()
                newimage.load()
                diffimage = PILdiff(newimage, oldimage)
                outfile = StringIO()
                diffimage.save(outfile, output_type)
                data = outfile.getvalue()
                outfile.close()
                headers = wikiutil.file_headers(content_type=content_type,
                                                content_length=len(data))
                app.cache.set(cid, (headers, data))
            except (IOError, ValueError) as err:
                logging.exception("error during PILdiff: {0}".format(
                    err.message))
                abort(404)  # TODO render user friendly error image
        else:
            # XXX TODO check ACL behaviour
            headers, data = c
        return Response(data, headers=headers)
Exemplo n.º 4
0
 def _do_get_modified(self, hash, force_attachment=False, mimetype=None):
     try:
         width = int(request.values.get("w"))
     except (TypeError, ValueError):
         width = None
     try:
         height = int(request.values.get("h"))
     except (TypeError, ValueError):
         height = None
     try:
         transpose = int(request.values.get("t"))
         assert 1 <= transpose <= 8
     except (TypeError, ValueError, AssertionError):
         transpose = 1
     if width or height or transpose != 1:
         # resize requested, XXX check ACL behaviour! XXX
         hash_name = HASH_ALGORITHM
         hash_hexdigest = self.rev.meta[hash_name]
         cid = cache_key(
             usage="ImageTransform",
             hash_name=hash_name,
             hash_hexdigest=hash_hexdigest,
             width=width,
             height=height,
             transpose=transpose,
         )
         c = app.cache.get(cid)
         if c is None:
             if mimetype:
                 content_type = mimetype
             else:
                 content_type = self.rev.meta[CONTENTTYPE]
             size = (width or 99999, height or 99999)
             content_type, data = self._transform(content_type, size=size, transpose_op=transpose)
             headers = wikiutil.file_headers(content_type=content_type, content_length=len(data))
             app.cache.set(cid, (headers, data))
         else:
             # XXX TODO check ACL behaviour
             headers, data = c
         return Response(data, headers=headers)
     else:
         return self._do_get(hash, force_attachment=force_attachment, mimetype=mimetype)
Exemplo n.º 5
0
    def _render_data_diff_raw(self, oldrev, newrev):
        hash_name = HASH_ALGORITHM
        cid = cache_key(usage="ImageDiff",
                        hash_name=hash_name,
                        hash_old=oldrev.meta[hash_name],
                        hash_new=newrev.meta[hash_name])
        c = app.cache.get(cid)
        if c is None:
            if PIL is None:
                abort(404)  # TODO render user friendly error image

            content_type = newrev.meta[CONTENTTYPE]
            if content_type == 'image/jpeg':
                output_type = 'JPEG'
            elif content_type == 'image/png':
                output_type = 'PNG'
            elif content_type == 'image/gif':
                output_type = 'GIF'
            else:
                raise ValueError("content_type {0!r} not supported".format(content_type))

            try:
                oldimage = PILImage.open(oldrev.data)
                newimage = PILImage.open(newrev.data)
                oldimage.load()
                newimage.load()
                diffimage = PILdiff(newimage, oldimage)
                outfile = StringIO()
                diffimage.save(outfile, output_type)
                data = outfile.getvalue()
                outfile.close()
                headers = wikiutil.file_headers(content_type=content_type, content_length=len(data))
                app.cache.set(cid, (headers, data))
            except (IOError, ValueError) as err:
                logging.exception("error during PILdiff: {0}".format(err.message))
                abort(404)  # TODO render user friendly error image
        else:
            # XXX TODO check ACL behaviour
            headers, data = c
        return Response(data, headers=headers)