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_update, #admin_json_single, #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_bulk_deleteObject

DELETE /admin/media/bulk



152
153
154
155
156
157
158
159
160
161
162
# File 'app/controllers/caboose/media_controller.rb', line 152

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

#admin_deleteObject

DELETE /admin/media/:id



144
145
146
147
148
149
# File 'app/controllers/caboose/media_controller.rb', line 144

def admin_delete
  return unless user_is_allowed('media', 'delete')
  Media.find(params[:id]).destroy
  ProductImage.where(:media_id => params[:id]).destroy_all
  render :json => { :success => true }
end

#admin_editObject

GET /admin/media/:id



71
72
73
74
75
# File 'app/controllers/caboose/media_controller.rb', line 71

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

#admin_edit_descriptionObject

GET /admin/media/:id/description



78
79
80
81
82
# File 'app/controllers/caboose/media_controller.rb', line 78

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

#admin_indexObject

GET /admin/media



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

GET /admin/media/json



44
45
46
47
48
49
50
51
# File 'app/controllers/caboose/media_controller.rb', line 44

def admin_json
  return if !user_is_allowed('media', 'view')
  render :json => false and return if @site.nil?
  
  id = params[:media_category_id]        
  cat = id ? MediaCategory.find(id) : MediaCategory.top_category(@site.id)      
  render :json => cat.api_hash
end

#admin_last_upload_processedObject

GET /admin/media/last-upload-processed



54
55
56
57
58
59
60
61
# File 'app/controllers/caboose/media_controller.rb', line 54

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

GET /admin/media/new



64
65
66
67
68
# File 'app/controllers/caboose/media_controller.rb', line 64

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

#admin_pre_uploadObject

POST /admin/media/pre-upload



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'app/controllers/caboose/media_controller.rb', line 165

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)                        
  m = Media.where(:media_category_id => media_category_id, :original_name => original_name, :name => name).first
  if m.nil?
    m = Media.create(:media_category_id => media_category_id, :original_name => original_name, :name => name, :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

PUT /admin/media/:id



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/controllers/caboose/media_controller.rb', line 85

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 'image_url'    then
        m.processed = false
        m.delay.download_image_from_url(value)
    end
  end
  
  m.save
  resp.success = save
  render :json => resp
end

#admin_update_imageObject

POST /admin/media/:id/image



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 124

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