Permalink
Browse files

added mimetype <-> extension webservice

  • Loading branch information...
1 parent f5ce61d commit 01dede81e786d85bc8f3f086935fa7bdf762dcef @ptarjan committed Aug 19, 2009
Showing with 1,303 additions and 11 deletions.
  1. +14 −4 index.html
  2. +1 −1 index.yaml
  3. +51 −6 main.py
  4. +1,236 −0 mime.types
  5. +1 −0 simplejson
View
@@ -9,18 +9,21 @@
.header h1 {
text-align : center;
}
-#examples {
+#examples, #sets, #mimetypes {
border : 1px solid #ddd;
padding : 20px;
margin : 10px 0px;
+}
+
+#examples {
background : #fff6ef;
}
#sets {
- border : 1px solid #ddd;
- padding : 20px;
- margin : 10px 0px;
background : #f6ffef;
}
+#mimetypes {
+ background : #effff6;
+}
{% endblock %}
{% block content %}
@@ -54,5 +57,12 @@
{% endfor %}
</ul>
</div>
+
+ <div id="mimetypes">
+ <h2>Mimetypes</h2>
+ <p>File extension to mimetype lookup like <a href="/ext/html">/ext/html</a> or <a href="/ext/m4a">/ext/m4a</a>
+ <p>Mimetype fo file extension like <a href="/mimetype/application/postscript">/mimetype/application/postscript</a> or <a href="/mimetype/application/zip">/mimetype/application/zip</a>
+ <p>Or the whole dump in <a href="/mimetypes">json</a> or <a href="/mimetypes?format=xml">xml</a> (from <a href="http://docs.python.org/library/mimetypes.html">python mimetypes</a> and <a href="http://www.home.unix-ag.org/bmeurer/files/dotfiles/dot.mime.types">apache</a>)
+ </div>
</div>
{% endblock %}
View
@@ -10,7 +10,7 @@ indexes:
# automatically uploaded to the admin console when you next deploy
# your application using appcfg.py.
-# Used 32 times in query history.
+# Used 35 times in query history.
- kind: Icon
properties:
- name: set
View
57 main.py
@@ -3,6 +3,7 @@
import os
import mimetypes
import logging
+import simplejson
from urllib import unquote
import wsgiref.handlers
@@ -18,6 +19,10 @@
from datetime import timedelta, datetime
+mimetypes.knownfiles.append("mime.types")
+mimetypes.init(mimetypes.knownfiles)
+
+
class Set(db.Model):
name = db.StringProperty(required=True)
modified = db.DateTimeProperty(auto_now=True)
@@ -88,17 +93,17 @@ def get(self, default_set="crystal"):
setname = parts[0]
mimetype = "/".join(parts[1:])
- guess, encoding = mimetypes.guess_type("dummy." + mimetype)
- if guess :
- logging.info("Guessed '%s' for '%s'" % (guess, mimetype))
- mimetype = guess
-
set = Set.all().filter("name = ", setname).get()
if not set :
setname = default_set
set = Set.all().filter("name = ", setname).get()
mimetype = "/".join(parts)
+ guess, encoding = mimetypes.guess_type("dummy." + mimetype)
+ if guess :
+ logging.info("Guessed '%s' for '%s'" % (guess, mimetype))
+ mimetype = guess
+
icon = Icon.all().filter("set =", set).filter("mimetype =", mimetype).get()
if not icon :
parts = mimetype.split("/")
@@ -214,13 +219,53 @@ def post(self, setname) :
return self.get(setname)
+class MimetypesHandler(webapp.RequestHandler):
+ def get(self) :
+ keys = mimetypes.types_map.keys()
+ keys.sort()
+ map = []
+ for k in keys :
+ map.append({k: mimetypes.types_map[k]})
+ if self.request.get("format") != "xml" :
+ self.response.headers['Content-Type'] = 'application/json'
+ self.response.out.write(simplejson.dumps(map))
+ else :
+ self.response.headers['Content-Type'] = 'application/xml'
+ o = self.response.out
+ o.write("<mimetypes>")
+ for f in map :
+ k = f.keys()[0]
+ v = f[k]
+ o.write("<file><ext>%s</ext><mimetype>%s</mimetype></file>" % (k,v))
+ o.write("</mimetypes>")
+
+class MimetypeLookupHandler(webapp.RequestHandler):
+ def get(self, method, type) :
+ if method == "ext" :
+ guess, handler = mimetypes.guess_type("dummy." + type)
+ if guess :
+ self.response.out.write(guess)
+ else :
+ self.response.set_status(404)
+ self.response.out.write("Extension '%s' has no known mimetype" % (type))
+ elif method == "mimetype" :
+ ext = mimetypes.guess_extension(type)
+ if ext and ext[0] == "." :
+ self.response.out.write(ext[1:])
+ else :
+ self.response.set_status(404)
+ self.response.out.write("Mimetype '%s' has no known extension" % (type))
+
+
def main():
application = webapp.WSGIApplication([
(r'/', IndexHandler),
(r'/favicon.ico', FaviconHandler),
(r'/(.+)/', SetHandler),
- (r'/create', CreateHandler),
+ (r'/create/?', CreateHandler),
(r'/create/(.+)', CreateIconHandler),
+ (r'/mimetypes', MimetypesHandler),
+ (r'/(ext|mimetype)/(.+)', MimetypeLookupHandler),
(r'/.+', IconHandler),
],
debug=True)
Oops, something went wrong.

0 comments on commit 01dede8

Please sign in to comment.