@samitouri / QOSami-HFS / commits / 9e3b05ae

missing important info in logs, like what has been deleted

Massimo Melina committed Apr 17, 2024 at 14:41 UTC 9e3b05aef2bb3a2fd146bb384ccb4b374bdc8eda
3 files changed +10 -6
admin/src/LogsPage.ts
+2 -2
@@ -254,9 +254,9 @@ function LogFile({ file, addToFooter, hidden }: { hidden?: boolean, file: string
254 if (extra?.ua && !showAgent)
255 setShowAgent(true)
256 x.notes = extra?.dl ? "fully downloaded"
257 - : (x.method === 'PUT' || extra?.ul) ? "uploaded " + formatBytes(extra.size, { sep: NBSP })
257 + : (x.method === 'PUT' || extra?.ul) ? "uploaded " + formatBytes(extra?.size, { sep: NBSP })
258 : x.status === HTTP_UNAUTHORIZED && x.uri?.startsWith(API_URL + 'loginSrp') ? "login failed" + prefix(':\n', extra?.u)
259 - : x.notes
259 + : _.map(extra?.params, (v, k) => `${k}: ${v}\n`).join('') + (x.notes || '')
260 return x
261 }
262 }
src/frontEndApis.ts
+5
@@ -49,6 +49,7 @@ export const frontEndApis: ApiHandlers = {
49
50 async create_folder({ uri, name }, ctx) {
51 apiAssertTypes({ string: { uri, name } })
52 + ctx.logExtra(null, { name, target: decodeURI(uri) })
53 if (!isValidFileName(name))
54 return new ApiError(HTTP_BAD_REQUEST, 'bad name')
55 const parentNode = await urlToNode(uri, ctx)
@@ -68,6 +69,7 @@ export const frontEndApis: ApiHandlers = {
69
70 async delete({ uri }, ctx) {
71 apiAssertTypes({ string: { uri } })
72 + ctx.logExtra(null, { target: decodeURI(uri) })
73 const node = await urlToNode(uri, ctx)
74 if (!node)
75 throw new ApiError(HTTP_NOT_FOUND)
@@ -87,6 +89,7 @@ export const frontEndApis: ApiHandlers = {
89
90 async rename({ uri, dest }, ctx) {
91 apiAssertTypes({ string: { uri, dest } })
92 + ctx.logExtra(null, { target: decodeURI(uri), destination: decodeURI(dest) })
93 if (dest.includes('/') || dirTraversal(dest))
94 throw new ApiError(HTTP_FORBIDDEN)
95 const node = await urlToNode(uri, ctx)
@@ -117,6 +120,7 @@ export const frontEndApis: ApiHandlers = {
120
121 async move_files({ uri_from, uri_to }, ctx) {
122 apiAssertTypes({ array: { uri_from }, string: { uri_to } })
123 + ctx.logExtra(null, { target: uri_from.map(decodeURI), destination: decodeURI(uri_to) })
124 const destNode = await urlToNode(uri_to, ctx)
125 const code = !destNode ? HTTP_NOT_FOUND : statusCodeForMissingPerm(destNode, 'can_upload', ctx)
126 if (code) return new ApiError(code)
@@ -139,6 +143,7 @@ export const frontEndApis: ApiHandlers = {
143
144 async comment({ uri, comment }, ctx) {
145 apiAssertTypes({ string: { uri, comment } })
146 + ctx.logExtra(null, { target: decodeURI(uri) })
147 const node = await urlToNode(uri, ctx)
148 if (!node)
149 throw new ApiError(HTTP_NOT_FOUND)
src/log.ts
+3 -4
@@ -137,7 +137,7 @@ export const logMw: Koa.Middleware = async (ctx, next) => {
137
138 declare module "koa" {
139 interface BaseContext {
140 - logExtra(o: Falsy | Dict<any>): void
140 + logExtra(o: Falsy | Dict<any>, params?: Dict<any>): void
141 }
142 interface DefaultState {
143 dontLog?: boolean // don't log this request
@@ -147,9 +147,8 @@ declare module "koa" {
147 }
148
149 events.on('app', () => { // wait for app to be set
150 - app.context.logExtra = function(o) { // no => as we need 'this'
151 - if (o)
152 - Object.assign((this as any).state.logExtra ||= {}, o)
150 + app.context.logExtra = function(anything, params) { // no => as we need 'this'
151 + _.merge((this as any).state, { logExtra: { ...anything, params } }) // params will be considered as parameters of the API
152 }
153 })
154