-Promise = require 'promise'
+Promise = require 'bluebird'
 path = require 'path'
 mime = require 'mime-types/index.js'
 stream = require 'stream'

							file.stream = streamifier.createReadStream buffer
							resolve file
			.catch Promise.reject
			
	# convert input file stream {name: filename, stream: stream} to base64 encoding string
	dataUrl: (file) ->
		new Promise (fulfill, reject) ->
			chunks = []
			out = file.stream.pipe(base64.encode())
			out.on 'data', (chunk) ->
				chunks.push chunk
			out.on 'end', ->
				fulfill "data:#{sails.services.file.type(file.name)};base64,#{chunks.join('')}"
		
	type: (name) ->
		mime.lookup(name)
				
	isImg: (name) ->
		(/^image/i).test module.exports.type(name)

	isAudio: (name) ->
		(/^audio/i).test module.exports.type(name)
		
	thumbName: (filename) ->
		[fullname, name, ext] = filename.match /(.*)\.([^\.]*)/
		"#{name}.thumb.#{ext}"
streamifier = require 'streamifier'

module.exports =
	# get file content of Model[field] from model user or group
	get: (Model, pk, field) ->
		new Promise (fulfill, reject) ->
			Model
