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 object
import hashlib
import json
import os
import sys
@ -125,26 +124,6 @@ class Db(object):
"""
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):
"""Get the hash value for a given key.

View File

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

View File

@ -81,6 +81,7 @@ def get_prefer_english_names():
__PREFER_ENGLISH_NAMES__ = bool(config['Geolocation']['prefer_english_names'])
return __PREFER_ENGLISH_NAMES__
def place_name(lat, lon, db, cache=True, logger=logging.getLogger()):
lookup_place_name_default = {'default': __DEFAULT_LOCATION__}
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
def lookup_osm(lat, lon, logger=logging.getLogger()):
prefer_english_names = get_prefer_english_names()