Class: Caboose::MediaController

Inherits:
ApplicationController show all
Defined in:
app/controllers/caboose/media_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#add_ga_event, #admin_add, #admin_bulk_add, #admin_bulk_delete, #admin_bulk_update, #before_before_action, #hashify_query_string, #init_cart, #logged_in?, #logged_in_user, #login_user, #logout_user, #parse_url_params, #reject_param, #under_construction_or_forwarding_domain?, #user_is_allowed, #user_is_allowed_to, #validate_cookie, #validate_token, #var, #verify_logged_in

Instance Method Details

#admin_deleteObject


164
165
166
167
168
169
170
171
172
173
# File 'app/controllers/caboose/media_controller.rb', line 164

def admin_delete
  return unless user_is_allowed('media', 'delete')
  
  model_ids = params[:id] == 'bulk' ? params[:ids] : [params[:id]]             
  model_ids.each do |media_id|
    ProductImage.where(:media_id => media_id).destroy_all
    Media.where(:id => media_id).destroy_all
  end                  
  render :json => { :success => true }
end

#admin_editObject


113
114
115
116
117
# File 'app/controllers/caboose/media_controller.rb', line 113

def admin_edit
  return unless user_is_allowed('media', 'edit')
  @media = Media.find(params[:id])
  render :layout => 'caboose/admin'
end

#admin_edit_descriptionObject


106
107
108
109
110
# File 'app/controllers/caboose/media_controller.rb', line 106

def admin_edit_description
  return unless user_is_allowed('media', 'edit')
  @media = Media.find(params[:id])
  render :layout => 'caboose/modal'
end

#admin_indexObject


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/controllers/caboose/media_controller.rb', line 12

def admin_index
  return if !user_is_allowed('media', 'view')
  render :file => 'caboose/extras/error_invalid_site' and return if @site.nil?
             
  config = YAML.load(File.read(Rails.root.join('config', 'aws.yml')))[Rails.env]      
  access_key = config['access_key_id']
  secret_key = config['secret_access_key']
  bucket     = config['bucket']
  bucket = Caboose::uploads_bucket && Caboose::uploads_bucket.strip.length > 0 ? Caboose::uploads_bucket : "#{bucket}-uploads"       
  policy = {        
    "expiration" => 1.hour.from_now.utc.xmlschema,
    "conditions" => [
      { "bucket" => bucket },          
      { "acl" => "public-read" },
      [ "starts-with", "$key", '' ],
      #[ "starts-with", "$Content-Type", "" ],
      [ 'starts-with', '$name', '' ], 	
      [ 'starts-with', '$Filename', '' ],          
    ]
  }
  @policy = Base64.encode64(policy.to_json).gsub(/\n/,'')      
  @signature = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), secret_key, @policy)).gsub("\n","")
  @s3_upload_url = "https://#{bucket}.s3.amazonaws.com/"
  @aws_access_key_id = access_key                            
  
  id = params[:media_category_id]        
  @top_media_category = MediaCategory.top_category(@site.id)
  @media_category = id ? MediaCategory.find(id) : @top_media_category
  render :layout => 'caboose/admin'      
end

#admin_jsonObject


73
74
75
76
77
78
# File 'app/controllers/caboose/media_controller.rb', line 73

def admin_json
  return if !user_is_allowed('media', 'view')
  render :json => false and return if @site.nil?
  arr = Media.where(:media_category_id => params[:media_category_id]).reorder(:sort_order).all
  render :json => arr.collect{ |m| m.api_hash }                  
end

#admin_json_singleObject


81
82
83
84
85
86
# File 'app/controllers/caboose/media_controller.rb', line 81

def admin_json_single
  return if !user_is_allowed('media', 'view')
  render :json => false and return if @site.nil?
  m = Media.where(:id => params[:id]).first
  render :json => m.api_hash      
end

#admin_last_upload_processedObject


89
90
91
92
93
94
95
96
# File 'app/controllers/caboose/media_controller.rb', line 89

def admin_last_upload_processed
  return if !user_is_allowed('media', 'view')
  render :json => false and return if @site.nil?
  #Setting.where(:site_id => @site.id, :name => 'last_upload_processed').destroy_all      
  s = Setting.where(:site_id => @site.id, :name => 'last_upload_processed').first      
  s = Setting.create(:site_id => @site.id, :name => 'last_upload_processed', :value => DateTime.now.utc.strftime("%FT%T%z")) if s.nil?                  
  render :json => { :last_upload_processed => s.value }
