1
- import datetime
1
+ import hashlib
2
2
import json
3
3
import os
4
4
import sqlite3
5
5
from modules import scripts
6
+ from PIL import Image
6
7
7
- version = 4
8
+ version = 5
8
9
9
10
path_recorder_file = os .path .join (scripts .basedir (), "path_recorder.txt" )
10
11
aes_cache_file = os .path .join (scripts .basedir (), "aes_scores.json" )
16
17
st = "Steps: "
17
18
timeout = 30
18
19
20
+ def create_filehash (cursor ):
21
+ cursor .execute ('''
22
+ CREATE TABLE IF NOT EXISTS filehash (
23
+ file TEXT PRIMARY KEY,
24
+ hash TEXT,
25
+ created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
26
+ updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP
27
+ )
28
+ ''' )
29
+
30
+ cursor .execute ('''
31
+ CREATE TRIGGER filehash_tr
32
+ AFTER UPDATE ON filehash
33
+ BEGIN
34
+ UPDATE filehash SET updated = CURRENT_TIMESTAMP WHERE file = OLD.file;
35
+ END;
36
+ ''' )
37
+
38
+ return
39
+
19
40
def create_db (cursor ):
20
41
cursor .execute ('''
21
42
CREATE TABLE IF NOT EXISTS db_data (
@@ -87,6 +108,8 @@ def create_db(cursor):
87
108
END;
88
109
''' )
89
110
111
+ create_filehash (cursor )
112
+
90
113
return
91
114
92
115
def migrate_path_recorder (cursor ):
@@ -213,6 +236,32 @@ def migrate_ranking(cursor):
213
236
214
237
return
215
238
239
+ def get_hash (file ):
240
+ # Get filehash without exif info
241
+ image = Image .open (file )
242
+ hash = hashlib .sha512 (image .tobytes ()).hexdigest ()
243
+ image .close ()
244
+
245
+ return hash
246
+
247
+ def migrate_filehash (cursor , version ):
248
+ if version <= "4" :
249
+ create_filehash (cursor )
250
+
251
+ cursor .execute ('''
252
+ SELECT file
253
+ FROM ranking
254
+ ''' )
255
+ for (file ,) in cursor .fetchall ():
256
+ if os .path .exists (file ):
257
+ hash = get_hash (file )
258
+ cursor .execute ('''
259
+ INSERT INTO filehash (file, hash)
260
+ VALUES (?, ?)
261
+ ''' , (file , hash ))
262
+
263
+ return
264
+
216
265
def update_db_data (cursor , key , value ):
217
266
cursor .execute ('''
218
267
INSERT OR REPLACE
@@ -341,26 +390,6 @@ def migrate_ranking_dirs(cursor, db_version):
341
390
342
391
return
343
392
344
- def get_c_time (file ):
345
- c_time = datetime .datetime .fromtimestamp (os .path .getctime (file )).strftime ('%Y-%m-%d %H:%M:%S' )
346
- return c_time
347
-
348
- def migrate_ranking_c_time (cursor ):
349
- cursor .execute ('''
350
- SELECT file
351
- FROM ranking
352
- ''' )
353
- for (file ,) in cursor .fetchall ():
354
- if os .path .exists (file ):
355
- c_time = get_c_time (file )
356
- cursor .execute ('''
357
- UPDATE ranking
358
- SET created = ?
359
- WHERE file = ?
360
- ''' , (c_time , file ))
361
-
362
- return
363
-
364
393
def check ():
365
394
if not os .path .exists (db_file ):
366
395
conn , cursor = transaction_begin ()
@@ -370,7 +399,7 @@ def check():
370
399
migrate_path_recorder (cursor )
371
400
migrate_exif_data (cursor )
372
401
migrate_ranking (cursor )
373
- migrate_ranking_c_time (cursor )
402
+ migrate_filehash (cursor , str ( version ) )
374
403
transaction_end (conn , cursor )
375
404
print ("Image Browser: Database created" )
376
405
db_version = get_version ()
@@ -382,8 +411,8 @@ def check():
382
411
migrate_path_recorder_dirs (cursor )
383
412
migrate_exif_data_dirs (cursor )
384
413
migrate_ranking_dirs (cursor , db_version [0 ])
385
- if db_version [0 ] <= "3 " :
386
- migrate_ranking_c_time (cursor )
414
+ if db_version [0 ] <= "4 " :
415
+ migrate_filehash (cursor , db_version [ 0 ] )
387
416
update_db_data (cursor , "version" , version )
388
417
print (f"Image Browser: Database upgraded from version { db_version [0 ]} to version { version } " )
389
418
transaction_end (conn , cursor )
@@ -402,6 +431,8 @@ def load_path_recorder():
402
431
return path_recorder
403
432
404
433
def select_ranking (file ):
434
+ hash = None
435
+
405
436
conn , cursor = transaction_begin ()
406
437
cursor .execute ('''
407
438
SELECT ranking
@@ -410,36 +441,54 @@ def select_ranking(file):
410
441
''' , (file ,))
411
442
ranking_value = cursor .fetchone ()
412
443
413
- # if ranking not found search again, without path (moved?)
444
+ # If ranking not found search again, without path (moved?)
414
445
if ranking_value is None :
415
- c_time = get_c_time (file )
416
446
name = os .path .basename (file )
417
447
cursor .execute ('''
418
- SELECT ranking
448
+ SELECT file, ranking
419
449
FROM ranking
420
450
WHERE name = ?
421
- AND created = ?
422
- ''' , (name , c_time ))
423
- ranking_value = cursor .fetchone ()
424
- # and insert with current filepath
425
- if ranking_value is not None :
451
+ ''' , (name ,))
452
+ result = cursor .fetchone ()
453
+ if result is not None :
454
+ (file_value , ranking_value ) = result
426
455
cursor .execute ('''
427
- DELETE
428
- FROM ranking
429
- WHERE name = ?
430
- AND created = ?
431
- ''' , (name , c_time ))
432
-
433
- (insert_ranking ,) = ranking_value
434
- cursor .execute ('''
435
- INSERT INTO ranking (file, name, ranking, created)
436
- VALUES (?, ?, ?, ?)
437
- ''' , (file , name , insert_ranking , c_time ))
438
-
456
+ SELECT hash
457
+ FROM filehash
458
+ WHERE file = ?
459
+ ''' , (file_value ,))
460
+ hash_value = cursor .fetchone ()
461
+ if hash is None :
462
+ hash = get_hash (file )
463
+ if hash_value is None :
464
+ hash_value = hash
465
+ else :
466
+ (hash_value ,) = hash_value
467
+
468
+ if hash_value != hash :
469
+ ranking_value = None
470
+
471
+ # and update with current filepath
472
+ if ranking_value is not None :
473
+ cursor .execute ('''
474
+ UPDATE ranking
475
+ SET file = ?
476
+ WHERE file = ?
477
+ ''' , (file , file_value ))
478
+
439
479
if ranking_value is None :
440
480
return_ranking = "None"
441
481
else :
442
482
(return_ranking ,) = ranking_value
483
+
484
+ if hash is None :
485
+ hash = get_hash (file )
486
+ cursor .execute ('''
487
+ INSERT OR REPLACE
488
+ INTO filehash (file, hash)
489
+ VALUES (?, ?)
490
+ ''' , (file , hash ))
491
+
443
492
transaction_end (conn , cursor )
444
493
445
494
return return_ranking
@@ -448,19 +497,24 @@ def update_ranking(file, ranking):
448
497
name = os .path .basename (file )
449
498
with sqlite3 .connect (db_file , timeout = timeout ) as conn :
450
499
cursor = conn .cursor ()
451
- c_time = get_c_time (file )
452
500
if ranking == "None" :
453
- cursor .execute ('''
454
- DELETE FROM ranking
455
- WHERE name = ?
456
- AND created = ?
457
- ''' , (name , c_time ))
501
+ cursor .execute ('''
502
+ DELETE FROM ranking
503
+ WHERE file = ?
504
+ ''' , (file ,))
458
505
else :
459
- cursor .execute ('''
460
- INSERT OR REPLACE
461
- INTO ranking (file, name, ranking, created)
462
- VALUES (?, ?, ?, ?)
463
- ''' , (file , name , ranking , c_time ))
506
+ cursor .execute ('''
507
+ INSERT OR REPLACE
508
+ INTO ranking (file, name, ranking)
509
+ VALUES (?, ?, ?)
510
+ ''' , (file , name , ranking ))
511
+
512
+ hash = get_hash (file )
513
+ cursor .execute ('''
514
+ INSERT OR REPLACE
515
+ INTO filehash (file, hash)
516
+ VALUES (?, ?)
517
+ ''' , (file , hash ))
464
518
465
519
return
466
520
0 commit comments