Metadata changes are made in dest files instead of source files

This commit is contained in:
Cédric Leporcq 2021-04-17 15:37:37 +02:00
parent df0bf3c28e
commit f181c618b3
5 changed files with 37 additions and 51 deletions

View File

@ -60,11 +60,8 @@ def import_file(_file, destination, album_from_folder, trash, allow_duplicates):
log.all('{"source":"%s", "error_msg":"Not a supported file"}' % _file) log.all('{"source":"%s", "error_msg":"Not a supported file"}' % _file)
return return
if album_from_folder:
media.set_album_from_folder()
dest_path = FILESYSTEM.process_file(_file, destination, dest_path = FILESYSTEM.process_file(_file, destination,
media, allowDuplicate=allow_duplicates, move=False) media, album_from_folder, allowDuplicate=allow_duplicates, move=False)
if dest_path: if dest_path:
log.all('%s -> %s' % (_file, dest_path)) log.all('%s -> %s' % (_file, dest_path))
if trash: if trash:

View File

@ -396,7 +396,7 @@ class ExifTool(object, with_metaclass(Singleton)):
""" """
return self.get_tag_batch(tag, [filename])[0] return self.get_tag_batch(tag, [filename])[0]
def set_tags_batch(self, tags, filenames): def set_tags_batch(self, tags, filenames, overwrite=True):
"""Writes the values of the specified tags for the given files. """Writes the values of the specified tags for the given files.
The first argument is a dictionary of tags and values. The tag names may The first argument is a dictionary of tags and values. The tag names may
@ -422,7 +422,8 @@ class ExifTool(object, with_metaclass(Singleton)):
params_utf8 = [] params_utf8 = []
for tag, value in tags.items(): for tag, value in tags.items():
params.append(u'-%s=%s' % (tag, value)) params.append(u'-%s=%s' % (tag, value))
if overwrite:
params.append('-overwrite_original')
params.extend(filenames) params.extend(filenames)
params_utf8 = [x.encode('utf-8') for x in params] params_utf8 = [x.encode('utf-8') for x in params]
return self.execute(*params_utf8) return self.execute(*params_utf8)

View File

@ -614,17 +614,20 @@ class FileSystem(object):
)) ))
return checksum return checksum
def process_file(self, _file, destination, media, **kwargs): def process_file(self, _file, destination, media, album_from_folder, **kwargs):
move = False move = False
if('move' in kwargs): if('move' in kwargs):
move = kwargs['move'] if kwargs['move']:
action = 'move'
else:
action = 'copy'
allow_duplicate = False allow_duplicate = False
if('allowDuplicate' in kwargs): if('allowDuplicate' in kwargs):
allow_duplicate = kwargs['allowDuplicate'] allow_duplicate = kwargs['allowDuplicate']
stat_info_original = os.stat(_file) stat_info_original = os.stat(_file)
metadata = media.get_metadata() metadata = media.get_metadata(album_from_folder)
if(not media.is_valid()): if(not media.is_valid()):
print('%s is not a valid media file. Skipping...' % _file) print('%s is not a valid media file. Skipping...' % _file)
@ -648,8 +651,6 @@ class FileSystem(object):
file_name = self.get_file_name(metadata) file_name = self.get_file_name(metadata)
dest_path = os.path.join(dest_directory, file_name) dest_path = os.path.join(dest_directory, file_name)
media.set_original_name()
# If source and destination are identical then # If source and destination are identical then
# we should not write the file. gh-210 # we should not write the file. gh-210
if(_file == dest_path): if(_file == dest_path):
@ -661,37 +662,27 @@ class FileSystem(object):
# exiftool renames the original file by appending '_original' to the # exiftool renames the original file by appending '_original' to the
# file name. A new file is written with new tags with the initial file # file name. A new file is written with new tags with the initial file
# name. See exiftool man page for more details. # name. See exiftool man page for more details.
exif_original_file = _file + '_original'
# Check if the source file was processed by exiftool and an _original # Check if the source file was processed by exiftool and an _original
# file was created. # file was created.
exif_original_file_exists = False
if(os.path.exists(exif_original_file)):
exif_original_file_exists = True
if(move is True): if(action == 'move'):
stat = os.stat(_file) stat = os.stat(_file)
# Move the processed file into the destination directory # Move the processed file into the destination directory
shutil.move(_file, dest_path) shutil.move(_file, dest_path)
if(exif_original_file_exists is True): elif action == 'copy':
# We can remove it as we don't need the initial file. shutil.copy2(_file, dest_path)
os.remove(exif_original_file)
else:
if(exif_original_file_exists is True):
# Move the newly processed file with any updated tags to the
# destination directory
shutil.move(_file, dest_path)
# Move the exif _original back to the initial source file
shutil.move(exif_original_file, _file)
else:
compatability._copyfile(_file, dest_path)
if action != 'dry-run':
# Set the utime based on what the original file contained # Set the utime based on what the original file contained
# before we made any changes. # before we made any changes.
# Then set the utime on the destination file based on metadata. # Then set the utime on the destination file based on metadata.
date_taken = self.get_date_taken(metadata) date_taken = self.get_date_taken(metadata)
self.set_utime_from_metadata(date_taken, dest_path) self.set_utime_from_metadata(date_taken, dest_path)
media.set_original_name(dest_path)
if album_from_folder:
media.set_album_from_folder(dest_path)
db = Db() db = Db()
db.add_hash(checksum, dest_path) db.add_hash(checksum, dest_path)

