-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
163 lines (116 loc) · 4.99 KB
/
app.py
File metadata and controls
163 lines (116 loc) · 4.99 KB
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
import os
from dotenv import load_dotenv
from flask import Flask, jsonify, request
from flask_debugtoolbar import DebugToolbarExtension
from flask_cors import CORS
from models import db, connect_db, Image, EXIFData, Tag, ImageTag
from pixly_aws import upload_image_to_aws
from shortuuid import uuid
from image_processing import get_exif_data, make_thumbnail, convert_to_grayscale, resize_image
BUCKET_THUMBNAILS_FOLDER = 'pixly/images/thumbnails/'
BUCKET_ORIGINALS_FOLDER = 'pixly/images/originals/'
load_dotenv()
app = Flask(__name__)
CORS(app)
app.config['SQLALCHEMY_DATABASE_URI'] = (
os.environ['DATABASE_URL'].replace("postgres://", "postgresql://"))
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_ECHO'] = True
app.config['SECRET_KEY'] = os.environ['SECRET_KEY']
toolbar = DebugToolbarExtension(app)
# debug = DebugToolbarExtension(app)
connect_db(app)
db.create_all()
@app.route("/api/images")
def get_images():
""" grabs all images from the database and returns as json """
search_term = request.args.get("searchTerm") # search term or None
is_filtering_width = request.args.get('isFilteringWidth') == 'true'
is_filtering_height = request.args.get('isFilteringHeight') == 'true'
min_width = request.args.get('minWidth') # '' or an integer string e.g. '10'
min_width = int(min_width) if is_filtering_width and min_width.isnumeric() else 0
max_width = request.args.get('maxWidth') # '' or an integer string e.g. '10'
max_width = int(max_width) if is_filtering_width and max_width.isnumeric() else float('inf')
min_height = request.args.get('minHeight') # '' or an integer string e.g. '10'
min_height = int(min_height) if is_filtering_height and min_height.isnumeric() else 0
max_height = request.args.get('maxHeight') # '' or an integer string e.g. '10'
max_height = int(max_height) if is_filtering_height and max_height.isnumeric() else float('inf')
# Filter by EXIF data
images = db.session.query(Image).join(EXIFData)
images = images.filter(EXIFData.width_px >= min_width)
images = images.filter(EXIFData.width_px <= max_width)
images = images.filter(EXIFData.height_px >= min_height)
images = images.filter(EXIFData.height_px <= max_height)
if search_term:
images = images.filter(Image.title.ilike(f"%{search_term}%"))
images = images.order_by(Image.id).all()
serialized = [image.serialize() for image in images]
return jsonify(images=serialized)
@app.route("/api/images/<int:id>")
def get_image(id):
""" grabs an image from the database and returns as json """
image = Image.query.get_or_404(id)
image_exif_data = image.exif_data[0]
serialized_image = image.serialize()
serialized_exif_data = image_exif_data.serialize()
serialized_exif_data.pop("image_id")
serialized_image['exif_data'] = serialized_exif_data
return jsonify(image=serialized_image)
@app.post("/api/images")
def upload_image():
""" post route for uploading image from front end """
image_file = request.files.get('imgFile')
file_extension = image_file.filename.split('.')[-1]
file_name = f'img_{uuid()}.{file_extension}'
form_data = request.form
filter = form_data['filter'] # '', 'bw'
resize_percentage = int(form_data['resize']) # 100, 75, 50, 25
if filter == 'bw':
image_file = convert_to_grayscale(image_file)
if resize_percentage != 100:
image_file = resize_image(image_file, resize_percentage)
upload_image_status = upload_image_to_aws(
image_file,
BUCKET_ORIGINALS_FOLDER,
file_name
)
thumbnail_file = make_thumbnail(image_file)
upload_thumbnail_status = upload_image_to_aws(
thumbnail_file,
BUCKET_THUMBNAILS_FOLDER,
file_name
)
if not upload_image_status or not upload_thumbnail_status:
return (jsonify(error="File failed to upload."), 500)
image = Image(
file_name=file_name,
title=form_data["title"],
caption=form_data["caption"],
photographer=form_data["photographer"])
db.session.add(image)
db.session.commit()
image_id = image.id
image_data = get_exif_data(image_file)
image_exif_data = EXIFData(
image_id = image_id,
height_px = image_data['height_px'],
width_px = image_data['width_px'],
device_manufacturer = image_data['device_manufacturer'],
device_model = image_data['device_model'],
focal_length = image_data['focal_length'],
f_stop = image_data['f_stop'],
exposure = image_data['exposure'],
location = image_data['location'],
taken_at = image_data['taken_at'],
)
db.session.add(image_exif_data)
db.session.commit()
serialized = image.serialize()
return (jsonify(image = serialized),201)
@app.patch("/api/images/<int:id>")
def addView(id):
""" increments view count by 1 for image """
image = Image.query.get_or_404(id)
image.views += 1
db.session.commit()
return jsonify(views = image.views)