mirror of
https://github.com/ankitects/anki.git
synced 2025-09-19 14:32:22 -04:00
detect image extension, catch errors, add timeout
This commit is contained in:
parent
56444ab967
commit
8e7ef1d1be
2 changed files with 20 additions and 3 deletions
|
@ -128,9 +128,21 @@ create table meta (dirMod int, lastUsn int); insert into meta values (0, 0);
|
||||||
def addFile(self, opath):
|
def addFile(self, opath):
|
||||||
return self.writeData(opath, open(opath, "rb").read())
|
return self.writeData(opath, open(opath, "rb").read())
|
||||||
|
|
||||||
def writeData(self, opath, data):
|
def writeData(self, opath, data, typeHint=None):
|
||||||
# if fname is a full path, use only the basename
|
# if fname is a full path, use only the basename
|
||||||
fname = os.path.basename(opath)
|
fname = os.path.basename(opath)
|
||||||
|
|
||||||
|
# if it's missing an extension and a type hint was provided, use that
|
||||||
|
if not os.path.splitext(fname)[1] and typeHint:
|
||||||
|
# mimetypes is returning '.jpe' even after calling .init(), so we'll do
|
||||||
|
# it manually instead
|
||||||
|
typeMap = {
|
||||||
|
"image/jpeg": ".jpg",
|
||||||
|
"image/png": ".png",
|
||||||
|
}
|
||||||
|
if typeHint in typeMap:
|
||||||
|
fname += typeMap[typeHint]
|
||||||
|
|
||||||
# make sure we write it in NFC form (on mac will autoconvert to NFD),
|
# make sure we write it in NFC form (on mac will autoconvert to NFD),
|
||||||
# and return an NFC-encoded reference
|
# and return an NFC-encoded reference
|
||||||
fname = unicodedata.normalize("NFC", fname)
|
fname = unicodedata.normalize("NFC", fname)
|
||||||
|
|
|
@ -591,26 +591,31 @@ to a cloze type first, via Edit>Change Note Type."""))
|
||||||
# fetch it into a temporary folder
|
# fetch it into a temporary folder
|
||||||
self.mw.progress.start(
|
self.mw.progress.start(
|
||||||
immediate=True, parent=self.parentWindow)
|
immediate=True, parent=self.parentWindow)
|
||||||
|
ct = None
|
||||||
try:
|
try:
|
||||||
if local:
|
if local:
|
||||||
req = urllib.request.Request(url, None, {
|
req = urllib.request.Request(url, None, {
|
||||||
'User-Agent': 'Mozilla/5.0 (compatible; Anki)'})
|
'User-Agent': 'Mozilla/5.0 (compatible; Anki)'})
|
||||||
filecontents = urllib.request.urlopen(req).read()
|
filecontents = urllib.request.urlopen(req).read()
|
||||||
else:
|
else:
|
||||||
r = requests.get(url)
|
r = requests.get(url, timeout=30)
|
||||||
if r.status_code != 200:
|
if r.status_code != 200:
|
||||||
showWarning(_("Unexpected response code: %s") % r.status_code)
|
showWarning(_("Unexpected response code: %s") % r.status_code)
|
||||||
return
|
return
|
||||||
filecontents = r.content
|
filecontents = r.content
|
||||||
|
ct = r.headers.get("content-type")
|
||||||
except urllib.error.URLError as e:
|
except urllib.error.URLError as e:
|
||||||
showWarning(_("An error occurred while opening %s") % e)
|
showWarning(_("An error occurred while opening %s") % e)
|
||||||
return
|
return
|
||||||
|
except requests.exceptions.RequestException as e:
|
||||||
|
showWarning(_("An error occurred while opening %s") % e)
|
||||||
|
return
|
||||||
finally:
|
finally:
|
||||||
self.mw.progress.finish()
|
self.mw.progress.finish()
|
||||||
# strip off any query string
|
# strip off any query string
|
||||||
url = re.sub("\?.*?$", "", url)
|
url = re.sub("\?.*?$", "", url)
|
||||||
path = urllib.parse.unquote(url)
|
path = urllib.parse.unquote(url)
|
||||||
return self.mw.col.media.writeData(path, filecontents)
|
return self.mw.col.media.writeData(path, filecontents, typeHint=ct)
|
||||||
|
|
||||||
# Paste/drag&drop
|
# Paste/drag&drop
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
Loading…
Reference in a new issue