model_attachment 0.0.15 → 0.0.17

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,9 +4,13 @@ module ModelAttachment
4
4
  # returns the aws url
5
5
  # +type+: type passed to has_attachment, ex. small, large
6
6
  def aws_url(type = "")
7
+ aws_connect
8
+
7
9
  begin
8
10
  return AWS::S3::S3Object.find(aws_key(type), default_bucket).url
9
- rescue
11
+ rescue Exception => e
12
+ log("Amazon: #{e.message}")
13
+ log("Backtrace: #{e.backtrace[0]}")
10
14
  log("Could not get object: #{aws_key(type)}")
11
15
  end
12
16
  end
@@ -52,11 +56,11 @@ module ModelAttachment
52
56
  aws_connect
53
57
 
54
58
  begin
55
- AWS::S3::S3Object.store(aws_key, open(full_filename), default_bucket, :content_type => content_type)
59
+ AWS::S3::S3Object.store(aws_key, open(full_filename, 'rb'), default_bucket, :content_type => content_type)
56
60
 
57
61
  # copy over modified files
58
62
  process_image_types do |name, value|
59
- AWS::S3::S3Object.store(aws_key(name.to_s), open(full_filename), default_bucket, :content_type => content_type)
63
+ AWS::S3::S3Object.store(aws_key(name.to_s), open(full_filename, 'rb'), default_bucket, :content_type => content_type)
60
64
  end
61
65
 
62
66
  self.bucket = default_bucket
@@ -92,18 +96,29 @@ module ModelAttachment
92
96
  def move_to_filesystem
93
97
  aws_connect
94
98
  begin
95
- open(full_filename, 'w') do |file|
96
- AWS::S3::S3Object.stream(path + file_name, default_bucket) do |chunk|
97
- file.write chunk
98
- end
99
+ # streaming causes encoding error
100
+
101
+ File.open(full_filename, 'wb') do |file|
102
+
103
+ file.write(AWS::S3::S3Object.value(path + file_name, default_bucket))
104
+ file.rewind
105
+
106
+ # AWS::S3::S3Object.stream(path + file_name, default_bucket) do |chunk|
107
+ # log("Encoding: #{chunk.encoding}")
108
+ # file.write chunk
109
+ # end
99
110
  end
100
111
 
101
112
  # copy over modified files
102
113
  process_image_types do |name, value|
103
- open(full_filename(name), 'w') do |file|
104
- AWS::S3::S3Object.stream(path + filename(name), default_bucket) do |chunk|
105
- file.write chunk
106
- end
114
+ File.open(full_filename(name), 'wb') do |file|
115
+
116
+ file.write(AWS::S3::S3Object.value(path + file_name, default_bucket))
117
+ file.rewind
118
+
119
+ # AWS::S3::S3Object.stream(path + filename(name), default_bucket) do |chunk|
120
+ # file.write chunk
121
+ # end
107
122
  end
108
123
  end
109
124
 
@@ -1,3 +1,3 @@
1
1
  module ModelAttachment
2
- VERSION = "0.0.15"
2
+ VERSION = "0.0.17"
3
3
  end
@@ -216,25 +216,20 @@ module ModelAttachment
216
216
  end
217
217
 
218
218
  private
219
-
220
- def process_image_types #:nodoc:
221
- if self.class.attachment_options[:types]
222
- self.class.attachment_options[:types].each do |name, value|
223
- if image?
224
- yield(name, value)
225
- end
226
- end
227
- end
228
- end
229
-
219
+
230
220
  # save the correct attribute info before the save
231
221
  def save_attributes
232
222
  return if file_name.nil? || file_name.class.to_s == "String"
233
223
  @temp_file = self.file_name
234
-
224
+
235
225
  # get original filename info and clean up for storage
236
- ext = File.extname(@temp_file.original_filename)
237
- base = File.basename(@temp_file.original_filename, ext).strip.gsub(/[^A-Za-z\d\.\-_]+/, '_')
226
+ filename = File.basename(@temp_file)
227
+ if @temp_file.original_filename
228
+ filename = @temp_file.original_filename
229
+ end
230
+
231
+ ext = File.extname(filename)
232
+ base = File.basename(filename, ext).strip.gsub(/[^A-Za-z\d\.\-_]+/, '_')
238
233
 
239
234
  # save attributes
240
235
  self.file_name = base + ext
@@ -248,31 +243,62 @@ module ModelAttachment
248
243
  def save_attached_files
249
244
  return if @temp_file.nil? or @temp_file == ""
250
245
  options = self.class.attachment_options
251
-
246
+
247
+ puts "Path: #{full_path} Basename: #{basename} Extension: #{extension}"
252
248
  log("Path: #{full_path} Basename: #{basename} Extension: #{extension}")
253
249
 
254
- # copy image to correct path
255
- FileUtils.mkdir_p(full_path)
256
- FileUtils.chmod(0755, full_path)
257
- FileUtils.mv(@temp_file.path, full_path + basename + extension)
258
-
259
- # run any processing passed in on images
260
- process_images
261
-
262
- @dirty = true
263
- @temp_file.close if @temp_file.respond_to?(:close)
264
- @temp_file = nil
250
+ begin
251
+ # copy image to correct path
252
+ unless Dir.exists?(full_path)
253
+ FileUtils.mkdir_p(full_path)
254
+ end
255
+ FileUtils.chmod(0755, full_path)
256
+
257
+ if File.exists?(@temp_file.path)
258
+ FileUtils.mv(@temp_file.path, full_path + basename + extension)
259
+ else
260
+ raise "File Error: #{@temp_file.path} does not exist"
261
+ end
262
+
263
+ # run any processing passed in on images
264
+ process_images
265
+ rescue Exception => e
266
+ puts "Error: #{e.message}"
267
+ puts "\tBacktrace: #{e.backtrace[0..2]}"
268
+
269
+ log("Error: #{e.message}")
270
+ log("\tBacktrace: #{e.backtrace[0]}")
271
+ ensure
272
+ @temp_file.close if @temp_file.respond_to?(:close)
273
+ @dirty = true
274
+ @temp_file = nil
275
+ end
265
276
  end
266
277
 
267
278
  # run each processor on file
268
279
  def process_images
269
280
  process_image_types do |name, value|
270
- command = value[:command]
271
- old_filename = full_filename
272
- new_filename = full_filename(name)
273
- log("Create #{name} by running #{command} on #{old_filename}")
274
- log("Created: #{new_filename}")
275
- `#{command} #{old_filename} #{new_filename}`
281
+ begin
282
+ command = value[:command]
283
+ old_filename = full_filename
284
+ new_filename = full_filename(name)
285
+ log("Create #{name} by running #{command} on #{old_filename}")
286
+ log("Created: #{new_filename}")
287
+ `#{command} #{old_filename} #{new_filename}`
288
+ rescue Exception => e
289
+ puts "Process Images Error: #{e.message}"
290
+ puts "\tBacktrace: #{e.backtrace[0]}"
291
+ end
292
+ end
293
+ end
294
+
295
+ def process_image_types #:nodoc:
296
+ if self.class.attachment_options[:types]
297
+ self.class.attachment_options[:types].each do |name, value|
298
+ if image?
299
+ yield(name, value)
300
+ end
301
+ end
276
302
  end
277
303
  end
278
304
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 15
9
- version: 0.0.15
8
+ - 17
9
+ version: 0.0.17
10
10
  platform: ruby
11
11
  authors:
12
12
  - Steve Walker
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-06-17 00:00:00 -04:00
17
+ date: 2010-06-29 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency