/**
 * Images
 *
 * @module      :: Model
 * @description :: A short summary of how this model works and what it represents.
 *
 */
var mv = require('mv');
var uuid = require('node-uuid');
// image converter
var gm = require('gm');
var path = require('path');
var mime = require('mime');
var async = require('async');

module.exports = function ImageModel(db, hooks, events, sanitizer) {
  // we.js will be set in one event
  var we = null;

  // set sequelize model define and options
  var model = {
    definition: {
      // - user given data text
      // 
      label: {
        type: db.Sequelize.STRING
      },
      description: {
        type: db.Sequelize.TEXT
      },

      // - data get from file
      // 
      name: {
        type: db.Sequelize.STRING,
        allowNull: false,
        unique: true        
      },

      size: {
        type: db.Sequelize.INTEGER,
      },

      encoding: {
        type: db.Sequelize.STRING,
      },

      active: {
        type: db.Sequelize.BOOLEAN,
        defaultValue: true        
      },

      originalname: {
        type: db.Sequelize.STRING
      },

      mime: {
        type: db.Sequelize.STRING
      },

      extension: {
        type: db.Sequelize.STRING
      },

      // creator: {
      //   model: 'user',
      //   required: true
      // },

      width: {
        type: db.Sequelize.STRING
      },

      height: {
        type: db.Sequelize.STRING
      }
    },

    options: {
      // table comment
      comment: "We.js we-core image table",


      classMethods: {
        // Upload the files
        upload: function(file, callback) {
          file.extension = file.filename.split('.').pop();

          // TODO add suport to files withouth extension
          if(!file.extension){
            sails.log.error('File extension not found', file);
            return callback('File extension not found', null);
          }

          file.newName =  uuid.v1() + '.' + file.extension;

          var newFilePath = path.resolve(sails.config.imageUploadPath + '/' + 'original' + '/' + file.newName);

          mv(file.path, newFilePath,{mkdirp: true}, function afterImageUpload(err) {
            if (err) {
              sails.log.error('Images.upload: Error on move file', err);
              return callback(err, null);
            }

            file.mime = mime.lookup(newFilePath);

            sails.log.verbose('arquivo movido para:',newFilePath);
            // get image size
            gm(newFilePath)
            .size(function (err, size) {
              if (err) {
                sails.log.error('Images.upload: Error on get image file size', err, newFilePath);
                return callback(err);
              }

              file.width = size.width;
              file.height = size.height;

              callback(null, file);
            });

          });
        },

        getStyleUrlFromImage: function(image) {
          var host = we.config.hostname;

          return {
            original: host + '/api/v1/images/original/' + image.name,
            thumbnail: host + '/api/v1/images/thumbnail/' + image.name,
            mini: host + '/api/v1/images/mini/' + image.name,
            medium: host + '/api/v1/images/medium/' + image.name,
            large: host + '/api/v1/images/large/' + image.name
          };
        },

        uploadMultiple: function(files, creatorId, callback){
          var uploadedFiles = [];
          var fileUp;

          async.each(files, function(file, next){
            file.path =  file.fd;

            Images.upload(file, function(err){
              if (err) {
                next(err);
              } else {
                fileUp = file;
                fileUp.name = file.newName;
                fileUp.originalFilename = file.originalFilename;
                fileUp.size = file.size;
                fileUp.extension = file.extension;
                fileUp.creator = creatorId;
                uploadedFiles.push(fileUp);
                next();
              }
            });
          },function (err) {
            if (err) {
              callback(err, null);
            }else{
              callback(null, uploadedFiles);
            }
          });
        }
      },

      instanceMethods: {
        toJSON: function() {
          var obj = this.values;
          obj.urls = db.models.image.getStyleUrlFromImage(obj);
          // set default objectType
          obj.objectType = 'image';
          return obj;
        }
      },
      hooks: {
        beforeCreate: function(record, options, next) {
          // sanitize
          record = sanitizer.sanitizeAllAttr(record);
          next();
        },

        beforeUpdate: function(record, options, next) {
          // sanitize
          record = sanitizer.sanitizeAllAttr(record);
          next();
        }
      }
    }
  }

  hooks.on('we:models:set:joins', function (wejs, done) {
    we = wejs;

    done();
  });

  return model;
}
@@ -1530,17 +1530,16 @@
 able%22,%0A%0A
-%0A
       cl
@@ -1564,2671 +1564,453 @@
     
-// Upload the files%0A        upload: function(file, callback) %7B%0A          file.extension = file.filename.split('.').pop();%0A%0A          // TODO add suport to files withouth extension%0A          if(!file.extension)%7B%0A            sails.log.error('File extension not found', file);%0A            return callback('File extension not found', null);%0A          %7D%0A%0A          file.newName =  uuid.v1() + '.' + file.extension;%0A%0A          var newFilePath = path.resolve(sails.config.imageUploadPath + '/' + 'original' + '/' + file.newName);%0A%0A          mv(file.path, newFilePath,%7Bmkdirp: true%7D, function afterImageUpload(err) %7B%0A            if (err) %7B%0A              sails.log.error('Images.upload: Error on move file', err);%0A              return callback(err, null);%0A            %7D%0A%0A            file.mime = mime.lookup(newFilePath);%0A%0A            sails.log.verbose('arquivo movido para:',newFilePath);%0A            // get image size%0A            gm(newFilePath)%0A            .size(function (err, size) %7B%0A              if (err) %7B%0A                sails.log.error('Images.upload: Error on get image file size', err, newFilePath);%0A                return callback(err);%0A              %7D%0A%0A              file.width = size.width;%0A              file.height = size.height;%0A%0A              callback(null, file);%0A            %7D);%0A%0A          %7D);%0A        %7D,%0A%0A        getStyleUrlFromImage: function(image) %7B%0A          var host = we.config.hostname;%0A%0A          return %7B%0A            original: host + '/api/v1/images/original/' + image.name,%0A            thumbnail: host + '/api/v1/images/thumbnail/' + image.name,%0A            mini: host + '/api/v1/images/mini/' + image.name,%0A            medium: host + '/api/v1/images/medium/' + image.name,%0A            large: host + '/api/v1/images/large/' + image.name%0A          %7D;%0A        %7D,%0A%0A        uploadMultiple: function(files, creatorId, callback)%7B%0A          var uploadedFiles = %5B%5D;%0A          var fileUp;%0A%0A          async.each(files, function(file, next)%7B%0A            file.path =  file.fd;%0A%0A            Images.upload(file, function(err)%7B%0A              if (err) %7B%0A                next(err);%0A              %7D else %7B%0A                fileUp = file;%0A                fileUp.name = file.newName;%0A                fileUp.originalFilename = file.originalFilename;%0A                fileUp.size = file.size;%0A                fileUp.extension = file.extension;%0A                fileUp.creator = creatorId;%0A                uploadedFiles.push(fileUp);%0A                next();%0A              %7D%0A            %7D);%0A          %7D,function (err) %7B%0A            if (err) %7B%0A              callback(err, null);%0A            %7Delse%7B%0A              callback(null, uploadedFiles);%0A            %7D%0A          %7D)
+getStyleUrlFromImage: function(image) %7B%0A          var host = we.config.hostname;%0A%0A          return %7B%0A            original: host + '/api/v1/images/original/' + image.name,%0A            thumbnail: host + '/api/v1/images/thumbnail/' + image.name,%0A            mini: host + '/api/v1/images/mini/' + image.name,%0A            medium: host + '/api/v1/images/medium/' + image.name,%0A            large: host + '/api/v1/images/large/' + image.name%0A          %7D
 ;%0A  

remove uneed image upload function