Skip to content

Commit f69f071

Browse files
author
Mike Taylor
authored
Merge pull request #1266 from karlcow/722/3
Fixes #722 - Handle two sizes images
2 parents 22967c9 + 1f8d058 commit f69f071

File tree

3 files changed

+46
-23
lines changed

3 files changed

+46
-23
lines changed

webcompat/api/uploads.py

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77
'''Flask Blueprint for image uploads.'''
88

99
import base64
10+
import datetime
11+
import io
1012
import json
1113
import os
1214
import re
15+
import uuid
1316

14-
from datetime import date
1517
from flask import abort
1618
from flask import Blueprint
1719
from flask import request
18-
from io import BytesIO
1920
from PIL import Image
2021
from werkzeug.datastructures import FileStorage
2122
from werkzeug.exceptions import RequestEntityTooLarge
22-
from uuid import uuid4
2323

2424
from webcompat import app
2525

@@ -40,7 +40,16 @@ class Upload(object):
4040

4141
def __init__(self, imagedata):
4242
self.image_object = self.to_image_object(imagedata)
43+
# computing the parameters to be used
44+
today = datetime.date.today()
45+
self.year = str(today.year)
46+
self.month = str(today.month)
47+
self.image_id = str(uuid.uuid4())
4348
self.file_ext = self.get_file_ext()
49+
self.image_path = self.img_path(self.month, self.year, self.image_id,
50+
thumb=False)
51+
self.thumb_path = self.img_path(self.month, self.year, self.image_id,
52+
thumb=True)
4453

4554
def to_image_object(self, imagedata):
4655
'''Method to return a Pillow Image object from the raw imagedata.'''
@@ -53,12 +62,20 @@ def to_image_object(self, imagedata):
5362
imagedata.startswith('data:image/')):
5463
# Chop off 'data:image/.+;base64,' before decoding
5564
imagedata = re.sub('^data:image/.+;base64,', '', imagedata)
56-
return Image.open(BytesIO(base64.b64decode(imagedata)))
65+
return Image.open(io.BytesIO(base64.b64decode(imagedata)))
5766
raise TypeError('TypeError: Not a valid image format')
5867
except TypeError:
5968
# Not a valid format
6069
abort(415)
6170

71+
def img_path(self, month, year, image_id, thumb=False):
72+
'''Return the right image path.'''
73+
thumb_string = ''
74+
if thumb:
75+
thumb_string = '-thumb'
76+
image_name = image_id + thumb_string + '.' + self.file_ext
77+
return os.path.join(year, month, image_name)
78+
6279
def get_file_ext(self):
6380
'''Method to return the file extension, as determined by Pillow.
6481
@@ -68,24 +85,22 @@ def get_file_ext(self):
6885
return 'jpg'
6986
return self.image_object.format.lower()
7087

71-
def get_filename(self):
88+
def get_filename(self, image_path):
7289
'''Method to return the uploaded filename (with extension).'''
73-
return self.get_url().split('/')[-1]
90+
return self.get_url(image_path).split('/')[-1]
7491

75-
def get_url(self):
92+
def get_url(self, image_path):
7693
'''Method to return a URL for the uploaded file.'''
77-
return app.config['UPLOADS_DEFAULT_URL'] + self.file_path
94+
return app.config['UPLOADS_DEFAULT_URL'] + image_path
7895

7996
def save(self):
8097
'''Check that the file is allowed, then save to filesystem.'''
8198
save_parameters = {}
8299
if self.file_ext not in self.ALLOWED_FORMATS:
83100
raise TypeError('Image file format not allowed')
84-
85-
today = date.today()
86-
self.file_path = os.path.join(str(today.year), str(today.month),
87-
str(uuid4()) + '.' + self.file_ext)
88-
file_dest = app.config['UPLOADS_DEFAULT_DEST'] + self.file_path
101+
# Paths of the images
102+
file_dest = app.config['UPLOADS_DEFAULT_DEST'] + self.image_path
103+
thumb_dest = app.config['UPLOADS_DEFAULT_DEST'] + self.thumb_path
89104
dest_dir = os.path.dirname(file_dest)
90105
if not os.path.exists(dest_dir):
91106
os.makedirs(dest_dir)
@@ -100,6 +115,10 @@ def save(self):
100115
save_parameters['save_all'] = True
101116
# unpacking save_parameters
102117
self.image_object.save(file_dest, **save_parameters)
118+
# Creating the thumbnail
119+
size = (700, 700)
120+
self.image_object.thumbnail(size, Image.BILINEAR)
121+
self.image_object.save(thumb_dest, **save_parameters)
103122

104123

105124
@uploads.route('/', methods=['POST'])
@@ -123,8 +142,9 @@ def upload():
123142
upload = Upload(imagedata)
124143
upload.save()
125144
data = {
126-
'filename': upload.get_filename(),
127-
'url': upload.get_url()
145+
'filename': upload.get_filename(upload.image_path),
146+
'url': upload.get_url(upload.image_path),
147+
'thumb_url': upload.get_url(upload.thumb_path)
128148
}
129149
return (json.dumps(data), 201, {'content-type': JSON_MIME})
130150
except (TypeError, IOError):

webcompat/static/js/lib/bugform.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -425,11 +425,13 @@ function BugForm() {
425425
this.uploadField.val(this.uploadField.get(0).defaultValue);
426426
};
427427
/*
428-
copy over the URL of a newly uploaded image asset to the bug
429-
description textarea.
428+
create the markdown with the URL of a newly uploaded image
429+
and its thumbnail URL assets to the bug description textarea.
430430
*/
431-
this.addImageURL = function(url) {
432-
var imageURL = ['![Screenshot Description](', url, ')'].join('');
431+
this.addImageURL = function(response) {
432+
var img_url = response.url;
433+
var thumb_url = response.thumb_url;
434+
var imageURL = ['[![Screenshot Description](', thumb_url, ')](', img_url, ')'].join('');
433435
this.descField.val(function(idx, value) {
434436
return value + '\n\n' + imageURL;
435437
});

webcompat/static/js/lib/issues.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,14 @@ issues.ImageUploadView = Backbone.View.extend({
199199
var DELIMITER = '\n\n';
200200
var textarea = $('.js-Comment-text');
201201
var textareaVal = textarea.val();
202-
var imageURL = _.template('![Screenshot of the site issue](<%= url %>)');
203-
var compiledImageURL = imageURL({url: response.url});
202+
var img_url = response.url;
203+
var thumb_url = response.thumb_url;
204+
var imageURL = ['[![Screenshot Description](', thumb_url, ')](', img_url, ')'].join('');
204205

205206
if (!$.trim(textareaVal)) {
206-
textarea.val(compiledImageURL);
207+
textarea.val(imageURL);
207208
} else {
208-
textarea.val(textareaVal + DELIMITER + compiledImageURL);
209+
textarea.val(textareaVal + DELIMITER + imageURL);
209210
}
210211
},
211212
// Adapted from bugform.js

0 commit comments

Comments
 (0)