-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
266 lines (226 loc) · 7.37 KB
/
utils.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
const AUDIO_EXT = ['.wav', '.mp3', '.mp4', '.aac', '.flac', '.ogg', '.webm'];
const AUDIO_MIME = ['audio/wav', 'audio/x-wav', 'audio/mpeg', 'audio/mp4', 'audio/aac', 'audio/flac', 'audio/ogg', 'application/ogg', 'audio/webm'];
/**
* Given a time in seconds, return a string in the format MM:SS.
*/
export function formatTime(time) {
if (time === -1) {
return '00:00';
}
const minutes = Math.floor(time / 60);
const seconds = Math.floor(time % 60);
return `${minutes < 10 ? "0" : ""}${minutes}:${seconds < 10 ? "0" : ""}${seconds}`;
}
/**
* Get today's date and time, formatted as YYYY-MM-DD HH:MM:SS.
*/
export function getFormattedDate() {
const today = new Date();
const year = today.getFullYear();
const month = today.getMonth() + 1;
const day = today.getDate();
const hour = today.getHours();
const minute = today.getMinutes();
const seconds = today.getSeconds();
return `${year}-${month < 10 ? "0" : ""}${month}-${day < 10 ? "0" : ""}${day} ${hour < 10 ? "0" : ""}${hour}:${minute < 10 ? "0" : ""}${minute}:${seconds < 10 ? "0" : ""}${seconds}`;
}
/**
* Get a somewhat unique ID.
*/
export function getUniqueId() {
let id = '';
id += 'abcdefghijklmnopqrstuvwxyz'.split('')[Math.floor(Math.random() * 26)];
id += 'abcdefghijklmnopqrstuvwxyz'.split('')[Math.floor(Math.random() * 26)];
id += '0123456789'.split('')[Math.floor(Math.random() * 10)];
id += '0123456789'.split('')[Math.floor(Math.random() * 10)];
return id += Date.now();
}
/**
* Using the FileSystem Access API, open a file picker and return the selected file(s).
*/
export async function openFilesFromDisk() {
if (!('showOpenFilePicker' in window)) {
return await legacyOpenFilesFromDisk();
}
// TODO: how to allow selecting a folder?
const handles = await window.showOpenFilePicker({
multiple: true,
types: [{
description: 'Audio files',
accept: {
"audio/*": AUDIO_EXT
}
}]
});
const files = [];
for (const handle of handles) {
const file = await handle.getFile();
if (file.type.startsWith('audio/')) {
files.push(file);
}
}
return files;
}
function legacyOpenFilesFromDisk() {
// Create an input type file element.
const input = document.createElement('input');
input.type = 'file';
input.multiple = true;
input.accept = [...AUDIO_EXT, ...AUDIO_MIME].join(',');
// Simulate a click on the input element.
const event = new MouseEvent('click', {
bubbles: true,
cancelable: true,
view: window
});
input.dispatchEvent(event);
// Wait for the file to be selected.
return new Promise((resolve) => {
input.onchange = (event) => {
resolve(event.target.files);
}
});
}
/**
* Given a string with at least one dot and some text after it, return the part between
* the start of the string and the last dot.
*/
function getFileNameWithoutExtension(fileName) {
return fileName.split('.').slice(0, -1).join('.');
}
export function getSongNameFromURL(url) {
const parsed = new URL(url);
let name = parsed.pathname.substring(parsed.pathname.lastIndexOf('/') + 1, parsed.pathname.lastIndexOf('.'));
return decodeURI(name);
}
function guessSongInfoFromString(str) {
// Test for the following pattern: 04 - artist - title
const match = str.match(/[0-9]+\s*-\s*([^-]+)\s*-\s*(.+)/);
if (match && match.length === 3) {
return {
artist: match[1],
title: match[2]
}
}
return { title: str };
}
function supportsModulesInWorkers() {
let supports = false;
const tester = {
get type() { supports = true; }
};
try {
const worker = new Worker('blob://', tester);
} finally {
return supports;
}
}
/**
* Use the parse-audio-metadata library to parse an audio file.
* Do this in a worker thread, spawning it first if needed.
* @param {File} The audio file to parse.
* @return {Promise} A promise that resolves to the song info.
*/
function guessSongInfoFromFile(file) {
return new Promise(resolve => {
if (!supportsModulesInWorkers()) {
// Our metadata parser is imported in the worker.
// Modules are not supported in workers in Firefox (and Safari?).
// So for now, just bail out.
resolve({});
return;
}
// Create a new one for every file, to avoid receiving messages about other songs.
const worker = new Worker('./audio-metadata-parse-worker.js', { type: "module" });
worker.addEventListener('message', event => {
worker.terminate();
resolve({
artist: event.data.artist,
title: event.data.title,
album: event.data.album
});
}, { once: true });
worker.postMessage(file);
});
}
export async function guessSongInfo(file) {
// First try from the file name.
const name = getFileNameWithoutExtension(file.name);
const fromFileName = guessSongInfoFromString(name);
// Next parse the audio metadata.
const fromMetadata = await guessSongInfoFromFile(file);
// If anything is missing from the metadata, complete it from the file name.
return {
album: fromMetadata.album || fromFileName.album || 'Unknown album',
artist: fromMetadata.artist || fromFileName.artist || 'Unknown artist',
title: fromMetadata.title || fromFileName.title || 'Unknown song'
};
}
/**
* Skins are expected to have a `:root` CSS rule with a `--back` custom color property.
* This function returns the CSS value of that property, so it can be used from JS.
*/
export function getCurrentSkinBackgroundColor() {
let color = null;
// Get the current skin stylesheet. It's the one that's not about.css.
const sheets = [...document.styleSheets].filter(s => !s.href || !s.href.includes('about.css'));
if (sheets.length !== 1) {
console.error('Could not find skin stylesheet.');
return parseColor('#000');
}
const rules = sheets[0].cssRules;
for (const rule of rules) {
if (rule.selectorText === ':root') {
color = rule.style.getPropertyValue('--back');
}
}
return parseColor(color ? color : '#000');
}
function parseColor(color) {
const div = document.createElement('div');
document.body.appendChild(div);
div.style.color = color.trim();
const style = getComputedStyle(div).color;
const match = style.match(/^rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i);
div.remove();
if (match) {
return [match[1], match[2], match[3]];
}
throw new Error(`Color ${color} could not be parsed`);
}
export function canShare(file) {
return file &&
navigator.share &&
navigator.canShare &&
navigator.canShare({ files: [new File([file], 'test', { type: file.type })] });
}
export function analyzeDataTransfer(event) {
const items = [...event.dataTransfer.items];
let containsSongs = false;
let containsImages = false;
let containsOthers = false;
for (const item of items) {
if (item.kind !== 'file') {
continue;
}
if (item.type.startsWith('audio/')) {
containsSongs = true;
} else if (item.type.startsWith('image/')) {
containsImages = true;
} else {
containsOthers = true;
}
}
const files = [...event.dataTransfer.files];
return { containsImages, containsSongs, containsOthers, files };
}
export async function getImageAsDataURI(url) {
const response = await fetch(url);
const blob = await response.blob();
const dataURI = await new Promise(resolve => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.readAsDataURL(blob);
});
return dataURI;
}