Skip to content

Commit

Permalink
Add metadata files to photo
Browse files Browse the repository at this point in the history
  • Loading branch information
derneuere committed May 14, 2023
1 parent 1909da4 commit 382c911
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 30 deletions.
46 changes: 22 additions & 24 deletions api/directory_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
calculate_hash,
extract_embedded_media,
has_embedded_media,
is_metadata,
is_valid_media,
is_video,
)
Expand Down Expand Up @@ -90,6 +91,25 @@ def handle_new_image(user, path, job_id):
)
return

if is_metadata(path):
photo_name = os.path.splitext(os.path.basename(path))[0]
photo_dir = os.path.dirname(path)
photo = Photo.objects.filter(
Q(files__path__contains=photo_dir)
& Q(files__path__contains=photo_name)
& ~Q(files__path__contains=os.path.basename(path))
).first()

if photo:
file = File.create(path, user)
photo.files.add(file)
photo.save()
else:
util.logger.warning(
"job {}: no photo to metadata file found {}".format(job_id, path)
)
return

photos: QuerySet[Photo] = Photo.objects.filter(Q(image_hash=hash))
if not photos.exists():
start = datetime.datetime.now()
Expand Down Expand Up @@ -122,9 +142,9 @@ def handle_new_image(user, path, job_id):
photo.files.add(file)
photo.main_file = file
photo.save()

photo._generate_thumbnail(True)

util.logger.info(
"job {}: generate thumbnails: {}, elapsed: {}".format(
job_id, path, elapsed
Expand Down Expand Up @@ -299,27 +319,6 @@ def photo_scanner(user, last_scan, full_scan, path, job_id):
)


def initialize_scan_process(*args, **kwargs):
"""
Each process will have its own exiftool instance
so we need to start _and_ stop it for each process.
multiprocessing.util.Finalize is _undocumented_ and
should perhaps not be relied on but I found no other
way. (See https://stackoverflow.com/a/24724452)
"""
from multiprocessing.util import Finalize

from api.util import exiftool_instance

et = exiftool_instance.__enter__()

def terminate(et):
et.terminate()

Finalize(et, terminate, args=(et,), exitpriority=16)


@job
def scan_photos(user, full_scan, job_id, scan_directory="", scan_files=[]):
if not os.path.exists(
Expand Down Expand Up @@ -381,7 +380,6 @@ def scan_photos(user, full_scan, job_id, scan_directory="", scan_files=[]):

with Pool(
processes=site_config.HEAVYWEIGHT_PROCESS,
initializer=initialize_scan_process,
) as pool:
pool.starmap(photo_scanner, all)

Expand Down
17 changes: 17 additions & 0 deletions api/migrations/0049_fix_metadata_files_as_main_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django.db import migrations


def delete_photos_with_metadata_as_main(apps, schema_editor):
Photo = apps.get_model("api", "Photo")
for photo in Photo.objects.filter(main_file__type=3):
photo.delete()


class Migration(migrations.Migration):
dependencies = [
("api", "0048_fix_null_height"),
]

operations = [
migrations.RunPython(delete_photos_with_metadata_as_main),
]
8 changes: 2 additions & 6 deletions api/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def get_sidecar_files_in_priority_order(media_file):
media_file + ".XMP",
]


def _get_existing_metadata_files_reversed(media_file, include_sidecar_files):
if include_sidecar_files:
files = [
Expand All @@ -108,10 +109,6 @@ def get_metadata(media_file, tags, try_sidecar=True):
If *try_sidecar* is `True`, use the value set in any XMP sidecar file
stored alongside *media_file*.
If *exiftool_instance* is running, leave it running when returning
from this function. Otherwise, start it and then terminate it before
returning.
Returns a list with the value of each tag in *tags* or `None` if the
tag was not found.
Expand All @@ -129,12 +126,11 @@ def get_metadata(media_file, tags, try_sidecar=True):
retrieved_value = et.get_tag(file, tag)
if retrieved_value is not None:
value = retrieved_value
values.append(value)#
values.append(value) #
return values


def write_metadata(media_file, tags, use_sidecar=True):

# To-Do: Replace with new File Structure
if use_sidecar:
file_path = get_sidecar_files_in_priority_order(media_file)[0]
Expand Down

0 comments on commit 382c911

Please sign in to comment.