This commit is contained in:
yumoqing 2019-12-03 11:30:15 +08:00
parent 7110fad601
commit 01bbb82b5d
3 changed files with 34 additions and 17 deletions

View File

@ -42,8 +42,8 @@ class ConfiguredServer:
def configPath(self,config): def configPath(self,config):
for p,prefix in config.website.paths: for p,prefix in config.website.paths:
res = ProcessorResource(prefix,p,show_index=True, res = ProcessorResource(prefix,p,show_index=True,
follow_symlinks=True) follow_symlinks=True,
res.setProcessors(config.website.processors or {}) indexs=configure.website.indexes,
res.setIndexes(config.website.indexes or []) processors=configure.website.processors)
self.app.router.register_resource(res) self.app.router.register_resource(res)

View File

@ -44,20 +44,23 @@ def i18nDICT(request):
l = c.langMapping.get(lang,lang) l = c.langMapping.get(lang,lang)
return json.dumps(i18n.getLangDict(l)).encode(c.website.coding) return json.dumps(i18n.getLangDict(l)).encode(c.website.coding)
class ProcessorResource(StaticResource): class ProcessorResource(StaticResource,Url2File):
def __init__(self, prefix: str, directory: PathLike, def __init__(self, prefix: str, directory: PathLike,
*, name: Optional[str]=None, *, name: Optional[str]=None,
expect_handler: Optional[_ExpectHandler]=None, expect_handler: Optional[_ExpectHandler]=None,
chunk_size: int=256 * 1024, chunk_size: int=256 * 1024,
show_index: bool=False, follow_symlinks: bool=False, show_index: bool=False, follow_symlinks: bool=False,
append_version: bool=False)-> None: append_version: bool=False,
super().__init__(prefix, directory, indexes:list=[],
processors:dict={}) None:
StaticResource.__init__(self,prefix, directory,
name=name, name=name,
expect_handler=expect_handler, expect_handler=expect_handler,
chunk_size=chunk_size, chunk_size=chunk_size,
show_index=show_index, show_index=show_index,
follow_symlinks=follow_symlinks, follow_symlinks=follow_symlinks,
append_version=append_version) append_version=append_version)
Url2File.__init__(self,directory,prefix,indexes,inherit=True)
gr = self._routes.get('GET') gr = self._routes.get('GET')
self._routes.update({'POST':gr}) self._routes.update({'POST':gr})
self._routes.update({'PUT':gr}) self._routes.update({'PUT':gr})

View File

@ -3,12 +3,14 @@
import os import os
class Url2File: class Url2File:
def __init__(self,paths: list,indexes: list, inherit: bool=False): def __init__(self,path:str,prefix: str,
self.paths = paths indexes: list, inherit: bool=False):
self.path = path
self.starts = prefix
self.indexes = indexes self.indexes = indexes
self.inherit = inherit self.inherit = inherit
def realurl(self,url): def realurl(self,url:str) -> str :
items = url.split('/') items = url.split('/')
items = [ i for i in items if i != '.' ] items = [ i for i in items if i != '.' ]
while '..' in items: while '..' in items:
@ -19,15 +21,26 @@ class Url2File:
break break
return '/'.join(items) return '/'.join(items)
def isFolder(self,url: str): def isFolder(self,url: str) ->bool:
for r in self.paths: if url.startswith(self.starts):
rp = r + url rp = self.path + url[len(self.starts):]
real_path = os.path.abspath(rp) real_path = os.path.abspath(rp)
if os.path.isdir(real_path): if os.path.isdir(real_path):
return True 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: for p in self.indexes:
rp = url + '/' + p rp = url + '/' + p
r = self.url2file(rp) r = self.url2file(rp)
@ -42,11 +55,12 @@ class Url2File:
if self.isFolder(url): if self.isFolder(url):
return self.defaultIndex(url) return self.defaultIndex(url)
for r in self.paths: if url.startswith(self.starts):
f = r + url f = self.path + url[len(self.starts):]
real_path = os.path.abspath(f) real_path = os.path.abspath(f)
if os.path.isfile(real_path): if os.path.isfile(real_path):
return f return f
if not self.inherit: if not self.inherit:
return None return None
items = url.split('/') items = url.split('/')
@ -56,7 +70,7 @@ class Url2File:
return self.url2file(url) return self.url2file(url)
return None return None
def relatedurl(self,url: str, name: str): def relatedurl(self,url: str, name: str) -> str:
if url[-1] == '/': if url[-1] == '/':
url = url[:-1] url = url[:-1]