forked from specify/web-asset-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_db.py
333 lines (282 loc) · 12.6 KB
/
image_db.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import mysql.connector
from mysql.connector import errorcode
from retrying import retry
import settings
from datetime import datetime
import logging
TIME_FORMAT_NO_OFFESET = "%Y-%m-%d %H:%M:%S"
TIME_FORMAT = TIME_FORMAT_NO_OFFESET + "%z"
class ImageDb():
def __init__(self):
self.cnx = None
def log(self, msg):
if settings.DEBUG_APP:
print(msg)
def retry_if_operational_error(exception):
pass
@retry(retry_on_exception=lambda e: isinstance(e, mysql.connector.OperationalError), stop_max_attempt_number=3,
wait_exponential_multiplier=2)
def get_cursor(self):
try:
if self.cnx is None:
self.connect()
return self.cnx.cursor(buffered=True)
except mysql.connector.OperationalError as e:
logging.warning("Failed to connect, resetting DB connection and sleeping")
self.reset_connection()
raise e
def reset_connection(self):
self.log(f"Resetting connection to {settings.SQL_HOST}")
if self.cnx:
try:
self.cnx.close()
except Exception:
pass
self.connect()
def connect(self):
try:
self.cnx = mysql.connector.connect(user=settings.SQL_USER,
password=settings.SQL_PASSWORD,
host=settings.SQL_HOST,
port=settings.SQL_PORT,
database=settings.SQL_DATABASE)
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
self.log(
f"SQL: Access denied to image server database. host: {settings.SQL_HOST} user: {settings.SQL_USER}")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
self.log("Database does not exist")
else:
self.log(err)
return False
except Exception as ex:
self.log(f"Unknown error:{ex}")
return False
self.log("Db connected")
return True
def create_tables(self):
TABLES = {}
TABLES['images'] = (
"CREATE TABLE if not exists `images`.`images` ("
" id int NOT NULL AUTO_INCREMENT primary key,"
" `original_filename` varchar(2000),"
" `url` varchar(500),"
" `universal_url` varchar(500),"
" `original_path` varchar(2000),"
" `redacted` BOOLEAN,"
" `internal_filename` varchar(500),"
" `notes` varchar(8192),"
" `datetime` datetime,"
" `collection` varchar(50)"
") ENGINE=InnoDB")
cursor = self.get_cursor()
for table_name in TABLES:
table_description = TABLES[table_name]
try:
self.log(f"Creating table {table_name}...")
self.log(f"Sql: {TABLES[table_name]}")
cursor.execute(table_description)
except mysql.connector.Error as err:
if err.errno == errorcode.ER_TABLE_EXISTS_ERROR:
self.log("already exists.")
else:
self.log(err.msg)
else:
self.log("OK")
cursor.close()
cursor = self.get_cursor()
cursor.execute(
"SELECT COUNT(*) FROM information_schema.columns WHERE table_name = 'images' AND column_name = 'orig_md5'")
column_exists = cursor.fetchone()[0]
if not column_exists:
# Add the "orig_md5" column to the "images" table
cursor.execute("ALTER TABLE images ADD COLUMN orig_md5 CHAR(32)")
cursor.close()
def create_image_record(self,
original_filename,
url,
internal_filename,
collection,
original_path,
notes,
redacted,
datetime_record,
original_image_md5
):
cursor = self.get_cursor()
if original_filename is None:
original_filename = "NULL"
if original_image_md5 is None:
original_image_md5 = "NULL"
add_image = (f"""INSERT INTO images
(original_filename, url, universal_url, internal_filename, collection,original_path,notes,redacted,datetime,orig_md5)
values (
"{original_filename}",
"{url}",
NULL,
"{internal_filename}",
"{collection}",
"{original_path}",
"{notes}",
"{int(redacted)}",
"{datetime_record.strftime(TIME_FORMAT_NO_OFFESET)}",
"{original_image_md5}")""")
self.log(f"Inserting imageInserting image record. SQL: {add_image}")
cursor.execute(add_image)
self.cnx.commit()
cursor.close()
@retry(retry_on_exception=lambda e: isinstance(e, Exception), stop_max_attempt_number=3)
def update_redacted(self, internal_filename, is_redacted):
sql = f"""
update images set redacted = {is_redacted} where internal_filename = '{internal_filename}'
"""
logging.debug(f"update redacted: {sql}")
cursor = self.get_cursor()
cursor.execute(sql)
self.cnx.commit()
cursor.close()
def get_record(self, where_clause):
cursor = self.get_cursor()
query = f"""SELECT id, original_filename, url, universal_url, internal_filename, collection,original_path, notes, redacted, datetime, orig_md5
FROM images
{where_clause}"""
cursor.execute(query)
record_list = []
for (
id, original_filename, url, universal_url, internal_filename, collection, original_path, notes,
redacted, datetime_record, orig_md5) in cursor:
record_list.append({'id': id,
'original_filename': original_filename,
'url': url,
'universal_url': universal_url,
'internal_filename': internal_filename,
'collection': collection,
'original_path': original_path,
'notes': notes,
'redacted': redacted,
'datetime': datetime.strptime(datetime_record, TIME_FORMAT),
'orig_md5': orig_md5
})
cursor.close()
return record_list
def get_image_record_by_internal_filename(self, internal_filename):
cursor = self.get_cursor()
query = f"""SELECT id, original_filename, url, universal_url, internal_filename, collection,original_path, notes, redacted, datetime, orig_md5
FROM images
WHERE internal_filename = '{internal_filename}'"""
cursor.execute(query)
record_list = []
for (id,
original_filename,
url,
universal_url,
internal_filename,
collection,
original_path,
notes,
redacted,
datetime_record,
orig_md5) in cursor:
record_list.append({'id': id,
'original_filename': original_filename,
'url': url,
'universal_url': universal_url,
'internal_filename': internal_filename,
'collection': collection,
'original_path': original_path,
'notes': notes,
'redacted': redacted,
'datetime': datetime_record.strftime(TIME_FORMAT),
'orig_md5': orig_md5
})
cursor.close()
return record_list
def get_image_record_by_pattern(self, pattern, column, exact, collection):
cursor = self.get_cursor()
if exact:
query = f"""SELECT id, original_filename, url, universal_url, internal_filename, collection,original_path, notes, redacted, datetime, orig_md5
FROM images
WHERE {column} = '{pattern}'"""
else:
query = f"""SELECT id, original_filename, url, universal_url, internal_filename, collection,original_path, notes, redacted, datetime, orig_md5
FROM images
WHERE {column} LIKE '{pattern}'"""
if collection is not None:
query += f""" AND collection = '{collection}'"""
self.log(f"Query get_image_record_by_{column}: {query}")
cursor.execute(query)
record_list = []
for (
id, original_filename, url, universal_url, internal_filename, collection, original_path, notes,
redacted, datetime_record, orig_md5) in cursor:
record_list.append({'id': id,
'original_filename': original_filename,
'url': url,
'universal_url': universal_url,
'internal_filename': internal_filename,
'collection': collection,
'original_path': original_path,
'notes': notes,
'redacted': redacted,
'datetime': datetime_record,
'orig_md5': orig_md5
})
self.log(f"Found at least one record: {record_list[-1]}")
cursor.close()
return record_list
def get_image_record_by_original_path(self, original_path, exact, collection):
record_list = self.get_image_record_by_pattern(original_path, 'original_path', exact, collection)
return record_list
def get_image_record_by_original_filename(self, original_filename, exact, collection):
record_list = self.get_image_record_by_pattern(original_filename, 'original_filename', exact, collection)
return record_list
def get_image_record_by_original_image_md5(self, md5, collection):
record_list = self.get_image_record_by_pattern(md5, 'orig_md5', True, collection)
return record_list
def delete_image_record(self, internal_filename):
cursor = self.get_cursor()
delete_image = (f"""delete from images where internal_filename='{internal_filename}' """)
self.log(f"deleting image record. SQL: {delete_image}")
cursor.execute(delete_image)
self.cnx.commit()
cursor.close()
def execute(self, sql):
cursor = self.get_cursor()
logging.debug(f"SQL: {sql}")
cursor.execute(sql)
self.cnx.commit()
cursor.close()
def get_collection_list(self):
cursor = self.get_cursor()
query = f"""select collection from collection"""
cursor.execute(query)
collection_list = []
for (collection) in cursor:
collection_list.append(collection)
#
# not used 4/10/23 - left for referenece for now
#
# def search(self, filename, match_exact_data):
# params = {
# 'filename': filename,
# 'exact': match_exact_data,
# 'token': self.generate_token(self.get_timestamp(), filename)
# }
#
# r = requests.get(self.build_url("getImageRecordByOrigFilename"), params=params)
# print(f"Search result: {r.status_code}")
# if (r.status_code == 404):
# print(f"No records found for {arg}")
# return False
# if r.status_code != 200:
# print(f"Unexpected search result: {r.status_code}; aborting.")
# return
# data = json.loads(r.text)
# print(
# f"collection, datetime, id, internal_filename, notes, original filename, original path, redacted, universal URL, URL")
# if len(data) == 0:
# print("No match.")
# else:
# for item in data:
# print(
# f"{item['collection']},{item['datetime']},{item['internal_filename']},{item['notes']},{item['original_filename']},{item['original_path']},{item['redacted']},{item['universal_url']},{item['url']}")