1

I am new to meteor and javascript and am trying to have the application search a folder of mp3 and then add the location and the name to a collection.

if there are pre-existing (javascript) methods, please let me know where I may find them. If you also know what might be retrofitted from atmospherejs or a git repo, also please let me know. Thank you!

user3795286
  • 136
  • 1
  • 14

1 Answers1

1

Here is a simple way to solve what you are trying to do

mp3list.html:

<head>
  <title>MP3 Collection</title>
</head>

<body>
  {{> mp3list}}
</body>

<template name="mp3list">
  <ul>
    {{#each mp3s}}
      <li>{{name}}</li>
    {{/each}}
  </ul>
</template>

mp3list.js

MP3s = new Mongo.Collection('mp3s');
MP3_DIRETORY = '/tmp/mp3';
INTERVAL_MILLISECONDS = 1000;

if (Meteor.isClient) {
  Template.mp3list.helpers({
    mp3s: function() {
      return MP3s.find();
    },
  });
}

if (Meteor.isServer) {
  var fs = Npm.require('fs');
  Meteor.setInterval(function() {
    var mp3s = fs.readdirSync(MP3_DIRETORY).filter(
      function(i) {
        return i.substr(i.length - 4) === '.mp3';
      }
    );
    mp3s.forEach(function(i) { MP3s.upsert({name: i}, { $set: {name: i}}); });

  }, INTERVAL_MILLISECONDS);
}

To extend this (i.e. recursive directory search) the answers here provide more details. However if your app is scanning a large number of files this simple approach will not scale. I'd suggest then looking at ionotify based solutions(assuming linux, other OSs will have similar APIs). watchr
may also be a good option (I haven't used it, or inotify++).

Community
  • 1
  • 1
JeremyK
  • 3,240
  • 1
  • 11
  • 24
  • Thanks for the code!. However I am getting errors on the server side: Exception in setInterval callback: Error: ENOENT, no such file or directory '/tmp/mp3'. I tried different versions of the '/' or none also './'. None of those worked. It couldnt be permissions issue could it? In addtion no mp3 will play from that directory only from /public/. – user3795286 Sep 14 '15 at 15:41
  • Are you seeing ENOENT errors when using relative paths (starting with ./ or ../ or similar) or also with full paths? If it's relative paths your (node) meteor process likely has a different working directory to what you expect (it's not the directory you typed meteor in, or the root of your project). Your server process will have the working directory PROJECTDIR/.meteor/local/build/programs/server by default. Start the Meteor shell and explore with these commands: process.cwd() - shows current working directory, and process.chdir('/tmp') - changes working directory. – JeremyK Sep 14 '15 at 20:32
  • Yes the working directory is the full local path. process.chdir('/tmp') throws many error messages. So I just set the relative paths via 5 '../' to get it to read from the PROJECTDIR directory. The user will be using their own files and not files served from a offsite server. So this should be a client side process I guess? – user3795286 Sep 15 '15 at 13:35
  • Doing this on the client is another problem altogether. Web browsers do not have access to the filesystem api for security reasons, though if you are using cordova to create mobile apps there are apis for accessing file systems, but will you will need to provide more details on your requirements (it's a new question). Regarding the ENOENT errors, I do suggest you use full paths. If you try this in Meteor Shell and it fails, you could post the commands and the errors and I'll try to provide more help then. – JeremyK Sep 17 '15 at 12:23