Make zuul_swift_upload.py more accurate for mime type detection
When a text file is quite generic like a .css or .js file libmagic will detect the file's content as text/plain. This patch adds a check of the mime database in order to guess the mime type given a file extension. This is only done if text/plain is detected by libmagic. Change-Id: Ic73332533fc4d6c1a89ccbdb59d44f3697c56dc0
This commit is contained in:
parent
a9ca416454
commit
ef04d7b047
@ -23,6 +23,7 @@ import argparse
|
||||
import logging
|
||||
import glob2
|
||||
import magic
|
||||
import mimetypes
|
||||
import os
|
||||
import Queue
|
||||
import requests
|
||||
@ -154,12 +155,20 @@ def get_file_mime(file_path):
|
||||
return None
|
||||
|
||||
if hasattr(magic, 'from_file'):
|
||||
return magic.from_file(file_path, mime=True)
|
||||
mime = magic.from_file(file_path, mime=True)
|
||||
else:
|
||||
# no magic.from_file, we might be using the libmagic bindings
|
||||
m = magic.open(magic.MAGIC_MIME)
|
||||
m.load()
|
||||
return m.file(file_path).split(';')[0]
|
||||
mime = m.file(file_path).split(';')[0]
|
||||
# libmagic can fail to detect the right mime type when content
|
||||
# is too generic. The case for css or js files. So in case
|
||||
# text/plain is detected then we rely on the mimetype db
|
||||
# to guess by file extension.
|
||||
if mime == 'text/plain':
|
||||
mime_guess = mimetypes.guess_type(file_path)[0]
|
||||
mime = mime_guess if mime_guess else mime
|
||||
return mime
|
||||
|
||||
|
||||
def get_file_metadata(file_path):
|
||||
|
Loading…
Reference in New Issue
Block a user