<commit_before>"use strict";

var Song = require('./song_model.js'),
    Q    = require('q'),
    echo = require('../main/echo.js'),
    fs   = require('fs'),
    helpers = require('./song_helpers.js'),
    path = require('path');

module.exports = exports = {
  get: function (req, res, next) {
    Q(Song.find({'echoData.status' : 'complete'}).exec())
      .then(function (songs) {
        res.json(songs);
      })
       .fail(function (reason) {
        next(reason);
      });
  },

  post: function (req, res, next) {
    // TODO: need to check if req.body is the entire song db entry    
    var song = req.body;
    Q(Song.create(song).exec())
      .then(function (id) {
        res.send(id);
      })
      .fail(function (reason) {
        next(reason);
      });
  },

  postUserData: function(req, res, next) {
    /* Find a song using the client provided md5
     * .exec() promisifies the result
     * Q() transforms the promise into a Q-style promise */
    if (req.body.base) {
      Q(Song.findOne({'echoData.md5': req.body.base}).exec())
      .then(function(song) {  // call SongSchema.methods.adjust on the found song after the promise returns
        song.adjust(req.body.increment);
      });

      // do similar for the song that is being compared against. note the negation
      Q(Song.findOne({'echoData.md5': req.body.compare}).exec())
      .then(function(song) {
        song.adjust(-req.body.increment);
      });
      res.send(200);  
    } else {
      res.send(404);
    }
  },

  // consider breaking the below function into two requests to get an actual url with a file name
  // TODO: refactor 
  getSong: function(req, res, next) {
    // grab md5 from request URL
    var md5 = req.params[0];

    Q(Song.findOne({'echoData.md5': md5}).exec())
      .then(function(song) {
        if (song) {
          // build path to song
          var filename = song.filename;
          // build path based on server folder structure
          var dirName = __dirname+'/lib';
          var path = dirName + '/' + filename;
          // serve static audio file
          res.sendfile(path);   
        } else {
          res.send(404);
        }
      })
      .fail(function(err) {
        throw(err);
      });
  },

  postSong: function(req, res, next) {
    console.log('receiving song', req.files);
    var song = req.files.file;
    var size = req.files.file.ws.bytesWritten;
    var type = song.type;
    var filename = song.originalFilename;

    // TODO: increment filenames if they're found
    var regex = /^(audio\/[a-z0-9]+)$/i;
    var bool = helpers.filesizeCheck(size) && helpers.filenameRegEx(filename) && regex.test(type);

    console.log(bool);
    
    if (bool) {
      var serverPath = __dirname + '/lib/' + filename; 
      var $fsRename = Q.nbind(fs.rename, fs);
      $fsRename(song.path, serverPath)
        .then(function() {
          console.log(serverPath)
          res.send(serverPath);
        })
        .fail(function(err) {
          throw(err)
        });
    } else {
      res.send(404, 'Sorry, please upload a .mp3 under 10 MB')
    }
  }
};<commit_msg>Adding more deployment logging for song upload<commit_after>"use strict";

var Song = require('./song_model.js'),
    Q    = require('q'),
    echo = require('../main/echo.js'),
    fs   = require('fs'),
    helpers = require('./song_helpers.js'),
    path = require('path');

module.exports = exports = {
  get: function (req, res, next) {
    Q(Song.find({'echoData.status' : 'complete'}).exec())
      .then(function (songs) {
        res.json(songs);
      })
       .fail(function (reason) {
        next(reason);
      });
  },

  post: function (req, res, next) {
    // TODO: need to check if req.body is the entire song db entry    
    var song = req.body;
    Q(Song.create(song).exec())
      .then(function (id) {
        res.send(id);
      })
      .fail(function (reason) {
        next(reason);
      });
  },

  postUserData: function(req, res, next) {
    /* Find a song using the client provided md5
     * .exec() promisifies the result
     * Q() transforms the promise into a Q-style promise */
    if (req.body.base) {
      Q(Song.findOne({'echoData.md5': req.body.base}).exec())
      .then(function(song) {  // call SongSchema.methods.adjust on the found song after the promise returns
        song.adjust(req.body.increment);
      });

      // do similar for the song that is being compared against. note the negation
      Q(Song.findOne({'echoData.md5': req.body.compare}).exec())
      .then(function(song) {
        song.adjust(-req.body.increment);
      });
      res.send(200);  
    } else {
      res.send(404);
    }
  },

  // consider breaking the below function into two requests to get an actual url with a file name
  // TODO: refactor 
  getSong: function(req, res, next) {
    // grab md5 from request URL
    var md5 = req.params[0];

    Q(Song.findOne({'echoData.md5': md5}).exec())
      .then(function(song) {
        if (song) {
          // build path to song
          var filename = song.filename;
          // build path based on server folder structure
          var dirName = __dirname+'/lib';
          var path = dirName + '/' + filename;
          // serve static audio file
          res.sendfile(path);   
        } else {
          res.send(404);
        }
      })
      .fail(function(err) {
        throw(err);
      });
  },

  postSong: function(req, res, next) {
    console.log('receiving song', req.files);
    var song = req.files.file;
    var size = req.files.file.ws.bytesWritten;
    var type = song.type;
    var filename = song.originalFilename;

    // TODO: increment filenames if they're found
    var regex = /^(audio\/[a-z0-9]+)$/i;
    var bool = helpers.filesizeCheck(size) && helpers.filenameRegEx(filename) && regex.test(type);

    console.log(bool);

    if (bool) {
      var serverPath = __dirname + '/lib/' + filename; 
      var $fsRename = Q.nbind(fs.rename, fs);
      console.log(serverPath);
      $fsRename(song.path, serverPath)
        .then(function() {
          console.log(serverPath)
          res.send(serverPath);
        })
        .fail(function(err) {
          console.log('error stuff');
          throw(err)
        });
    } else {
      res.send(404, 'Sorry, please upload a .mp3 under 10 MB')
    }
  }
};
{'fraction_non_alphanumeric': 0.14965343415248897, 'fraction_numerical': 0.007876496534341524, 'mean_word_length': 2.139960435212661, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '222511', 'n_tokens_mistral': 2069, 'n_tokens_neox': 1990, 'n_words': 499}