Class: Caboose::ImagesController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#admin_add, #admin_bulk_add, #admin_bulk_delete, #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, #user_is_allowed, #user_is_allowed_to, #validate_cookie, #validate_token, #var, #verify_logged_in

Instance Method Details

#admin_deleteObject

DELETE /admin/images/:id



65
66
67
68
69
70
71
72
73
# File 'app/controllers/caboose/images_controller.rb', line 65

def admin_delete
  return unless user_is_allowed('images', 'delete')
  img = MediaImage.find(params[:id])      
  resp = StdClass.new({
    'redirect' => "/admin/images?media_category_id=#{img.media_category_id}"
  })
  img.destroy            
  render :json => resp
end

#admin_editObject

GET /admin/images/:id



39
40
41
42
43
# File 'app/controllers/caboose/images_controller.rb', line 39

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

#admin_indexObject

GET /admin/images



12
13
14
15
16
17
18
19
# File 'app/controllers/caboose/images_controller.rb', line 12

def admin_index
  return if !user_is_allowed('images', 'view')
  render :file => 'caboose/extras/error_invalid_site' and return if @site.nil?
              
  id = params[:media_category_id]        
  @media_category = id ? MediaCategory.find(id) : MediaCategory.top_image_category(@site.id)      
  render :layout => 'caboose/admin'      
end

#admin_jsonObject

GET /admin/images/json



22
23
24
25
26
27
28
29
# File 'app/controllers/caboose/images_controller.rb', line 22

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

#admin_newObject

GET /admin/images/new



32
33
34
35
36
# File 'app/controllers/caboose/images_controller.rb', line 32

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

#admin_processObject

GET /admin/images/:id/process



124
125
126
127
128
129
# File 'app/controllers/caboose/images_controller.rb', line 124

def admin_process
  return if !user_is_allowed('images', 'edit')
  mi = MediaImage.find(params[:id])      
  mi.delay.process
  render :json => true      
end

#admin_process_finishedObject

GET /admin/images/:id/finished



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/controllers/caboose/images_controller.rb', line 132

def admin_process_finished      
  return if !user_is_allowed('images', 'edit')
  mi = MediaImage.find(params[:id])
  resp = StdClass.new
  if mi.image_file_name && mi.image_file_name.strip.length > 0
    resp.is_finished = true
    resp.tiny_url     = mi.image.url(:tiny)
    resp.thumb_url    = mi.image.url(:thumb)
    resp.large_url    = mi.image.url(:large)
    resp.original_url = mi.image.url(:original)
  else
    resp.is_finished = false
  end
  render :json => resp
end

#admin_s3_resultObject



119
120
121
# File 'app/controllers/caboose/images_controller.rb', line 119

def admin_s3_result
  render :layout => 'caboose/empty'      
end

#admin_sign_s3Object

GET /admin/images/sign-s3



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/controllers/caboose/images_controller.rb', line 76

def admin_sign_s3
  
  name = params[:name]      
  mi = MediaImage.create(
    :media_category_id => params[:media_category_id], 
    :name => params[:name]
  )
  key = "media-images/#{mi.id}#{File.extname(name)}".downcase
  
  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" => 10.seconds.from_now.utc.xmlschema,
    "conditions" => [
      { "bucket" => bucket },
      ["starts-with", "$key", key],
      { "acl" => "public-read" },
      { "success_action_status" => "200" }
      #{ "success_action_redirect" => "/admin/images/s3-result" }          
    ]
  }
  policy = Base64.encode64(policy.to_json).gsub(/\n/,'')      
  signature = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), secret_key, policy)).gsub("\n","")
  
  render :json => {
    :media_image_id => mi.id,
    :url => "https://#{bucket}.s3.amazonaws.com",
    :fields => {
      :key                     => key,
      'AWSAccessKeyId'         => access_key,
      :acl                     => 'public-read',
      :success_action_status   => '200',
      #:success_action_redirect => '/admin/images/s3-result',
      :policy                  => policy, 
      :signature               => signature
    }
  }
  
end

#admin_updateObject

PUT /admin/images/:id



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/controllers/caboose/images_controller.rb', line 46

def admin_update
  return unless user_is_allowed('images', 'edit')
  
  resp = StdClass.new({'attributes' => {}})
  image = MediaImage.find(params[:id])
  
  save = true      
  params.each do |name, value|
    case name
      when 'name'         then image.name         = value
      when 'description'  then image.description  = value          
    end
  end

  resp.success = save && image.save
  render :json => resp
end

#before_actionObject



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

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