Skip to content

Commit 8b748ed

Browse files
committed
filehash logic replaces ctime logic, yfszzx#71
1 parent 67cd1da commit 8b748ed

File tree

1 file changed

+110
-56
lines changed

1 file changed

+110
-56
lines changed

scripts/wib/wib_db.py

+110-56
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import datetime
1+
import hashlib
22
import json
33
import os
44
import sqlite3
55
from modules import scripts
6+
from PIL import Image
67

7-
version = 4
8+
version = 5
89

910
path_recorder_file = os.path.join(scripts.basedir(), "path_recorder.txt")
1011
aes_cache_file = os.path.join(scripts.basedir(), "aes_scores.json")
@@ -16,6 +17,26 @@
1617
st = "Steps: "
1718
timeout = 30
1819

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+
1940
def create_db(cursor):
2041
cursor.execute('''
2142
CREATE TABLE IF NOT EXISTS db_data (
@@ -87,6 +108,8 @@ def create_db(cursor):
87108
END;
88109
''')
89110

111+
create_filehash(cursor)
112+
90113
return
91114

92115
def migrate_path_recorder(cursor):
@@ -213,6 +236,32 @@ def migrate_ranking(cursor):
213236

214237
return
215238

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+
216265
def update_db_data(cursor, key, value):
217266
cursor.execute('''
218267
INSERT OR REPLACE
@@ -341,26 +390,6 @@ def migrate_ranking_dirs(cursor, db_version):
341390

342391
return
343392

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-
364393
def check():
365394
if not os.path.exists(db_file):
366395
conn, cursor = transaction_begin()
@@ -370,7 +399,7 @@ def check():
370399
migrate_path_recorder(cursor)
371400
migrate_exif_data(cursor)
372401
migrate_ranking(cursor)
373-
migrate_ranking_c_time(cursor)
402+
migrate_filehash(cursor, str(version))
374403
transaction_end(conn, cursor)
375404
print("Image Browser: Database created")
376405
db_version = get_version()
@@ -382,8 +411,8 @@ def check():
382411
migrate_path_recorder_dirs(cursor)
383412
migrate_exif_data_dirs(cursor)
384413
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])
387416
update_db_data(cursor, "version", version)
388417
print(f"Image Browser: Database upgraded from version {db_version[0]} to version {version}")
389418
transaction_end(conn, cursor)
@@ -402,6 +431,8 @@ def load_path_recorder():
402431
return path_recorder
403432

404433
def select_ranking(file):
434+
hash = None
435+
405436
conn, cursor = transaction_begin()
406437
cursor.execute('''
407438
SELECT ranking
@@ -410,36 +441,54 @@ def select_ranking(file):
410441
''', (file,))
411442
ranking_value = cursor.fetchone()
412443

413-
# if ranking not found search again, without path (moved?)
444+
# If ranking not found search again, without path (moved?)
414445
if ranking_value is None:
415-
c_time = get_c_time(file)
416446
name = os.path.basename(file)
417447
cursor.execute('''
418-
SELECT ranking
448+
SELECT file, ranking
419449
FROM ranking
420450
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
426455
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+
439479
if ranking_value is None:
440480
return_ranking = "None"
441481
else:
442482
(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+
443492
transaction_end(conn, cursor)
444493

445494
return return_ranking
@@ -448,19 +497,24 @@ def update_ranking(file, ranking):
448497
name = os.path.basename(file)
449498
with sqlite3.connect(db_file, timeout=timeout) as conn:
450499
cursor = conn.cursor()
451-
c_time = get_c_time(file)
452500
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,))
458505
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))
464518

465519
return
466520

0 commit comments

Comments
 (0)