Class: Fogged::Resource

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/fogged/resource.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.search(params) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/models/fogged/resource.rb', line 8

def self.search(params)
  results = all

  if params[:query]
    results = results.where(
      "#{table_name}.name LIKE :query",
      :query => "%#{params[:query].to_s.downcase}%"
    )
  end

  results
end

Instance Method Details

#encode!(inline = false) ⇒ Object



118
119
120
# File 'app/models/fogged/resource.rb', line 118

def encode!(inline = false)
  Resources::Encoder.for(self).encode!(inline)
end

#encoding?Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
79
# File 'app/models/fogged/resource.rb', line 73

def encoding?
  unless encoding_progress.present? &&
         (video? || (image? && Fogged.active_job_enabled))
    return
  end
  encoding_progress < 100
end

#find_size!Object



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/models/fogged/resource.rb', line 104

def find_size!
  if Fogged.test_enabled
    return update!(
      :width => 800,
      :height => 600
    )
  end
  size = FastImage.size(url)
  update!(
    :width => size.first,
    :height => size.second
  ) unless size.blank?
end

#fogged_fileObject



91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/models/fogged/resource.rb', line 91

def fogged_file
  @_fogged_file ||= begin
    files = Fogged.resources.files
    file = files.get(fogged_name) || files.create(
      :key => fogged_name,
      :body => "",
      :content_type => content_type
    )
    file.public = "public_read"
    file.tap(&:save)
  end
end

#h264_urlObject



35
36
37
38
# File 'app/models/fogged/resource.rb', line 35

def h264_url
  return unless video? && Fogged.zencoder_enabled
  url.gsub(fogged_name, fogged_name_for(:h264))
end

#image?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'app/models/fogged/resource.rb', line 69

def image?
  content_type.start_with?("image/")
end

#mpeg_urlObject



40
41
42
43
# File 'app/models/fogged/resource.rb', line 40

def mpeg_url
  return unless video? && Fogged.zencoder_enabled
  url.gsub(fogged_name, fogged_name_for(:mpeg))
end

#process!(inline = false) ⇒ Object



81
82
83
84
# File 'app/models/fogged/resource.rb', line 81

def process!(inline = false)
  find_size! if image?
  encode!(inline)
end

#thumbnail_urlsObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/models/fogged/resource.rb', line 50

def thumbnail_urls
  return unless Fogged.active_job_enabled

  case
  when video? && Fogged.zencoder_enabled
    5.times.map do |n|
      url.gsub(fogged_name, fogged_name_for(:thumbnails, n))
    end
  when image? && Fogged.minimagick_enabled
    Fogged.thumbnail_sizes.size.times.map do |n|
      url.gsub(fogged_name, fogged_name_for(:thumbnails, n))
    end
  end
end

#upload_urlObject



21
22
23
24
25
26
27
28
29
# File 'app/models/fogged/resource.rb', line 21

def upload_url
  fogged_file.service.put_object_url(
    Fogged.resources.key,
    fogged_file.key,
    2.minutes.from_now,
    "Content-Type" => content_type,
    "x-amz-acl" => "public-read"
  )
end

#urlObject



31
32
33
# File 'app/models/fogged/resource.rb', line 31

def url
  Fogged.file_public_url(fogged_name)
end

#video?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'app/models/fogged/resource.rb', line 65

def video?
  content_type.start_with?("video/")
end

#webm_urlObject



45
46
47
48
# File 'app/models/fogged/resource.rb', line 45

def webm_url
  return unless video? && Fogged.zencoder_enabled
  url.gsub(fogged_name, fogged_name_for(:webm))
end

#write(content) ⇒ Object



86
87
88
89
# File 'app/models/fogged/resource.rb', line 86

def write(content)
  fogged_file.body = content
  fogged_file.save
end