Detecting font files

main
Bob Mottram 2020-05-26 20:29:15 +01:00
parent 7cd33447ca
commit aacbf8cbb9
2 changed files with 87 additions and 36 deletions

View File

@ -603,7 +603,6 @@ def saveMediaInFormPOST(mediaBytes, debug: bool,
mediaLocation = -1 mediaLocation = -1
searchStr = '' searchStr = ''
startPos = 0
filename = None filename = None
# directly search the binary array for the beginning # directly search the binary array for the beginning
@ -616,14 +615,9 @@ def saveMediaInFormPOST(mediaBytes, debug: bool,
'mp4': 'video/mp4', 'mp4': 'video/mp4',
'ogv': 'video/ogv', 'ogv': 'video/ogv',
'mp3': 'audio/mpeg', 'mp3': 'audio/mpeg',
'ogg': 'audio/ogg', 'ogg': 'audio/ogg'
'wOFF': 'application/font-woff',
'wOF2': 'application/font-woff2',
'ttf': 'application/x-font-truetype',
'OTTO': 'application/x-font-opentype'
} }
detectedExtension = None detectedExtension = None
isFont = False
for extension, contentType in extensionList.items(): for extension, contentType in extensionList.items():
searchStr = b'Content-Type: ' + contentType.encode('utf8', 'ignore') searchStr = b'Content-Type: ' + contentType.encode('utf8', 'ignore')
mediaLocation = mediaBytes.find(searchStr) mediaLocation = mediaBytes.find(searchStr)
@ -638,26 +632,10 @@ def saveMediaInFormPOST(mediaBytes, debug: bool,
searchStr.decode().split('/')[0].replace('Content-Type: ', '') searchStr.decode().split('/')[0].replace('Content-Type: ', '')
detectedExtension = extension detectedExtension = extension
break break
else:
# font binaries
searchStr = extension.encode('utf8', 'ignore')
mediaLocation = mediaBytes.find(searchStr)
if mediaLocation > -1:
if extension == 'wOFF':
detectedExtension = 'woff'
elif extension == 'wOF2':
detectedExtension = 'woff2'
elif extension == 'OTTO':
detectedExtension = 'otf'
isFont = True
break
if not filename: if not filename:
return None, None return None, None
if isFont:
startPos = mediaLocation
else:
# locate the beginning of the image, after any # locate the beginning of the image, after any
# carriage returns # carriage returns
startPos = mediaLocation + len(searchStr) startPos = mediaLocation + len(searchStr)
@ -668,8 +646,7 @@ def saveMediaInFormPOST(mediaBytes, debug: bool,
break break
# remove any existing image files with a different format # remove any existing image files with a different format
extensionTypes = ('png', 'jpg', 'jpeg', 'gif', 'webp', extensionTypes = ('png', 'jpg', 'jpeg', 'gif', 'webp')
'woff', 'woff2', 'ttf', 'otf')
for ex in extensionTypes: for ex in extensionTypes:
if ex == detectedExtension: if ex == detectedExtension:
continue continue
@ -687,6 +664,74 @@ def saveMediaInFormPOST(mediaBytes, debug: bool,
return filename, attachmentMediaType return filename, attachmentMediaType
def saveFontInFormPOST(fontBytes, debug: bool,
filenameBase=None) -> (str, str):
"""Saves the given font bytes extracted from http form POST
Returns the filename and attachment type
"""
if not fontBytes:
if debug:
print('DEBUG: No font found within POST')
return None, None
fontLocation = -1
searchStr = ''
filename = None
# directly search the binary array for the beginning
# of a font file
extensionList = {
'wOFF': 'application/font-woff',
'wOF2': 'application/font-woff2',
'ttf': 'application/x-font-truetype',
'OTTO': 'application/x-font-opentype'
}
detectedExtension = None
for extension, contentType in extensionList.items():
# font binaries
searchStr = extension.encode('utf8', 'ignore')
fontLocation = fontBytes.find(searchStr)
if fontLocation > -1:
if extension == 'wOFF':
detectedExtension = 'woff'
elif extension == 'wOF2':
detectedExtension = 'woff2'
elif extension == 'OTTO':
detectedExtension = 'otf'
if detectedExtension:
filename = filenameBase + '.' + detectedExtension
attachmentMediaType = contentType
break
if not filename:
# default to ttf if file type not explicitly detected
detectedExtension = 'ttf'
filename = filenameBase + '.' + detectedExtension
attachmentMediaType = 'ttf'
fontLocation = 0
startPos = fontLocation
# remove any existing image files with a different format
extensionTypes = ('woff', 'woff2', 'ttf', 'otf')
for ex in extensionTypes:
if ex == detectedExtension:
continue
possibleOtherFormat = \
filename.replace('.temp', '').replace('.' +
detectedExtension, '.' +
ex)
if os.path.isfile(possibleOtherFormat):
os.remove(possibleOtherFormat)
fd = open(filename, 'wb')
fd.write(fontBytes[startPos:])
fd.close()
return filename, attachmentMediaType
def extractTextFieldsInPOST(postBytes, boundary, debug: bool) -> {}: def extractTextFieldsInPOST(postBytes, boundary, debug: bool) -> {}:
"""Returns a dictionary containing the text fields of a http form POST """Returns a dictionary containing the text fields of a http form POST
The boundary argument comes from the http header The boundary argument comes from the http header

View File

@ -163,6 +163,7 @@ from content import replaceEmojiFromTags
from content import addHtmlTags from content import addHtmlTags
from content import extractMediaInFormPOST from content import extractMediaInFormPOST
from content import saveMediaInFormPOST from content import saveMediaInFormPOST
from content import saveFontInFormPOST
from content import extractTextFieldsInPOST from content import extractTextFieldsInPOST
from media import removeMetaData from media import removeMetaData
from cache import storePersonInCache from cache import storePersonInCache
@ -5450,6 +5451,11 @@ class PubServer(BaseHTTPRequestHandler):
nickname + '@' + self.server.domain + \ nickname + '@' + self.server.domain + \
'/' + mType + '.temp' '/' + mType + '.temp'
if mType == 'customFont':
filename, attachmentMediaType = \
saveFontInFormPOST(mediaBytes, self.server.debug,
filenameBase)
else:
filename, attachmentMediaType = \ filename, attachmentMediaType = \
saveMediaInFormPOST(mediaBytes, self.server.debug, saveMediaInFormPOST(mediaBytes, self.server.debug,
filenameBase) filenameBase)