[extensions-web] Support viewing of images and SVG files in the review view.
- From: Jasper St. Pierre <jstpierre src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [extensions-web] Support viewing of images and SVG files in the review view.
- Date: Fri, 30 Sep 2011 17:40:03 +0000 (UTC)
commit c18e6e36c886c65bc1fc0f56cf733242e5a96abc
Author: Jasper St. Pierre <jstpierre mecheye net>
Date: Tue Sep 27 12:27:50 2011 -0400
Support viewing of images and SVG files in the review view.
sweettooth/review/views.py | 44 +++++++++++++++++++++++++++++++++-----------
1 files changed, 33 insertions(+), 11 deletions(-)
---
diff --git a/sweettooth/review/views.py b/sweettooth/review/views.py
index ef98073..16ac94f 100644
--- a/sweettooth/review/views.py
+++ b/sweettooth/review/views.py
@@ -4,6 +4,9 @@ try:
except ImportError:
import simplejson as json
+import base64
+import os.path
+
import pygments
import pygments.util
import pygments.lexers
@@ -23,6 +26,15 @@ from django.views.generic.detail import SingleObjectMixin
from review.models import CodeReview, ChangeStatusLog, get_all_reviewers
from extensions import models
+IMAGE_TYPES = {
+ '.png': 'image/png',
+ '.jpg': 'image/jpeg',
+ '.jpeg': 'image/jpeg',
+ '.gif': 'image/gif',
+ '.bmp': 'image/bmp',
+ '.svg': 'image/svg+xml',
+}
+
def can_review_extension(user, extension):
if user == extension.creator:
return True
@@ -57,17 +69,27 @@ class AjaxGetFilesView(SingleObjectMixin, View):
for filename in zipfile.namelist():
raw = zipfile.open(filename, 'r').read()
- try:
- lexer = pygments.lexers.guess_lexer_for_filename(filename, raw)
- except pygments.util.ClassNotFound:
- # released pygments doesn't yet have .json
- # so hack around it here.
- if filename.endswith('.json'):
- lexer = pygments.lexers.get_lexer_by_name('js')
- else:
- lexer = pygments.lexers.get_lexer_by_name('text')
-
- html = pygments.highlight(raw, lexer, self.formatter)
+ base, extension = os.path.splitext(filename)
+
+ if extension in IMAGE_TYPES:
+ mime = IMAGE_TYPES[extension]
+ raw_base64 = base64.standard_b64encode(raw)
+
+ html = '<img src="data:%s;base64,%s">' % (mime, raw_base64,)
+
+ else:
+ try:
+ lexer = pygments.lexers.guess_lexer_for_filename(filename, raw)
+ except pygments.util.ClassNotFound:
+ # released pygments doesn't yet have .json
+ # so hack around it here.
+ if filename.endswith('.json'):
+ lexer = pygments.lexers.get_lexer_by_name('js')
+ else:
+ lexer = pygments.lexers.get_lexer_by_name('text')
+
+ html = pygments.highlight(raw, lexer, self.formatter)
+
files.append(dict(filename=filename, raw=raw, html=html))
return HttpResponse(mark_safe(json.dumps(files)),
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]