This commit is contained in:
yumoqing 2025-02-06 16:43:49 +08:00
parent d921864cb1
commit 8b470b3df9
2 changed files with 29 additions and 42 deletions

View File

@ -23,47 +23,34 @@ def path_decode(dpath):
async def file_upload(request): async def file_upload(request):
pass pass
async def file_download(request, filepath, content_type=None): async def file_handle(request, filepath, download=False):
filename = os.path.basename(filepath) filename = os.path.basename(filepath)
r = web.FileResponse(filepath) debug(f'{filepath=}, {filename=}, {download=}')
ct = content_type headers = {}
if ct is None: if download:
ct, encoding = mimetypes.guess_type(filepath) headers = {
if ct is not None: 'Content-Disposition': f'attachment; filename="{filename}"'
r.content_type = ct }
else: r = web.FileResponse(filepath, chunk_size=8096, headers=headers)
r.content_type = 'application/octet-stream'
r.content_disposition = 'attachment; filename=%s' % filename
r.enable_compression() r.enable_compression()
return r return r
if os.path.exists(filepath):
length = os.path.getsize(filepath) async def file_download(request, filepath):
response = web.Response( return file_handler(request, filepath, download=True)
status=200,
headers = {
'Content-Disposition': 'attrachment;filename={}'.format(filename)
}
)
await response.prepare(request)
cnt = 0
async with aiofiles.open(filepath, 'rb') as f:
chunk = await f.read(10240000)
cnt = cnt + len(chunk)
await response.write(chunk)
await response.fsyn()
await response.write_eof()
return response
raise HTTPNotFound
async def path_download(request, kw, *params): async def path_download(request, kw, *params):
path = kw.get('path') path = kw.get('path')
download = False
if kw.get('download'):
download = True
fs = FileStorage() fs = FileStorage()
fp = fs.realPath(path) fp = fs.realPath(path)
debug(f'path_download():download filename={fp}') debug(f'path_download():download filename={fp}')
return await file_download(request, fp) return await file_handle(request, fp, download)
rf = RegisterFunction() rf = RegisterFunction()
rf.register('idfile', path_download) rf.register('idfile', path_download)
rf.register('download', path_download)

View File

@ -19,7 +19,10 @@ class XtermProcessor(PythonScriptProcessor):
async def ws_2_process(self, ws): async def ws_2_process(self, ws):
async for msg in ws: async for msg in ws:
if msg.type == aiohttp.WSMsgType.TEXT: if msg.type == aiohttp.WSMsgType.TEXT:
self.p_obj.stdin.write(msg.data) if msg.data == '_#_heartbeat_#_':
await ws_send(ws, '_#_heartbeat_#_')
else:
self.p_obj.stdin.write(msg.data)
elif msg.type == aiohttp.WSMsgType.ERROR: elif msg.type == aiohttp.WSMsgType.ERROR:
# print('ws connection closed with exception %s' % ws.exception()) # print('ws connection closed with exception %s' % ws.exception())
return return
@ -218,31 +221,28 @@ class WebsocketProcessor(PythonScriptProcessor):
lenv.update(params) lenv.update(params)
params_kw = lenv.params_kw params_kw = lenv.params_kw
userid = lenv.params_kw.userid or await lenv.get_user() userid = lenv.params_kw.userid or await lenv.get_user()
debug(f'========== debug ===========')
del lenv['request'] del lenv['request']
txt = await self.loadScript(self.real_path) txt = await self.loadScript(self.real_path)
ws = web.WebSocketResponse() ws = web.WebSocketResponse()
debug(f'========== debug ===========')
try: try:
await ws.prepare(request) await ws.prepare(request)
except Exception as e: except Exception as e:
exception(f'--------except: {e}') exception(f'--------except: {e}')
print_exc() print_exc()
raise e raise e
debug(f'========== debug ===========')
ws_pool = WsPool(ws, request['client_ip'], request.path, request.app) ws_pool = WsPool(ws, request['client_ip'], request.path, request.app)
debug(f'========== debug ===========') debug(f'========== debug ===========')
async for msg in ws: async for msg in ws:
if msg.type == aiohttp.WSMsgType.TEXT: if msg.type == aiohttp.WSMsgType.TEXT:
debug(f'========== {msg=} ===========') if msg.data == '_#_heartbeat_#_':
if msg.data == 'exit': await ws_send(ws, '_#_heartbeat_#_')
break else:
debug(f'WS:msg from client={msg}') lenv['ws_data'] = msg.data
lenv['ws_data'] = msg.data lenv['ws_pool'] = ws_pool
lenv['ws_pool'] = ws_pool exec(txt,lenv,lenv)
exec(txt,lenv,lenv) func = lenv['myfunc']
func = lenv['myfunc'] resp = await func(request,**lenv)
resp = await func(request,**lenv)
elif msg.type == aiohttp.WSMsgType.ERROR: elif msg.type == aiohttp.WSMsgType.ERROR:
error('ws connection closed with exception %s' % ws.exception()) error('ws connection closed with exception %s' % ws.exception())
break break