Move checksum function to filesystem

This commit is contained in:
Cédric Leporcq 2021-07-31 21:26:04 +02:00
parent b252a9d76a
commit 930fb35dba
3 changed files with 6 additions and 25 deletions

View File

@ -4,7 +4,6 @@ Methods for interacting with information Dozo caches about stored media.
from builtins import map from builtins import map
from builtins import object from builtins import object
import hashlib
import json import json
import os import os
import sys import sys
@ -125,26 +124,6 @@ class Db(object):
""" """
return key in self.hash_db return key in self.hash_db
def checksum(self, file_path, blocksize=65536):
"""Create a hash value for the given file.
See http://stackoverflow.com/a/3431835/1318758.
:param str file_path: Path to the file to create a hash for.
:param int blocksize: Read blocks of this size from the file when
creating the hash.
:returns: str or None
"""
hasher = hashlib.sha256()
with open(file_path, 'rb') as f:
buf = f.read(blocksize)
while len(buf) > 0:
hasher.update(buf)
buf = f.read(blocksize)
return hasher.hexdigest()
return None
def get_hash(self, key): def get_hash(self, key):
"""Get the hash value for a given key. """Get the hash value for a given key.

View File

@ -185,7 +185,6 @@ class FileSystem(object):
return folder_name return folder_name
def get_part(self, item, mask, metadata, db, subdirs): def get_part(self, item, mask, metadata, db, subdirs):
"""Parse a specific folder's name given a mask and metadata. """Parse a specific folder's name given a mask and metadata.
@ -582,6 +581,7 @@ class FileSystem(object):
dest_directory = os.path.join(destination, dest_directory = os.path.join(destination,
os.path.dirname(file_path)) os.path.dirname(file_path))
dest_path = os.path.join(destination, file_path) dest_path = os.path.join(destination, file_path)
self.create_directory(dest_directory) self.create_directory(dest_directory)
result = self.sort_file(src_path, dest_path, remove_duplicates) result = self.sort_file(src_path, dest_path, remove_duplicates)
if result: if result:
@ -687,7 +687,7 @@ class FileSystem(object):
for image in images: for image in images:
if not os.path.isfile(image): if not os.path.isfile(image):
continue continue
checksum1 = db.checksum(image) checksum1 = self.checksum(image)
# Process files # Process files
# media = get_media_class(src_path, False, self.logger) # media = get_media_class(src_path, False, self.logger)
# TODO compare metadata # TODO compare metadata
@ -697,7 +697,7 @@ class FileSystem(object):
moved_imgs = set() moved_imgs = set()
for img_path in ci.find_similar(image, similarity): for img_path in ci.find_similar(image, similarity):
similar = True similar = True
checksum2 = db.checksum(img_path) checksum2 = self.checksum(img_path)
# move image into directory # move image into directory
name = os.path.splitext(os.path.basename(image))[0] name = os.path.splitext(os.path.basename(image))[0]
directory_name = 'similar_to_' + name directory_name = 'similar_to_' + name
@ -748,7 +748,7 @@ class FileSystem(object):
img_path = os.path.join(dirname, subdir, file_name) img_path = os.path.join(dirname, subdir, file_name)
if os.path.isdir(img_path): if os.path.isdir(img_path):
continue continue
checksum = db.checksum(img_path) checksum = self.checksum(img_path)
dest_path = os.path.join(dirname, os.path.basename(img_path)) dest_path = os.path.join(dirname, os.path.basename(img_path))
result = self.move_file(img_path, dest_path, checksum, db) result = self.move_file(img_path, dest_path, checksum, db)
if not result: if not result:

View File

@ -81,6 +81,7 @@ def get_prefer_english_names():
__PREFER_ENGLISH_NAMES__ = bool(config['Geolocation']['prefer_english_names']) __PREFER_ENGLISH_NAMES__ = bool(config['Geolocation']['prefer_english_names'])
return __PREFER_ENGLISH_NAMES__ return __PREFER_ENGLISH_NAMES__
def place_name(lat, lon, db, cache=True, logger=logging.getLogger()): def place_name(lat, lon, db, cache=True, logger=logging.getLogger()):
lookup_place_name_default = {'default': __DEFAULT_LOCATION__} lookup_place_name_default = {'default': __DEFAULT_LOCATION__}
if(lat is None or lon is None): if(lat is None or lon is None):
@ -131,6 +132,7 @@ def place_name(lat, lon, db, cache=True, logger=logging.getLogger()):
return lookup_place_name return lookup_place_name
def lookup_osm(lat, lon, logger=logging.getLogger()): def lookup_osm(lat, lon, logger=logging.getLogger()):
prefer_english_names = get_prefer_english_names() prefer_english_names = get_prefer_english_names()