fix: some errors ("not found") were not translated
Massimo Melina committed
May 9, 2023 at 14:19 UTC
955f2a492013c10a254402d791b968129b79ae04
1 file changed
+24
-5
src/middlewares.ts
+24
-5
@@ -6,7 +6,7 @@ import {
6
ADMIN_URI, API_URI,
7
BUILD_TIMESTAMP,
8
DEV, DAY,
9
- HTTP_FORBIDDEN, HTTP_NOT_FOUND, HTTP_FOOL,
9
+ HTTP_FORBIDDEN, HTTP_NOT_FOUND, HTTP_FOOL, HTTP_UNAUTHORIZED,
10
} from './const'
11
import { FRONTEND_URI } from './const'
12
import { statusCodeForMissingPerm, nodeIsDirectory, urlToNode, vfs, walkNode, VfsNode, getNodeName } from './vfs'
@@ -38,6 +38,7 @@ import { allowAdmin, favicon } from './adminApis'
38
import { constants } from 'zlib'
39
import { getHttpsWorkingPort } from './listen'
40
import { defineConfig } from './config'
41
+import { getLangData } from './lang'
42
43
const forceHttps = defineConfig('force_https', true)
44
const ignoreProxies = defineConfig('ignore_proxies', false)
@@ -96,13 +97,13 @@ export const serveGuiAndSharedFiles: Koa.Middleware = async (ctx, next) => {
97
return ctx.redirect(ctx.state.revProxyPath + ADMIN_URI)
98
if (path.startsWith(ADMIN_URI))
99
return allowAdmin(ctx) ? serveAdminPrefixed(ctx,next)
99
- : (ctx.status = HTTP_FORBIDDEN)
100
+ : sendErrorPage(ctx, HTTP_FORBIDDEN)
101
if (ctx.method === 'PUT') { // curl -T file url/
102
const decPath = decodeURI(path)
103
let rest = basename(decPath)
104
const folder = await urlToNode(dirname(decPath), ctx, vfs, v => rest = v+'/'+rest)
105
if (!folder)
105
- return ctx.status = HTTP_NOT_FOUND
106
+ return sendErrorPage(ctx, HTTP_NOT_FOUND)
107
const dest = uploadWriter(folder, rest, ctx)
108
if (dest) {
109
await pipeline(ctx.req, dest)
@@ -116,7 +117,7 @@ export const serveGuiAndSharedFiles: Koa.Middleware = async (ctx, next) => {
117
if (node?.default && (path.endsWith('/') || !node.default.match(/\.html?$/i))) // final/ needed on browser to make resource urls correctly
118
node = await urlToNode(node.default, ctx, node)
119
if (!node)
119
- return ctx.status = HTTP_NOT_FOUND
120
+ return sendErrorPage(ctx, HTTP_NOT_FOUND)
121
if (ctx.method === 'POST') { // curl -F upload=@file url/
122
ctx.body = {}
123
const form = formidable({
@@ -136,7 +137,7 @@ export const serveGuiAndSharedFiles: Koa.Middleware = async (ctx, next) => {
137
return ctx.redirect(ctx.state.revProxyPath + ctx.originalUrl.replace(/(\?|$)/, '/$1')) // keep query-string, if any
138
if (statusCodeForMissingPerm(node, 'can_list', ctx)) {
139
if (ctx.status === HTTP_FORBIDDEN)
139
- return
140
+ return sendErrorPage(ctx, HTTP_FORBIDDEN)
141
const browserDetected = ctx.get('Upgrade-Insecure-Requests') || ctx.get('Sec-Fetch-Mode') // ugh, heuristics
142
if (!browserDetected) // we don't want to trigger basic authentication on browsers, it's meant for download managers only
143
return ctx.set('WWW-Authenticate', 'Basic') // we support basic authentication
@@ -149,6 +150,24 @@ export const serveGuiAndSharedFiles: Koa.Middleware = async (ctx, next) => {
150
: serveFrontendFiles(ctx, next)
151
}
152
153
+// to be used with errors whose recipient is possibly human
154
+export async function sendErrorPage(ctx: Koa.Context, code: number) {
155
+ ctx.status = code
156
+ const msg = (errorMessages as any)[ctx.status]
157
+ if (!msg) return
158
+ const lang = await getLangData(ctx)
159
+ if (!lang) return
160
+ const trans = (Object.values(lang)[0] as any)?.translate
161
+ if (!trans) return
162
+ ctx.body = trans[msg]
163
+}
164
+
165
+const errorMessages = {
166
+ [HTTP_NOT_FOUND]: "Not found",
167
+ [HTTP_UNAUTHORIZED]: "Unauthorized",
168
+ [HTTP_FORBIDDEN]: "Forbidden",
169
+}
170
+
171
const baseUrl = defineConfig('base_url', '')
172
173
async function sendFolderList(node: VfsNode, ctx: Koa.Context) {