未验证 提交 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):
}
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();
if (response && 'status' in response) {
if (response.status !== 0) {
const t = await logErrorAndReturnT(response);
throw new Error((response as ErrorData).msg || t('errors:error'));
} else {
return (response as SuccessData<T>).data;
}
}
return response;
} else {
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);
});
} catch (e) {
const t = await logErrorAndReturnT(e);
throw new Error(t('errors:parse-error'));
}
if (response && 'status' in response) {
if (response.status !== 0) {
const t = await logErrorAndReturnT(response);
throw new Error((response as ErrorData).msg || t('errors:error'));
} else {
return (response as SuccessData<T>).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 response;
} else {
let data: Blob;
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) {
const t = await logErrorAndReturnT(e);
throw new Error(t('errors:parse-error'));
return {data, type: res.headers.get('Content-Type'), filename};
}
}
......
......@@ -69,6 +69,12 @@ def create_app(args):
public_path = args.public_path
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
def get_locale():
lang = args.language
......@@ -87,7 +93,7 @@ def create_app(args):
@app.route('/')
def base():
return redirect(public_path, code=302)
return redirect(append_query_string(public_path), code=302)
@app.route('/favicon.ico')
def favicon():
......@@ -98,10 +104,7 @@ def create_app(args):
@app.route(public_path + '/')
def index():
query_string = ''
if request.query_string:
query_string = '?' + request.query_string.decode()
return redirect(public_path + '/index' + query_string, code=302)
return redirect(append_query_string(public_path + '/index'), code=302)
@app.route(public_path + '/<path:filename>')
def serve_static(filename):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册