mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
28 lines
773 B
TypeScript
28 lines
773 B
TypeScript
import { execFile } from 'child_process'
|
|
|
|
import { buildFileCheckCmd } from '@odyssey/utils'
|
|
import type { IFileExistenceChecker, IFileInput } from '@odyssey/game'
|
|
|
|
function sanitizeId(id: string) {
|
|
return id.replace(/[^a-zA-Z0-9]/g, '')
|
|
}
|
|
|
|
export class CLIFileExistenceChecker implements IFileExistenceChecker {
|
|
constructor(private readonly containerId: string) {}
|
|
exists(file: IFileInput) {
|
|
return new Promise<boolean>((resolve) => {
|
|
const cmd = buildFileCheckCmd(file)
|
|
// WARNING: This is vulnerable to command injection
|
|
execFile(
|
|
'docker',
|
|
['exec', sanitizeId(this.containerId), ...cmd],
|
|
{ timeout: 1000 },
|
|
(error) => {
|
|
const exists = !error
|
|
resolve(exists)
|
|
}
|
|
)
|
|
})
|
|
}
|
|
}
|