-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
142 lines (118 loc) · 5.3 KB
/
api.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
import os
from flask import request, jsonify
from werkzeug.utils import secure_filename
from app import app, PLOTS_FOLDER
from functions import process_images, allowed_file
@app.route('/api/list_filenames', methods=['GET'])
def list_filenames():
"""
Get request to list all filenames in a particular folder
"""
# Query ?folder_name=your_folder_name
folder_name = request.args.get('folder_name', None)
if not folder_name:
app.logger.warning('No folder_name provided')
return jsonify({'reason': 'Must provide folder_name as ?folder_name=your_folder_name'}), 400
else:
image_folder = os.path.join(app.config['UPLOAD_FOLDER'], folder_name)
# check if folder exists
if not os.path.exists(image_folder):
return jsonify({'reason': 'folder_name %s does not exist' % folder_name}), 400
files = os.listdir(image_folder)
json_data = {'args': ['--images', image_folder + "/*", '--out_dir', PLOTS_FOLDER + '/' + folder_name]}
return jsonify({
'folder_name': folder_name,
'filenames': files,
'create_pixplot_post_info': {
'url': request.url_root + 'api/pixplot',
'json': json_data,
'images_folder': image_folder,
'plot_folder_root': PLOTS_FOLDER,
},
}), 200
@app.route('/api/send_photo', methods=['POST'])
def upload_photo_api():
"""
Upload a single photo to a specific folder
data = {'folder_name': 'desired_name'}
files = {'image': open('image_1.jpg', 'rb')}
"""
folder_name = request.form.get('folder_name', None)
if not folder_name:
app.logger.warning('No folder_name provided')
return jsonify({'reason': 'Must provide folder_name for single images'}), 400
image_folder = os.path.join(app.config['UPLOAD_FOLDER'], folder_name)
# Make folder if it does not already exist
if not os.path.isdir(image_folder):
os.mkdir(image_folder)
image = request.files.get('image', None)
if image and allowed_file(image.filename):
filename = secure_filename(image.filename)
app.logger.info(f'Uploading {filename} to {folder_name}')
image.save(os.path.join(image_folder, filename))
json_data = {'args': ['--images', image_folder + "/*", '--out_dir', PLOTS_FOLDER + '/' + folder_name]}
response = {
'upload_location': request.url_root + 'uploads/' + folder_name,
'folder_name': folder_name,
'create_pixplot_post_info': {
'url': request.url_root + 'api/pixplot',
'json': json_data,
'images_folder': image_folder,
'plot_folder_root': PLOTS_FOLDER,
},
}
return response, 200
else:
app.logger.warning('No image received')
return jsonify({'reason': 'No image received'}), 400
@app.route('/api/send_metadata', methods=['POST'])
def upload_metadata_api():
"""
Upload the metadata.csv file to a specific folder
data = {'folder_name': 'desired_name'}
files = {'metadata': open('metadata.csv'), 'rb')}
"""
folder_name = request.form.get('folder_name', None)
if not folder_name:
app.logger.warning('No folder_name provided')
return jsonify({'reason': 'Must provide folder_name for single images'}), 400
image_folder = os.path.join(app.config['UPLOAD_FOLDER'], folder_name)
# Make folder if it does not already exist
if not os.path.isdir(image_folder):
os.mkdir(image_folder)
metadata = request.files.get('metadata', None)
if metadata and allowed_file(metadata.filename, {'csv'}):
metadata_file_path = os.path.join(image_folder, 'metadata.csv')
metadata.save(metadata_file_path)
json_data = {'args': ['--images', image_folder + "/*", '--out_dir', PLOTS_FOLDER + '/' + folder_name,
'--metadata', metadata_file_path]}
response = {
'upload_location': request.url_root + 'uploads/' + folder_name,
'folder_name': folder_name,
'create_pixplot_post_info': {
'url': request.url_root + 'api/pixplot',
'json': json_data,
'images_folder': image_folder,
'plot_folder_root': PLOTS_FOLDER,
'metadata_filepath': metadata_file_path,
},
}
return response, 200
else:
app.logger.warning('No metadata received')
return jsonify({'reason': 'No metadata received'}), 400
@app.route('/api/send_photos', methods=['POST'])
def upload_photos_api():
"""
Upload multiple photos (and metadata if provided) to folder.
If no folder_name provided, a random one will be generated.
files = [
('metadata', open('metadata.csv'), 'rb')),
('images', open('image_1.jpg', 'rb')),
('images', open('image_2.jpg', 'rb')),
]
Optional:
data = {'folder_name' : 'desired_name'}
"""
result_response, status_code = process_images(request)
return jsonify(result_response)