Skip to content

Commit

Permalink
feat: add an option to allow automatically removing system reserved c…
Browse files Browse the repository at this point in the history
…haracters from the path when saving
  • Loading branch information
WhiteMinds committed Apr 9, 2024
1 parent bdb9889 commit fc76651
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/http-server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@autorecord/http-server",
"version": "1.0.0",
"version": "1.1.0",
"description": "Expose manager interface through HTTP",
"main": "./lib/index.js",
"private": true,
Expand Down
15 changes: 14 additions & 1 deletion packages/http-server/src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const recorderManager = createRecorderManager<RecorderExtra>({

export const defaultManagerConfig: ManagerConfig = {
savePathRule: path.join(paths.data, '{platform}/{owner}/{year}-{month}-{date} {hour}-{min}-{sec} {title}.mp4'),
autoRemoveSystemReservedChars: true,
autoCheckLiveStatusAndRecord: true,
autoCheckInterval: 1000,
ffmpegOutputArgs: recorderManager.ffmpegOutputArgs,
Expand All @@ -55,6 +56,10 @@ export async function initRecorderManager(serverOpts: ServerOpts): Promise<void>
managerConfig.ffmpegOutputArgs = recorderManager.ffmpegOutputArgs
managerConfig.savePathRule += '.mp4'
}
if (managerConfig.autoRemoveSystemReservedChars == null) {
// v1.0.0 -> v1.1.0
managerConfig.autoRemoveSystemReservedChars = true
}

Object.assign(recorderManager, managerConfig)

Expand All @@ -66,7 +71,14 @@ export async function initRecorderManager(serverOpts: ServerOpts): Promise<void>
recorderManager.on('Updated', () => {
writeJSONFileSync<ManagerConfig>(
managerConfigPath,
pick(recorderManager, 'savePathRule', 'autoCheckLiveStatusAndRecord', 'autoCheckInterval', 'ffmpegOutputArgs'),
pick(
recorderManager,
'savePathRule',
'autoRemoveSystemReservedChars',
'autoCheckLiveStatusAndRecord',
'autoCheckInterval',
'ffmpegOutputArgs',
),
)
})

Expand Down Expand Up @@ -178,6 +190,7 @@ export async function genSRTFile(extraDataPath: string, srtPath: string): Promis

interface ManagerConfig {
savePathRule: string
autoRemoveSystemReservedChars: boolean
autoCheckLiveStatusAndRecord: boolean
autoCheckInterval: number
ffmpegOutputArgs: string
Expand Down
10 changes: 9 additions & 1 deletion packages/http-server/src/routes/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ import { asyncRouteHandler } from './utils'
const router = Router()

function getManager(args: API.getManager.Args): API.getManager.Resp {
return pick(recorderManager, 'savePathRule', 'autoCheckLiveStatusAndRecord', 'autoCheckInterval', 'ffmpegOutputArgs')
return pick(
recorderManager,
'savePathRule',
'autoRemoveSystemReservedChars',
'autoCheckLiveStatusAndRecord',
'autoCheckInterval',
'ffmpegOutputArgs',
)
}

function updateManager(args: API.updateManager.Args): API.updateManager.Resp {
Expand Down Expand Up @@ -56,6 +63,7 @@ router
// TODO: 这里先不做 schema 校验,以后再加
(req.body ?? {}) as API.updateManager.Args,
'savePathRule',
'autoRemoveSystemReservedChars',
'autoCheckLiveStatusAndRecord',
'autoCheckInterval',
'ffmpegOutputArgs',
Expand Down
17 changes: 17 additions & 0 deletions packages/manager/src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface RecorderProvider<E extends AnyObject> {

const configurableProps = [
'savePathRule',
'autoRemoveSystemReservedChars',
'autoCheckLiveStatusAndRecord',
'autoCheckInterval',
'ffmpegOutputArgs',
Expand Down Expand Up @@ -77,6 +78,7 @@ export interface RecorderManager<
stopCheckLoop: (this: RecorderManager<ME, P, PE, E>) => void

savePathRule: string
autoRemoveSystemReservedChars: boolean
ffmpegOutputArgs: string
}

Expand Down Expand Up @@ -197,6 +199,8 @@ export function createRecorderManager<
opts.savePathRule ??
path.join(process.cwd(), '{platform}/{owner}/{year}-{month}-{date} {hour}-{min}-{sec} {title}.mp4'),

autoRemoveSystemReservedChars: opts.autoRemoveSystemReservedChars ?? true,

ffmpegOutputArgs:
opts.ffmpegOutputArgs ??
'-c copy' +
Expand Down Expand Up @@ -272,8 +276,21 @@ export function genSavePathFromRule<
sec: formatDate(now, 'ss'),
...extData,
}
if (manager.autoRemoveSystemReservedChars) {
for (const key in params) {
params[key] = removeSystemReservedChars(params[key])
}
}

return formatTemplate(manager.savePathRule, params)
}

// TODO: 目前只对 windows 做了处理
function removeSystemReservedChars(filename: string) {
if (process.platform !== 'win32') return filename

// Refs: https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions
return filename.replace(/[\\/:*?"<>|]/g, '')
}

export type GetProviderExtra<P> = P extends RecorderProvider<infer E> ? E : never
1 change: 1 addition & 0 deletions packages/web/src/messages/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export default {
settings: {
global_recording_settings: 'Global Recording Settings',
save_path_rule: 'Save Path Rule',
auto_remove_reserved_chars: 'Auto remove system reserved characters',
spr_alert_title: 'How to use variables in the save path?',
spr_alert_subtitle: "Wrap the variable name with `{'{}'}` to use it, such as `/path/{'{platform}'}`",
filed_name: 'Variable Name',
Expand Down
1 change: 1 addition & 0 deletions packages/web/src/messages/ru.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export default {
settings: {
global_recording_settings: 'Глобальные настройки записи',
save_path_rule: 'Правило сохранения пути',
auto_remove_reserved_chars: 'Автоматически удалить системные зарезервированные символы',
spr_alert_title: 'Как использовать переменные в пути сохранения?',
spr_alert_subtitle: "Оберните имя переменной в `{'{}'}`, чтобы использовать его, например `/path/{'{platform}'}`",
filed_name: 'Имя переменной',
Expand Down
1 change: 1 addition & 0 deletions packages/web/src/messages/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export default {
settings: {
global_recording_settings: '全局录制设置',
save_path_rule: '保存路径规则',
auto_remove_reserved_chars: '自动移除系统保留字符',
spr_alert_title: '如何在保存路径中使用变量?',
spr_alert_subtitle: "使用 `{'{}'}` 包裹住变量名即可,如 `/path/{'{platform}'}`",
filed_name: '变量名',
Expand Down
7 changes: 7 additions & 0 deletions packages/web/src/views/RecordersManage/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
v-model="manager.savePathRule"
append-inner-icon="mdi-help-circle"
@click:append-inner="savePathRuleAlertVisible = !savePathRuleAlertVisible"
hide-details
/>
<v-alert v-model="savePathRuleAlertVisible" closable>
<v-alert-title>{{ t('settings.spr_alert_title') }}</v-alert-title>
Expand Down Expand Up @@ -79,6 +80,12 @@
</v-table>
</v-alert>

<v-checkbox
:label="t('settings.auto_remove_reserved_chars')"
v-model="manager.autoRemoveSystemReservedChars"
hide-details
/>

<v-text-field :label="t('settings.ffmpeg_output_args')" v-model="manager.ffmpegOutputArgs" />

<v-checkbox
Expand Down

0 comments on commit fc76651

Please sign in to comment.