/**
 * FileController
 *
 * @description :: Server-side logic for managing files
 * @help        :: See http://sailsjs.org/#!/documentation/concepts/Controllers
 */
var logEitherWay = function(err, result){
	if (err) console.log(err);
	console.log("Created: " + created);
};

module.exports = {

	upload: function  (req, res) {
		console.log(req.body);
		req.file('document').upload({
			dirname: require('path').resolve(sails.config.appPath, './assets/filestorage')
		}, function (err, files) {
			if (err) return res.serverError(err);
			for (var i = 0; i < files.length; i ++){
				Doc.create({
					name: files[i].filename,
					location: files[i].fd.split("assets/")[1],
					description: req.body.description
				}).exec(logEitherWay);
			}
			return res.redirect("/#/docs");
			// return res.json({
			// 	message: files.length + ' file(s) uploaded successfully!',
			// 	files: files
			// });
		});
	}
};
