more verbose hash check

This commit is contained in:
q
2016-12-21 20:57:05 +02:00
parent 7662a59a7e
commit 6b57aadf88

View File

@@ -17,6 +17,7 @@ MINSIZE=0
MIME=magic.open(magic.MAGIC_NONE) MIME=magic.open(magic.MAGIC_NONE)
#MIME=magic.open(magic.MAGIC_MIME) #MIME=magic.open(magic.MAGIC_MIME)
MIME.load() MIME.load()
ANIM=['-','\\','|','/','-','_','_']
def setup_options(): def setup_options():
parser=ArgumentParser(description="Maintains the list of images sqlite file") parser=ArgumentParser(description="Maintains the list of images sqlite file")
@@ -106,11 +107,11 @@ def add_single(conn,filename,change=False,hash=None,minsize=0,fullfile=False):
fsize=os.path.getsize(filename) fsize=os.path.getsize(filename)
mime=MIME.file(filename.encode('UTF-8')) mime=MIME.file(filename.encode('UTF-8'))
except IOError: except IOError:
print("File not found. Bad link?") print("File '%s' not found. Bad link?"%(filename,))
return return
except UnicodeDecodeError: except UnicodeDecodeError:
mime="NA" mime="NA"
if change: if change:
db.execute("UPDATE list SET date=?, hash=?, size=?, mime=? \ db.execute("UPDATE list SET date=?, hash=?, size=?, mime=? \
WHERE file=?",(ftime,hash,fsize,mime,filename)) WHERE file=?",(ftime,hash,fsize,mime,filename))
@@ -310,11 +311,16 @@ def get_folder_contents(db,path):
def get_md5(filename,fullfile=False): def get_md5(filename,fullfile=False):
''' returns content based hash, only first 50Mb is read, unless user wants the whole file ''' ''' returns content based hash, only first 50Mb is read, unless user wants the whole file '''
if fullfile: if fullfile:
block_size=2**20 anim_i=0
block_size=2**24
md5 = hashlib.md5() md5 = hashlib.md5()
with open(filename,'rb') as f: with open(filename,'rb') as f:
for chunk in iter(lambda: f.read(block_size), b''): for chunk in iter(lambda: f.read(block_size), b''):
sys.stdout.write('\r'+ANIM[anim_i])
sys.stdout.flush()
anim_i=(anim_i+1)%len(ANIM)
md5.update(chunk) md5.update(chunk)
sys.stdout.write('\r')
return md5.hexdigest() return md5.hexdigest()
return hashlib.md5(open(filename,'rb').read(1024*1024*50)).hexdigest() return hashlib.md5(open(filename,'rb').read(1024*1024*50)).hexdigest()