mirror of
https://github.com/gryf/slack-backup.git
synced 2025-12-17 19:40:21 +01:00
Changed behavoiur for duplicates
Till now, if we download certain files (like those attached to the conversation) and we already have the file with the same name, number in format '%03d' was added just before extension. That way there could be possibility, that the very same file will be downloaded and stored multiple times, like: file.png file.001.png file.002.png ... This commit prevents that by adding comparison between files we already have and file which is downloaded from slack. Adding another file with additional number will only have place when stored file and downloaded have different content.
This commit is contained in:
@@ -4,6 +4,8 @@ Some utils functions. Jsut to not copypaste the code around
|
||||
import errno
|
||||
import os
|
||||
import logging
|
||||
import tempfile
|
||||
import hashlib
|
||||
|
||||
|
||||
def makedirs(path):
|
||||
@@ -19,3 +21,24 @@ def makedirs(path):
|
||||
logging.error("Cannot create `%s'. There is some file on the "
|
||||
"way; cannot proceed.", path)
|
||||
raise
|
||||
|
||||
|
||||
def get_temp_name():
|
||||
"""Return temporary file name"""
|
||||
fdesc, fname = tempfile.mkstemp()
|
||||
os.close(fdesc)
|
||||
return fname
|
||||
|
||||
|
||||
def same_files(file1, file2):
|
||||
"""
|
||||
Compare files by calculating hash for each of them. Return True if hash is
|
||||
identical, False otherwise
|
||||
"""
|
||||
with open(file1, 'rb') as fobj:
|
||||
hash1 = hashlib.sha256(fobj.read())
|
||||
|
||||
with open(file2, 'rb') as fobj:
|
||||
hash2 = hashlib.sha256(fobj.read())
|
||||
|
||||
return hash1.hexdigest() == hash2.hexdigest()
|
||||
|
||||
Reference in New Issue
Block a user