From bd7f42475f8f00fd6a3c2092fadb51b275b754d8 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Tue, 24 Mar 2026 14:14:03 +0800 Subject: [PATCH] bugfix --- ahserver/filedownload.py | 11 ++++++----- ahserver/globalEnv.py | 19 ++----------------- setup.cfg | 2 +- 3 files changed, 9 insertions(+), 23 deletions(-) diff --git a/ahserver/filedownload.py b/ahserver/filedownload.py index 0e4b16b..33cd66a 100644 --- a/ahserver/filedownload.py +++ b/ahserver/filedownload.py @@ -1,6 +1,6 @@ import os import asyncio - +import urllib import aiofiles import mimetypes from aiohttp.web_exceptions import HTTPNotFound @@ -23,20 +23,21 @@ def path_decode(dpath): async def file_upload(request): pass -async def file_handle(request, filepath, download=False): +async def file_response(request, filepath, download=False): filename = os.path.basename(filepath) + filename = urllib.parse.quote(filename) debug(f'{filepath=}, {filename=}, {download=}') headers = {} if download: headers = { - 'Content-Disposition': f'attachment; filename="{filename}"' + 'Content-Disposition': f'attachment; filename="{filename}"; filename*=UTF-8\'\'{safe_name}' } r = web.FileResponse(filepath, chunk_size=8096, headers=headers) r.enable_compression() return r async def file_download(request, filepath): - return await file_handle(request, filepath, download=True) + return await file_response(request, filepath, download=True) async def path_download(request, params_kw, *params, **kw): path = params_kw.get('path') @@ -46,7 +47,7 @@ async def path_download(request, params_kw, *params, **kw): fs = FileStorage() fp = fs.realPath(path) debug(f'path_download():download filename={fp}') - return await file_handle(request, fp, download) + return await file_response(request, fp, download) rf = RegisterFunction() rf.register('idfile', path_download) diff --git a/ahserver/globalEnv.py b/ahserver/globalEnv.py index 3eb19fa..8f4c5dc 100644 --- a/ahserver/globalEnv.py +++ b/ahserver/globalEnv.py @@ -64,7 +64,7 @@ from .xlsxData import XLSXData from .uriop import URIOp from .error import Success, Error, NeedLogin, NoPermission from .filetest import current_fileno -from .filedownload import path_download, file_download +from .filedownload import path_download, file_download, file_response from .filestorage import FileStorage, downloadfile2url from .serverenv import ServerEnv @@ -220,22 +220,6 @@ def configValue(ks): def visualcoding(): return configValue('.website.visualcoding'); -def file_download(request,path,name,coding='utf8'): - f = openfile(path,'rb') - b = f.read() - f.close() - fname = quote(name).encode(coding) - hah = b"attachment; filename=" + fname - # print('file head=',hah.decode(coding)) - request.setHeader(b'Content-Disposition',hah) - request.setHeader(b'Expires',0) - request.setHeader(b'Cache-Control',b'must-revalidate, post-check=0, pre-check=0') - request.setHeader(b'Content-Transfer-Encoding',b'binary') - request.setHeader(b'Pragma',b'public') - request.setHeader(b'Content-Length',len(b)) - request.write(b) - request.finish() - def paramify(data, ns): ac = ArgsConvert('${', '}$') return ac.convert(data, ns) @@ -318,6 +302,7 @@ def initEnv(): g.rfexe = RegisterFunction().exe g.stream_response = stream_response g.webpath = webpath + g.file_response = file_response g.file_download = file_download g.path_download = path_download g.partial = partial diff --git a/setup.cfg b/setup.cfg index 3b266bf..e4b8d30 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,7 +1,7 @@ # setup.cfg [metadata] name = ahserver -version = 1.1.3 +version = 1.1.4 description = A application server base on aiohttp author = yu moqing author_email = yumoqing@gmail.com