This commit is contained in:
yumoqing 2026-03-24 14:14:03 +08:00
parent 6a1ecfed78
commit bd7f42475f
3 changed files with 9 additions and 23 deletions

View File

@ -1,6 +1,6 @@
import os import os
import asyncio import asyncio
import urllib
import aiofiles import aiofiles
import mimetypes import mimetypes
from aiohttp.web_exceptions import HTTPNotFound from aiohttp.web_exceptions import HTTPNotFound
@ -23,20 +23,21 @@ def path_decode(dpath):
async def file_upload(request): async def file_upload(request):
pass pass
async def file_handle(request, filepath, download=False): async def file_response(request, filepath, download=False):
filename = os.path.basename(filepath) filename = os.path.basename(filepath)
filename = urllib.parse.quote(filename)
debug(f'{filepath=}, {filename=}, {download=}') debug(f'{filepath=}, {filename=}, {download=}')
headers = {} headers = {}
if download: if download:
headers = { 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 = web.FileResponse(filepath, chunk_size=8096, headers=headers)
r.enable_compression() r.enable_compression()
return r return r
async def file_download(request, filepath): 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): async def path_download(request, params_kw, *params, **kw):
path = params_kw.get('path') path = params_kw.get('path')
@ -46,7 +47,7 @@ async def path_download(request, params_kw, *params, **kw):
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_handle(request, fp, download) return await file_response(request, fp, download)
rf = RegisterFunction() rf = RegisterFunction()
rf.register('idfile', path_download) rf.register('idfile', path_download)

View File

@ -64,7 +64,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 .filedownload import path_download, file_download from .filedownload import path_download, file_download, file_response
from .filestorage import FileStorage, downloadfile2url from .filestorage import FileStorage, downloadfile2url
from .serverenv import ServerEnv from .serverenv import ServerEnv
@ -220,22 +220,6 @@ def configValue(ks):
def visualcoding(): def visualcoding():
return configValue('.website.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): def paramify(data, ns):
ac = ArgsConvert('${', '}$') ac = ArgsConvert('${', '}$')
return ac.convert(data, ns) return ac.convert(data, ns)
@ -318,6 +302,7 @@ def initEnv():
g.rfexe = RegisterFunction().exe g.rfexe = RegisterFunction().exe
g.stream_response = stream_response g.stream_response = stream_response
g.webpath = webpath g.webpath = webpath
g.file_response = file_response
g.file_download = file_download g.file_download = file_download
g.path_download = path_download g.path_download = path_download
g.partial = partial g.partial = partial

View File

@ -1,7 +1,7 @@
# setup.cfg # setup.cfg
[metadata] [metadata]
name = ahserver name = ahserver
version = 1.1.3 version = 1.1.4
description = A application server base on aiohttp description = A application server base on aiohttp
author = yu moqing author = yu moqing
author_email = yumoqing@gmail.com author_email = yumoqing@gmail.com