// Generated by CoffeeScript 1.10.0
var Binary, File, americano, fs, log, mimetype, moment, request;

fs = require('fs');

americano = require('cozydb');

request = require('request');

moment = require('moment');

mimetype = require('mimetype');

Binary = require('./binary');

log = require('printit')({
  prefix: 'konnectors'
});

module.exports = File = americano.getModel('File', {
  path: String,
  name: String,
  creationDate: String,
  lastModification: String,
  "class": String,
  mime: String,
  size: Number,
  binary: Object,
  modificationHistory: Object,
  clearance: [Object],
  tags: [String]
});

File.all = function(params, callback) {
  return File.request("all", params, callback);
};

File.byFolder = function(params, callback) {
  return File.request("byFolder", params, callback);
};

File.byFullPath = function(params, callback) {
  return File.request("byFullPath", params, callback);
};

File.isPresent = function(fullPath, callback) {
  return File.request("byFullPath", {
    key: fullPath
  }, function(err, files) {
    if (err) {
      callback(err);
    }
    return callback(null, (files != null) && files.length > 0);
  });
};

File.createNew = function(fileName, path, url, tags, callback) {
  var attachBinary, data, filePath, getFileClass, mime, now, options, stream;
  now = moment().toISOString();
  filePath = "/tmp/" + fileName;
  mime = mimetype.lookup(fileName) || 'application/pdf';
  getFileClass = function(type) {
    var fileClass;
    switch (type.split('/')[0]) {
      case 'image':
        fileClass = "image";
        break;
      case 'application':
        fileClass = "document";
        break;
      case 'text':
        fileClass = "document";
        break;
      case 'audio':
        fileClass = "music";
        break;
      case 'video':
        fileClass = "video";
        break;
      default:
        fileClass = "file";
    }
    return fileClass;
  };
  data = {
    name: fileName,
    path: path,
    creationDate: now,
    lastModification: now,
    tags: tags,
    "class": getFileClass(mime),
    mime: mime
  };
  attachBinary = function(newFile) {
    return newFile.attachBinary(filePath, {
      "name": "file"
    }, function(err) {
      if (err) {
        log.error(err);
        return callback(err);
      } else {
        return fs.unlink(filePath, function() {
          return File.find(newFile.id, function(err, file) {
            return callback(err, file);
          });
        });
      }
    });
  };
  options = {
    uri: url,
    method: 'GET',
    jar: true
  };
  stream = request(options, function(err, res, body) {
    var error, stats;
    if ((res != null ? res.statusCode : void 0) === 200) {
      try {
        stats = fs.statSync(filePath);
        data.size = stats["size"];
        return File.create(data, function(err, newFile) {
          if (err) {
            log.error(err);
            return callback(err);
          } else {
            return attachBinary(newFile);
          }
        });
      } catch (error) {
        err = error;
        return callback(err);
      }
    } else {
      if (res != null) {
        log.error(res.statusCode, res.body);
      }
      return callback(new Error('Cannot download file, wrong url'));
    }
  });
  return stream.pipe(fs.createWriteStream(filePath));
};

File.prototype.destroyWithBinary = function(callback) {
  var binary;
  if (this.binary != null) {
    binary = new Binary(this.binary.file);
    return binary.destroy((function(_this) {
      return function(err) {
        if (err) {
          log.error("Cannot destroy binary linked to document " + _this.id);
        }
        return _this.destroy(callback);
      };
    })(this));
  } else {
    return this.destroy(callback);
  }
};
