Spaces:
Runtime error
Runtime error
Update server.js
Browse files
server.js
CHANGED
|
@@ -10,6 +10,13 @@ const PORT = process.env.PORT || 7860;
|
|
| 10 |
app.use(express.static('public'));
|
| 11 |
app.use('/music', express.static('music'));
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
app.get('/tracks', async (req, res) => {
|
| 14 |
fs.readdir('music', async (err, files) => {
|
| 15 |
if (err) {
|
|
@@ -18,15 +25,17 @@ app.get('/tracks', async (req, res) => {
|
|
| 18 |
}
|
| 19 |
|
| 20 |
const trackDetails = [];
|
| 21 |
-
for (const file of files
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
}
|
| 31 |
|
| 32 |
res.json(trackDetails);
|
|
|
|
| 10 |
app.use(express.static('public'));
|
| 11 |
app.use('/music', express.static('music'));
|
| 12 |
|
| 13 |
+
const SUPPORTED_FORMATS = ['.mp3', '.mp4', '.wav', '.flac', '.ogg', '.opus']; // Add or remove formats as needed
|
| 14 |
+
|
| 15 |
+
function isSupportedFormat(filename) {
|
| 16 |
+
const ext = path.extname(filename).toLowerCase();
|
| 17 |
+
return SUPPORTED_FORMATS.includes(ext);
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
app.get('/tracks', async (req, res) => {
|
| 21 |
fs.readdir('music', async (err, files) => {
|
| 22 |
if (err) {
|
|
|
|
| 25 |
}
|
| 26 |
|
| 27 |
const trackDetails = [];
|
| 28 |
+
for (const file of files) {
|
| 29 |
+
if (isSupportedFormat(file)) { // Use the utility function here
|
| 30 |
+
const filePath = path.join(__dirname, 'music', file);
|
| 31 |
+
try {
|
| 32 |
+
const metadata = await mm.parseFile(filePath, { native: true });
|
| 33 |
+
const artwork = metadata.common.picture && metadata.common.picture[0] ? `data:${metadata.common.picture[0].format};base64,${metadata.common.picture[0].data.toString('base64')}` : '';
|
| 34 |
+
trackDetails.push({filename: file, artwork});
|
| 35 |
+
} catch (error) {
|
| 36 |
+
console.error(`Error reading metadata for file: ${file}`, error);
|
| 37 |
+
}
|
| 38 |
+
}
|
| 39 |
}
|
| 40 |
|
| 41 |
res.json(trackDetails);
|