mimetypes improvement: make mimetypes easier to use by allowing the extension to start with a dot which is what splitFile().ext returns
This commit is contained in:
parent
a22dba4a8b
commit
e2787c557c
1 changed files with 7 additions and 2 deletions
|
|
@ -491,6 +491,8 @@ const mimes* = {
|
|||
"vrml": "x-world/x-vrml",
|
||||
"wrl": "x-world/x-vrml"}
|
||||
|
||||
from strutils import startsWith
|
||||
|
||||
proc newMimetypes*(): MimeDB =
|
||||
## Creates a new Mimetypes database. The database will contain the most
|
||||
## common mimetypes.
|
||||
|
|
@ -498,8 +500,11 @@ proc newMimetypes*(): MimeDB =
|
|||
|
||||
proc getMimetype*(mimedb: MimeDB, ext: string, default = "text/plain"): string =
|
||||
## Gets mimetype which corresponds to ``ext``. Returns ``default`` if ``ext``
|
||||
## could not be found.
|
||||
result = mimedb.mimes.getOrDefault(ext)
|
||||
## could not be found. ``ext`` can start with an optional dot which is ignored.
|
||||
if ext.startsWith("."):
|
||||
result = mimedb.mimes.getOrDefault(ext.substr(1))
|
||||
else:
|
||||
result = mimedb.mimes.getOrDefault(ext)
|
||||
if result == "":
|
||||
return default
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue