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)
fd = open(filename, 'wb')
if not fd:
return None, None
fd.write(mediaBytes[startPos:])
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

View File

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

View File

@ -184,6 +184,19 @@ body, html {
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 {
font-style: var(--cw-style);
font-weight: var(--cw-weight);

View File

@ -54,6 +54,11 @@ def removeMetaData(imageFilename: str, outputFilename: str) -> None:
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()
for m in permittedMedia:
if imageFilename.endswith('.' + m):

View File

@ -3055,7 +3055,10 @@ def _addPostStringToTimeline(postStr: str, boxname: str,
return False
elif boxname == 'tlmedia':
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
# add the post to the dictionary
postsInBox.append(postStr)

View File

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

View File

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