Class: S3Assets::Model

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Timestamps::Created
Defined in:
lib/s3_assets/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_s3_params(bucket, key) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/s3_assets/model.rb', line 52

def self.from_s3_params(bucket, key)
  return nil if bucket.blank? || key.blank?

  key = URI.encode(key, "!@#$%^&*()+=[]{} ")
  url = "https://s3.amazonaws.com/#{bucket}/#{key}"
  self.new(absolute_url: url)
end

Instance Method Details

#dj_priorityObject



49
50
# File 'lib/s3_assets/model.rb', line 49

def dj_priority
end

#downloadObject



25
26
27
# File 'lib/s3_assets/model.rb', line 25

def download
  ::S3Assets::Utility.download(self.asset.url)
end

#fetch_and_store_from_url!Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/s3_assets/model.rb', line 60

def fetch_and_store_from_url!
  return unless self.processable?

  begin
    self.remote_asset_url = self.absolute_url
    self.save!
  rescue CarrierWave::DownloadError => ex
    if ex.message.include?("Invalid Location URI")
      # "http://www.hiretale.com/files/resize_logo/13126logo original.png"
      # is not working without doing this due to redirect URL not encoded properly
      page = Mechanize.new.head(self.absolute_url)
      self.remote_asset_url = page.uri.to_s
      self.save!
    else
      raise ex
    end
  end
end

#filenameObject



42
43
44
45
46
47
# File 'lib/s3_assets/model.rb', line 42

def filename
  return nil if self.asset.url.blank?
  file_name = self.asset.url.split("?").first
  file_name = file_name.split("/").last
  file_name
end

#image?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/s3_assets/model.rb', line 21

def image?
  self.content_type.to_s.downcase.include? "image" unless self.content_type.nil?
end

#original_filenameObject



29
30
31
32
# File 'lib/s3_assets/model.rb', line 29

def original_filename
  return nil if filename.blank?
  URI.unescape(filename)
end

#processable?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/s3_assets/model.rb', line 34

def processable?
  self.absolute_url.present? && self.asset.blank?
end

#processed?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/s3_assets/model.rb', line 38

def processed?
  !(processable?)
end