From d801482f97e1c2465f5c6a9795b53d5b6e201f5d Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Thu, 1 Jul 2021 12:02:11 +0100 Subject: [PATCH] Include width and height metadata for attached images --- media.py | 30 ++++++++++++++++++++++++++++++ tests.py | 19 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/media.py b/media.py index 3d374ae22..2f788117e 100644 --- a/media.py +++ b/media.py @@ -9,6 +9,7 @@ __module_group__ = "Timeline" import os import datetime +import subprocess from random import randint from hashlib import sha1 from auth import createPassword @@ -245,6 +246,13 @@ def attachMedia(baseDir: str, httpPrefix: str, } if mediaType.startswith('image/'): attachmentJson['focialPoint'] = [0.0, 0.0] + # find the dimensions of the image and add them as metadata + attachImageWidth, attachImageHeight = \ + getImageDimensions(imageFilename) + if attachImageWidth and attachImageHeight: + attachmentJson['width'] = attachImageWidth + attachmentJson['height'] = attachImageHeight + postJson['attachment'] = [attachmentJson] if baseDir: @@ -298,3 +306,25 @@ def pathIsAudio(path: str) -> bool: path.endswith('.mp3'): return True return False + + +def getImageDimensions(imageFilename: str) -> (int, int): + """Returns the dimensions of an image file + """ + try: + result = subprocess.run(['identify', '-format', '"%wx%h"', + imageFilename], stdout=subprocess.PIPE) + except BaseException: + return None, None + if not result: + return None, None + dimensionsStr = result.stdout.decode('utf-8').replace('"', '') + if 'x' not in dimensionsStr: + return None, None + widthStr = dimensionsStr.split('x')[0] + if not widthStr.isdigit(): + return None, None + heightStr = dimensionsStr.split('x')[1] + if not heightStr.isdigit(): + return None, None + return int(widthStr), int(heightStr) diff --git a/tests.py b/tests.py index c9de2eeab..972541101 100644 --- a/tests.py +++ b/tests.py @@ -86,6 +86,7 @@ from announce import sendAnnounceViaServer from city import parseNogoString from city import spoofGeolocation from city import pointInNogo +from media import getImageDimensions from media import getMediaPath from media import getAttachmentMediaType from delete import sendDeleteViaServer @@ -792,6 +793,10 @@ def testPostMessageBetweenServers(): alicePersonCache = {} aliceCachedWebfingers = {} attachedImageFilename = baseDir + '/img/logo.png' + testImageWidth, testImageHeight = \ + getImageDimensions(attachedImageFilename) + assert testImageWidth + assert testImageHeight mediaType = getAttachmentMediaType(attachedImageFilename) attachedImageDescription = 'Logo' isArticle = False @@ -875,6 +880,20 @@ def testPostMessageBetweenServers(): assert 'Why is a mouse when it spins?' in \ receivedJson['object']['content'] assert 'यह एक परीक्षण है' in receivedJson['object']['content'] + print('Check that message received from Alice contains an attachment') + assert receivedJson['object']['attachment'] + assert len(receivedJson['object']['attachment']) == 1 + attached = receivedJson['object']['attachment'][0] + pprint(attached) + assert attached.get('type') + assert attached.get('url') + assert attached['mediaType'] == 'image/png' + assert '/media/' in attached['url'] + assert attached['url'].endswith('.png') + assert attached.get('width') + assert attached.get('height') + assert attached['width'] > 0 + assert attached['height'] > 0 print('\n\n*******************************************************') print("Bob likes Alice's post")