riddhiman commited on
Commit
8fb2fb5
·
verified ·
1 Parent(s): dcb0850

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +24 -1
server.js CHANGED
@@ -39,7 +39,30 @@ app.get('/tracks', async (req, res) => {
39
  const filePath = path.join(__dirname, 'music', file);
40
  try {
41
  const metadata = await mm.parseFile(filePath, { native: true });
42
- const artwork = metadata.common.picture && metadata.common.picture[0] ? `data:${metadata.common.picture[0].format};base64,${metadata.common.picture[0].data.toString('base64')}` : '';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  trackDetails.push({filename: file, artwork});
44
  } catch (error) {
45
  console.error(`Error reading metadata for file: ${file}`, error);
 
39
  const filePath = path.join(__dirname, 'music', file);
40
  try {
41
  const metadata = await mm.parseFile(filePath, { native: true });
42
+ let artwork = '';
43
+ // Fallback path for default icon
44
+ const fallbackIconPath = path.join(__dirname, 'music', 'icon.png');
45
+
46
+ try {
47
+ let imageBuffer;
48
+ if (metadata.common.picture && metadata.common.picture[0]) {
49
+ imageBuffer = await sharp(metadata.common.picture[0].data)
50
+ .resize(256, 256, { fit: 'inside' })
51
+ .toBuffer();
52
+ } else {
53
+ // Use the default icon if no album art is present
54
+ imageBuffer = await sharp(fallbackIconPath)
55
+ .resize(256, 256, { fit: 'inside' })
56
+ .toBuffer();
57
+ }
58
+
59
+ // Determine the image format
60
+ const imageFormat = metadata.common.picture && metadata.common.picture[0] ? metadata.common.picture[0].format : 'image/png';
61
+
62
+ artwork = `data:${imageFormat};base64,${imageBuffer.toString('base64')}`;
63
+ } catch (e) {
64
+ console.error(`Error processing artwork for file: ${file}`, e);
65
+ }
66
  trackDetails.push({filename: file, artwork});
67
  } catch (error) {
68
  console.error(`Error reading metadata for file: ${file}`, error);