Include width and height metadata for attached images

main
Bob Mottram 2021-07-01 12:02:11 +01:00
parent 96a58c4428
commit d801482f97
2 changed files with 49 additions and 0 deletions

View File

@ -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)

View File

@ -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")