From e7495338906e4f17c6394f59028226444347dfb3 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Wed, 28 Dec 2022 20:51:56 +0100 Subject: embed metadata (title, description) in the file --- fietsboek/data.py | 28 ++++++++++++++++++++++++++++ fietsboek/views/edit.py | 9 ++++++++- fietsboek/views/upload.py | 7 +++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/fietsboek/data.py b/fietsboek/data.py index 922600b..6db8294 100644 --- a/fietsboek/data.py +++ b/fietsboek/data.py @@ -3,6 +3,7 @@ Data are objects that belong to a track (such as images), but are not stored in the database itself. This module makes access to such data objects easier. """ +import datetime import random import string import shutil @@ -12,6 +13,7 @@ from typing import List, BinaryIO, Optional from pathlib import Path import brotli +import gpxpy from filelock import FileLock from .util import secure_filename @@ -142,6 +144,32 @@ class TrackDataDir: """ return brotli.decompress(self.gpx_path().read_bytes()) + def engrave_metadata( + self, title: str, description: str, author_name: str, time: datetime.datetime + ): + """Engrave the given metadata into the GPX file. + + Note that this will erase all existing metadata in the given fields. + + :param title: The title of the track. + :param description: The description of the track. + :param creator: Name of the track's creator. + :param time: Time of the track. + """ + gpx = gpxpy.parse(self.decompress_gpx()) + # First we delete the existing metadata + for track in gpx.tracks: + track.name = None + track.description = None + + # Now we add the new metadata + gpx.author_name = author_name + gpx.name = title + gpx.description = description + gpx.time = time + + self.compress_gpx(gpx.to_xml().encode("utf-8")) + def backup(self): """Create a backup of the GPX file.""" shutil.copy(self.gpx_path(), self.backup_path()) diff --git a/fietsboek/views/edit.py b/fietsboek/views/edit.py index b3dcf1e..6d9cfc6 100644 --- a/fietsboek/views/edit.py +++ b/fietsboek/views/edit.py @@ -10,6 +10,7 @@ from pyramid.httpexceptions import HTTPFound, HTTPBadRequest from sqlalchemy import select from .. import models, util +from ..data import TrackDataDir from ..models.track import Visibility, TrackType @@ -81,7 +82,7 @@ def do_edit(request): if any(user not in track.tagged_people and user not in user_friends for user in tagged_people): return HTTPBadRequest() - data = request.data_manager.open(track.id) + data: TrackDataDir = request.data_manager.open(track.id) tz_offset = datetime.timedelta(minutes=int(request.params["date-tz"])) date = datetime.datetime.fromisoformat(request.params["date"]) with data.lock(): @@ -97,6 +98,12 @@ def do_edit(request): track.sync_tags(tags) edit_images(request, request.context) + data.engrave_metadata( + title=track.title, + description=track.description, + author_name=track.owner.name, + time=track.date, + ) return HTTPFound(request.route_url("details", track_id=track.id)) diff --git a/fietsboek/views/upload.py b/fietsboek/views/upload.py index f0f7793..7f7820f 100644 --- a/fietsboek/views/upload.py +++ b/fietsboek/views/upload.py @@ -192,6 +192,13 @@ def do_finish_upload(request): LOGGER.debug("Starting to edit images for the upload") edit.edit_images(request, track) + manager.engrave_metadata( + title=track.title, + description=track.description, + author_name=track.owner.name, + time=track.date, + ) + request.session.flash(request.localizer.translate(_("flash.upload_success"))) return HTTPFound(request.route_url("details", track_id=track.id)) -- cgit v1.2.3