log rotation will preserve file extension
Massimo Melina committed
Jan 25, 2024 at 00:24 UTC
3ab018b035adca81d64141bd423228279590f63c
4 files changed
+14
-8
src/cross.ts
+4
@@ -135,6 +135,10 @@ export function removeStarting(sub: string, s: string) {
135
return s.startsWith(sub) ? s.slice(sub.length) : s
136
}
137
138
+export function strinsert(s: string, at: number, insert: string, remove=0) {
139
+ return s.slice(0, at) + insert + s.slice(at + remove)
140
+}
141
+
142
export function splitAt(sub: string | number, all: string): [string, string] {
143
if (typeof sub === 'number')
144
return [all.slice(0, sub), all.slice(sub + 1)]
src/github.ts
+3
-3
@@ -2,7 +2,7 @@
2
3
import events from './events'
4
import { DAY, httpString, httpStream, unzip, AsapStream, debounceAsync } from './misc'
5
-import { DISABLING_POSTFIX, findPluginByRepo, getAvailablePlugins, getPluginInfo, mapPlugins,
5
+import { DISABLING_SUFFIX, findPluginByRepo, getAvailablePlugins, getPluginInfo, mapPlugins,
6
parsePluginSource, PATH as PLUGINS_PATH, Repo } from './plugins'
7
import { ApiError } from './apiMiddleware'
8
import _ from 'lodash'
@@ -79,12 +79,12 @@ export async function downloadPlugin(repo: Repo, { branch='', overwrite=false }=
79
if (!folder || path.endsWith('/')) return false
80
let dest = path.slice(folder.length)
81
if (dest === MAIN) // avoid being possibly loaded before the download is complete
82
- dest += DISABLING_POSTFIX
82
+ dest += DISABLING_SUFFIX
83
dest = join(installPath, dest)
84
return rm(dest, { force: true }).then(() => dest, () => false)
85
})
86
const main = join(installPath, MAIN)
87
- await rename(main + DISABLING_POSTFIX, main) // we are good now, restore name
87
+ await rename(main + DISABLING_SUFFIX, main) // we are good now, restore name
88
.catch(e => { throw e.code !== 'ENOENT' ? e : new ApiError(HTTP_NOT_ACCEPTABLE, "missing main file") })
89
return folder
90
}
src/log.ts
+5
-3
@@ -9,7 +9,8 @@ import { stat } from 'fs/promises'
9
import _ from 'lodash'
10
import { createFileWithPath, prepareFolder } from './util-files'
11
import { getCurrentUsername } from './auth'
12
-import { DAY, makeNetMatcher, tryJson, Dict, Falsy, CFG } from './misc'
12
+import { DAY, makeNetMatcher, tryJson, Dict, Falsy, CFG, strinsert } from './misc'
13
+import { extname } from 'path'
14
import events from './events'
15
import { getConnection } from './connections'
16
import { app } from './index'
@@ -92,9 +93,10 @@ export const logMw: Koa.Middleware = async (ctx, next) => {
93
|| rotate === 'd' && (passed >= DAY || now.getDate() !== last.getDate()) // checking passed will solve the case when the day of the month is the same but a month has passed
94
|| rotate === 'w' && (passed >= 7*DAY || now.getDay() < last.getDay())) {
95
stream.end()
95
- const postfix = last.getFullYear() + '-' + doubleDigit(last.getMonth() + 1) + '-' + doubleDigit(last.getDate())
96
+ const suffix = '-' + last.getFullYear() + '-' + doubleDigit(last.getMonth() + 1) + '-' + doubleDigit(last.getDate())
97
+ const newPath = strinsert(path, path.length - extname(path).length, suffix)
98
try { // other logging requests shouldn't happen while we are renaming. Since this is very infrequent we can tolerate solving this by making it sync.
97
- renameSync(path, path + '-' + postfix)
99
+ renameSync(path, newPath)
100
}
101
catch(e: any) { // ok, rename failed, but this doesn't mean we ain't gonna log
102
console.error(String(e || e.message))
src/plugins.ts
+2
-2
@@ -21,7 +21,7 @@ import { dirname, join, resolve } from 'path'
21
import { watchLoadCustomHtml } from './customHtml'
22
23
export const PATH = 'plugins'
24
-export const DISABLING_POSTFIX = '-disabled'
24
+export const DISABLING_SUFFIX = '-disabled'
25
export const STORAGE_FOLDER = 'storage'
26
27
const plugins: Record<string, Plugin> = {}
@@ -288,7 +288,7 @@ export async function rescan() {
288
patterns.push(adjustStaticPathForGlob(APP_PATH) + '/' + patterns[0])
289
const met = []
290
for (const { path, dirent } of await glob(patterns, { onlyFiles: false, suppressErrors: true, objectMode: true })) {
291
- if (!dirent.isDirectory() || path.endsWith(DISABLING_POSTFIX)) continue
291
+ if (!dirent.isDirectory() || path.endsWith(DISABLING_SUFFIX)) continue
292
const id = path.split('/').slice(-1)[0]!
293
met.push(id)
294
const w = pluginWatchers.get(id)