From 499527bbe8f4ce911c12c73deec74b9d3efed40e Mon Sep 17 00:00:00 2001 From: yumoqing Date: Wed, 22 Feb 2023 23:37:09 +0800 Subject: [PATCH] bugfix --- ahserver/configuredServer.py | 2 ++ ahserver/filestorage.py | 66 +++++++++++++++++++++++++++++++++-- ahserver/processorResource.py | 7 +--- 3 files changed, 67 insertions(+), 8 deletions(-) diff --git a/ahserver/configuredServer.py b/ahserver/configuredServer.py index b6bfd45..48e1d46 100644 --- a/ahserver/configuredServer.py +++ b/ahserver/configuredServer.py @@ -15,6 +15,7 @@ from .processorResource import ProcessorResource from .auth_api import AuthAPI from .myTE import setupTemplateEngine from .globalEnv import initEnv +from .filestorage import TmpFileRecord class ConfiguredServer(AppLogger): def __init__(self, auth_klass=AuthAPI, workdir=None): @@ -34,6 +35,7 @@ class ConfiguredServer(AppLogger): self.configPath(config) def run(self): + a = TmpFileRecord() config = getConfig() ssl_context = None if config.website.ssl: diff --git a/ahserver/filestorage.py b/ahserver/filestorage.py index d585ac6..337a99e 100644 --- a/ahserver/filestorage.py +++ b/ahserver/filestorage.py @@ -1,17 +1,79 @@ # fileUpload.py +import asyncio import os import time import tempfile import aiofiles +import json +import time from appPublic.folderUtils import _mkdir from appPublic.jsonConfig import getConfig +from appPublic.Singleton import SingletonDecorator +@SingletonDecorator +class TmpFileRecord: + def __init__(self, timeout=3600): + self.filetime = {} + self.changed_flg = False + self.timeout = timeout + self.time_period = 10 + self.filename = self.savefilename() + self.loop = asyncio.get_event_loop() + self.loop.call_later(0.01, self.load) + print('TmpFileRecord() .........................') + + def newtmpfile(self, path:str): + self.filetime[path] = time.time() + self.change_flg = True + + def savefilename(self): + config = getConfig() + root = config.filesroot or tempfile.gettempdir() + return root + '/tmpfile_rec.json' + + async def save(self): + if not self.change_flg: + return + async with aiofiles.open(self.filename, 'bw') as f: + s = json.dumps(self.filetime) + b = s.encode('utf-8') + await f.write(b) + await f.flush() + self.change_flg = False + + async def load(self): + print('load() called ............') + fn = self.filename + if not os.path.isfile(fn): + return + async with aiofiles.open(fn, 'br') as f: + b = await f.read() + s = b.decode('utf-8') + self.filetime = json.loads(s) + + self.remove() + + async def remove(self): + tim = time.time() + ft = {k:v for k,v in self.filetime.items()} + for k,v in ft: + if tim - v > self.timeout: + self.rmfile(k) + del self.tiletime[k] + await self.save() + self.loop.call_later(self.time_period, self.remove) + + def rmfile(self, name:str): + config = getConfig() + os.remove(config.fileroot + name) + class FileStorage: def __init__(self): config = getConfig() self.root = config.filesroot or tempfile.gettempdir() + self.tfr = TmpFileRecord() def realPath(self,path): if path[0] == '/': @@ -48,6 +110,6 @@ class FileStorage: siz += len(d); await f.write(d) await f.flush() - print(f'{name=} file({fpath}) write {siz} bytes') - + # print(f'{name=} file({fpath}) write {siz} bytes') + self.tfr.newtmpfile(fpath) return fpath diff --git a/ahserver/processorResource.py b/ahserver/processorResource.py index 905fbb0..bedf074 100644 --- a/ahserver/processorResource.py +++ b/ahserver/processorResource.py @@ -135,7 +135,7 @@ class ProcessorResource(AppLogger, StaticResource,Url2File): print_exc() print('-----------except out ------------') break; - print(f'getPostData():{ns=}') + # print(f'getPostData():{ns=}') # showcallstack() return ns @@ -193,15 +193,10 @@ class ProcessorResource(AppLogger, StaticResource,Url2File): async def getArgs(): - print(f'getArgs, url={str(request.url)}') - if hasattr(request, 'params_kw'): - return request.params_kw - print('request.params=', request.get('params_kw')) ns = DictObject() if request.method == 'POST': return await self.getPostData(request) ns = multiDict2Dict(request.query) - request.params_kw = ns return ns self.y_env.i18n = serveri18n