-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
212 lines (173 loc) · 6.84 KB
/
index.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
let downloadArrayBufferAsFlac = (arrBuf, name = '') => {
let a = document.createElement('a');
a.href = URL.createObjectURL(new Blob([arrBuf], {type: 'audio/flac'}));
a.download = name+'.flac';
document.head.appendChild(a);
a.click();
a.remove();
};
let editor;
let t;
function removeRow(e) {
e.target.parentElement.parentElement.remove();
}
function addRow(field = '', value = '') {
let r = t.insertRow();
let add = type => {
let c = r.insertCell();
return c.appendChild(document.createElement(type));
};
let i = add('input');
i.value = field;
if (!field) i.focus();
add('input').value = value;
i = add('button');
i.textContent = 'X';
i.addEventListener('click', removeRow);
}
function fillTable() {
while (t.rows[1]) t.deleteRow(1);
editor._vorbisComment.comments.toStringArray().forEach(comment=>{
let field = comment.split('=')[0];
let value = comment.split('=')[1];
addRow(field, value);
})
}
function parseTable() {
let rows = Array.from(t.rows).slice(1);
let strings = rows.map(row=>
(row.cells[0].firstChild.value && row.cells[1].firstChild.value)
? row.cells[0].firstChild.value +'='+ row.cells[1].firstChild.value
: undefined)
.filter(e=>e!==undefined);
strings.forEach(string=> editor.addComment(string));
}
function removePicture(e) {
e.target.parentElement.remove();
let i = document.getElementById('images');
if (!i.firstElementChild)
i.classList.add('hidden');
}
function addPicture(blobUrl, mime, apic = 3) {
let i = document.getElementById('images');
i.classList.remove('hidden');
let d = i.appendChild(document.createElement('div'));
d.classList.add('image');
let a = d.appendChild(document.createElement('a'));
a.target = '_blank';
a.href = blobUrl;
a.dataset.mime = mime;
a.dataset.apic = apic;
a.appendChild(document.createElement('img')).src = blobUrl;
let r = d.appendChild(document.createElement('span'));
r.classList.add('remove_image');
r.textContent = 'X';
r.addEventListener('click', removePicture);
}
function fillPictures() {
let i = document.getElementById('images');
while (i.firstElementChild)
i.firstElementChild.remove();
editor.metadata.blocks.forEach(block=>{
if (block.blockType === 'PICTURE') {
let array = block.data.data;
let apic = block.data.APICtype;
let mime;
if (array[0]===0x42 && array[1]===0x4D) mime = 'image/bmp';
if (array[0]===0x47 && array[1]===0x49 && array[2]===0x46) mime = 'image/gif';
if (array[0]===0xFF && array[1]===0xD8 && array[2]===0xFF) mime = 'image/jpeg';
if (array[0]===0x89 && array[1]===0x50 && array[2]===0x4E && array[2]===0x47) mime = 'image/png';
if (array[0]===0x49 && array[1]===0x49 && array[2]===0x2A && array[3]===0x00) mime = 'image/tiff';
if (array[0]===0x40 && array[1]===0x40 && array[2]===0x00 && array[3]===0x2A) mime = 'image/tiff';
if (array[8]===0x57 && array[9]===0x45 && array[10]===0x42 && array[11]===0x50) mime = 'image/webp';
if (mime !== block.data.MIMEType) console.log('Wrong mime type in source FLAC metadata ~ : %s, real: %s', block.data.MIMEType, mime);
let url = URL.createObjectURL(new Blob([array], {type: mime}));
addPicture(url, mime, apic);
}
})
}
function parseImages(callback) {
let counter = 0;
if (!document.querySelector('#images a')) return callback();
document.querySelectorAll('#images a').forEach((a, n, arr)=>{
let xhr = new XMLHttpRequest();
xhr.open('GET', a.href, true);
xhr.responseType = 'arraybuffer';
xhr.addEventListener('load', function(){
if (this.status !== 200) return;
editor.addPicture({
data: xhr.response,
APICtype: a.dataset.apic,
MIMEType: a.dataset.mime,
});
if (++counter === arr.length)
callback();
});
xhr.send();
})
}
function processFile(file) {
let blob = file.slice();
if (file.type !== 'audio/flac') {
if (file.type === 'image/bmp'
|| file.type === 'image/gif'
|| file.type === 'image/jpeg'
|| file.type === 'image/png'
|| file.type === 'image/tiff'
|| file.type === 'image/webp') {
if (!editor) return alert('Open FLAC first');
let url = URL.createObjectURL(blob);
return addPicture(url, file.type);
} else {
if (!editor) return alert('It\'s not FLAC file');
return alert('It\'s not FLAC file nor image');
}
}
document.getElementById('drop_text').textContent = file.name;
let reader = new FileReader();
reader.addEventListener("loadend", ()=>{
let arrayBuffer = reader.result;
editor = new FLACMetadataEditor(arrayBuffer);
console.log(editor);
document.getElementById('down_button').classList.remove('hidden');
document.getElementById('meta_table').classList.remove('hidden');
fillTable();
fillPictures();
});
reader.readAsArrayBuffer(blob);
}
function processFiles(files) {
Array.from(files).forEach(processFile);
}
document.addEventListener('drop', e=>{
e.preventDefault();
processFiles(e.dataTransfer.files);
e.dataTransfer.clearData();
});
document.addEventListener('dragover', e=>e.preventDefault());
window.addEventListener('load', ()=>{
t = document.getElementById('meta_table');
document.getElementById('down_button').addEventListener('click', e=>{
editor._vorbisComment.vendorString = "AHOHNMYC/FLACMetadataEditor v" + editor.scriptVersion;
editor.removeComment();
// Remove images
editor.metadata.blocks = editor.metadata.blocks.filter(block=>block.blockType!=='PICTURE');
parseTable();
parseImages(()=>{
editor.serializeMetadata();
downloadArrayBufferAsFlac(editor.arrayBuffer, document.getElementById('drop_text').textContent.replace('.flac', '_edited'));
});
});
document.getElementById('openfile_button').addEventListener('click', e=> {
let i = document.createElement('input');
i.type = 'file';
i.accept = '.flac' + (editor ? ', .bmp, .gif, .jpg, .png, .tif, .tiff, .webp' : '');
i.addEventListener('change', e=>{
processFiles(e.path[0].files);
});
i.click();
});
document.getElementById('add_row_button').addEventListener('click', e=> {
addRow();
});
});