This commit is contained in:
yumoqing 2024-07-17 21:18:26 +08:00
parent 2a8d945470
commit d1d9fb7e04
2 changed files with 30 additions and 14 deletions

View File

@ -11,6 +11,7 @@ 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 from appPublic.Singleton import SingletonDecorator
from appPublic.log import info, debug, warning, exception, critical
@SingletonDecorator @SingletonDecorator
class TmpFileRecord: class TmpFileRecord:
@ -22,7 +23,6 @@ class TmpFileRecord:
self.filename = self.savefilename() self.filename = self.savefilename()
self.loop = asyncio.get_event_loop() self.loop = asyncio.get_event_loop()
self.loop.call_later(0.01, self.load) self.loop.call_later(0.01, self.load)
# print('TmpFileRecord() .........................')
def newtmpfile(self, path:str): def newtmpfile(self, path:str):
self.filetime[path] = time.time() self.filetime[path] = time.time()
@ -31,7 +31,8 @@ class TmpFileRecord:
def savefilename(self): def savefilename(self):
config = getConfig() config = getConfig()
root = config.filesroot or tempfile.gettempdir() root = config.filesroot or tempfile.gettempdir()
return root + '/tmpfile_rec.json' pid = os.getpid()
return root + f'/tmpfile_rec_{pid}.json'
async def save(self): async def save(self):
if not self.change_flg: if not self.change_flg:
@ -44,7 +45,6 @@ class TmpFileRecord:
self.change_flg = False self.change_flg = False
async def load(self): async def load(self):
# print('load() called ............')
fn = self.filename fn = self.filename
if not os.path.isfile(fn): if not os.path.isfile(fn):
return return
@ -55,6 +55,13 @@ class TmpFileRecord:
self.remove() self.remove()
def file_useful(self, fpath):
try:
del self.filetime[fpath]
except Exception as e:
exception(f'Exception:{str(e)}')
pass
async def remove(self): async def remove(self):
tim = time.time() tim = time.time()
ft = {k:v for k,v in self.filetime.items()} ft = {k:v for k,v in self.filetime.items()}
@ -72,35 +79,44 @@ class TmpFileRecord:
class FileStorage: class FileStorage:
def __init__(self): def __init__(self):
config = getConfig() config = getConfig()
self.root = config.filesroot or tempfile.gettempdir() self.root = os.path.abspath(config.filesroot or tempfile.gettempdir())
self.tfr = TmpFileRecord() self.tfr = TmpFileRecord()
def realPath(self,path): def realPath(self,path):
if path[0] == '/': if path[0] == '/':
path = path[1:] path = path[1:]
p = os.path.join(self.root,path) p = os.path.abspath(os.path.join(self.root,path))
return p return p
def _name2path(self,name): def _name2path(self,name, userid=None):
name = os.path.basename(name) name = os.path.basename(name)
paths=[191,193,197,199,97] paths=[191,193,197,97]
v = int(time.time()*1000000) v = int(time.time()*1000000)
# b = name.encode('utf8') if not isinstance(name,bytes) else name # b = name.encode('utf8') if not isinstance(name,bytes) else name
# v = int.from_bytes(b,byteorder='big',signed=False) # v = int.from_bytes(b,byteorder='big',signed=False)
path = os.path.abspath(os.path.join(self.root, root = self.root
if userid:
root += f'/{userid}'
path = os.path.abspath(os.path.join(root,
str(v % paths[0]), str(v % paths[0]),
str(v % paths[1]), str(v % paths[1]),
str(v % paths[2]), str(v % paths[2]),
str(v % paths[3]), str(v % paths[3]),
str(v % paths[4]),
name)) name))
return path return path
async def save(self,name,read_data): async def save(self,name,read_data, userid=None):
# print(f'FileStorage():save():{name=}') p = self._name2path(name, userid=userid)
p = self._name2path(name)
fpath = p[len(self.root):] fpath = p[len(self.root):]
info(f'{p=}, {fpath=},{self.root} ')
_mkdir(os.path.dirname(p)) _mkdir(os.path.dirname(p))
if isinstance(read_data, str):
b = read_data.encode('utf-8')
async with aiofiles.open(p, 'wb') as f:
await f.write(b)
await f.flush()
self.tfr.newtmpfile(fpath)
return fpath
async with aiofiles.open(p,'wb') as f: async with aiofiles.open(p,'wb') as f:
siz = 0 siz = 0
while 1: while 1:
@ -110,7 +126,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')
self.tfr.newtmpfile(fpath) self.tfr.newtmpfile(fpath)
return fpath return fpath

View File

@ -146,7 +146,8 @@ class ProcessorResource(StaticResource,Url2File):
value = '' value = ''
if hasattr(field,'filename') and field.filename is not None: if hasattr(field,'filename') and field.filename is not None:
saver = FileStorage() saver = FileStorage()
value = await saver.save(field.filename,field.read_chunk) userid = await auth.get_auth(request)
value = await saver.save(field.filename,field.read_chunk, userid=userid)
else: else:
value = await field.read(decode=True) value = await field.read(decode=True)
value = value.decode('utf-8') value = value.decode('utf-8')