-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
192 lines (151 loc) · 5.37 KB
/
main.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
import os
from io import BytesIO
from flask import Flask, abort, jsonify, request, make_response
from werkzeug.http import generate_etag
from api import cloud, photo
from imagesize import get as get_size
from api.helpers import get_exif, latinize, push_message
from api.config import CONFIG
app = Flask(__name__)
@app.route('/_ah/warmup')
def warmup():
return 'warmup', 200, {}
@app.route('/api/<verb>/bucket_info', methods=['GET', 'PUT'])
def bucket_info(verb):
"""
verb: get|add|del|set
"""
if request.method == 'GET':
param = {'verb': verb} # get|set
else:
param = request.json
return jsonify(cloud.bucketInfo(param))
@app.route('/api/download/<filename>', methods=['GET'])
def download(filename):
blob = photo.storage_blob(filename)
if blob:
data = blob.download_as_bytes()
response = make_response(data)
response.headers['Content-Type'] = blob.content_type
response.headers['Content-Disposition'] = f'attachment; filename={filename}'
return response
abort(404, description='Resource not found')
@app.route('/api/exif/<filename>', methods=['GET'])
def exif(filename):
blob = photo.storage_blob(filename)
if blob:
buff = blob.download_as_bytes()
out = get_exif(buff)
out['date'] = out['date'].strftime(CONFIG['date_time_format'])
try:
out['dim'] = list(get_size(BytesIO(buff)))
except:
pass
return out
abort(404, description='Resource not found')
@app.route('/api/counters', methods=['GET'])
def collection():
return jsonify(photo.counters_stat())
@app.route('/api/search', methods=['GET'])
def search():
page = None
per_page = None
filters = []
# Return an iterator of (key, value) pairs.
params = request.args.items(multi=True)
for key, val in params:
if key in ('_page', 'per_page', 'text', 'tags', 'year', 'month', 'day', 'model', 'lens', 'nick') and val:
if key == '_page':
page = val
elif key == 'per_page':
per_page = int(val)
elif key == 'year' or key == 'month' or key == 'day':
filters.append((key, '=', int(val)))
elif key == 'text':
filters.append((key, '=', latinize(val)))
else:
filters.append((key, '=', val))
objects, token, error = photo.results(filters, page, per_page)
return jsonify({
'objects': objects,
'_page': page if page else 'FP',
'_next': token,
'error': error
})
@app.route('/api/user', methods=['POST'])
def user():
json_ = request.get_json(silent=True)
assert json_ is not None, 'Cannot get user'
uid = cloud.login_user(json_['user'])
return jsonify({"success": uid is not None})
@app.route('/api/user/register', methods=['PUT'])
def update_user():
json_ = request.get_json(silent=True)
assert json_ is not None, 'Cannot get token'
uid = json_['uid']
token = json_['token']
changed = cloud.register_user(uid, token)
return jsonify({"changed": changed})
@app.route('/api/registrations', methods=['GET'])
def registrations():
return jsonify(cloud.registrations())
@app.route('/api/add', methods=['POST'])
def add():
""" ImmutableMultiDict([('photos', <FileStorage: u'selo.jpg' ('image/jpeg')>), ...]) """
# token = request.form.get('token')
# files = request.files.getlist('photos')
file = request.files.get('photos')
response = photo.add(file)
return jsonify(response)
@app.route('/api/edit', defaults={'id_': None}, methods=['PUT'])
@app.route('/api/edit/<int:id_>', methods=['PUT'])
def edit(id_):
response = photo.edit(id_, request.json)
return jsonify(response)
@app.route('/api/remove/<filename>', methods=['DELETE'])
def remove(filename):
""" Remove unpublished photo """
response = photo.remove_from_bucket(filename)
return jsonify(response)
@app.route('/api/delete/<int:id_>', methods=['DELETE'])
def delete(id_):
response = photo.remove(id_)
return jsonify(response['success'])
@app.route('/api/<verb>', methods=['POST'])
def runner(verb):
json_ = request.get_json(silent=True)
assert json_ is not None, 'Cannot get token'
token = json_['token']
job = None
if verb == 'fix':
job = cloud.Fixer()
elif verb == 'repair':
job = cloud.Repair()
if job:
job.TOKEN = token
job.run()
return jsonify(True)
else:
return jsonify(False)
@app.route('/api/<verb>/<field>', methods=['POST'])
def rebuilder(verb, field):
json_ = request.get_json(silent=True)
assert json_ is not None, 'Cannot get token'
token = json_['token']
if field and verb == 'rebuild':
tally = cloud.rebuilder(field, token)
return jsonify(tally)
else:
return jsonify(False)
@app.route('/api/message/send', methods=['POST'])
def push():
json_ = request.get_json(silent=True)
assert json_ is not None, 'Cannot get token'
recipients = json_['recipients']
message = json_['message']
return push_message(recipients, message)
if __name__ == '__main__':
# This is used when running locally only. When deploying to Google App
# Engine, a webserver process such as Gunicorn will serve the app. This
# can be configured by adding an `entrypoint` to app.yaml.
app.run(host='127.0.0.1', port=6060, debug=True)