diff --git a/content.py b/content.py index c1b2ff418..32858c926 100644 --- a/content.py +++ b/content.py @@ -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 diff --git a/daemon.py b/daemon.py index ccf9b446e..cfefb8300 100644 --- a/daemon.py +++ b/daemon.py @@ -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, diff --git a/epicyon-profile.css b/epicyon-profile.css index 6ef938ec7..8b2efc23a 100644 --- a/epicyon-profile.css +++ b/epicyon-profile.css @@ -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); diff --git a/media.py b/media.py index eb0df3d43..358c98932 100644 --- a/media.py +++ b/media.py @@ -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): diff --git a/posts.py b/posts.py index a2340eaa1..33959a7cc 100644 --- a/posts.py +++ b/posts.py @@ -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) diff --git a/webapp_media.py b/webapp_media.py index f837e9c20..576c3c9fd 100644 --- a/webapp_media.py +++ b/webapp_media.py @@ -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 += \ - '
\n\n\n
\n' return content diff --git a/webapp_utils.py b/webapp_utils.py index a0f7d0076..0f4fcb761 100644 --- a/webapp_utils.py +++ b/webapp_utils.py @@ -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 += '
\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 += '
\n' + mediaStyleAdded = True + if attachmentCtr > 0: attachmentStr += '
' if boxName == 'tlmedia': @@ -862,15 +925,9 @@ def getPostAttachmentsAsHtml(postJsonObject: {}, boxName: str, translate: {}, '" alt="' + imageDescription + '" title="' + \ imageDescription + '" class="attachment">\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 += '
' if boxName == 'tlmedia': @@ -878,16 +935,20 @@ def getPostAttachmentsAsHtml(postJsonObject: {}, boxName: str, translate: {}, if not isMuted: galleryStr += ' \n' galleryStr += \ - ' \n' if postJsonObject['object'].get('url'): videoPostUrl = postJsonObject['object']['url'] @@ -913,18 +974,20 @@ def getPostAttachmentsAsHtml(postJsonObject: {}, boxName: str, translate: {}, galleryStr += '
\n' attachmentStr += \ - '
' attachmentCtr += 1 - elif (mediaType == 'audio/mpeg' or - mediaType == 'audio/ogg'): + elif _isAudioMimeType(mediaType): extension = '.mp3' if attach['url'].endswith('.ogg'): extension = '.ogg' @@ -980,7 +1043,8 @@ def getPostAttachmentsAsHtml(postJsonObject: {}, boxName: str, translate: {}, translate['Your browser does not support the audio tag.'] attachmentStr += '\n\n' attachmentCtr += 1 - attachmentStr += '
' + if mediaStyleAdded: + attachmentStr += '' return attachmentStr, galleryStr