Sha256: 4fbe7c226baa277e3bb862f426e6904422a0cd7a456c016b2ee40d53be980f7f

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

require 'aws/s3'

class SourceFile < ActiveRecord::Base
  attr_accessible :url, :bucket, :key

  validates_presence_of :file_name, :file_content_type, :file_size, :key, :bucket

  before_validation(:on => :create) do
    self.file_name = key.split('/').last if key
    # for some reason, the response from AWS seems to escape the slashes in the keys, this line will unescape the slash
    self.url = url.gsub(/%2F/, '/') if url
    self.file_size ||= s3_object.try(:size)
    self.file_content_type ||= s3_object.try(:content_type)
  end
  # make all attributes readonly after creating the record (not sure we need this?)
  after_create { readonly! }
  # cleanup; destroy corresponding file on S3
  after_destroy { s3_object.try(:delete) }

  def to_jq_upload
    { 
      'id' => id,
      'name' => file_name,
      'size' => file_size,
      'url' => url,
      'image' => self.is_image?,
      'delete_url' => Rails.application.routes.url_helpers.source_file_path(self)
    }
  end

  def is_image?
    !!file_content_type.try(:match, /image/)
  end

  #---- start S3 related methods -----
  def s3_object
    @s3_object ||= AWS::S3::S3Object.find(key, bucket) if self.class.open_aws && key
  rescue
    nil
  end

  def self.open_aws
    unless @aws_connected
      AWS::S3::Base.establish_connection!(
        :access_key_id     => S3CorsFileupload::Config.access_key_id,
        :secret_access_key => S3CorsFileupload::Config.secret_access_key
      )
    end
    @aws_connected ||= AWS::S3::Base.connected?
  end
  #---- end S3 related methods -----

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
s3_cors_fileupload-0.2.0 lib/generators/s3_cors_fileupload/install/templates/source_file.rb
s3_cors_fileupload-0.2.0.pre1 lib/generators/s3_cors_fileupload/install/templates/source_file.rb