Example #1
0
	startTailing: function(options) {

		var fd  = typeof options == "string" ? options : "";
		options = typeof options == "object" ? options : {};

		options.interval = options.interval  || 100;
		options.mode     = options.mode      || 'lines';
		options.onErr    = options.onErr     || null;
		options.onReady  = options.onReady   || null;
		
		// Allow null encoding for raw binary (want to write a unit test for this case? :D).
		options.encoding = typeof options.encoding == "undefined" ? 'utf-8' : options.encoding;

		fd          = fd || options.fd;
		var ms      = options.interval,
		mode        = options.mode,
		encoding    = options.encoding,
		fileTailer  = fileWatcher.watch(fd, ms, options.OnErr, options.onReady);

		fileTailer.conf = options; // expose these to user.

		// Convenience event.  Also prevents halts if user is not catching errors
		fileTailer.on('error',function(e) {
			if (!(e.code && e.code == "ENOENT")) {
				fileTailer.emit('tailError', e);
			};
		});

		fileTailer.on('sizeChange', function(newSize, oldSize) {

			if ((newSize-oldSize) > 0) {
				var stream = fs.createReadStream(fd,{
					encoding:encoding,
					start:oldSize,
					end:newSize
				});
				
				if (mode == "lines") {
					stream.on('data', function(chunk) {
						var lines = chunk.toString().trim().split('\n');	
						for( var i = 0; i < lines.length; i++ ){
							fileTailer.emit('line', lines[i]);
						}
					});
				} else {
					fileTailer.emit('stream', stream);
				}
			}
		});

		return fileTailer;
	}
Example #2
0
          Playlist.insert(playlistEntry, function(error, id){
                if(!error){
                    // if cached download the song
                    if(Options.findOne().download){
                      // default arguments youtube-dl
                      var args = ['--format=251/171/140/250/249/bestaudio'];

                      // get the song
                      var song = downloader(url, args);

                      // strip the filename from characters reserved by the filesystem
                      var filename = `${sanitize(info.title, " ")}.mp3`;

                      // save the song to HDD
                      song.pipe(fs.createWriteStream(`songs/${filename}`));

                      Playlist.update({_id: id}, {$set: {'file': `songs/${filename}`}});
                    }

                    // playlistLength is the length BEFORE inserting the new song
                    // if this is the only song in the playlist, start it
                    if(playlistLength == 0){
                      if(Options.findOne().download){
                        // check for the filesize because when 'play' is called the early, the filesize is so small
                        // playback stops immediatly
                        var fileWatcher = fsw.watch(`songs/${filename}`).on('sizeChange', Meteor.bindEnvironment(function (newSize, oldSize){
                          // when the file is bigger than 200kB play it
                          if(newSize > 500000){
                            Meteor.call('play');
                            // release the file watcher
                            fileWatcher.stop();
                          }
                        }));
                      }
                      // uncached case is just streaming anyway
                      else{
                        Meteor.call('play');
                      }
                    }

                    future.return(true);
                }
                else{
                    console.log("Error in enqueue: " + error.message);
                    future.return(false);
                }

          });