未验证 提交 8e562d4e 编写于 作者: P Peter Pan 提交者: GitHub

minor bugs fix (#804)

* fix: error message cannot display correctly

* fix: query string missing when redirect from root to index
上级 1802fd96
...@@ -80,44 +80,50 @@ export async function fetcher<T = unknown>(url: string, options?: RequestInit): ...@@ -80,44 +80,50 @@ export async function fetcher<T = unknown>(url: string, options?: RequestInit):
} }
let response: Data<T> | T; let response: Data<T> | T;
try { if (res.headers.get('content-type')?.includes('application/json')) {
if (res.headers.get('content-type')?.includes('application/json')) { try {
response = await res.json(); response = await res.json();
if (response && 'status' in response) { } catch (e) {
if (response.status !== 0) { const t = await logErrorAndReturnT(e);
const t = await logErrorAndReturnT(response); throw new Error(t('errors:parse-error'));
throw new Error((response as ErrorData).msg || t('errors:error')); }
} else { if (response && 'status' in response) {
return (response as SuccessData<T>).data; if (response.status !== 0) {
} const t = await logErrorAndReturnT(response);
} throw new Error((response as ErrorData).msg || t('errors:error'));
return response; } else {
} else { return (response as SuccessData<T>).data;
const data = await res.blob();
const disposition = res.headers.get('Content-Disposition');
// support safari
if (!data.arrayBuffer) {
data.arrayBuffer = async () =>
new Promise<ArrayBuffer>((resolve, reject) => {
const fileReader = new FileReader();
fileReader.addEventListener('load', e =>
e.target ? resolve(e.target.result as ArrayBuffer) : reject()
);
fileReader.readAsArrayBuffer(data);
});
} }
let filename: string | null = null; }
if (disposition && disposition.indexOf('attachment') !== -1) { return response;
const matches = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec(disposition); } else {
if (matches != null && matches[1]) { let data: Blob;
filename = matches[1].replace(/['"]/g, ''); try {
} data = await res.blob();
} catch (e) {
const t = await logErrorAndReturnT(e);
throw new Error(t('errors:parse-error'));
}
const disposition = res.headers.get('Content-Disposition');
// support safari
if (!data.arrayBuffer) {
data.arrayBuffer = async () =>
new Promise<ArrayBuffer>((resolve, reject) => {
const fileReader = new FileReader();
fileReader.addEventListener('load', e =>
e.target ? resolve(e.target.result as ArrayBuffer) : reject()
);
fileReader.readAsArrayBuffer(data);
});
}
let filename: string | null = null;
if (disposition && disposition.indexOf('attachment') !== -1) {
const matches = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec(disposition);
if (matches != null && matches[1]) {
filename = matches[1].replace(/['"]/g, '');
} }
return {data, type: res.headers.get('Content-Type'), filename};
} }
} catch (e) { return {data, type: res.headers.get('Content-Type'), filename};
const t = await logErrorAndReturnT(e);
throw new Error(t('errors:parse-error'));
} }
} }
......
...@@ -69,6 +69,12 @@ def create_app(args): ...@@ -69,6 +69,12 @@ def create_app(args):
public_path = args.public_path public_path = args.public_path
api_path = public_path + '/api' api_path = public_path + '/api'
def append_query_string(url):
query_string = ''
if request.query_string:
query_string = '?' + request.query_string.decode()
return url + query_string
@babel.localeselector @babel.localeselector
def get_locale(): def get_locale():
lang = args.language lang = args.language
...@@ -87,7 +93,7 @@ def create_app(args): ...@@ -87,7 +93,7 @@ def create_app(args):
@app.route('/') @app.route('/')
def base(): def base():
return redirect(public_path, code=302) return redirect(append_query_string(public_path), code=302)
@app.route('/favicon.ico') @app.route('/favicon.ico')
def favicon(): def favicon():
...@@ -98,10 +104,7 @@ def create_app(args): ...@@ -98,10 +104,7 @@ def create_app(args):
@app.route(public_path + '/') @app.route(public_path + '/')
def index(): def index():
query_string = '' return redirect(append_query_string(public_path + '/index'), code=302)
if request.query_string:
query_string = '?' + request.query_string.decode()
return redirect(public_path + '/index' + query_string, code=302)
@app.route(public_path + '/<path:filename>') @app.route(public_path + '/<path:filename>')
def serve_static(filename): def serve_static(filename):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册