-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate-to-2.js
44 lines (40 loc) · 1.68 KB
/
migrate-to-2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const id3 = require('node-id3')
const { promisify } = require('util')
const { rename } = require('fs/promises')
const readId3 = promisify(id3.read.bind(id3))
const updateId3 = promisify(id3.update.bind(id3))
const pathLib = require('path')
const sanitizeFilename = require('sanitize-filename')
module.exports = async (paths, force) => {
for (const path of paths) {
try {
console.log(`Migrating ${path}...`)
const id3 = await readId3(path)
// get current version (defaults to 1.0.0)
const userDefinedText = id3.userDefinedText || []
const dzdlVersionObject = userDefinedText.find(text => text.description === 'dzdl-version')
const dzdlVersion = typeof dzdlVersionObject === 'object' ? dzdlVersionObject.value : '1.0.0'
if (dzdlVersion.startsWith('1.') || force) {
// migrate filename
// In ID3v2.3 the artists are separated by /,
// but an artist's name can contain / too (AC/DC),
// so if there is a /, it's impossible to know
// who the leading artist is (AC or AC/DC?).
// So in this case, we default to the album artist.
const artist = id3.artist.includes('/') ? id3.performerInfo : id3.artist
const newPath = pathLib.resolve(
pathLib.dirname(path),
sanitizeFilename(`${artist} - ${id3.title} (${id3.album}).mp3`)
)
await rename(path, newPath)
// update version
if (dzdlVersion !== '2.0.0') {
userDefinedText.push({ description: 'dzdl-version', value: '2.0.0' })
await updateId3({ userDefinedText }, newPath)
}
}
} catch (err) {
console.error(`Couldn't process the ID3 tag of ${path}!`, err)
}
}
}