refactor: fix file condition checks

This commit is contained in:
wancat committed 2024-02-19 18:35:39 +08:00
1 parent 64215d3ef1
commit 4ab3096fa5
5 files changed
+16 -20

No files matched your search

+29
View File
@@ -0,0 +1,29 @@
import { execFile } from 'child_process'
import { buildFileCheckCmd } from '@linux-odyssey/utils'
import type { IFileCondition } from '@linux-odyssey/models'
function sanitizeId(id: string) {
return id.replace(/[^a-zA-Z0-9]/g, '')
}
// eslint-disable-next-line import/prefer-default-export
export function checkFile(id: string, file: IFileCondition) {
return new Promise((resolve, reject) => {
const cmd = buildFileCheckCmd(file)
// WARNING: This is vulnerable to command injection
execFile(
'docker',
['exec', sanitizeId(id), ...cmd],
{ timeout: 1000 },
(error) => {
const exists = !error
if (exists === file.exists) {
resolve(true)
} else {
reject(new Error('File check failed'))
}
}
)
})
}