|
1
|
|
|
import { getTagsFromId3Tag } from '../id3-tag' |
|
2
|
|
|
import { isFunction, isString } from '../util' |
|
3
|
|
|
import { Tags, TagIdentifiers } from '../types/Tags' |
|
4
|
|
|
import { Options } from '../types/Options' |
|
5
|
|
|
import { |
|
6
|
|
|
getId3TagDataFromFileAsync, |
|
7
|
|
|
getId3TagDataFromFileSync |
|
8
|
|
|
} from '../file-read' |
|
9
|
|
|
import { FileReadOptions, ReadCallback } from '../types/read' |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Reads ID3-Tags from a buffer. |
|
13
|
|
|
* @deprecated Use `readFromBuffer` instead. |
|
14
|
|
|
* @public |
|
15
|
|
|
*/ |
|
16
|
|
|
export function read(buffer: Buffer, options?: Options): Tags |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Reads ID3-Tags synchronously from a file. |
|
20
|
|
|
* @deprecated Use `readFromFileSync` instead. |
|
21
|
|
|
* @public |
|
22
|
|
|
*/ |
|
23
|
|
|
export function read(filepath: string, options?: FileReadOptions): Tags |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Reads ID3-Tags asynchronously from passed filepath. |
|
27
|
|
|
* @deprecated Use `readFromFile` instead. |
|
28
|
|
|
* @public |
|
29
|
|
|
*/ |
|
30
|
|
|
export function read(filebuffer: string, callback: ReadCallback): void |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Reads ID3-Tags asynchronously from passed filepath. |
|
34
|
|
|
* @deprecated Use `readFromFile` instead. |
|
35
|
|
|
* @public |
|
36
|
|
|
*/ |
|
37
|
|
|
export function read( |
|
38
|
|
|
filebuffer: string, options: FileReadOptions, callback: ReadCallback |
|
39
|
|
|
): void |
|
40
|
|
|
|
|
41
|
|
|
export function read( |
|
42
|
|
|
filebuffer: string | Buffer, |
|
43
|
|
|
optionsOrCallback?: FileReadOptions | ReadCallback, |
|
44
|
|
|
callback?: ReadCallback |
|
45
|
|
|
): Tags | TagIdentifiers | void { |
|
46
|
|
|
const options: FileReadOptions = |
|
47
|
|
|
(isFunction(optionsOrCallback) ? {} : optionsOrCallback) ?? {} |
|
48
|
|
|
callback = |
|
49
|
|
|
isFunction(optionsOrCallback) ? optionsOrCallback : callback |
|
50
|
|
|
|
|
51
|
|
|
if (isFunction(callback)) { |
|
52
|
|
|
return readAsync(filebuffer, options, callback) |
|
53
|
|
|
} |
|
54
|
|
|
return readSync(filebuffer, options) |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
function readSync(filebuffer: string | Buffer, options: FileReadOptions) { |
|
58
|
|
|
if (isString(filebuffer)) { |
|
59
|
|
|
filebuffer = getId3TagDataFromFileSync(filebuffer, options)[0] ?? Buffer.alloc(0) |
|
60
|
|
|
} |
|
61
|
|
|
return getTagsFromId3Tag(filebuffer, options) |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
function readAsync( |
|
65
|
|
|
filebuffer: string | Buffer, |
|
66
|
|
|
options: FileReadOptions, |
|
67
|
|
|
callback: ReadCallback |
|
68
|
|
|
) { |
|
69
|
|
|
if (isString(filebuffer)) { |
|
70
|
|
|
getId3TagDataFromFileAsync(filebuffer, options).then( |
|
71
|
|
|
(buffers) => callback( |
|
72
|
|
|
null, decodeTagBuffers(buffers, options) |
|
73
|
|
|
), |
|
74
|
|
|
(error) => callback(error, null) |
|
75
|
|
|
) |
|
76
|
|
|
} else { |
|
77
|
|
|
callback(null, getTagsFromId3Tag(filebuffer, options)) |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// New API |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Reads ID3-Tags from a buffer. |
|
85
|
|
|
* |
|
86
|
|
|
* @public |
|
87
|
|
|
*/ |
|
88
|
|
|
export function readFromBuffer( |
|
89
|
|
|
buffer: Buffer, |
|
90
|
|
|
options: Options = {} |
|
91
|
|
|
): Tags | TagIdentifiers { |
|
92
|
|
|
return getTagsFromId3Tag(buffer, options) |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Reads ID3-Tags synchronously from a file. |
|
97
|
|
|
* |
|
98
|
|
|
* @public |
|
99
|
|
|
*/ |
|
100
|
|
|
export function readFromFileSync( |
|
101
|
|
|
filepath: string, |
|
102
|
|
|
options: FileReadOptions = {} |
|
103
|
|
|
): Tags | TagIdentifiers { |
|
104
|
|
|
const buffers = getId3TagDataFromFileSync(filepath, options) |
|
105
|
|
|
return decodeTagBuffers(buffers, options) |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Reads ID3-Tags asynchronously from a file. |
|
110
|
|
|
* |
|
111
|
|
|
* @public |
|
112
|
|
|
*/ |
|
113
|
|
|
export function readFromFile( |
|
114
|
|
|
filepath: string, |
|
115
|
|
|
callback: ReadCallback |
|
116
|
|
|
): void |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Reads ID3-Tags asynchronously from a file. |
|
120
|
|
|
* |
|
121
|
|
|
* @public |
|
122
|
|
|
*/ |
|
123
|
|
|
export function readFromFile( |
|
124
|
|
|
filepath: string, |
|
125
|
|
|
options: FileReadOptions, |
|
126
|
|
|
callback: ReadCallback |
|
127
|
|
|
): void |
|
128
|
|
|
|
|
129
|
|
|
export function readFromFile( |
|
130
|
|
|
filepath: string, |
|
131
|
|
|
optionsOrCallback?: FileReadOptions | ReadCallback, |
|
132
|
|
|
maybeCallback: ReadCallback = () => { /* */ } |
|
133
|
|
|
): void { |
|
134
|
|
|
const options: FileReadOptions = |
|
135
|
|
|
(isFunction(optionsOrCallback) ? {} : optionsOrCallback) ?? {} |
|
136
|
|
|
const callback = |
|
137
|
|
|
isFunction(optionsOrCallback) ? optionsOrCallback : maybeCallback |
|
138
|
|
|
|
|
139
|
|
|
getId3TagDataFromFileAsync(filepath, options).then( |
|
140
|
|
|
(buffers) => callback( |
|
141
|
|
|
null, decodeTagBuffers(buffers, options) |
|
142
|
|
|
), |
|
143
|
|
|
(error) => callback(error, null) |
|
144
|
|
|
) |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
// For now, just take the first one |
|
148
|
|
|
function decodeTagBuffers(buffers: Buffer[], options: Options) { |
|
149
|
|
|
const firstBuffer = buffers[0] ?? Buffer.alloc(0) |
|
150
|
|
|
return getTagsFromId3Tag(firstBuffer, options) |
|
151
|
|
|
} |
|
152
|
|
|
|