Class: Orange::AssetResource

Inherits:
ModelResource
  • Object
show all
Defined in:
lib/orange-more/assets/resources/asset_resource.rb

Instance Method Summary collapse

Instance Method Details

#asset_html(packet, id = false) ⇒ Object



195
196
197
198
199
# File 'lib/orange-more/assets/resources/asset_resource.rb', line 195

def asset_html(packet, id = false)
  id ||= packet['route.resource_id']
  m = model_class.get(id)
  m ? m.to_asset_tag : false
end

#change(packet, opts = {}) ⇒ Object



171
172
173
# File 'lib/orange-more/assets/resources/asset_resource.rb', line 171

def change(packet, opts = {})
  do_view(packet, :change, opts)
end

#ensure_dir!Object



106
107
108
109
110
111
112
# File 'lib/orange-more/assets/resources/asset_resource.rb', line 106

def ensure_dir!
  if(options[:s3_bucket])
    AWS::S3::Bucket.create(options[:s3_bucket]) unless AWS::S3::Bucket.find(options[:s3_bucket])
  else
    FileUtils.mkdir_p(orange.app_dir('assets','uploaded')) unless File.exists?(orange.app_dir('assets','uploaded'))
  end
end

#find_extras(packet, mode, opts = {}) ⇒ Object



175
176
177
# File 'lib/orange-more/assets/resources/asset_resource.rb', line 175

def find_extras(packet, mode, opts = {})
  {:list => model_class.all}
end

#handle_new_file(filename, file) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/orange-more/assets/resources/asset_resource.rb', line 114

def handle_new_file(filename, file)
  s3_connect!
  ensure_dir!
  if(options[:s3_bucket])
    filename = unique_s3_name(filename)
    AWS::S3::S3Object.store(filename, file, options[:s3_bucket], :access => :public_read)
  else
    filename = unique_local_name(filename)
    FileUtils.cp(file.path, orange.app_dir('assets','uploaded', filename))
    FileUtils.chmod(0644, orange.app_dir('assets','uploaded', filename))
  end
  return filename
end

#insert(packet, opts = {}) ⇒ Object



167
168
169
# File 'lib/orange-more/assets/resources/asset_resource.rb', line 167

def insert(packet, opts = {})
  do_view(packet, :insert, opts)
end

#new(packet, opts = {}) ⇒ Object

Creates a new model object and saves it (if a post), then reroutes to the main page

Parameters:

  • packet (Orange::Packet)

    the packet being routed



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/orange-more/assets/resources/asset_resource.rb', line 152

def new(packet, opts = {})
  no_reroute = opts.delete(:no_reroute) 
  xhr = packet.request.xhr? || packet.request.params["fake_xhr"]
  if packet.request.post? || !opts.blank?
    params = opts.with_defaults(opts.delete(:params) || packet.request.params[@my_orange_name.to_s] || {})
    before = beforeNew(packet, params)
    obj = onNew(packet, params) if before
    afterNew(packet, obj, params) if before
    obj.save if obj && before
  end
  packet.reroute(@my_orange_name, :orange) unless (xhr || no_reroute)
  packet['template.disable'] = true if xhr
  (xhr ? obj.to_s : obj) || false
end

#onDelete(packet, m, opts = {}) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/orange-more/assets/resources/asset_resource.rb', line 179

def onDelete(packet, m, opts = {})
  begin
    if(m.s3_bucket)
      s3_connect!
      AWS::S3::S3Object.delete(m.path, m.s3_bucket) if m.path
      AWS::S3::S3Object.delete(m.secondary_path, m.s3_bucket) if m.secondary_path
    else
      FileUtils.rm(orange.app_dir('assets','uploaded', m.path)) if m.path
      FileUtils.rm(orange.app_dir('assets','uploaded', m.secondary_path)) if m.secondary_path
    end
  rescue
    # Problem deleting file
  end
  m.destroy if m
end

#onNew(packet, params = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/orange-more/assets/resources/asset_resource.rb', line 53

def onNew(packet, params = {})
  m = false
  if(file = params['file'][:tempfile])
    file_path = handle_new_file(params['file'][:filename], file)
    if(params['file2'] && secondary = params['file2'][:tempfile]) 
      secondary_path = handle_new_file(params['file2'][:filename], secondary)
    else
      secondary_path = nil
    end
    
    params['path'] = file_path if file_path
    params['secondary_path'] = secondary_path if secondary_path
    params['mime_type'] = params['file'][:type] if file_path
    params['secondary_mime_type'] = params['file2'][:type] if secondary_path
    params.delete('file')
    params.delete('file2')
    params['s3_bucket'] = options[:s3_bucket] if options[:s3_bucket]
    m = model_class.new(params)
  end
  m
