5 __all__ = ["filehandler"]
7 class filehandler(object):
8 def __init__(self, basedir):
11 def handledir(self, req, path):
14 def handlefile(self, req, path):
15 ctype = "application/octet-stream"
16 bn = os.path.basename(path)
18 ext = bn[bn.rindex('.') + 1:].lower()
19 if ext == "jpg" or ext == "jpeg":
31 req.ohead["Content-Type"] = ctype
32 return open(path, "r")
34 def resolvefile(self, req, curpath, el):
35 if os.path.isfile(pj(curpath, el)):
36 return pj(curpath, el)
38 for f in os.listdir(curpath):
45 def handlefinal(self, req, curpath, el):
47 return self.handledir(req, curpath)
48 return self.handlefile(req, self.resolvefile(req, curpath, el))
50 def handlenonfinal(self, req, curpath, el, rest):
53 def handleentry(self, req, curpath, el, rest):
56 if '/' in el or el[0] == '.':
58 if os.path.isdir(pj(curpath, el)):
59 return self.handlepath(req, pj(curpath, el), rest)
60 return self.handlenonfinal(req, curpath, el, rest)
62 def handlepath(self, req, curpath, rest):
65 return self.handlefinal(req, curpath, rest)
67 return self.handleentry(req, curpath, rest[:p], rest[p + 1:])
69 def handle(self, req):
71 if len(pi) > 0 and pi[0] == '/':
73 return self.handlepath(req, self.basedir, pi)