riddhiman commited on
Commit
6486919
·
verified ·
1 Parent(s): b35bfe1

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +20 -12
server.js CHANGED
@@ -18,27 +18,35 @@ function isSupportedFormat(filename) {
18
  }
19
 
20
  app.get('/tracks', async (req, res) => {
 
 
 
21
  fs.readdir('music', async (err, files) => {
22
  if (err) {
23
  console.error('Error reading music directory', err);
24
  return res.sendStatus(500);
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);
42
  });
43
  });
44
 
 
18
  }
19
 
20
  app.get('/tracks', async (req, res) => {
21
+ const pageNumber = parseInt(req.query.page) || 1;
22
+ const pageSize = 5; // Number of items per page
23
+
24
  fs.readdir('music', async (err, files) => {
25
  if (err) {
26
  console.error('Error reading music directory', err);
27
  return res.sendStatus(500);
28
  }
29
 
30
+ const filteredFiles = files.filter(file => isSupportedFormat(file));
31
+ const pageStart = (pageNumber - 1) * pageSize;
32
+ const pageEnd = pageStart + pageSize;
33
+
34
+ // Slice files for the requested page
35
+ const pagedFiles = filteredFiles.slice(pageStart, pageEnd);
36
+
37
  const trackDetails = [];
38
+ for (const file of pagedFiles) {
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);
46
+ }
 
 
47
  }
48
 
49
+ res.json({tracks: trackDetails, total: filteredFiles.length});
50
  });
51
  });
52