forked from indymedia/epicyon
Detect media mime types
parent
91f9b8fbcb
commit
dc98dc514d
104
daemon.py
104
daemon.py
|
@ -166,6 +166,7 @@ from shares import getSharesFeedForPerson
|
||||||
from shares import addShare
|
from shares import addShare
|
||||||
from shares import removeShare
|
from shares import removeShare
|
||||||
from shares import expireShares
|
from shares import expireShares
|
||||||
|
from utils import mediaFileMimeType
|
||||||
from utils import getCSS
|
from utils import getCSS
|
||||||
from utils import firstParagraphFromString
|
from utils import firstParagraphFromString
|
||||||
from utils import clearFromPostCaches
|
from utils import clearFromPostCaches
|
||||||
|
@ -4774,25 +4775,7 @@ class PubServer(BaseHTTPRequestHandler):
|
||||||
self._304()
|
self._304()
|
||||||
return
|
return
|
||||||
|
|
||||||
mediaFileType = 'image/png'
|
mediaFileType = mediaFileMimeType(mediaFilename)
|
||||||
if mediaFilename.endswith('.png'):
|
|
||||||
mediaFileType = 'image/png'
|
|
||||||
elif mediaFilename.endswith('.jpg'):
|
|
||||||
mediaFileType = 'image/jpeg'
|
|
||||||
elif mediaFilename.endswith('.gif'):
|
|
||||||
mediaFileType = 'image/gif'
|
|
||||||
elif mediaFilename.endswith('.webp'):
|
|
||||||
mediaFileType = 'image/webp'
|
|
||||||
elif mediaFilename.endswith('.avif'):
|
|
||||||
mediaFileType = 'image/avif'
|
|
||||||
elif mediaFilename.endswith('.mp4'):
|
|
||||||
mediaFileType = 'video/mp4'
|
|
||||||
elif mediaFilename.endswith('.ogv'):
|
|
||||||
mediaFileType = 'video/ogv'
|
|
||||||
elif mediaFilename.endswith('.mp3'):
|
|
||||||
mediaFileType = 'audio/mpeg'
|
|
||||||
elif mediaFilename.endswith('.ogg'):
|
|
||||||
mediaFileType = 'audio/ogg'
|
|
||||||
|
|
||||||
with open(mediaFilename, 'rb') as avFile:
|
with open(mediaFilename, 'rb') as avFile:
|
||||||
mediaBinary = avFile.read()
|
mediaBinary = avFile.read()
|
||||||
|
@ -4859,7 +4842,7 @@ class PubServer(BaseHTTPRequestHandler):
|
||||||
if self.server.iconsCache.get(mediaStr):
|
if self.server.iconsCache.get(mediaStr):
|
||||||
mediaBinary = self.server.iconsCache[mediaStr]
|
mediaBinary = self.server.iconsCache[mediaStr]
|
||||||
self._set_headers_etag(mediaFilename,
|
self._set_headers_etag(mediaFilename,
|
||||||
'image/png',
|
mediaFileMimeType(mediaFilename),
|
||||||
mediaBinary, None,
|
mediaBinary, None,
|
||||||
callingDomain)
|
callingDomain)
|
||||||
self._write(mediaBinary)
|
self._write(mediaBinary)
|
||||||
|
@ -4868,8 +4851,9 @@ class PubServer(BaseHTTPRequestHandler):
|
||||||
if os.path.isfile(mediaFilename):
|
if os.path.isfile(mediaFilename):
|
||||||
with open(mediaFilename, 'rb') as avFile:
|
with open(mediaFilename, 'rb') as avFile:
|
||||||
mediaBinary = avFile.read()
|
mediaBinary = avFile.read()
|
||||||
|
mimeType = mediaFileMimeType(mediaFilename)
|
||||||
self._set_headers_etag(mediaFilename,
|
self._set_headers_etag(mediaFilename,
|
||||||
'image/png',
|
mimeType,
|
||||||
mediaBinary, None,
|
mediaBinary, None,
|
||||||
callingDomain)
|
callingDomain)
|
||||||
self._write(mediaBinary)
|
self._write(mediaBinary)
|
||||||
|
@ -4893,39 +4877,11 @@ class PubServer(BaseHTTPRequestHandler):
|
||||||
return
|
return
|
||||||
with open(mediaFilename, 'rb') as avFile:
|
with open(mediaFilename, 'rb') as avFile:
|
||||||
mediaBinary = avFile.read()
|
mediaBinary = avFile.read()
|
||||||
if mediaFilename.endswith('.png'):
|
mimeType = mediaFileMimeType(mediaFilename)
|
||||||
self._set_headers_etag(mediaFilename,
|
self._set_headers_etag(mediaFilename,
|
||||||
'image/png',
|
mimeType,
|
||||||
mediaBinary, None,
|
mediaBinary, None,
|
||||||
callingDomain)
|
callingDomain)
|
||||||
elif mediaFilename.endswith('.jpg'):
|
|
||||||
self._set_headers_etag(mediaFilename,
|
|
||||||
'image/jpeg',
|
|
||||||
mediaBinary, None,
|
|
||||||
callingDomain)
|
|
||||||
elif mediaFilename.endswith('.gif'):
|
|
||||||
self._set_headers_etag(mediaFilename,
|
|
||||||
'image/gif',
|
|
||||||
mediaBinary, None,
|
|
||||||
callingDomain)
|
|
||||||
elif mediaFilename.endswith('.webp'):
|
|
||||||
self._set_headers_etag(mediaFilename,
|
|
||||||
'image/webp',
|
|
||||||
mediaBinary, None,
|
|
||||||
callingDomain)
|
|
||||||
elif mediaFilename.endswith('.avif'):
|
|
||||||
self._set_headers_etag(mediaFilename,
|
|
||||||
'image/avif',
|
|
||||||
mediaBinary, None,
|
|
||||||
callingDomain)
|
|
||||||
else:
|
|
||||||
# default to jpeg
|
|
||||||
self._set_headers_etag(mediaFilename,
|
|
||||||
'image/jpeg',
|
|
||||||
mediaBinary, None,
|
|
||||||
callingDomain)
|
|
||||||
# self._404()
|
|
||||||
return
|
|
||||||
self._write(mediaBinary)
|
self._write(mediaBinary)
|
||||||
self._benchmarkGETtimings(GETstartTime, GETtimings,
|
self._benchmarkGETtimings(GETstartTime, GETtimings,
|
||||||
'icon shown done',
|
'icon shown done',
|
||||||
|
@ -8337,7 +8293,8 @@ class PubServer(BaseHTTPRequestHandler):
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
tries += 1
|
tries += 1
|
||||||
if mediaBinary:
|
if mediaBinary:
|
||||||
self._set_headers_etag(qrFilename, 'image/png',
|
mimeType = mediaFileMimeType(qrFilename)
|
||||||
|
self._set_headers_etag(qrFilename, mimeType,
|
||||||
mediaBinary, None,
|
mediaBinary, None,
|
||||||
callingDomain)
|
callingDomain)
|
||||||
self._write(mediaBinary)
|
self._write(mediaBinary)
|
||||||
|
@ -8375,7 +8332,8 @@ class PubServer(BaseHTTPRequestHandler):
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
tries += 1
|
tries += 1
|
||||||
if mediaBinary:
|
if mediaBinary:
|
||||||
self._set_headers_etag(bannerFilename, 'image/png',
|
mimeType = mediaFileMimeType(bannerFilename)
|
||||||
|
self._set_headers_etag(bannerFilename, mimeType,
|
||||||
mediaBinary, None,
|
mediaBinary, None,
|
||||||
callingDomain)
|
callingDomain)
|
||||||
self._write(mediaBinary)
|
self._write(mediaBinary)
|
||||||
|
@ -8416,7 +8374,8 @@ class PubServer(BaseHTTPRequestHandler):
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
tries += 1
|
tries += 1
|
||||||
if mediaBinary:
|
if mediaBinary:
|
||||||
self._set_headers_etag(bannerFilename, 'image/png',
|
mimeType = mediaFileMimeType(bannerFilename)
|
||||||
|
self._set_headers_etag(bannerFilename, mimeType,
|
||||||
mediaBinary, None,
|
mediaBinary, None,
|
||||||
callingDomain)
|
callingDomain)
|
||||||
self._write(mediaBinary)
|
self._write(mediaBinary)
|
||||||
|
@ -9364,8 +9323,8 @@ class PubServer(BaseHTTPRequestHandler):
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
tries += 1
|
tries += 1
|
||||||
if mediaBinary:
|
if mediaBinary:
|
||||||
self._set_headers_etag(mediaFilename,
|
mimeType = mediaFileMimeType(mediaFilename)
|
||||||
'image/png',
|
self._set_headers_etag(mediaFilename, mimeType,
|
||||||
mediaBinary, cookie,
|
mediaBinary, cookie,
|
||||||
callingDomain)
|
callingDomain)
|
||||||
self._write(mediaBinary)
|
self._write(mediaBinary)
|
||||||
|
@ -9404,8 +9363,8 @@ class PubServer(BaseHTTPRequestHandler):
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
tries += 1
|
tries += 1
|
||||||
if mediaBinary:
|
if mediaBinary:
|
||||||
self._set_headers_etag(screenFilename,
|
mimeType = mediaFileMimeType(screenFilename)
|
||||||
'image/png',
|
self._set_headers_etag(screenFilename, mimeType,
|
||||||
mediaBinary, cookie,
|
mediaBinary, cookie,
|
||||||
callingDomain)
|
callingDomain)
|
||||||
self._write(mediaBinary)
|
self._write(mediaBinary)
|
||||||
|
@ -9449,7 +9408,7 @@ class PubServer(BaseHTTPRequestHandler):
|
||||||
tries += 1
|
tries += 1
|
||||||
if mediaBinary:
|
if mediaBinary:
|
||||||
self._set_headers_etag(iconFilename,
|
self._set_headers_etag(iconFilename,
|
||||||
'image/png',
|
mediaFileMimeType(iconFilename),
|
||||||
mediaBinary, cookie,
|
mediaBinary, cookie,
|
||||||
callingDomain)
|
callingDomain)
|
||||||
self._write(mediaBinary)
|
self._write(mediaBinary)
|
||||||
|
@ -10821,26 +10780,7 @@ class PubServer(BaseHTTPRequestHandler):
|
||||||
except BaseException:
|
except BaseException:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
mediaFileType = 'application/json'
|
mediaFileType = mediaFileMimeType(checkPath)
|
||||||
if checkPath.endswith('.png'):
|
|
||||||
mediaFileType = 'image/png'
|
|
||||||
elif checkPath.endswith('.jpg'):
|
|
||||||
mediaFileType = 'image/jpeg'
|
|
||||||
elif checkPath.endswith('.gif'):
|
|
||||||
mediaFileType = 'image/gif'
|
|
||||||
elif checkPath.endswith('.webp'):
|
|
||||||
mediaFileType = 'image/webp'
|
|
||||||
elif checkPath.endswith('.avif'):
|
|
||||||
mediaFileType = 'image/avif'
|
|
||||||
elif checkPath.endswith('.mp4'):
|
|
||||||
mediaFileType = 'video/mp4'
|
|
||||||
elif checkPath.endswith('.ogv'):
|
|
||||||
mediaFileType = 'video/ogv'
|
|
||||||
elif checkPath.endswith('.mp3'):
|
|
||||||
mediaFileType = 'audio/mpeg'
|
|
||||||
elif checkPath.endswith('.ogg'):
|
|
||||||
mediaFileType = 'audio/ogg'
|
|
||||||
|
|
||||||
self._set_headers_head(mediaFileType, fileLength,
|
self._set_headers_head(mediaFileType, fileLength,
|
||||||
etag, callingDomain)
|
etag, callingDomain)
|
||||||
|
|
||||||
|
|
24
utils.py
24
utils.py
|
@ -1502,3 +1502,27 @@ def weekDayOfMonthStart(monthNumber: int, year: int) -> int:
|
||||||
"""
|
"""
|
||||||
firstDayOfMonth = datetime.datetime(year, monthNumber, 1, 0, 0)
|
firstDayOfMonth = datetime.datetime(year, monthNumber, 1, 0, 0)
|
||||||
return int(firstDayOfMonth.strftime("%w")) + 1
|
return int(firstDayOfMonth.strftime("%w")) + 1
|
||||||
|
|
||||||
|
|
||||||
|
def mediaFileMimeType(filename: str) -> str:
|
||||||
|
"""Given a media filename return its mime type
|
||||||
|
"""
|
||||||
|
if '.' not in filename:
|
||||||
|
return 'image/png'
|
||||||
|
extensions = {
|
||||||
|
'json': 'application/json',
|
||||||
|
'png': 'image/png',
|
||||||
|
'jpg': 'image/jpeg',
|
||||||
|
'jpeg': 'image/jpeg',
|
||||||
|
'gif': 'image/gif',
|
||||||
|
'webp': 'image/webp',
|
||||||
|
'avif': 'image/avif',
|
||||||
|
'mp3': 'audio/mpeg',
|
||||||
|
'ogg': 'audio/ogg',
|
||||||
|
'mp4': 'video/mp4',
|
||||||
|
'ogv': 'video/ogv'
|
||||||
|
}
|
||||||
|
fileExt = filename.split('.')[-1]
|
||||||
|
if not extensions.get(fileExt):
|
||||||
|
return 'image/png'
|
||||||
|
return extensions[fileExt]
|
||||||
|
|
Loading…
Reference in New Issue