Skip to content

Commit

Permalink
Save photo after creating file object
Browse files Browse the repository at this point in the history
- this will ensure that we do not create broken files, when the thumbnail process crashes
  • Loading branch information
derneuere committed May 10, 2023
1 parent 41cc3f4 commit 4841c74
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions api/directory_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,27 +92,39 @@ def handle_new_image(user, path, job_id):

photos: QuerySet[Photo] = Photo.objects.filter(Q(image_hash=hash))
if not photos.exists():
start = datetime.datetime.now()
elapsed = (datetime.datetime.now() - start).total_seconds()
photo: Photo = Photo()
photo.image_hash = hash
photo.owner = user
photo.added_on = datetime.datetime.now().replace(tzinfo=pytz.utc)
photo.geolocation_json = {}
photo.video = is_video(path)
photo.save()

elapsed = (datetime.datetime.now() - start).total_seconds()
util.logger.info(
"job {}: create database entry: {}, elapsed: {}".format(
job_id, path, elapsed
)
)
file = File.create(path, user)
if has_embedded_media(file):
em_path = extract_embedded_media(file)
if em_path:
em_file = File.create(em_path, user)
file.embedded_media.add(em_file)

elapsed = (datetime.datetime.now() - start).total_seconds()
util.logger.info(
"job {}: extract embedded media: {}, elapsed: {}".format(
job_id, path, elapsed
)
)
photo.files.add(file)
photo.main_file = file

photo.save()

photo._generate_thumbnail(True)
start = datetime.datetime.now()
elapsed = (datetime.datetime.now() - start).total_seconds()

util.logger.info(
"job {}: generate thumbnails: {}, elapsed: {}".format(
job_id, path, elapsed
Expand Down

0 comments on commit 4841c74

Please sign in to comment.