mirror of
https://github.com/codex-storage/deluge.git
synced 2025-01-11 20:14:13 +00:00
improve the LookupResource class so plugin resources can be put in a sub directory
This commit is contained in:
parent
5e829ca160
commit
3367d0af48
@ -245,27 +245,40 @@ class LookupResource(resource.Resource, component.Component):
|
|||||||
def __init__(self, name, *directories):
|
def __init__(self, name, *directories):
|
||||||
resource.Resource.__init__(self)
|
resource.Resource.__init__(self)
|
||||||
component.Component.__init__(self, name)
|
component.Component.__init__(self, name)
|
||||||
self.__directories = list(directories)
|
|
||||||
|
|
||||||
@property
|
self.__paths = {}
|
||||||
def directories(self):
|
for directory in directories:
|
||||||
return self.__directories
|
self.addDirectory(directory)
|
||||||
|
|
||||||
|
def addDirectory(self, directory, path=""):
|
||||||
|
log.debug("Adding directory `%s` with path `%s`", directory, path)
|
||||||
|
paths = self.__paths.get(path, [])
|
||||||
|
paths.append(directory)
|
||||||
|
self.__paths[path] = paths
|
||||||
|
|
||||||
def getChild(self, path, request):
|
def getChild(self, path, request):
|
||||||
request.path = path
|
if hasattr(request, 'lookup_path'):
|
||||||
|
request.lookup_path += '/' + path
|
||||||
|
else:
|
||||||
|
request.lookup_path = path
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def render(self, request):
|
def render(self, request):
|
||||||
log.debug("Requested path: '%s'", request.path)
|
log.debug("Requested path: '%s'", request.lookup_path)
|
||||||
for lookup in self.directories:
|
path = os.path.dirname(request.lookup_path)
|
||||||
if request.path in os.listdir(lookup):
|
|
||||||
path = os.path.join(lookup, request.path)
|
if path not in self.__paths:
|
||||||
|
request.setResponseCode(http.NOT_FOUND)
|
||||||
|
return "<h1>404 - Not Found</h1>"
|
||||||
|
|
||||||
|
filename = os.path.basename(request.path)
|
||||||
|
for directory in self.__paths[path]:
|
||||||
|
if filename in os.listdir(directory):
|
||||||
|
path = os.path.join(directory, filename)
|
||||||
log.debug("Serving path: '%s'", path)
|
log.debug("Serving path: '%s'", path)
|
||||||
mime_type = mimetypes.guess_type(path)
|
mime_type = mimetypes.guess_type(path)
|
||||||
request.setHeader("content-type", mime_type[0])
|
request.setHeader("content-type", mime_type[0])
|
||||||
return open(path, "rb").read()
|
return open(path, "rb").read()
|
||||||
request.setResponseCode(http.NOT_FOUND)
|
|
||||||
return "<h1>404 - Not Found</h1>"
|
|
||||||
|
|
||||||
class TopLevel(resource.Resource):
|
class TopLevel(resource.Resource):
|
||||||
addSlash = True
|
addSlash = True
|
||||||
|
Loading…
x
Reference in New Issue
Block a user