Lowercase ext before querying the mimedb on mimetypes module, change isMainModule to runnableExamples (#10644)
This commit is contained in:
parent
cf87687c40
commit
f5e55162ba
1 changed files with 29 additions and 11 deletions
|
|
@ -9,6 +9,8 @@
|
||||||
|
|
||||||
## This module implements a mimetypes database
|
## This module implements a mimetypes database
|
||||||
import strtabs
|
import strtabs
|
||||||
|
from strutils import startsWith, toLowerAscii, strip
|
||||||
|
|
||||||
type
|
type
|
||||||
MimeDB* = object
|
MimeDB* = object
|
||||||
mimes: StringTableRef
|
mimes: StringTableRef
|
||||||
|
|
@ -1878,37 +1880,53 @@ const mimes* = {
|
||||||
"~": "application/x-trash"
|
"~": "application/x-trash"
|
||||||
}
|
}
|
||||||
|
|
||||||
from strutils import startsWith
|
|
||||||
|
|
||||||
proc newMimetypes*(): MimeDB =
|
func newMimetypes*(): MimeDB =
|
||||||
## Creates a new Mimetypes database. The database will contain the most
|
## Creates a new Mimetypes database. The database will contain the most
|
||||||
## common mimetypes.
|
## common mimetypes.
|
||||||
result.mimes = mimes.newStringTable()
|
result.mimes = mimes.newStringTable()
|
||||||
|
|
||||||
proc getMimetype*(mimedb: MimeDB, ext: string, default = "text/plain"): string =
|
func getMimetype*(mimedb: MimeDB, ext: string, default = "text/plain"): string =
|
||||||
## Gets mimetype which corresponds to ``ext``. Returns ``default`` if ``ext``
|
## Gets mimetype which corresponds to ``ext``. Returns ``default`` if ``ext``
|
||||||
## could not be found. ``ext`` can start with an optional dot which is ignored.
|
## could not be found. ``ext`` can start with an optional dot which is ignored.
|
||||||
|
## ``ext`` is lowercased before querying ``mimedb``.
|
||||||
if ext.startsWith("."):
|
if ext.startsWith("."):
|
||||||
result = mimedb.mimes.getOrDefault(ext.substr(1))
|
result = mimedb.mimes.getOrDefault(ext.toLowerAscii.substr(1))
|
||||||
else:
|
else:
|
||||||
result = mimedb.mimes.getOrDefault(ext)
|
result = mimedb.mimes.getOrDefault(ext.toLowerAscii())
|
||||||
if result == "":
|
if result == "":
|
||||||
return default
|
return default
|
||||||
|
|
||||||
proc getExt*(mimedb: MimeDB, mimetype: string, default = "txt"): string =
|
func getExt*(mimedb: MimeDB, mimetype: string, default = "txt"): string =
|
||||||
## Gets extension which corresponds to ``mimetype``. Returns ``default`` if
|
## Gets extension which corresponds to ``mimetype``. Returns ``default`` if
|
||||||
## ``mimetype`` could not be found. Extensions are returned without the
|
## ``mimetype`` could not be found. Extensions are returned without the
|
||||||
## leading dot.
|
## leading dot. ``mimetype`` is lowercased before querying ``mimedb``.
|
||||||
result = default
|
result = default
|
||||||
|
let mimeLowered = mimetype.toLowerAscii()
|
||||||
for e, m in mimedb.mimes:
|
for e, m in mimedb.mimes:
|
||||||
if m == mimetype:
|
if m == mimeLowered:
|
||||||
result = e
|
result = e
|
||||||
|
|
||||||
proc register*(mimedb: var MimeDB, ext: string, mimetype: string) =
|
func register*(mimedb: var MimeDB, ext: string, mimetype: string) =
|
||||||
## Adds ``mimetype`` to the ``mimedb``.
|
## Adds ``mimetype`` to the ``mimedb``.
|
||||||
mimedb.mimes[ext] = mimetype
|
## ``mimetype`` and ``ext`` are lowercased before registering on ``mimedb``.
|
||||||
|
assert ext.strip.len > 0, "ext argument can not be empty string"
|
||||||
|
assert mimetype.strip.len > 0, "mimetype argument can not be empty string"
|
||||||
|
mimedb.mimes[ext.toLowerAscii()] = mimetype.toLowerAscii()
|
||||||
|
|
||||||
when isMainModule:
|
runnableExamples:
|
||||||
var m = newMimetypes()
|
var m = newMimetypes()
|
||||||
assert m.getMimetype("mp4") == "video/mp4"
|
assert m.getMimetype("mp4") == "video/mp4"
|
||||||
assert m.getExt("text/html") == "html"
|
assert m.getExt("text/html") == "html"
|
||||||
|
## Values can be uppercase too.
|
||||||
|
assert m.getMimetype("MP4") == "video/mp4"
|
||||||
|
assert m.getExt("TEXT/HTML") == "html"
|
||||||
|
## If values are invalid then ``default`` is returned.
|
||||||
|
assert m.getMimetype("INVALID") == "text/plain"
|
||||||
|
assert m.getExt("INVALID/NONEXISTENT") == "txt"
|
||||||
|
assert m.getMimetype("") == "text/plain"
|
||||||
|
assert m.getExt("") == "txt"
|
||||||
|
## Register new Mimetypes.
|
||||||
|
m.register(ext="fakext", mimetype="text/fakelang")
|
||||||
|
assert m.getMimetype("fakext") == "text/fakelang"
|
||||||
|
assert m.getMimetype("FaKeXT") == "text/fakelang"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue