-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: DVD creation and extraction (#10)
- Loading branch information
Showing
10 changed files
with
169 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import util from 'node:util'; | ||
import fs from 'node:fs'; | ||
import ChdmanBin from './chdmanBin.js'; | ||
import { CompressionAlgorithm } from './common.js'; | ||
|
||
export interface CreateDvdOptions { | ||
outputFilename: string, | ||
outputParentFilename?: string, | ||
force?: boolean, | ||
inputFilename: string, | ||
compression?: 'none' | CompressionAlgorithm[], | ||
numProcessors?: number | ||
} | ||
|
||
export interface ExtractDvdOptions { | ||
outputFilename: string, | ||
outputBinFilename?: string, | ||
force?: boolean, | ||
inputFilename: string, | ||
inputParentFilename?: string, | ||
} | ||
|
||
export default { | ||
/** | ||
* Create a DVD CHD. | ||
*/ | ||
async createDvd(options: CreateDvdOptions): Promise<void> { | ||
const existedBefore = await util.promisify(fs.exists)(options.outputFilename); | ||
try { | ||
await ChdmanBin.run([ | ||
'createdvd', | ||
'--output', options.outputFilename, | ||
...(options.outputParentFilename ? ['--outputparent', String(options.outputParentFilename)] : []), | ||
...(options.force === true ? ['--force'] : []), | ||
'--input', options.inputFilename, | ||
...(options.compression === undefined | ||
? [] | ||
: ['--compression', Array.isArray(options.compression) ? options.compression.join(',') : options.compression]), | ||
...(options.numProcessors === undefined ? [] : ['--numprocessors', String(options.numProcessors)]), | ||
]); | ||
} catch (error) { | ||
// chdman can leave cruft when it fails | ||
if (!existedBefore) { | ||
await util.promisify(fs.rm)(options.outputFilename, { force: true }); | ||
} | ||
throw error; | ||
} | ||
}, | ||
|
||
/** | ||
* Extract a DVD CHD. | ||
*/ | ||
async extractDvd(options: ExtractDvdOptions): Promise<void> { | ||
await ChdmanBin.run([ | ||
'extractdvd', | ||
'--output', options.outputFilename, | ||
...(options.outputBinFilename ? ['--outputbin', options.outputBinFilename] : []), | ||
...(options.force === true ? ['--force'] : []), | ||
'--input', options.inputFilename, | ||
...(options.inputParentFilename ? ['--inputparent', options.inputParentFilename] : []), | ||
]); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import path from 'node:path'; | ||
import os from 'node:os'; | ||
import util from 'node:util'; | ||
import fs from 'node:fs'; | ||
import crypto from 'node:crypto'; | ||
import ChdmanInfo from '../../src/chdman/chdmanInfo.js'; | ||
import TestUtil from '../testUtil.js'; | ||
import ChdmanDvd from '../../src/chdman/chdmanDvd.js'; | ||
|
||
// https://unix.stackexchange.com/a/33634 | ||
|
||
test('should fail on nonexistent file', async () => { | ||
const temporaryChd = `${await TestUtil.mktemp(path.join(os.tmpdir(), 'dummy'))}.chd`; | ||
const temporaryIso = `${await TestUtil.mktemp(path.join(os.tmpdir(), 'dummy'))}.iso`; | ||
|
||
try { | ||
await expect(ChdmanDvd.createDvd({ | ||
inputFilename: os.devNull, | ||
outputFilename: temporaryChd, | ||
})).rejects.toBeDefined(); | ||
await expect(ChdmanInfo.info({ | ||
inputFilename: temporaryIso, | ||
})).rejects.toBeDefined(); | ||
await expect(ChdmanDvd.createDvd({ | ||
inputFilename: temporaryChd, | ||
outputFilename: temporaryIso, | ||
})).rejects.toBeDefined(); | ||
} finally { | ||
await util.promisify(fs.rm)(temporaryChd, { force: true }); | ||
} | ||
}); | ||
|
||
test.each([ | ||
[path.join('test', 'fixtures', 'iso', '2048.iso')], | ||
[path.join('test', 'fixtures', 'iso', '16384.iso')], | ||
])('should create, info, and extract: %s', async (iso) => { | ||
const temporaryChd = `${await TestUtil.mktemp(path.join(os.tmpdir(), path.basename(iso)))}.chd`; | ||
const temporaryIso = `${await TestUtil.mktemp(path.join(os.tmpdir(), 'dummy'))}.hd`; | ||
|
||
try { | ||
await ChdmanDvd.createDvd({ | ||
inputFilename: iso, | ||
outputFilename: temporaryChd, | ||
}); | ||
await expect(TestUtil.exists(temporaryChd)).resolves.toEqual(true); | ||
|
||
const info = await ChdmanInfo.info({ | ||
inputFilename: temporaryChd, | ||
}); | ||
expect(info.fileVersion).toBeGreaterThan(0); | ||
expect(info.logicalSize).toBeGreaterThan(0); | ||
expect(info.hunkSize).toBeGreaterThan(0); | ||
expect(info.totalHunks).toBeGreaterThan(0); | ||
expect(info.unitSize).toBeGreaterThan(0); | ||
expect(info.totalUnits).toBeGreaterThan(0); | ||
expect(info.compression.length).toBeGreaterThan(0); | ||
expect(info.chdSize).toBeGreaterThan(0); | ||
expect(info.ratio).toBeGreaterThan(0); | ||
expect(info.sha1).toBeTruthy(); | ||
expect(info.dataSha1).toBeTruthy(); | ||
|
||
await ChdmanDvd.extractDvd({ | ||
inputFilename: temporaryChd, | ||
outputFilename: temporaryIso, | ||
}); | ||
await expect(TestUtil.exists(temporaryIso)).resolves.toEqual(true); | ||
expect(crypto.createHash('sha1').update(await util.promisify(fs.readFile)(iso)).digest('hex')) | ||
.toEqual(crypto.createHash('sha1').update(await util.promisify(fs.readFile)(temporaryIso)).digest('hex')); | ||
} finally { | ||
await util.promisify(fs.rm)(temporaryChd, { force: true }); | ||
await util.promisify(fs.rm)(temporaryIso, { force: true }); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
Binary file not shown.
Binary file not shown.
Binary file not shown.