end

#admin_newObject


99
100
101
102
103
# File 'app/controllers/caboose/media_controller.rb', line 99

def admin_new
  return unless user_is_allowed('media', 'add')
  @media_category_id = params[:media_category_id]             
  render :layout => 'caboose/admin'
end

#admin_policyObject


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/caboose/media_controller.rb', line 44

def admin_policy
  return if !user_is_allowed('media', 'view')
  
  config = YAML.load(File.read(Rails.root.join('config', 'aws.yml')))[Rails.env]      
  access_key = config['access_key_id']
  secret_key = config['secret_access_key']
  bucket     = config['bucket']      
  policy = {        
    "expiration" => 1.hour.from_now.utc.xmlschema,
    "conditions" => [
      { "bucket" => "#{bucket}-uploads" },          
      { "acl" => "public-read" },
      [ "starts-with", "$key", '' ],
      #[ "starts-with", "$Content-Type", 'image/' ],          
      [ 'starts-with', '$name', '' ], 	
      [ 'starts-with', '$Filename', '' ],          
    ]
  }
  policy = Base64.encode64(policy.to_json).gsub(/\n/,'')
  render :json => {
    :policy             => policy,      
    :signature          => Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), secret_key, policy)).gsub("\n",""),      
    :s3_upload_url      => "https://#{bucket}-uploads.s3.amazonaws.com/",
    :aws_access_key_id  => access_key,
    :top_media_category => Caboose::MediaCategory.top_category(@site.id),
  }
end

#admin_pre_uploadObject


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'app/controllers/caboose/media_controller.rb', line 176

def admin_pre_upload
  return unless user_is_allowed('media', 'view')
  media_category_id = params[:media_category_id]
  original_name = params[:name]
  name = Caboose::Media.upload_name(original_name)    
  desc = original_name.blank? ? "Media File" : original_name.gsub(/\.[0-9a-z]+$/i,'')
  file_type = params[:file_type]
  if ['image/gif', 'image/jpeg', 'image/png', 'image/tiff'].include? file_type
    image_content_type = file_type
  else
    file_content_type = file_type
  end
  m = Media.where(:media_category_id => media_category_id, :original_name => original_name, :name => name).first
  if m.nil?
    max = Media.where(:media_category_id => media_category_id).maximum(:sort_order)
    m = Media.create(:media_category_id => media_category_id, :description => desc, :sort_order => (max ? (max + 1) : 0), :original_name => original_name, :name => name, :image_content_type => image_content_type, :file_content_type => file_content_type, :processed => false)
  end
  p = Product.where(:media_category_id => media_category_id).last
  if p
    pi = ProductImage.create(:product_id => p.id, :media_id => m.id)
  end
  render :json => { :success => true }
end

#admin_updateObject


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/controllers/caboose/media_controller.rb', line 120

def admin_update
  return unless user_is_allowed('media', 'edit')
  
  resp = StdClass.new({'attributes' => {}})
  m = Media.find(params[:id])
  
  save = true      
  params.each do |name, value|
    case name
      when 'name'         then m.name         = value
      when 'description'  then m.description  = value
      when 'sort_order'   then m.sort_order   = value
      when 'image_url'    then
        m.processed = false
        m.delay(:queue => 'caboose_media', :priority => 2).download_image_from_url(value)
    end
  end
  
  m.save
  resp.success = save
  render :json => resp
end

#admin_update_imageObject


144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/controllers/caboose/media_controller.rb', line 144

def admin_update_image
  return unless user_is_allowed('media', 'edit')
  
  resp = StdClass.new
  new_url = params[:new_url]
  m = Media.where(:id => params[:id]).first
  
  if m.nil?
    resp.error = "Invalid media id."              
  elsif new_url.nil? || new_url.strip.length == 0
    resp.error = "Invalid image URL."
  else                
    m.image = URI.parse(new_url)
    m.save
    resp.success = "Image saved successfully."              
  end
  render :json => resp
end

#before_actionObject


7
8
9
# File 'app/controllers/caboose/media_controller.rb', line 7

def before_action
  @page = Page.page_with_uri(request.host_with_port, '/admin')
end