# -*- encoding : utf-8 -*-
class DataFile < ActiveRecord::Base
	belongs_to :file_folder


	mount_uploader :file, FileUploader

	before_save do
		if !self.name?
			self.name = File.basename(self.file.filename, File.extname(self.file.filename)).to_s if self.file?
		end

	end
  
  before_create { generate_token() }



	def file_type
	
		
		mime = `file --mime -br "#{self.file.path}"`.strip.split(';')[0]
		mime
		
	end

	def abstract_file_name
		self.name
	end

	def abstract_file_name_slug
		self.name.to_slug
	end

  

  def generate_token()
    begin
      self[:token] = SecureRandom.urlsafe_base64
    end while DataFile.exists?(:token => self[:token])
  end


end
