From 01bbb82b5ddcc9234132ba959d8e5ffb769b050c Mon Sep 17 00:00:00 2001 From: yumoqing Date: Tue, 3 Dec 2019 11:30:15 +0800 Subject: [PATCH] ttt --- ahserver/configuredServer.py | 6 +++--- ahserver/processorResource.py | 9 ++++++--- ahserver/url2file.py | 36 ++++++++++++++++++++++++----------- 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/ahserver/configuredServer.py b/ahserver/configuredServer.py index 7fe4b9e..6e32725 100644 --- a/ahserver/configuredServer.py +++ b/ahserver/configuredServer.py @@ -42,8 +42,8 @@ class ConfiguredServer: def configPath(self,config): for p,prefix in config.website.paths: res = ProcessorResource(prefix,p,show_index=True, - follow_symlinks=True) - res.setProcessors(config.website.processors or {}) - res.setIndexes(config.website.indexes or []) + follow_symlinks=True, + indexs=configure.website.indexes, + processors=configure.website.processors) self.app.router.register_resource(res) diff --git a/ahserver/processorResource.py b/ahserver/processorResource.py index 371eb7d..4edab77 100644 --- a/ahserver/processorResource.py +++ b/ahserver/processorResource.py @@ -44,20 +44,23 @@ def i18nDICT(request): l = c.langMapping.get(lang,lang) return json.dumps(i18n.getLangDict(l)).encode(c.website.coding) -class ProcessorResource(StaticResource): +class ProcessorResource(StaticResource,Url2File): def __init__(self, prefix: str, directory: PathLike, *, name: Optional[str]=None, expect_handler: Optional[_ExpectHandler]=None, chunk_size: int=256 * 1024, show_index: bool=False, follow_symlinks: bool=False, - append_version: bool=False)-> None: - super().__init__(prefix, directory, + append_version: bool=False, + indexes:list=[], + processors:dict={}) None: + StaticResource.__init__(self,prefix, directory, name=name, expect_handler=expect_handler, chunk_size=chunk_size, show_index=show_index, follow_symlinks=follow_symlinks, append_version=append_version) + Url2File.__init__(self,directory,prefix,indexes,inherit=True) gr = self._routes.get('GET') self._routes.update({'POST':gr}) self._routes.update({'PUT':gr}) diff --git a/ahserver/url2file.py b/ahserver/url2file.py index 0a65dd0..56a4e28 100644 --- a/ahserver/url2file.py +++ b/ahserver/url2file.py @@ -3,12 +3,14 @@ import os class Url2File: - def __init__(self,paths: list,indexes: list, inherit: bool=False): - self.paths = paths + def __init__(self,path:str,prefix: str, + indexes: list, inherit: bool=False): + self.path = path + self.starts = prefix self.indexes = indexes self.inherit = inherit - def realurl(self,url): + def realurl(self,url:str) -> str : items = url.split('/') items = [ i for i in items if i != '.' ] while '..' in items: @@ -19,15 +21,26 @@ class Url2File: break return '/'.join(items) - def isFolder(self,url: str): - for r in self.paths: - rp = r + url + def isFolder(self,url: str) ->bool: + if url.startswith(self.starts): + rp = self.path + url[len(self.starts):] real_path = os.path.abspath(rp) if os.path.isdir(real_path): return True - return False + else + return False + + def isFile(self,url:str) ->bool: + if url.startswith(self.starts): + rp = self.path + url[len(self.starts):] + real_path = os.path.abspath(rp) + if os.path.isfile(real_path): + return True + else + return False - def defaultIndex(self,url: str): + + def defaultIndex(self,url: str) -> str: for p in self.indexes: rp = url + '/' + p r = self.url2file(rp) @@ -42,11 +55,12 @@ class Url2File: if self.isFolder(url): return self.defaultIndex(url) - for r in self.paths: - f = r + url + if url.startswith(self.starts): + f = self.path + url[len(self.starts):] real_path = os.path.abspath(f) if os.path.isfile(real_path): return f + if not self.inherit: return None items = url.split('/') @@ -56,7 +70,7 @@ class Url2File: return self.url2file(url) return None - def relatedurl(self,url: str, name: str): + def relatedurl(self,url: str, name: str) -> str: if url[-1] == '/': url = url[:-1]