gh-139 Update logic to set file access/modify time

This commit is contained in:
Jaisen Mathai 2016-10-21 17:09:26 -07:00
parent f00d8b8744
commit ea34ccbf6c
3 changed files with 83 additions and 67 deletions

View File

@ -48,9 +48,6 @@ def import_file(_file, destination, album_from_folder, trash, allow_duplicates):
print('{"source":"%s", "error_msg":"Not a supported file"}' % _file)
return
if media.__name__ == 'Video':
FILESYSTEM.set_date_from_path_video(media)
if album_from_folder:
media.set_album_from_folder()

View File

@ -14,7 +14,6 @@ import time
from elodie import geolocation
from elodie import constants
from elodie.localstorage import Db
from elodie.media.media import Media
class FileSystem(object):
@ -214,80 +213,42 @@ class FileSystem(object):
shutil.move(_file, dest_path)
os.utime(dest_path, (stat.st_atime, stat.st_mtime))
else:
# Do not use copy2(), will have an issue when copying to a network/mounted drive
# using copy and manual set_date_from_filename gets the job done
# Do not use copy2(), will have an issue when copying to a
# network/mounted drive using copy and manual
# set_date_from_filename gets the job done
shutil.copy(_file, dest_path)
self.set_date_from_filename(dest_path)
self.set_utime(media)
db.add_hash(checksum, dest_path)
db.update_hash_db()
return dest_path
def set_date_from_filename(self, file):
def set_utime(self, media):
""" Set the modification time on the file base on the file name.
"""
date_taken = None
file_name = os.path.basename(file)
# Initialize date taken to what's returned from the metadata function.
# If the folder and file name follow a time format of
# YYYY-MM-DD_HH-MM-SS-IMG_0001.JPG then we override the date_taken
(year, month, day, hour, minute, second) = [None] * 6
year_month_day_match = re.search('(\d{4})-(\d{2})-(\d{2})_(\d{2})-(\d{2})-(\d{2})', file_name)
if(year_month_day_match is not None):
(year, month, day, hour, minute, second) = year_month_day_match.groups()
# check if the file system path indicated a date and if so we
# override the metadata value
if(year is not None and month is not None and day is not None and hour is not None and minute is not None and second is not None):
date_taken = time.strptime(
'{}-{}-{} {}:{}:{}'.format(year, month, day, hour, minute, second),
'%Y-%m-%d %H:%M:%S'
)
os.utime(file, (time.time(), time.mktime(date_taken)))
def set_date_from_path_video(self, video):
"""Set the modification time on the file based on the file path.
Noop if the path doesn't match the format YYYY-MM/DD-IMG_0001.JPG.
:param elodie.media.video.Video video: An instance of Video.
"""
date_taken = None
video_file_path = video.get_file_path()
# Initialize date taken to what's returned from the metadata function.
# If the folder and file name follow a time format of
# YYYY-MM/DD-IMG_0001.JPG then we override the date_taken
(year, month, day) = [None] * 3
directory = os.path.dirname(video_file_path)
# If the directory matches we get back a match with
# groups() = (year, month)
year_month_match = re.search('(\d{4})-(\d{2})', directory)
if(year_month_match is not None):
(year, month) = year_month_match.groups()
day_match = re.search(
'^(\d{2})',
os.path.basename(video.get_file_path())
file_path = media.get_file_path()
metadata = media.get_metadata()
date_taken = metadata['date_taken']
base_name = metadata['base_name']
year_month_day_match = re.search(
'^(\d{4})-(\d{2})-(\d{2})_(\d{2})-(\d{2})-(\d{2})',
base_name
)
if(day_match is not None):
day = day_match.group(1)
if(year_month_day_match is not None):
(year, month, day, hour, minute, second) = year_month_day_match.groups() # noqa
date_taken = time.strptime(
'{}-{}-{} {}:{}:{}'.format(year, month, day, hour, minute, second), # noqa
'%Y-%m-%d %H:%M:%S'
)
# check if the file system path indicated a date and if so we
# override the metadata value
if(year is not None and month is not None):
if(day is not None):
date_taken = time.strptime(
'{}-{}-{}'.format(year, month, day),
'%Y-%m-%d'
)
else:
date_taken = time.strptime(
'{}-{}'.format(year, month),
'%Y-%m'
)
os.utime(video_file_path, (time.time(), time.mktime(date_taken)))
os.utime(file_path, (time.time(), time.mktime(date_taken)))
else:
# We don't make any assumptions about time zones and
# assume local time zone.
date_taken_in_seconds = time.mktime(date_taken)
os.utime(file_path, (time.time(), (date_taken_in_seconds)))

View File

@ -364,3 +364,61 @@ def test_process_video_with_album_then_title():
assert origin_checksum is not None, origin_checksum
assert origin_checksum != destination_checksum, destination_checksum
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
def test_set_utime_with_exif_date():
filesystem = FileSystem()
temporary_folder, folder = helper.create_working_folder()
origin = os.path.join(folder,'photo.jpg')
shutil.copyfile(helper.get_file('plain.jpg'), origin)
media_initial = Photo(origin)
metadata_initial = media_initial.get_metadata()
initial_stat = os.stat(origin)
initial_time = int(min(initial_stat.st_mtime, initial_stat.st_ctime))
initial_checksum = helper.checksum(origin)
assert initial_time != time.mktime(metadata_initial['date_taken'])
filesystem.set_utime(media_initial)
final_stat = os.stat(origin)
final_checksum = helper.checksum(origin)
media_final = Photo(origin)
metadata_final = media_final.get_metadata()
shutil.rmtree(folder)
assert initial_stat.st_mtime != final_stat.st_mtime
assert final_stat.st_mtime == time.mktime(metadata_final['date_taken'])
assert initial_checksum == final_checksum
def test_set_utime_without_exif_date():
filesystem = FileSystem()
temporary_folder, folder = helper.create_working_folder()
origin = os.path.join(folder,'photo.jpg')
shutil.copyfile(helper.get_file('no-exif.jpg'), origin)
media_initial = Photo(origin)
metadata_initial = media_initial.get_metadata()
initial_stat = os.stat(origin)
initial_time = int(min(initial_stat.st_mtime, initial_stat.st_ctime))
initial_checksum = helper.checksum(origin)
assert initial_time == time.mktime(metadata_initial['date_taken'])
filesystem.set_utime(media_initial)
final_stat = os.stat(origin)
final_checksum = helper.checksum(origin)
media_final = Photo(origin)
metadata_final = media_final.get_metadata()
shutil.rmtree(folder)
assert initial_time == final_stat.st_mtime
assert final_stat.st_mtime == time.mktime(metadata_final['date_taken']), (final_stat.st_mtime, time.mktime(metadata_final['date_taken']))
assert initial_checksum == final_checksum