summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2017-11-28 17:34:21 +0100
committerAraq <rumpf_a@web.de>2017-11-28 17:34:30 +0100
commite2787c557cdabb45d90fa67fd54e57fab920171b (patch)
treeb1fb711b6293a81c7e63a8983d6079059342108a /lib
parenta22dba4a8bcfc04ef444ae9fd6b762ec093ac9e4 (diff)
downloadNim-e2787c557cdabb45d90fa67fd54e57fab920171b.tar.gz
mimetypes improvement: make mimetypes easier to use by allowing the extension to start with a dot which is what splitFile().ext returns
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/mimetypes.nim9
1 files changed, 7 insertions, 2 deletions
diff --git a/lib/pure/mimetypes.nim b/lib/pure/mimetypes.nim
index 1e315afb4..b397ef47b 100644
--- a/lib/pure/mimetypes.nim
+++ b/lib/pure/mimetypes.nim
@@ -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