7
7
'''Flask Blueprint for image uploads.'''
8
8
9
9
import base64
10
+ import datetime
11
+ import io
10
12
import json
11
13
import os
12
14
import re
15
+ import uuid
13
16
14
- from datetime import date
15
17
from flask import abort
16
18
from flask import Blueprint
17
19
from flask import request
18
- from io import BytesIO
19
20
from PIL import Image
20
21
from werkzeug .datastructures import FileStorage
21
22
from werkzeug .exceptions import RequestEntityTooLarge
22
- from uuid import uuid4
23
23
24
24
from webcompat import app
25
25
@@ -40,7 +40,16 @@ class Upload(object):
40
40
41
41
def __init__ (self , imagedata ):
42
42
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 ())
43
48
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 )
44
53
45
54
def to_image_object (self , imagedata ):
46
55
'''Method to return a Pillow Image object from the raw imagedata.'''
@@ -53,12 +62,20 @@ def to_image_object(self, imagedata):
53
62
imagedata .startswith ('data:image/' )):
54
63
# Chop off 'data:image/.+;base64,' before decoding
55
64
imagedata = re .sub ('^data:image/.+;base64,' , '' , imagedata )
56
- return Image .open (BytesIO (base64 .b64decode (imagedata )))
65
+ return Image .open (io . BytesIO (base64 .b64decode (imagedata )))
57
66
raise TypeError ('TypeError: Not a valid image format' )
58
67
except TypeError :
59
68
# Not a valid format
60
69
abort (415 )
61
70
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
+
62
79
def get_file_ext (self ):
63
80
'''Method to return the file extension, as determined by Pillow.
64
81
@@ -68,24 +85,22 @@ def get_file_ext(self):
68
85
return 'jpg'
69
86
return self .image_object .format .lower ()
70
87
71
- def get_filename (self ):
88
+ def get_filename (self , image_path ):
72
89
'''Method to return the uploaded filename (with extension).'''
73
- return self .get_url ().split ('/' )[- 1 ]
90
+ return self .get_url (image_path ).split ('/' )[- 1 ]
74
91
75
- def get_url (self ):
92
+ def get_url (self , image_path ):
76
93
'''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
78
95
79
96
def save (self ):
80
97
'''Check that the file is allowed, then save to filesystem.'''
81
98
save_parameters = {}
82
99
if self .file_ext not in self .ALLOWED_FORMATS :
83
100
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
89
104
dest_dir = os .path .dirname (file_dest )
90
105
if not os .path .exists (dest_dir ):
91
106
os .makedirs (dest_dir )
@@ -100,6 +115,10 @@ def save(self):
100
115
save_parameters ['save_all' ] = True
101
116
# unpacking save_parameters
102
117
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 )
103
122
104
123
105
124
@uploads .route ('/' , methods = ['POST' ])
@@ -123,8 +142,9 @@ def upload():
123
142
upload = Upload (imagedata )
124
143
upload .save ()
125
144
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 )
128
148
}
129
149
return (json .dumps (data ), 201 , {'content-type' : JSON_MIME })
130
150
except (TypeError , IOError ):
0 commit comments