From 5d065767631fb65983bd7558781931562fec1a01 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Thu, 8 Aug 2024 17:47:53 +0800 Subject: [PATCH] bugfix --- ahserver/filestorage.py | 16 ++++++++++++++-- ahserver/globalEnv.py | 8 +++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/ahserver/filestorage.py b/ahserver/filestorage.py index 342001a..d7f05e3 100755 --- a/ahserver/filestorage.py +++ b/ahserver/filestorage.py @@ -105,18 +105,30 @@ class FileStorage: name)) 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): 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') + if isinstance(read_data, str) or isinstance(read_data, bytes): + b = read_data + 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: diff --git a/ahserver/globalEnv.py b/ahserver/globalEnv.py index f1b4561..aedfd3f 100755 --- a/ahserver/globalEnv.py +++ b/ahserver/globalEnv.py @@ -34,7 +34,7 @@ from .xlsxData import XLSXData from .uriop import URIOp from .error import Success, Error, NeedLogin, NoPermission from .filetest import current_fileno - +from .filestorage import FileStorage from .serverenv import ServerEnv def data2xlsx(rows,headers=None): @@ -57,6 +57,11 @@ def data2xlsx(rows,headers=None): wb.close() 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): def __init__(self,fp,*args,**kwargs): super(FileOutZone,self).__init__(*args,**kwargs) @@ -193,6 +198,7 @@ def initEnv(): g.DictObject = DictObject g.async_sleep = asyncio.sleep g.quotedstr = quotedstr + g.save_file = save_file def set_builtins(): all_builtins = [ i for i in dir(builtins) if not i.startswith('_')]