-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimages.py
268 lines (236 loc) · 8.19 KB
/
images.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
from datetime import datetime
import os
import shutil
from flask import (
abort,
flash,
Flask,
g,
redirect,
render_template,
request,
send_from_directory,
session,
)
from flask import url_for
from PIL import Image
from werkzeug.utils import secure_filename
import entities
import utils
IMAGE_FOLDER = "/var/www/photoserver/"
DEFAULT_IMAGES = {"H": "default_h.jpg", "V": "default_v.jpg"}
CREATE_DATE_KEY = 36867
def GET_list(orient=None, filt=None, clear=None, page_size=None):
orient = orient or "A"
try:
offset = int(orient)
orient = g.orient if hasattr(g, "orient") else "A"
except ValueError:
offset = 0
orient = "A"
orient_order = "HVSAH"
orient_pos = orient_order.index(orient)
g.orient = orient
g.next_orient = orient_order[orient_pos + 1]
if orient and orient != "A":
kwargs = {"orientation": orient}
else:
kwargs = {}
g.image_offset = offset or (g.image_offset if hasattr(g, "image_offset") else 0)
page_size = page_size or utils.DEFAULT_PAGE_SIZE
change = (-1 * page_size) if request.form.get("back") else page_size
kwargs["offset"] = g.image_offset
kwargs["page_size"] = page_size
kwargs["count"] = True
g.image_offset += change
if clear:
filt = ""
session.pop("keywords", "")
kwargs["keywords"] = ""
else:
filt = filt or session.get("keywords", "")
if filt:
kwargs["keywords"] = filt
session["keywords"] = g.keywords = filt
images = entities.Image.list(**kwargs)
g.image_count = len(images)
g.image_offset = max(min(g.image_offset, g.image_count), 0)
g.last_image = g.image_offset >= g.image_count
g.images = [img.to_dict() for img in images]
# list_db_images(orient=g.orient, filt=filt)
for img in g.images:
img["size"] = utils.human_fmt(img["size"])
g.thumb_path = os.path.join(IMAGE_FOLDER, "thumbs")
return render_template("image_list.html")
def show(pkid):
sql = "select * from image where pkid = %s"
with utils.DbCursor() as crs:
res = crs.execute(sql, (pkid,))
if not res:
abort(404)
g.kw_data = utils.all_keywords()
g.image = crs.fetchone()
g.image["size"] = utils.human_fmt(g.image["size"])
return render_template("image_detail.html")
def update_list():
rf = request.form
if rf["filter"]:
return GET_list(filt=rf["filter"], clear=rf.get("clear"))
sql = "update image set keywords = %s where pkid = %s;"
keys = rf.keys()
new_fields = [key for key in keys if key.startswith("key_")]
for new_field in new_fields:
pkid = new_field.split("_")[-1]
orig_field = "orig_%s" % pkid
new_val = rf.get(new_field)
orig_val = rf.get(orig_field)
if new_val == orig_val:
continue
with utils.DbCursor() as crs:
crs.execute(sql, (new_val, pkid))
return GET_list()
def update():
rf = request.form
if "delete" in rf:
return delete()
pkid = rf["pkid"]
name = rf["name"]
orig_name = rf["orig_name"]
keywords = rf["keywords"]
sql = """
update image set name = %s, keywords = %s
where pkid = %s; """
with utils.DbCursor() as crs:
crs.execute(sql, (name, keywords, pkid))
if name != orig_name:
_rename_image(orig_name, name)
return redirect(url_for("list_images"))
def _rename_image(orig_name, new_name):
fpath_orig = os.path.join(IMAGE_FOLDER, orig_name)
fpath_new = os.path.join(IMAGE_FOLDER, new_name)
shutil.move(fpath_orig, fpath_new)
# Move the thumbnail file, too
tpath_orig = os.path.join(IMAGE_FOLDER, "thumbs", orig_name)
tpath_new = os.path.join(IMAGE_FOLDER, "thumbs", new_name)
shutil.move(tpath_orig, tpath_new)
def delete(pkid=None):
if pkid is None:
# Form
pkid = request.form["pkid"]
with utils.DbCursor() as crs:
# Get the file name
sql = "select name from image where pkid = %s"
res = crs.execute(sql, (pkid,))
if not res:
abort(404)
fname = crs.fetchone()["name"]
sql = "delete from image where pkid = %s"
crs.execute(sql, (pkid,))
sql = "delete from album_image where image_id = %s"
crs.execute(sql, (pkid,))
# Now delete the file, if it is present
fpath = os.path.join(IMAGE_FOLDER, fname)
try:
os.unlink(fpath)
except OSError:
pass
return redirect(url_for("list_images"))
def upload_form():
return render_template("upload.html")
def isduplicate(name):
"""See if another file of the same name exists."""
sql = "select pkid from image where name = %s;"
with utils.DbCursor() as crs:
res = crs.execute(sql, (name,))
return bool(res)
def upload_thumb():
"""Used when another app has uploaded the main file to the cloud, and is
sending the thumb for local display.
"""
image = request.files["thumb_file"]
fname = request.form["filename"]
tpath = os.path.join(IMAGE_FOLDER, "thumbs", fname)
image.save(tpath)
return "OK"
def upload_file():
image = request.files["image_file"]
fname = secure_filename(image.filename)
# Make sure that there isn't another file by that name
if isduplicate(fname):
flash("Image already exists!!", "error")
return redirect(url_for("upload_image_form"))
fpath = os.path.join(IMAGE_FOLDER, fname)
image.save(fpath)
try:
img_obj = Image.open(fpath)
except IOError:
flash("Not a valid image", "err")
os.unlink(fpath)
return redirect(url_for("upload_image_form"))
imgtype = img_obj.format
orientation = utils.get_img_orientation(fpath)
width, height = img_obj.size
rf = request.form
keywords = rf["file_keywords"] or fname
size = os.stat(fpath).st_size
created = img_obj._getexif().get(CREATE_DATE_KEY)
updated = datetime.fromtimestamp(os.stat(fpath).st_ctime)
# Make a thumbnail
thumb_size = (120, 120)
img_obj.thumbnail(thumb_size)
thumb_path_parts = list(os.path.split(fpath))
thumb_path_parts.insert(-1, "thumbs")
thumb_path = os.path.join(*thumb_path_parts)
try:
img_obj.save(thumb_path, format=imgtype)
except Exception as e:
print("EXCEPTION", e)
# Save the info in the database
pkid = utils.gen_uuid()
sql = """
insert into image (pkid, keywords, name, orientation, width,
height, imgtype, size, created, updated)
values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s); """
vals = (pkid, keywords, fname, orientation, width, height, imgtype, size, created, updated)
with utils.DbCursor() as crs:
crs.execute(sql, vals)
return redirect(url_for("list_images"))
def download(img_name):
if img_name in DEFAULT_IMAGES.values():
directory = "."
else:
directory = IMAGE_FOLDER
return send_from_directory(directory, img_name)
def set_album():
album_name = request.form.get("album_name")
if not album_name:
abort(400, "No value for 'album_name' received")
image_name = request.form.get("image_name")
if not image_name:
abort(400, "No value for 'image_name' received")
with utils.DbCursor() as crs:
# Get the image
sql = "select pkid, orientation from image where name = %s"
crs.execute(sql, (image_name,))
image = crs.fetchone()
if not image:
abort(404, "Image %s not found" % image_name)
image_id = image["pkid"]
orientation = image["orientation"]
# Get the album (if it exists)
sql = "select pkid from album where name = %s"
crs.execute(sql, (album_name,))
album = crs.fetchone()
if album:
album_id = album["pkid"]
else:
# Create it
album_id = utils.gen_uuid()
sql = """insert into album (pkid, name, orientation)
values (%s, %s, %s); """
vals = (album_id, album_name, orientation)
crs.execute(sql, vals)
# Now add the image to the album
sql = "insert into album_image (album_id, image_id) values (%s, %s) ;"
crs.execute(sql, (album_id, image_id))
return "Success!"