bugfix
This commit is contained in:
parent
2a8d945470
commit
d1d9fb7e04
@ -11,6 +11,7 @@ import time
|
||||
from appPublic.folderUtils import _mkdir
|
||||
from appPublic.jsonConfig import getConfig
|
||||
from appPublic.Singleton import SingletonDecorator
|
||||
from appPublic.log import info, debug, warning, exception, critical
|
||||
|
||||
@SingletonDecorator
|
||||
class TmpFileRecord:
|
||||
@ -22,7 +23,6 @@ class TmpFileRecord:
|
||||
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()
|
||||
@ -31,7 +31,8 @@ class TmpFileRecord:
|
||||
def savefilename(self):
|
||||
config = getConfig()
|
||||
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):
|
||||
if not self.change_flg:
|
||||
@ -44,7 +45,6 @@ class TmpFileRecord:
|
||||
self.change_flg = False
|
||||
|
||||
async def load(self):
|
||||
# print('load() called ............')
|
||||
fn = self.filename
|
||||
if not os.path.isfile(fn):
|
||||
return
|
||||
@ -55,6 +55,13 @@ class TmpFileRecord:
|
||||
|
||||
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):
|
||||
tim = time.time()
|
||||
ft = {k:v for k,v in self.filetime.items()}
|
||||
@ -72,35 +79,44 @@ class TmpFileRecord:
|
||||
class FileStorage:
|
||||
def __init__(self):
|
||||
config = getConfig()
|
||||
self.root = config.filesroot or tempfile.gettempdir()
|
||||
self.root = os.path.abspath(config.filesroot or tempfile.gettempdir())
|
||||
self.tfr = TmpFileRecord()
|
||||
|
||||
def realPath(self,path):
|
||||
if path[0] == '/':
|
||||
path = path[1:]
|
||||
p = os.path.join(self.root,path)
|
||||
p = os.path.abspath(os.path.join(self.root,path))
|
||||
return p
|
||||
|
||||
def _name2path(self,name):
|
||||
def _name2path(self,name, userid=None):
|
||||
name = os.path.basename(name)
|
||||
paths=[191,193,197,199,97]
|
||||
paths=[191,193,197,97]
|
||||
v = int(time.time()*1000000)
|
||||
# b = name.encode('utf8') if not isinstance(name,bytes) else name
|
||||
# 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[1]),
|
||||
str(v % paths[2]),
|
||||
str(v % paths[3]),
|
||||
str(v % paths[4]),
|
||||
name))
|
||||
return path
|
||||
|
||||
async def save(self,name,read_data):
|
||||
# print(f'FileStorage():save():{name=}')
|
||||
p = self._name2path(name)
|
||||
async def save(self,name,read_data, userid=None):
|
||||
p = self._name2path(name, userid=userid)
|
||||
fpath = p[len(self.root):]
|
||||
info(f'{p=}, {fpath=},{self.root} ')
|
||||
_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:
|
||||
siz = 0
|
||||
while 1:
|
||||
@ -110,7 +126,6 @@ class FileStorage:
|
||||
siz += len(d);
|
||||
await f.write(d)
|
||||
await f.flush()
|
||||
# print(f'{name=} file({fpath}) write {siz} bytes')
|
||||
self.tfr.newtmpfile(fpath)
|
||||
return fpath
|
||||
|
||||
|
@ -146,7 +146,8 @@ class ProcessorResource(StaticResource,Url2File):
|
||||
value = ''
|
||||
if hasattr(field,'filename') and field.filename is not None:
|
||||
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:
|
||||
value = await field.read(decode=True)
|
||||
value = value.decode('utf-8')
|
||||
|
Loading…
Reference in New Issue
Block a user