-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_client.py
244 lines (196 loc) · 8.5 KB
/
image_client.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
import json
import requests, hmac
import time
import sys
import server_host_settings
from uuid import uuid4
import datetime
import logging
import os
from monitoring_tools import MonitoringTools
from datetime import datetime, timezone, timedelta
from urllib.parse import quote
TIME_FORMAT = "%Y-%m-%d %H:%M:%S%z"
class UploadFailureException(Exception):
pass
class DuplicateImageException(Exception):
pass
class DeleteFailureException(Exception):
pass
class FileNotFoundException(Exception):
pass
class ImageClient:
def __init__(self, config=None):
self.logger = logging.getLogger(f'Client.{self.__class__.__name__}')
self.ptc_timezone = timezone(timedelta(hours=-8), name="PST")
self.update_time_delta()
self.config = config
if config.MAILING_LIST:
if hasattr(config, 'ACTIVE_REPORT_PATH'):
report_path = config.ACTIVE_REPORT_PATH
active = True
else:
report_path = config.REPORT_PATH
active = False
# dict to create report of image import
self.monitoring_dict = {}
self.monitoring_tools = MonitoringTools(config=config, report_path=report_path, active=active)
def split_filepath(self, filepath):
cur_filename = os.path.basename(filepath)
cur_file_ext = cur_filename.split(".")[-1]
return cur_filename, cur_file_ext
def build_url(self, endpoint):
host = server_host_settings.SERVER_NAME
port = server_host_settings.SERVER_PORT
prefix = server_host_settings.SERVER_PREFIX
return f"{prefix}://{host}:{port}/{endpoint}"
def update_time_delta_from_response(self, response):
global server_time_delta
try:
timestamp = response.headers['X-Timestamp']
except KeyError:
server_time_delta = 0
return
server_time_delta = int(timestamp) - int(time.time())
print(f"Updated server time delta to {server_time_delta}")
def get_timestamp(self):
"""Return an integer timestamp with one second resolution for
the current moment.
"""
return int(time.time()) + server_time_delta
def update_time_delta(self):
response = requests.get(self.build_url(""))
self.update_time_delta_from_response(response)
def generate_token(self, filename):
"""Generate the auth token for the given filename and timestamp. """
timestamp = self.get_timestamp()
# print(f"image client timestamp: {timestamp}", flush=True)
msg = str(timestamp).encode() + filename.encode()
mac = hmac.new(server_host_settings.SERVER_KEY.encode(), msg=msg, digestmod='md5')
return ':'.join((mac.hexdigest(), str(timestamp)))
def delete_from_image_server(self, attach_loc, collection):
data = {
'filename': attach_loc,
'coll': collection,
'token': self.generate_token(attach_loc),
}
url = self.build_url("filedelete")
self.logger.debug(f"Deleting {url} from server")
r = requests.post(url, data=data)
if r.status_code == 404:
raise FileNotFoundException
if r.status_code != 200:
print(f"Deletion failed, aborted: {r.status_code}:{r.text}")
raise DeleteFailureException
def upload_to_image_server(self, full_path, redacted, collection, original_path=None, id=None):
if full_path is None or redacted is None or collection is None:
errstring = f"Bad input failures to upload to image server: {full_path} {redacted} {collection}"
print(errstring, file=sys.stderr, flush=True)
raise UploadFailureException(errstring)
datetime_now = datetime.now(self.ptc_timezone)
local_filename = full_path
uuid = str(uuid4())
extension = local_filename.split(".")[-1]
attach_loc = uuid + "." + extension
if original_path is not None:
upload_path = original_path
else:
upload_path = full_path
data = {
'store': attach_loc,
'type': 'image',
'coll': collection,
'token': self.generate_token(attach_loc),
'original_filename': os.path.basename(local_filename),
'original_path': upload_path,
'redacted': str(redacted),
'notes': None,
'datetime': datetime_now.strftime(TIME_FORMAT)
}
files = {
'image': (attach_loc, open(local_filename, 'rb')),
}
url = self.build_url("fileupload")
self.logger.debug(f"Attempting upload of local converted file {local_filename} to {url}")
r = requests.post(url, files=files, data=data)
if id is None:
id = 'N/A'
if r.status_code != 200:
self.logger.error(f"FAIL - return code {r.status_code}. data: {data}")
if r.status_code == 409:
self.logger.error(f"Image already in server; skipping for {upload_path}")
raise DuplicateImageException
else:
self.logger.error(f"Image upload aborted: {r.status_code}:{r.text}")
if self.config.MAILING_LIST:
self.monitoring_dict = self.monitoring_tools.append_monitoring_dict(self.monitoring_dict, id,
original_path, False)
raise UploadFailureException
else:
params = {
'filename': attach_loc,
'coll': collection,
'type': 'image',
'token': self.generate_token(attach_loc)
}
r = requests.get(self.build_url("getfileref"), params=params)
url = r.text
assert r.status_code == 200
logging.info(f"Uploaded: {local_filename},{attach_loc},{url}")
logging.info("adding to image")
if self.config.MAILING_LIST:
self.monitoring_dict = self.monitoring_tools.append_monitoring_dict(self.monitoring_dict, id,
original_path, True)
self.logger.debug("Upload to file server complete")
return url, attach_loc
# works for just basename +ext. "exact" does a sql "like" operation
def check_image_db_if_filename_imported(self, collection, filename, exact=False):
params = {
'file_string': quote(filename),
'coll': collection,
'exact': exact,
'search_type': 'filename',
'token': self.generate_token(quote(filename))
}
return self.decode_response(params)
def write_exif_image_metadata(self, exif_dict, collection, filename):
data = {'filename': filename,
'coll': collection,
'exif_dict': json.dumps(exif_dict),
'token': self.generate_token(filename)
}
url = self.build_url('updateexifdata')
response = requests.post(url, data=data)
if response.status_code == 200:
self.logger.debug(f"EXIF data for '{filename}' updated successfully.")
else:
self.logger.error(
f"Failed to update EXIF data for '{filename}': {response.status_code} - {response.text}")
self.logger.debug(f"modifying EXIF data for {filename} complete")
def read_exif_image_data(self, collection, filename, datatype):
params = {'filename': filename,
'coll': collection,
'dt': datatype,
'search_type': 'filename',
'token': self.generate_token(filename)
}
url = self.build_url("getexifdata")
response = requests.get(url=url, params=params)
if response.status_code == 200:
return response.json()
else:
return None
def decode_response(self, params):
url = self.build_url("getImageRecord")
r = requests.get(url, params=params)
if r.status_code == 404:
self.logger.debug(f"Checked {params['file_string']} and found no duplicates")
return False
if r.status_code == 200:
self.logger.debug(f"Checked {params['file_string']} - already imported")
return True
if r.status_code == 500:
self.logger.error(f"500: Internal server error checking {params['file_string']}")
self.logger.error(f"URL: {url}")
assert False
assert False