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):
pass
async def file_download(request, filepath, content_type=None):
async def file_handle(request, filepath, download=False):
filename = os.path.basename(filepath)
r = web.FileResponse(filepath)
ct = content_type
if ct is None:
ct, encoding = mimetypes.guess_type(filepath)
if ct is not None:
r.content_type = ct
else:
r.content_type = 'application/octet-stream'
r.content_disposition = 'attachment; filename=%s' % filename
debug(f'{filepath=}, {filename=}, {download=}')
headers = {}
if download:
headers = {
'Content-Disposition': f'attachment; filename="{filename}"'
}
r = web.FileResponse(filepath, chunk_size=8096, headers=headers)
r.enable_compression()
return r
if os.path.exists(filepath):
length = os.path.getsize(filepath)
response = web.Response(
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 file_download(request, filepath):
return file_handler(request, filepath, download=True)
async def path_download(request, kw, *params):
path = kw.get('path')
download = False
if kw.get('download'):
download = True
fs = FileStorage()
fp = fs.realPath(path)
debug(f'path_download():download filename={fp}')
return await file_download(request, fp)
return await file_handle(request, fp, download)
rf = RegisterFunction()
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 for msg in ws:
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:
# print('ws connection closed with exception %s' % ws.exception())
return
@ -218,31 +221,28 @@ class WebsocketProcessor(PythonScriptProcessor):
lenv.update(params)
params_kw = lenv.params_kw
userid = lenv.params_kw.userid or await lenv.get_user()
debug(f'========== debug ===========')
del lenv['request']
txt = await self.loadScript(self.real_path)
ws = web.WebSocketResponse()
debug(f'========== debug ===========')
try:
await ws.prepare(request)
except Exception as e:
exception(f'--------except: {e}')
print_exc()
raise e
debug(f'========== debug ===========')
ws_pool = WsPool(ws, request['client_ip'], request.path, request.app)
debug(f'========== debug ===========')
async for msg in ws:
if msg.type == aiohttp.WSMsgType.TEXT:
debug(f'========== {msg=} ===========')
if msg.data == 'exit':
break
debug(f'WS:msg from client={msg}')
lenv['ws_data'] = msg.data
lenv['ws_pool'] = ws_pool
exec(txt,lenv,lenv)
func = lenv['myfunc']
resp = await func(request,**lenv)
if msg.data == '_#_heartbeat_#_':
await ws_send(ws, '_#_heartbeat_#_')
else:
lenv['ws_data'] = msg.data
lenv['ws_pool'] = ws_pool
exec(txt,lenv,lenv)
func = lenv['myfunc']
resp = await func(request,**lenv)
elif msg.type == aiohttp.WSMsgType.ERROR:
error('ws connection closed with exception %s' % ws.exception())
break