var readTorrent = require('read-torrent'),
  bodyParser = require('body-parser'),
  peerflix = require('peerflix'),
  uuid = require('node-uuid'),
  app = require('express')(),
  omx = require('omxctrl'),
  fs = require('fs'),
  tempDir = '/tmp/',
  engine;

var STATES = ['PLAYING', 'PAUSED', 'IDLE'];
var PORT = process.argv[2] || 9090;

var mappings = {
  '/pause': 'pause',
  '/speedup': 'increaseSpeed',
  '/speeddown': 'decreaseSpeed',
  '/nextaudio': 'nextAudioStream',
  '/prevaudio': 'previousAudioStream',
  '/nextsubtitle': 'nextSubtitleStream',
  '/prevsubtitle': 'previousSubtitleStream',
  '/togglesubtitle': 'toggleSubtitles',
  '/volumeup': 'increaseVolume',
  '/volumedown': 'decreaseVolume',
  '/forward': 'seekForward',
  '/backward': 'seekBackward',
  '/fastforward': 'seekFastForward',
  '/fastbackward': 'seekFastBackward'
};

app.use(bodyParser());

var stop = function() {
  if (!engine) return;
  engine.destroy();
  engine = null;
};

var createTempFilename = function() {
  return tempDir + 'torrentcast_' + uuid.v4();
};

var clearTempFiles = function() {
  fs.readdir(tempDir, function(err, files) {
    if (err) return;
    files.forEach(function(file) {
      if (file.substr(0, 11) === 'torrentcast') {
        fs.rmdir(tempDir + file);
      }
    });
  });
};

app.post('/play', function(req, res) {
  if (!req.body.url) return res.send(400, { error: 'torrent url missung' });
  readTorrent(req.body.url, function(err, torrent) {
    if (err) return res.send(400, { error: 'torrent link could not be parsed' });
    if (engine) stop();
    clearTempFiles();

    engine = peerflix(torrent, {
      connections: 100,
      path: createTempFilename(),
      buffer: (1.5 * 1024 * 1024).toString()
    });

    engine.server.on('listening', function() {
      omx.play('http://127.0.0.1:' + engine.server.address().port + '/');
      res.send(200);
    });
  });
});

app.post('/stop', function(req, res) {
  stop();
  res.send(200);
});

app.get('/state', function(req, res) {
  res.send(200, STATES[omx.getState()]);
});

for (var route in mappings) {
  (function(method) {
    app.post(route, function(req, res) {
      omx[method]();
      res.send(200);
    });
  })(mappings[route]);
}


module.exports = function() {
  console.log('torrentcast running on port', PORT);
  app.listen(PORT);
};
@@ -75,16 +75,52 @@
 rser'),%0A
+  tempDir = require('os').tmpdir(),%0A
   peerfl
@@ -145,16 +145,16 @@
 flix'),%0A
-
   uuid =
@@ -234,16 +234,42 @@
 ctrl'),%0A
+  path = require('path'),%0A
   fs = r
@@ -286,29 +286,8 @@
 '),%0A
-  tempDir = '/tmp/',%0A
   en
@@ -1042,25 +1042,34 @@
  return 
+path.join(
 tempDir
- +
+,
  'torren
@@ -1087,16 +1087,17 @@
 uid.v4()
+)
 ;%0A%7D;%0A%0Ava
@@ -1300,23 +1300,33 @@
 dir(
+path.join(
 tempDir
- +
+,
  file)
+)
 ;%0A  
@@ -2278,17 +2278,16 @@
 e%5D);%0A%7D%0A%0A
-%0A
 module.e
@@ -2380,10 +2380,11 @@
 (PORT);%0A
-
 %7D;
+%0A

use os.tempdir() to find out what the tempdir ist