Add utils.guess_mimetype
This commit is contained in:
parent
24148c649e
commit
490fe5e1a3
@ -29,7 +29,6 @@ import json
|
|||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
import textwrap
|
import textwrap
|
||||||
import mimetypes
|
|
||||||
import urllib
|
import urllib
|
||||||
import collections
|
import collections
|
||||||
import base64
|
import base64
|
||||||
@ -368,8 +367,7 @@ def qute_help(url):
|
|||||||
bdata = utils.read_file(path, binary=True)
|
bdata = utils.read_file(path, binary=True)
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
raise SchemeOSError(e)
|
raise SchemeOSError(e)
|
||||||
mimetype, _encoding = mimetypes.guess_type(urlpath)
|
mimetype = utils.guess_mimetype(urlpath)
|
||||||
assert mimetype is not None, url
|
|
||||||
return mimetype, bdata
|
return mimetype, bdata
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -527,9 +525,7 @@ def qute_pdfjs(url):
|
|||||||
filename = QUrlQuery(url).queryItemValue('filename')
|
filename = QUrlQuery(url).queryItemValue('filename')
|
||||||
with open(filename, 'rb') as f:
|
with open(filename, 'rb') as f:
|
||||||
data = f.read()
|
data = f.read()
|
||||||
mimetype, _encoding = mimetypes.guess_type(filename)
|
mimetype = utils.guess_mimetype(filename, fallback=True)
|
||||||
if mimetype is None:
|
|
||||||
mimetype = 'application/octet-stream'
|
|
||||||
return mimetype, data
|
return mimetype, data
|
||||||
|
|
||||||
if url.path() == '/web/viewer.html':
|
if url.path() == '/web/viewer.html':
|
||||||
@ -547,7 +543,5 @@ def qute_pdfjs(url):
|
|||||||
"pdfjs resource requested but not found: {}".format(e.path))
|
"pdfjs resource requested but not found: {}".format(e.path))
|
||||||
raise NotFoundError("Can't find pdfjs resource '{}'".format(e.path))
|
raise NotFoundError("Can't find pdfjs resource '{}'".format(e.path))
|
||||||
else:
|
else:
|
||||||
mimetype, _encoding = mimetypes.guess_type(url.fileName())
|
mimetype = utils.guess_mimetype(url.fileName(), fallback=True)
|
||||||
if mimetype is None:
|
|
||||||
mimetype = 'application/octet-stream'
|
|
||||||
return mimetype, data
|
return mimetype, data
|
||||||
|
@ -22,7 +22,6 @@
|
|||||||
import os
|
import os
|
||||||
import os.path
|
import os.path
|
||||||
import contextlib
|
import contextlib
|
||||||
import mimetypes
|
|
||||||
import html
|
import html
|
||||||
|
|
||||||
import jinja2
|
import jinja2
|
||||||
@ -108,9 +107,8 @@ class Environment(jinja2.Environment):
|
|||||||
"""Get a data: url for the broken qutebrowser logo."""
|
"""Get a data: url for the broken qutebrowser logo."""
|
||||||
data = utils.read_file(path, binary=True)
|
data = utils.read_file(path, binary=True)
|
||||||
filename = utils.resource_filename(path)
|
filename = utils.resource_filename(path)
|
||||||
mimetype = mimetypes.guess_type(filename)
|
mimetype = utils.guess_mimetype(filename)
|
||||||
assert mimetype is not None, path
|
return urlutils.data_url(mimetype, data).toString()
|
||||||
return urlutils.data_url(mimetype[0], data).toString()
|
|
||||||
|
|
||||||
def getattr(self, obj, attribute):
|
def getattr(self, obj, attribute):
|
||||||
"""Override jinja's getattr() to be less clever.
|
"""Override jinja's getattr() to be less clever.
|
||||||
|
@ -33,6 +33,7 @@ import contextlib
|
|||||||
import socket
|
import socket
|
||||||
import shlex
|
import shlex
|
||||||
import glob
|
import glob
|
||||||
|
import mimetypes
|
||||||
|
|
||||||
from PyQt5.QtCore import QUrl
|
from PyQt5.QtCore import QUrl
|
||||||
from PyQt5.QtGui import QColor, QClipboard, QDesktopServices
|
from PyQt5.QtGui import QColor, QClipboard, QDesktopServices
|
||||||
@ -683,3 +684,19 @@ def chunk(elems, n):
|
|||||||
raise ValueError("n needs to be at least 1!")
|
raise ValueError("n needs to be at least 1!")
|
||||||
for i in range(0, len(elems), n):
|
for i in range(0, len(elems), n):
|
||||||
yield elems[i:i + n]
|
yield elems[i:i + n]
|
||||||
|
|
||||||
|
|
||||||
|
def guess_mimetype(filename, fallback=False):
|
||||||
|
"""Guess a mimetype based on a filename.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
filename: The filename to check.
|
||||||
|
fallback: Fall back to application/octet-stream if unknown.
|
||||||
|
"""
|
||||||
|
mimetype, _encoding = mimetypes.guess_type(filename)
|
||||||
|
if mimetype is None:
|
||||||
|
if fallback:
|
||||||
|
return 'application/octet-stream'
|
||||||
|
else:
|
||||||
|
raise ValueError("Got None mimetype for {}".format(filename))
|
||||||
|
return mimetype
|
||||||
|
@ -816,3 +816,16 @@ def test_chunk(elems, n, expected):
|
|||||||
def test_chunk_invalid(n):
|
def test_chunk_invalid(n):
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
list(utils.chunk([], n))
|
list(utils.chunk([], n))
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('filename, expected', [
|
||||||
|
('test.jpg', 'image/jpeg'),
|
||||||
|
('test.blabla', 'application/octet-stream'),
|
||||||
|
])
|
||||||
|
def test_guess_mimetype(filename, expected):
|
||||||
|
assert utils.guess_mimetype(filename, fallback=True) == expected
|
||||||
|
|
||||||
|
|
||||||
|
def test_guess_mimetype_no_fallback():
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
utils.guess_mimetype('test.blabla')
|
||||||
|
Loading…
Reference in New Issue
Block a user