This commit is contained in:
yumoqing 2023-02-22 23:37:09 +08:00
parent a14b95abd7
commit 499527bbe8
3 changed files with 67 additions and 8 deletions

View File

@ -15,6 +15,7 @@ from .processorResource import ProcessorResource
from .auth_api import AuthAPI from .auth_api import AuthAPI
from .myTE import setupTemplateEngine from .myTE import setupTemplateEngine
from .globalEnv import initEnv from .globalEnv import initEnv
from .filestorage import TmpFileRecord
class ConfiguredServer(AppLogger): class ConfiguredServer(AppLogger):
def __init__(self, auth_klass=AuthAPI, workdir=None): def __init__(self, auth_klass=AuthAPI, workdir=None):
@ -34,6 +35,7 @@ class ConfiguredServer(AppLogger):
self.configPath(config) self.configPath(config)
def run(self): def run(self):
a = TmpFileRecord()
config = getConfig() config = getConfig()
ssl_context = None ssl_context = None
if config.website.ssl: if config.website.ssl:

View File

@ -1,17 +1,79 @@
# fileUpload.py # fileUpload.py
import asyncio
import os import os
import time import time
import tempfile import tempfile
import aiofiles import aiofiles
import json
import time
from appPublic.folderUtils import _mkdir from appPublic.folderUtils import _mkdir
from appPublic.jsonConfig import getConfig 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: class FileStorage:
def __init__(self): def __init__(self):
config = getConfig() config = getConfig()
self.root = config.filesroot or tempfile.gettempdir() self.root = config.filesroot or tempfile.gettempdir()
self.tfr = TmpFileRecord()
def realPath(self,path): def realPath(self,path):
if path[0] == '/': if path[0] == '/':
@ -48,6 +110,6 @@ class FileStorage:
siz += len(d); siz += len(d);
await f.write(d) await f.write(d)
await f.flush() 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 return fpath

View File

@ -135,7 +135,7 @@ class ProcessorResource(AppLogger, StaticResource,Url2File):
print_exc() print_exc()
print('-----------except out ------------') print('-----------except out ------------')
break; break;
print(f'getPostData():{ns=}') # print(f'getPostData():{ns=}')
# showcallstack() # showcallstack()
return ns return ns
@ -193,15 +193,10 @@ class ProcessorResource(AppLogger, StaticResource,Url2File):
async def getArgs(): 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() ns = DictObject()
if request.method == 'POST': if request.method == 'POST':
return await self.getPostData(request) return await self.getPostData(request)
ns = multiDict2Dict(request.query) ns = multiDict2Dict(request.query)
request.params_kw = ns
return ns return ns
self.y_env.i18n = serveri18n self.y_env.i18n = serveri18n