View File

@ -91,6 +91,11 @@ class Base(object):
return self.metadata return self.metadata
source = self.source source = self.source
folder = os.path.basename(os.path.dirname(source))
album = self.get_album()
album_from_folder = True
if album_from_folder and (album is None or album == ''):
album = folder
self.metadata = { self.metadata = {
'date_original': self.get_date_attribute(self.date_original), 'date_original': self.get_date_attribute(self.date_original),
@ -100,11 +105,11 @@ class Base(object):
'camera_model': self.get_camera_model(), 'camera_model': self.get_camera_model(),
'latitude': self.get_coordinate('latitude'), 'latitude': self.get_coordinate('latitude'),
'longitude': self.get_coordinate('longitude'), 'longitude': self.get_coordinate('longitude'),
'album': self.get_album(), 'album': album,
'title': self.get_title(), 'title': self.get_title(),
'mime_type': self.get_mimetype(), 'mime_type': self.get_mimetype(),
'original_name': self.get_original_name(), 'original_name': self.get_original_name(),
'base_name': os.path.splitext(os.path.basename(source))[0], 'base_name': folder,
'extension': self.get_extension(), 'extension': self.get_extension(),
'directory_path': os.path.dirname(source) 'directory_path': os.path.dirname(source)
} }
@ -164,7 +169,7 @@ class Base(object):
""" """
return None return None
def set_album_from_folder(self): def set_album_from_folder(self, path):
"""Set the album attribute based on the leaf folder name """Set the album attribute based on the leaf folder name
:returns: bool :returns: bool
@ -180,7 +185,7 @@ class Base(object):
if(len(folder) == 0): if(len(folder) == 0):
return False return False
status = self.set_album(folder) self.set_album(folder)
if status == False: if status == False:
return False return False
return True return True

View File

@ -248,20 +248,18 @@ class Media(Base):
self.exif_metadata = None self.exif_metadata = None
super(Media, self).reset_cache() super(Media, self).reset_cache()
def set_album(self, album): def set_album(self, name, path):
"""Set album for a photo """Set album EXIF tag if not already set.
:param str name: Name of album :returns: True, False, None
:returns: bool
""" """
if(not self.is_valid()): if self.get_album() is not None:
return None return None
tags = {self.album_keys[0]: album} tags = {self.album_keys[0]: name}
status = self.__set_tags(tags) status = ExifTool().set_tags(tags, path)
self.reset_cache() self.reset_cache()
return status != ''
return status
def set_date_original(self, time): def set_date_original(self, time):
"""Set the date/time a photo was taken. """Set the date/time a photo was taken.
@ -314,27 +312,21 @@ class Media(Base):
return status return status
def set_original_name(self, name=None): def set_original_name(self, path):
"""Sets the original name EXIF tag if not already set. """Sets the original name EXIF tag if not already set.
:returns: True, False, None :returns: True, False, None
""" """
if(not self.is_valid()):
return None
# If EXIF original name tag is set then we return. # If EXIF original name tag is set then we return.
if self.get_original_name() is not None: if self.get_original_name() is not None:
return None return None
source = self.source name = os.path.basename(path)
if not name:
name = os.path.basename(source)
tags = {self.original_name_key: name} tags = {self.original_name_key: name}
status = self.__set_tags(tags) status = ExifTool().set_tags(tags, path)
self.reset_cache() self.reset_cache()
return status return status != ''
def set_title(self, title): def set_title(self, title):
"""Set title for a photo. """Set title for a photo.