Skip to content

Split any YouTube song into stems

TypeScript

fix(export): keep dotted file names and drop partial m4a on failure

David Naviafec9a3c319bd
diff --git a/src/main/export-session.ts b/src/main/export-session.ts
index 6e01738..dd3a0ad 100644
--- a/src/main/export-session.ts
+++ b/src/main/export-session.ts
@@ -1,6 +1,6 @@
import { randomUUID } from 'crypto'
import { writeFile, rm } from 'fs/promises'
-import { dirname, join, parse } from 'path'
+import { dirname, join } from 'path'
import { sanitizeName } from './library'
// No Electron here on purpose: the dialogs live in the IPC handlers, so this
@@ -29,8 +29,11 @@ export async function writeToSession(
let dest: string
if (session.kind === 'file') {
if (fileName !== null) throw new Error('A single-file export takes no file name')
- const p = parse(session.path)
- dest = join(p.dir, `${p.name}.${ext}`)
+ // only a real audio extension is swapped: a title like "Mr. Brightside"
+ // has a dot that path.parse would take for one and cut the name there
+ dest = /\.(wav|m4a)$/i.test(session.path)
+ ? session.path.replace(/\.(wav|m4a)$/i, `.${ext}`)
+ : `${session.path}.${ext}`
} else {
if (fileName === null) throw new Error('A folder export needs a file name')
dest = join(session.path, `${sanitizeName(fileName)}.${ext}`)
@@ -44,6 +47,10 @@ export async function writeToSession(
try {
await writeFile(tmp, wav)
await transcode(tmp, dest)
+ } catch (err) {
+ // a transcoder that dies mid-write leaves a truncated file the user would take for the export
+ await rm(dest, { force: true })
+ throw err
} finally {
await rm(tmp, { force: true })
}
diff --git a/tests/export-session.test.mjs b/tests/export-session.test.mjs
index c7d33ee..b127a51 100644
--- a/tests/export-session.test.mjs
+++ b/tests/export-session.test.mjs
@@ -1,7 +1,7 @@
import { test, after } from 'node:test'
import assert from 'node:assert/strict'
import { registerTsHooks } from './helpers/ts-resolve.mjs'
-import { mkdtempSync, readFileSync, existsSync, readdirSync, rmSync } from 'node:fs'
+import { mkdtempSync, readFileSync, existsSync, readdirSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
@@ -67,3 +67,35 @@ test('the format pinned at begin decides the output, not the caller', async () =
const out = await writeToSession(token, 'a', wav, noTranscode, 'aac')
assert.equal(out, join(dir, 'a.wav'))
})
+
+test('file session appends the extension when the name has dots but no audio extension', async () => {
+ const dir = mkdtempSync(join(root, 'dots-'))
+ const token = openSession('file', join(dir, 'Mr. Brightside - drums'), 'wav')
+ const out = await writeToSession(token, null, wav, noTranscode)
+ assert.equal(out, join(dir, 'Mr. Brightside - drums.wav'))
+ closeSession(token)
+})
+
+test('file session replaces a known audio extension whatever its case', async () => {
+ const dir = mkdtempSync(join(root, 'case-'))
+ const upper = openSession('file', join(dir, 'song.M4A'), 'wav')
+ assert.equal(await writeToSession(upper, null, wav, noTranscode), join(dir, 'song.wav'))
+ const same = openSession('file', join(dir, 'take.wav'), 'wav')
+ assert.equal(await writeToSession(same, null, wav, noTranscode), join(dir, 'take.wav'))
+ closeSession(upper)
+ closeSession(same)
+})
+
+test('failing transcode removes a partial destination file', async () => {
+ const dir = mkdtempSync(join(root, 'partial-'))
+ const token = openSession('file', join(dir, 'song.m4a'), 'aac')
+ await assert.rejects(
+ writeToSession(token, null, wav, async (_src, dest) => {
+ writeFileSync(dest, 'half an m4a')
+ throw new Error('ffmpeg died')
+ }),
+ /ffmpeg died/
+ )
+ assert.deepEqual(readdirSync(dir), [])
+ closeSession(token)
+})