main
Bob Mottram 2021-03-07 13:15:20 +00:00
commit 5860d90473
7 changed files with 133 additions and 38 deletions

View File

@ -973,9 +973,16 @@ def saveMediaInFormPOST(mediaBytes, debug: bool,
os.remove(possibleOtherFormat) os.remove(possibleOtherFormat)
fd = open(filename, 'wb') fd = open(filename, 'wb')
if not fd:
return None, None
fd.write(mediaBytes[startPos:]) fd.write(mediaBytes[startPos:])
fd.close() fd.close()
if not os.path.isfile(filename):
print('WARN: Media file could not be written to file: ' + filename)
return None, None
print('Uploaded media file written: ' + filename)
return filename, attachmentMediaType return filename, attachmentMediaType

View File

@ -12673,7 +12673,9 @@ class PubServer(BaseHTTPRequestHandler):
postImageFilename) postImageFilename)
else: else:
if os.path.isfile(filename): if os.path.isfile(filename):
os.rename(filename, filename.replace('.temp', '')) newFilename = filename.replace('.temp', '')
os.rename(filename, newFilename)
filename = newFilename
fields = \ fields = \
extractTextFieldsInPOST(postBytes, boundary, extractTextFieldsInPOST(postBytes, boundary,

View File

@ -184,6 +184,19 @@ body, html {
image-rendering: var(--rendering); image-rendering: var(--rendering);
} }
video {
width: 100%;
}
figure {
padding-left: 0;
margin-left: 0;
padding-right: 0;
margin-right: 0;
margin-top: 0;
height: auto;
}
.cw { .cw {
font-style: var(--cw-style); font-style: var(--cw-style);
font-weight: var(--cw-weight); font-weight: var(--cw-weight);

View File

@ -54,6 +54,11 @@ def removeMetaData(imageFilename: str, outputFilename: str) -> None:
def _isMedia(imageFilename: str) -> bool: def _isMedia(imageFilename: str) -> bool:
"""Is the given file a media file?
"""
if not os.path.isfile(imageFilename):
print('WARN: Media file does not exist ' + imageFilename)
return False
permittedMedia = getMediaExtensions() permittedMedia = getMediaExtensions()
for m in permittedMedia: for m in permittedMedia:
if imageFilename.endswith('.' + m): if imageFilename.endswith('.' + m):

View File

@ -3055,7 +3055,10 @@ def _addPostStringToTimeline(postStr: str, boxname: str,
return False return False
elif boxname == 'tlmedia': elif boxname == 'tlmedia':
if '"Create"' in postStr: if '"Create"' in postStr:
if 'mediaType' not in postStr or 'image/' not in postStr: if ('mediaType' not in postStr or
('image/' not in postStr and
'video/' not in postStr and
'audio/' not in postStr)):
return False return False
# add the post to the dictionary # add the post to the dictionary
postsInBox.append(postStr) postsInBox.append(postStr)

View File

@ -220,8 +220,7 @@ def _addEmbeddedAudio(translate: {}, content: str) -> str:
return content return content
def _addEmbeddedVideo(translate: {}, content: str, def _addEmbeddedVideo(translate: {}, content: str) -> str:
width=400, height=300) -> str:
"""Adds embedded video for mp4/webm/ogv """Adds embedded video for mp4/webm/ogv
""" """
if not ('.mp4' in content or '.webm' in content or '.ogv' in content): if not ('.mp4' in content or '.webm' in content or '.ogv' in content):
@ -258,14 +257,16 @@ def _addEmbeddedVideo(translate: {}, content: str,
continue continue
url = w url = w
content += \ content += \
'<center>\n<video width="' + str(width) + '" height="' + \ '<center><figure id="videoContainer" ' + \
str(height) + '" controls>\n' 'data-fullscreen="false">\n' + \
' <video id="video" controls ' + \
'preload="metadata">\n'
content += \ content += \
'<source src="' + url + '" type="video/' + \ '<source src="' + url + '" type="video/' + \
extension.replace('.', '') + '">\n' extension.replace('.', '') + '">\n'
content += \ content += \
translate['Your browser does not support the video element.'] translate['Your browser does not support the video element.']
content += '</video>\n</center>\n' content += '</video>\n</figure>\n</center>\n'
return content return content

View File

@ -785,6 +785,75 @@ def addEmojiToDisplayName(baseDir: str, httpPrefix: str,
return displayName return displayName
def _isImageMimeType(mimeType: str) -> bool:
"""Is the given mime type an image?
"""
imageMimeTypes = (
'image/png',
'image/jpeg',
'image/webp',
'image/avif',
'image/svg+xml',
'image/gif'
)
if mimeType in imageMimeTypes:
return True
return False
def _isVideoMimeType(mimeType: str) -> bool:
"""Is the given mime type a video?
"""
videoMimeTypes = (
'video/mp4',
'video/webm',
'video/ogv'
)
if mimeType in videoMimeTypes:
return True
return False
def _isAudioMimeType(mimeType: str) -> bool:
"""Is the given mime type an audio file?
"""
audioMimeTypes = (
'audio/mpeg',
'audio/ogg'
)
if mimeType in audioMimeTypes:
return True
return False
def _isAttachedImage(attachmentFilename: str) -> bool:
"""Is the given attachment filename an image?
"""
if '.' not in attachmentFilename:
return False
imageExt = (
'png', 'jpg', 'jpeg', 'webp', 'avif', 'svg', 'gif'
)
ext = attachmentFilename.split('.')[-1]
if ext in imageExt:
return True
return False
def _isAttachedVideo(attachmentFilename: str) -> bool:
"""Is the given attachment filename a video?
"""
if '.' not in attachmentFilename:
return False
videoExt = (
'mp4', 'webm', 'ogv'
)
ext = attachmentFilename.split('.')[-1]
if ext in videoExt:
return True
return False
def getPostAttachmentsAsHtml(postJsonObject: {}, boxName: str, translate: {}, def getPostAttachmentsAsHtml(postJsonObject: {}, boxName: str, translate: {},
isMuted: bool, avatarLink: str, isMuted: bool, avatarLink: str,
replyStr: str, announceStr: str, likeStr: str, replyStr: str, announceStr: str, likeStr: str,
@ -801,7 +870,8 @@ def getPostAttachmentsAsHtml(postJsonObject: {}, boxName: str, translate: {},
return attachmentStr, galleryStr return attachmentStr, galleryStr
attachmentCtr = 0 attachmentCtr = 0
attachmentStr += '<div class="media">\n' attachmentStr = ''
mediaStyleAdded = False
for attach in postJsonObject['object']['attachment']: for attach in postJsonObject['object']['attachment']:
if not (attach.get('mediaType') and attach.get('url')): if not (attach.get('mediaType') and attach.get('url')):
continue continue
@ -810,19 +880,12 @@ def getPostAttachmentsAsHtml(postJsonObject: {}, boxName: str, translate: {},
imageDescription = '' imageDescription = ''
if attach.get('name'): if attach.get('name'):
imageDescription = attach['name'].replace('"', "'") imageDescription = attach['name'].replace('"', "'")
if mediaType == 'image/png' or \ if _isImageMimeType(mediaType):
mediaType == 'image/jpeg' or \ if _isAttachedImage(attach['url']):
mediaType == 'image/webp' or \ if not attachmentStr:
mediaType == 'image/avif' or \ attachmentStr += '<div class="media">\n'
mediaType == 'image/svg+xml' or \ mediaStyleAdded = True
mediaType == 'image/gif':
if attach['url'].endswith('.png') or \
attach['url'].endswith('.jpg') or \
attach['url'].endswith('.jpeg') or \
attach['url'].endswith('.webp') or \
attach['url'].endswith('.avif') or \
attach['url'].endswith('.svg') or \
attach['url'].endswith('.gif'):
if attachmentCtr > 0: if attachmentCtr > 0:
attachmentStr += '<br>' attachmentStr += '<br>'
if boxName == 'tlmedia': if boxName == 'tlmedia':
@ -862,15 +925,9 @@ def getPostAttachmentsAsHtml(postJsonObject: {}, boxName: str, translate: {},
'" alt="' + imageDescription + '" title="' + \ '" alt="' + imageDescription + '" title="' + \
imageDescription + '" class="attachment"></a>\n' imageDescription + '" class="attachment"></a>\n'
attachmentCtr += 1 attachmentCtr += 1
elif (mediaType == 'video/mp4' or elif _isVideoMimeType(mediaType):
mediaType == 'video/webm' or if _isAttachedVideo(attach['url']):
mediaType == 'video/ogv'): extension = attach['url'].split('.')[-1]
extension = '.mp4'
if attach['url'].endswith('.webm'):
extension = '.webm'
elif attach['url'].endswith('.ogv'):
extension = '.ogv'
if attach['url'].endswith(extension):
if attachmentCtr > 0: if attachmentCtr > 0:
attachmentStr += '<br>' attachmentStr += '<br>'
if boxName == 'tlmedia': if boxName == 'tlmedia':
@ -878,16 +935,20 @@ def getPostAttachmentsAsHtml(postJsonObject: {}, boxName: str, translate: {},
if not isMuted: if not isMuted:
galleryStr += ' <a href="' + attach['url'] + '">\n' galleryStr += ' <a href="' + attach['url'] + '">\n'
galleryStr += \ galleryStr += \
' <video width="600" height="400" controls>\n' ' <figure id="videoContainer" ' + \
'data-fullscreen="false">\n' + \
' <video id="video" controls ' + \
'preload="metadata">\n'
galleryStr += \ galleryStr += \
' <source src="' + attach['url'] + \ ' <source src="' + attach['url'] + \
'" alt="' + imageDescription + \ '" alt="' + imageDescription + \
'" title="' + imageDescription + \ '" title="' + imageDescription + \
'" class="attachment" type="video/' + \ '" class="attachment" type="video/' + \
extension.replace('.', '') + '">' extension + '">'
idx = 'Your browser does not support the video tag.' idx = 'Your browser does not support the video tag.'
galleryStr += translate[idx] galleryStr += translate[idx]
galleryStr += ' </video>\n' galleryStr += ' </video>\n'
galleryStr += ' </figure>\n'
galleryStr += ' </a>\n' galleryStr += ' </a>\n'
if postJsonObject['object'].get('url'): if postJsonObject['object'].get('url'):
videoPostUrl = postJsonObject['object']['url'] videoPostUrl = postJsonObject['object']['url']
@ -913,18 +974,20 @@ def getPostAttachmentsAsHtml(postJsonObject: {}, boxName: str, translate: {},
galleryStr += '</div>\n' galleryStr += '</div>\n'
attachmentStr += \ attachmentStr += \
'<center><video width="400" height="300" controls>' '<center><figure id="videoContainer" ' + \
'data-fullscreen="false">\n' + \
' <video id="video" controls ' + \
'preload="metadata">\n'
attachmentStr += \ attachmentStr += \
'<source src="' + attach['url'] + '" alt="' + \ '<source src="' + attach['url'] + '" alt="' + \
imageDescription + '" title="' + imageDescription + \ imageDescription + '" title="' + imageDescription + \
'" class="attachment" type="video/' + \ '" class="attachment" type="video/' + \
extension.replace('.', '') + '">' extension + '">'
attachmentStr += \ attachmentStr += \
translate['Your browser does not support the video tag.'] translate['Your browser does not support the video tag.']
attachmentStr += '</video></center>' attachmentStr += '</video></figure></center>'
attachmentCtr += 1 attachmentCtr += 1
elif (mediaType == 'audio/mpeg' or elif _isAudioMimeType(mediaType):
mediaType == 'audio/ogg'):
extension = '.mp3' extension = '.mp3'
if attach['url'].endswith('.ogg'): if attach['url'].endswith('.ogg'):
extension = '.ogg' extension = '.ogg'
@ -980,6 +1043,7 @@ def getPostAttachmentsAsHtml(postJsonObject: {}, boxName: str, translate: {},
translate['Your browser does not support the audio tag.'] translate['Your browser does not support the audio tag.']
attachmentStr += '</audio>\n</center>\n' attachmentStr += '</audio>\n</center>\n'
attachmentCtr += 1 attachmentCtr += 1
if mediaStyleAdded:
attachmentStr += '</div>' attachmentStr += '</div>'
return attachmentStr, galleryStr return attachmentStr, galleryStr