end

#onSave(packet, obj, params = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/orange-more/assets/resources/asset_resource.rb', line 75

def onSave(packet, obj, params = {})
  if(params['file'] && file = params['file'][:tempfile])
    file_path = handle_new_file(params['file'][:filename], file)
    if(params['file2'] && secondary = params['file2'][:tempfile]) 
      secondary_path = handle_new_file(params['file2'][:filename], secondary)
    else
      secondary_path = nil
    end
    
    params['path'] = file_path if file_path
    params['secondary_path'] = secondary_path if secondary_path
    params['mime_type'] = params['file'][:type] if file_path
    params['secondary_mime_type'] = params['file2'][:type] if secondary_path
    params['s3_bucket'] = options[:s3_bucket] if options[:s3_bucket]
  end
  params.delete('file')
  params.delete('file2')
  obj.update(params)
end

#s3_connect!Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/orange-more/assets/resources/asset_resource.rb', line 95

def s3_connect!
  if(options[:s3_bucket])
    id = options[:s3_access_key_id] || ENV['S3_KEY']
    secret = options[:s3_secret_access_key] || ENV['S3_SECRET']
    AWS::S3::Base.establish_connection!(
        :access_key_id     => id,
        :secret_access_key => secret
      )
  end
end

#stack_initObject



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
42
43
44
45
46
47
48
49
50
51
# File 'lib/orange-more/assets/resources/asset_resource.rb', line 15

def stack_init
  if orange.options[:s3_bucket]
    require 'aws/s3'
    options[:s3_bucket] = orange.options[:s3_bucket]
    options[:s3_access_key_id] = orange.options[:s3_access_key_id]
    options[:s3_secret_access_key] = orange.options[:s3_secret_access_key]
  end
  orange[:admin, true].add_link("Content", :resource => @my_orange_name, :text => 'Assets')
  orange[:radius, true].define_tag "asset" do |tag|
    if tag.attr['id']
      ret = (m = model_class.first(:id => tag.attr['id'])) ? m.to_asset_tag : 'Invalid Asset'
      if tag.attr['wrap']
        ret = "<div class='#{tag.attr['wrap']}'>#{ret}</div>"
      else
        ret
      end
    else
      ''
    end
  end
  orange[:scaffold].add_scaffold_type(:asset) do |name, val, opts|
    if opts[:show]
      opts[:model].to_asset_tag
    else
      packet = opts[:packet]
      
      asset_html = val ? orange[:assets].asset_html(packet, val) : ""
      ret = "<input type=\"hidden\" value=\"#{val}\" name=\"#{opts[:model_name]}[#{name}]\" />"
      if val.blank?
        ret += "<span class='asset_preview'></span><a class='insert_asset' rel=\"#{opts[:model_name]}[#{name}]\" href='/admin/assets/insert'>Insert Asset</a>"
      else
        ret += "<span class='asset_preview'>#{asset_html}</span><a class='insert_asset' rel=\"#{opts[:model_name]}[#{name}]\" href='/admin/assets/#{val}/change'>Change Asset</a>"
      end
      ret = "<label for=''>#{opts[:display_name]}</label><br />" + ret if opts[:label]
    end
  end
end

#unique_local_name(filename) ⇒ Object



139
140
141
142
143
144
145
146
147
148
# File 'lib/orange-more/assets/resources/asset_resource.rb', line 139

def unique_local_name(filename)
  return filename unless File.exists?(orange.app_dir('assets','uploaded', filename))
  i = 1
  extname = File.extname(filename)
  basename = File.basename(filename, extname)
  while File.exists?(orange.app_dir('assets', 'uploaded', "#{basename}_#{i}#{extname}"))
    i += 1
  end
  "#{basename}_#{i}#{extname}"
end

#unique_s3_name(filename) ⇒ Object



128
129
130
131
132
133
134
135
136
137
# File 'lib/orange-more/assets/resources/asset_resource.rb', line 128

def unique_s3_name(filename)
  return filename unless AWS::S3::S3Object.exists?(filename, options[:s3_bucket])
  i = 1
  extname = File.extname(filename)
  basename = File.basename(filename, extname)
  while AWS::S3::S3Object.exists?("#{basename}_#{i}#{extname}", options[:s3_bucket])
    i += 1
  end
  "#{basename}_#{i}#{extname}"
end