This commit is contained in:
yumoqing 2024-08-08 17:47:53 +08:00
parent f596df7696
commit 5d06576763
2 changed files with 21 additions and 3 deletions

View File

@ -105,11 +105,22 @@ class FileStorage:
name)) name))
return path return path
def remove(self, path):
try:
if path[0] == '/':
path = path[1:]
p = os.path.join(self.root, path)
os.remove(p)
except Exception as e:
exception(f'{path=}, {p=} remove error')
async def save(self,name,read_data, userid=None): async def save(self,name,read_data, userid=None):
p = self._name2path(name, userid=userid) p = self._name2path(name, userid=userid)
fpath = p[len(self.root):] fpath = p[len(self.root):]
info(f'{p=}, {fpath=},{self.root} ') info(f'{p=}, {fpath=},{self.root} ')
_mkdir(os.path.dirname(p)) _mkdir(os.path.dirname(p))
if isinstance(read_data, str) or isinstance(read_data, bytes):
b = read_data
if isinstance(read_data, str): if isinstance(read_data, str):
b = read_data.encode('utf-8') b = read_data.encode('utf-8')
async with aiofiles.open(p, 'wb') as f: async with aiofiles.open(p, 'wb') as f:
@ -117,6 +128,7 @@ class FileStorage:
await f.flush() await f.flush()
self.tfr.newtmpfile(fpath) self.tfr.newtmpfile(fpath)
return 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:

View File

@ -34,7 +34,7 @@ from .xlsxData import XLSXData
from .uriop import URIOp from .uriop import URIOp
from .error import Success, Error, NeedLogin, NoPermission from .error import Success, Error, NeedLogin, NoPermission
from .filetest import current_fileno from .filetest import current_fileno
from .filestorage import FileStorage
from .serverenv import ServerEnv from .serverenv import ServerEnv
def data2xlsx(rows,headers=None): def data2xlsx(rows,headers=None):
@ -57,6 +57,11 @@ def data2xlsx(rows,headers=None):
wb.close() wb.close()
return name return name
async def save_file(str_or_bytes, filename):
fs = FileStorage()
r = await fs.save(filename, str_or_bytes)
return r
class FileOutZone(Exception): class FileOutZone(Exception):
def __init__(self,fp,*args,**kwargs): def __init__(self,fp,*args,**kwargs):
super(FileOutZone,self).__init__(*args,**kwargs) super(FileOutZone,self).__init__(*args,**kwargs)
@ -193,6 +198,7 @@ def initEnv():
g.DictObject = DictObject g.DictObject = DictObject
g.async_sleep = asyncio.sleep g.async_sleep = asyncio.sleep
g.quotedstr = quotedstr g.quotedstr = quotedstr
g.save_file = save_file
def set_builtins(): def set_builtins():
all_builtins = [ i for i in dir(builtins) if not i.startswith('_')] all_builtins = [ i for i in dir(builtins) if not i.startswith('_')]