diff options
author | Daniel Schadt <kingdread@gmx.de> | 2022-12-28 20:51:56 +0100 |
---|---|---|
committer | Daniel Schadt <kingdread@gmx.de> | 2022-12-28 20:51:56 +0100 |
commit | e7495338906e4f17c6394f59028226444347dfb3 (patch) | |
tree | 8f8f59ebf27296233069faf202afe08b3ac53558 | |
parent | d4c20086e745d3eb61de6fdbeba88792e10b83f5 (diff) | |
download | fietsboek-e7495338906e4f17c6394f59028226444347dfb3.tar.gz fietsboek-e7495338906e4f17c6394f59028226444347dfb3.tar.bz2 fietsboek-e7495338906e4f17c6394f59028226444347dfb3.zip |
embed metadata (title, description) in the file
-rw-r--r-- | fietsboek/data.py | 28 | ||||
-rw-r--r-- | fietsboek/views/edit.py | 9 | ||||
-rw-r--r-- | fietsboek/views/upload.py | 7 |
3 files changed, 43 insertions, 1 deletions
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)) |