Skip to content

Commit

Permalink
Merge 7937832 into 6bde290
Browse files Browse the repository at this point in the history
  • Loading branch information
mattca committed Apr 10, 2018
2 parents 6bde290 + 7937832 commit 51fa743
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 42 deletions.
71 changes: 46 additions & 25 deletions elodie/filesystem.py
Expand Up @@ -320,7 +320,35 @@ def parse_mask_for_location(self, mask, location_parts, place_name):

return folder_name

def process_checksum(self, _file, allow_duplicate):
db = Db()
checksum = db.checksum(_file)
if(checksum is None):
log.info('Could not get checksum for %s. Skipping...' % _file)
return None

# If duplicates are not allowed then we check if we've seen this file
# before via checksum. We also check that the file exists at the
# location we believe it to be.
# If we find a checksum match but the file doesn't exist where we
# believe it to be then we write a debug log and proceed to import.
checksum_file = db.get_hash(checksum)
if(allow_duplicate is False and checksum_file is not None):
if(os.path.isfile(checksum_file)):
log.info('%s already exists at %s. Skipping...' % (
_file,
checksum_file
))
return None
else:
log.info('%s matched checksum but file not found at %s. Importing again...' % ( # noqa
_file,
checksum_file
))
return checksum

def process_file(self, _file, destination, media, **kwargs):

move = False
if('move' in kwargs):
move = kwargs['move']
Expand All @@ -333,6 +361,10 @@ def process_file(self, _file, destination, media, **kwargs):
print('%s is not a valid media file. Skipping...' % _file)
return

original_checksum = self.process_checksum(_file, allow_duplicate)
if(original_checksum is None):
return

media.set_original_name()
metadata = media.get_metadata()

Expand All @@ -342,31 +374,10 @@ def process_file(self, _file, destination, media, **kwargs):
file_name = self.get_file_name(media)
dest_path = os.path.join(dest_directory, file_name)

db = Db()
checksum = db.checksum(_file)
if(checksum is None):
log.info('Could not get checksum for %s. Skipping...' % _file)
new_checksum = self.process_checksum(_file, allow_duplicate)
if(new_checksum is None):
return

# If duplicates are not allowed then we check if we've seen this file
# before via checksum. We also check that the file exists at the
# location we believe it to be.
# If we find a checksum match but the file doesn't exist where we
# believe it to be then we write a debug log and proceed to import.
checksum_file = db.get_hash(checksum)
if(allow_duplicate is False and checksum_file is not None):
if(os.path.isfile(checksum_file)):
log.info('%s already exists at %s. Skipping...' % (
_file,
checksum_file
))
return
else:
log.info('%s matched checksum but file not found at %s. Importing again...' % ( # noqa
_file,
checksum_file
))

# If source and destination are identical then
# we should not write the file. gh-210
if(_file == dest_path):
Expand All @@ -378,12 +389,22 @@ def process_file(self, _file, destination, media, **kwargs):
if(move is True):
stat = os.stat(_file)
shutil.move(_file, dest_path)
if(os.path.exists(_file + '_original')):
os.remove(_file + '_original')
os.utime(dest_path, (stat.st_atime, stat.st_mtime))
else:
compatability._copyfile(_file, dest_path)
# exiftool preserves the original file with '_original' appended to
# the file name.
if(os.path.exists(_file + '_original')):
shutil.move(_file, dest_path)
shutil.move(_file + '_original', _file)
else:
compatability._copyfile(_file, dest_path)
self.set_utime_from_metadata(media.get_metadata(), dest_path)

db.add_hash(checksum, dest_path)
db = Db()
db.add_hash(original_checksum, dest_path)
db.add_hash(new_checksum, dest_path)
db.update_hash_db()

return dest_path
Expand Down
1 change: 0 additions & 1 deletion elodie/media/media.py
Expand Up @@ -53,7 +53,6 @@ def __init__(self, source=None):
self.original_name_key = 'XMP:OriginalFileName'
self.set_gps_ref = True
self.exiftool_addedargs = [
'-overwrite_original',
u'-config',
u'"{}"'.format(constants.exiftool_config)
]
Expand Down
Binary file added elodie/tests/files/photo.cr2
Binary file not shown.
32 changes: 16 additions & 16 deletions elodie/tests/filesystem_test.py
Expand Up @@ -506,8 +506,8 @@ def test_process_file_plain():
shutil.rmtree(folder)
shutil.rmtree(os.path.dirname(os.path.dirname(destination)))

