core: add querystring and originalPath property on request

pull/461/head
undefined 2 years ago
parent 512a912080
commit 9576d35e21

@ -107,7 +107,7 @@ class ApiHandler extends Handler {
}
async get() {
const q = decodeURIComponent(this.ctx.request.querystring);
const q = decodeURIComponent(this.request.querystring);
if (q === 'schema') {
this.response.type = 'application/json';
this.response.body = { schema: schemaStr };

@ -34,7 +34,9 @@ export default async (ctx, next) => {
body: ctx.request.body,
files: ctx.request.files as any,
query: ctx.query,
querystring: ctx.querystring,
path: ctx.path,
originalPath: ctx.originalPath,
params: ctx.params,
referer: ctx.request.headers.referer || '',
json: (ctx.request.headers.accept || '').includes('application/json'),

@ -45,7 +45,9 @@ export interface HydroRequest {
body: any;
files: Record<string, import('formidable').File>;
query: any;
querystring: string;
path: string;
originalPath: string;
params: any;
referer: string;
json: boolean;
@ -114,17 +116,18 @@ export class HandlerCommon {
session: Record<string, any>;
/** @deprecated */
domainId: string;
ctx: Context = global.app;
constructor(
public ctx: KoaContext, public args: Record<string, any>,
public context: KoaContext, public args: Record<string, any>,
public request: HydroRequest, public response: HydroResponse,
public user: User, public domain: DomainDoc, public UiContext: Record<string, any>,
) {
this.render = ctx.render.bind(ctx);
this.renderHTML = ctx.renderHTML.bind(ctx);
this.url = ctx.getUrl.bind(ctx);
this.translate = ctx.translate.bind(ctx);
this.session = ctx.session;
this.render = context.render.bind(context);
this.renderHTML = context.renderHTML.bind(context);
this.url = context.getUrl.bind(context);
this.translate = context.translate.bind(context);
this.session = context.session;
this.domainId = args.domainId;
}
@ -140,8 +143,8 @@ export class HandlerCommon {
renderTitle(str: string) {
const name = this.domain?.ui?.name || system.get('server.name');
if (this.UiContext.extraTitleContent) return `${this.ctx.translate(str)} - ${this.UiContext.extraTitleContent} - ${name}`;
return `${this.ctx.translate(str)} - ${name}`;
if (this.UiContext.extraTitleContent) return `${this.translate(str)} - ${this.UiContext.extraTitleContent} - ${name}`;
return `${this.translate(str)} - ${name}`;
}
checkPerm(...args: bigint[]) {
@ -193,9 +196,9 @@ export class Handler extends HandlerCommon {
if (error.stack) logger.error(error.stack);
}
if (this.user?._id === 0 && (error instanceof PermissionError || error instanceof PrivilegeError)) {
this.response.redirect = this.ctx.getUrl('user_login', {
this.response.redirect = this.url('user_login', {
query: {
redirect: (this.ctx.originalPath || this.request.path) + this.ctx.search,
redirect: (this.context.originalPath || this.request.path) + this.context.search,
},
});
} else {
@ -330,10 +333,10 @@ export class ConnectionHandler extends HandlerCommon {
}
onerror(err: HydroError) {
if (err instanceof UserFacingError) err.stack = this.ctx.HydroContext.request.path;
if (err instanceof UserFacingError) err.stack = this.request.path;
if (!(err instanceof NotFoundError)
&& !((err instanceof PrivilegeError || err instanceof PermissionError) && this.user?._id === 0)) {
logger.error(`Path:${this.ctx.HydroContext.request.path}, User:${this.user?._id}(${this.user?.uname})`);
logger.error(`Path:${this.request.path}, User:${this.user?._id}(${this.user?.uname})`);
logger.error(err);
}
this.send({

@ -135,7 +135,11 @@ class ImportQduojHandler extends Handler {
}
}
export const name = 'import-qduoj';
export async function apply(ctx: Context) {
ctx.Route('problem_import_qduoj', '/problem/import/qduoj', ImportQduojHandler, PERM.PERM_CREATE_PROBLEM);
ctx.inject('ProblemAdd', 'problem_import_qduoj', { icon: 'copy', text: 'From QDUOJ Export' });
ctx.i18n.load('zh', {
'From QDUOJ Export': '从 QDUOJ 导入',
});
}

@ -1 +0,0 @@
From QDUOJ Export: 从 QDUOJ 导入

@ -6,8 +6,5 @@
"repository": "https://github.com/hydro-dev/Hydro.git",
"author": "undefined <i@undefined.moe>",
"license": "AGPL-3.0-or-later",
"preferUnplugged": true,
"dependencies": {
"decode-html": "^2.0.0"
}
"preferUnplugged": true
}

Loading…
Cancel
Save