mirror of https://gitlab.com/bashrc2/epicyon
Include width and height metadata for attached images
parent
96a58c4428
commit
d801482f97
30
media.py
30
media.py
|
@ -9,6 +9,7 @@ __module_group__ = "Timeline"
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import datetime
|
import datetime
|
||||||
|
import subprocess
|
||||||
from random import randint
|
from random import randint
|
||||||
from hashlib import sha1
|
from hashlib import sha1
|
||||||
from auth import createPassword
|
from auth import createPassword
|
||||||
|
@ -245,6 +246,13 @@ def attachMedia(baseDir: str, httpPrefix: str,
|
||||||
}
|
}
|
||||||
if mediaType.startswith('image/'):
|
if mediaType.startswith('image/'):
|
||||||
attachmentJson['focialPoint'] = [0.0, 0.0]
|
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]
|
postJson['attachment'] = [attachmentJson]
|
||||||
|
|
||||||
if baseDir:
|
if baseDir:
|
||||||
|
@ -298,3 +306,25 @@ def pathIsAudio(path: str) -> bool:
|
||||||
path.endswith('.mp3'):
|
path.endswith('.mp3'):
|
||||||
return True
|
return True
|
||||||
return False
|
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)
|
||||||
|
|
19
tests.py
19
tests.py
|
@ -86,6 +86,7 @@ from announce import sendAnnounceViaServer
|
||||||
from city import parseNogoString
|
from city import parseNogoString
|
||||||
from city import spoofGeolocation
|
from city import spoofGeolocation
|
||||||
from city import pointInNogo
|
from city import pointInNogo
|
||||||
|
from media import getImageDimensions
|
||||||
from media import getMediaPath
|
from media import getMediaPath
|
||||||
from media import getAttachmentMediaType
|
from media import getAttachmentMediaType
|
||||||
from delete import sendDeleteViaServer
|
from delete import sendDeleteViaServer
|
||||||
|
@ -792,6 +793,10 @@ def testPostMessageBetweenServers():
|
||||||
alicePersonCache = {}
|
alicePersonCache = {}
|
||||||
aliceCachedWebfingers = {}
|
aliceCachedWebfingers = {}
|
||||||
attachedImageFilename = baseDir + '/img/logo.png'
|
attachedImageFilename = baseDir + '/img/logo.png'
|
||||||
|
testImageWidth, testImageHeight = \
|
||||||
|
getImageDimensions(attachedImageFilename)
|
||||||
|
assert testImageWidth
|
||||||
|
assert testImageHeight
|
||||||
mediaType = getAttachmentMediaType(attachedImageFilename)
|
mediaType = getAttachmentMediaType(attachedImageFilename)
|
||||||
attachedImageDescription = 'Logo'
|
attachedImageDescription = 'Logo'
|
||||||
isArticle = False
|
isArticle = False
|
||||||
|
@ -875,6 +880,20 @@ def testPostMessageBetweenServers():
|
||||||
assert 'Why is a mouse when it spins?' in \
|
assert 'Why is a mouse when it spins?' in \
|
||||||
receivedJson['object']['content']
|
receivedJson['object']['content']
|
||||||
assert 'यह एक परीक्षण है' 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('\n\n*******************************************************')
|
||||||
print("Bob likes Alice's post")
|
print("Bob likes Alice's post")
|
||||||
|
|
Loading…
Reference in New Issue