assert origin_checksum is not None, origin_checksum
assert origin_checksum == destination_checksum, destination_checksum
assert origin_checksum is not None
assert destination_checksum is not None
assert helper.path_tz_fix(os.path.join('2015-12-Dec','Unknown Location','2015-12-05_00-59-26-photo.jpg')) in destination, destination

def test_process_file_with_title():
Expand All @@ -526,8 +526,8 @@ def test_process_file_with_title():
shutil.rmtree(folder)
shutil.rmtree(os.path.dirname(os.path.dirname(destination)))

assert origin_checksum is not None, origin_checksum
assert origin_checksum == destination_checksum, destination_checksum
assert origin_checksum is not None
assert destination_checksum is not None
assert helper.path_tz_fix(os.path.join('2015-12-Dec','Unknown Location','2015-12-05_00-59-26-photo-some-title.jpg')) in destination, destination

def test_process_file_with_location():
Expand All @@ -546,8 +546,8 @@ def test_process_file_with_location():
shutil.rmtree(folder)
shutil.rmtree(os.path.dirname(os.path.dirname(destination)))

assert origin_checksum is not None, origin_checksum
assert origin_checksum == destination_checksum, destination_checksum
assert origin_checksum is not None
assert destination_checksum is not None
assert helper.path_tz_fix(os.path.join('2015-12-Dec','Sunnyvale','2015-12-05_00-59-26-photo.jpg')) in destination, destination

def test_process_file_with_location_and_title():
Expand All @@ -566,8 +566,8 @@ def test_process_file_with_location_and_title():
shutil.rmtree(folder)
shutil.rmtree(os.path.dirname(os.path.dirname(destination)))

assert origin_checksum is not None, origin_checksum
assert origin_checksum == destination_checksum, destination_checksum
assert origin_checksum is not None
assert destination_checksum is not None
assert helper.path_tz_fix(os.path.join('2015-12-Dec','Sunnyvale','2015-12-05_00-59-26-photo-some-title.jpg')) in destination, destination

def test_process_file_with_album():
Expand All @@ -586,8 +586,8 @@ def test_process_file_with_album():
shutil.rmtree(folder)
shutil.rmtree(os.path.dirname(os.path.dirname(destination)))

assert origin_checksum is not None, origin_checksum
assert origin_checksum == destination_checksum, destination_checksum
assert origin_checksum is not None
assert destination_checksum is not None
assert helper.path_tz_fix(os.path.join('2015-12-Dec','Test Album','2015-12-05_00-59-26-photo.jpg')) in destination, destination

def test_process_file_with_album_and_title():
Expand All @@ -606,8 +606,8 @@ def test_process_file_with_album_and_title():
shutil.rmtree(folder)
shutil.rmtree(os.path.dirname(os.path.dirname(destination)))

assert origin_checksum is not None, origin_checksum
assert origin_checksum == destination_checksum, destination_checksum
assert origin_checksum is not None
assert destination_checksum is not None
assert helper.path_tz_fix(os.path.join('2015-12-Dec','Test Album','2015-12-05_00-59-26-photo-some-title.jpg')) in destination, destination

def test_process_file_with_album_and_title_and_location():
Expand All @@ -626,8 +626,8 @@ def test_process_file_with_album_and_title_and_location():
shutil.rmtree(folder)
shutil.rmtree(os.path.dirname(os.path.dirname(destination)))

assert origin_checksum is not None, origin_checksum
assert origin_checksum == destination_checksum, destination_checksum
assert origin_checksum is not None
assert destination_checksum is not None
assert helper.path_tz_fix(os.path.join('2015-12-Dec','Test Album','2015-12-05_00-59-26-photo-some-title.jpg')) in destination, destination

# gh-89 (setting album then title reverts album)
Expand All @@ -650,8 +650,8 @@ def test_process_video_with_album_then_title():
shutil.rmtree(folder)
shutil.rmtree(os.path.dirname(os.path.dirname(destination)))

assert origin_checksum is not None, origin_checksum
assert origin_checksum != destination_checksum, destination_checksum
assert origin_checksum is not None
assert destination_checksum is not None
assert helper.path_tz_fix(os.path.join('2015-01-Jan','test_album','2015-01-19_12-45-11-movie-test_title.mov')) in destination, destination

@mock.patch('elodie.config.config_file', '%s/config.ini-fallback-folder' % gettempdir())
Expand Down

0 comments on commit 51fa743

Please sign in to comment.