attached 0.1.7 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile CHANGED
@@ -1,7 +1,3 @@
1
1
  source 'http://rubygems.org'
2
2
 
3
- gem 'rails'
4
-
5
- gem 'guid'
6
-
7
- gem 'aws-s3', :require => 'aws/s3'
3
+ gemspec
@@ -36,6 +36,7 @@ module Attached
36
36
  @options ||= {
37
37
  :path => "/:name/:style/:identifier:extension",
38
38
  :default => :original,
39
+ :medium => :aws,
39
40
  :credentials => {},
40
41
  :styles => {},
41
42
  :processors => [],
@@ -110,14 +111,12 @@ module Attached
110
111
  @file = file.respond_to?(:tempfile) ? file.tempfile : file
111
112
 
112
113
  extension ||= File.extname(file.original_filename) if file.respond_to?(:original_filename)
113
- extension ||= File.extname(file.path)
114
+ extension ||= File.extname(file.path) if file.respond_to?(:path)
114
115
 
115
116
  instance_set :size, file.size
116
117
  instance_set :extension, extension
117
118
  instance_set :identifier, identifier
118
119
 
119
- self.queue[self.default] = self.file
120
-
121
120
  process
122
121
  end
123
122
 
@@ -260,13 +259,15 @@ module Attached
260
259
 
261
260
  private
262
261
 
263
- # Helper function for calling processors.
262
+ # Helper function for calling processors (will queue default).
264
263
  #
265
264
  # Usage:
266
265
  #
267
266
  # self.process
268
267
 
269
268
  def process
269
+ self.queue[self.default] = self.file
270
+
270
271
  self.processors.each do |processor|
271
272
  self.styles.each do |style, options|
272
273
  case processor
@@ -1,20 +1,16 @@
1
1
  require 'attached/processor'
2
2
 
3
- begin
4
- require 'rmagick'
5
- rescue LoadError
6
- require 'RMagick'
7
- rescue LoadError
8
- raise "Installation of 'rmagick' is required before using the 'image' processor"
9
- end
10
-
11
3
  module Attached
12
4
 
13
5
  class Image < Processor
14
6
 
15
7
 
16
8
  attr_reader :path
17
- attr_reader :extname
9
+ attr_reader :extension
10
+
11
+ attr_reader :width
12
+ attr_reader :height
13
+ attr_reader :operation
18
14
 
19
15
  # Create a processor.
20
16
  #
@@ -27,8 +23,24 @@ module Attached
27
23
  def initialize(file, options = {}, attachment = nil)
28
24
  super
29
25
 
30
- @path = @file.path
31
- @extname = File.extname(@file.path)
26
+ @path = self.file.path
27
+
28
+ @size = options[:size]
29
+ @extension = options[:extension]
30
+
31
+ @width, @height, @operation = @size.match(/(\d*)x?(\d*)(.*)/)[1..3] if @size
32
+
33
+ @width ||= options[:width]
34
+ @height ||= options[:height]
35
+ @operation ||= options[:operation]
36
+
37
+ @extension ||= File.extname(self.file.path)
38
+
39
+ @width = Integer(self.width)
40
+ @height = Integer(self.height)
41
+
42
+ raise "Image processor requires specification of 'width' or 'size'" unless self.width
43
+ raise "Image processor requires specification of 'height' or 'size'" unless self.height
32
44
  end
33
45
 
34
46
 
@@ -39,32 +51,34 @@ module Attached
39
51
  # self.process
40
52
 
41
53
  def process
42
- result = Tempfile.new(["", options['extension'] || self.extname])
54
+ result = Tempfile.new(["", self.extension])
43
55
  result.binmode
44
-
45
- image = ::Magick::Image.read(self.path)
46
- image_list = ::Magick::ImageList.new
47
-
48
- width, height, operation = self.options[:size].match(/\b(\d*)x?(\d*)\b([\#\<\>])?/)[1..3] if self.options[:size]
49
-
50
- width ||= self.options[:width]
51
- height ||= self.options[:height]
52
- operation ||= self.options[:operation]
53
56
 
54
- width = width.to_i
55
- height = height.to_i
56
-
57
- image.each do |frame|
57
+ begin
58
+ parameters = []
59
+
60
+ parameters << self.path
61
+
58
62
  case operation
59
- when /!/ then puts "hi"
60
- when /#/ then image_list << frame.resize_to_fill(width, height)
61
- when /</ then image_list << frame.resize_to_fit(width, height)
62
- when />/ then image_list << frame.resize_to_fit(width, height)
63
- else image_list << frame.resize(width, height)
63
+ when '#' then parameters << "-resize #{width}x#{height}^ -gravity center -extent #{width}x#{height}"
64
+ when '<' then parameters << "-resize #{width}x#{height}\\<"
65
+ when '>' then parameters << "-resize #{width}x#{height}\\>"
66
+ else parameters << "-resize #{width}x#{height}"
64
67
  end
65
- end
68
+
69
+ parameters << result.path
70
+
71
+ parameters = parameters.join(" ").squeeze(" ")
72
+
73
+ `convert #{parameters}`
74
+
75
+ raise "Command 'convert' failed. Ensure upload file is an image and options are correct." unless $?.exitstatus == 0
76
+
77
+ rescue Errno::ENOENT
78
+
79
+ raise "Command 'convert' not found. Ensure 'Image Magick' is installed."
66
80
 
67
- image_list.write(result.path)
81
+ end
68
82
 
69
83
  return result
70
84
  end
@@ -7,12 +7,14 @@ module Attached
7
7
  #
8
8
  # Usage:
9
9
  #
10
- # Attached::Storage.medium(aws)
10
+ # Attached::Storage.medium()
11
+ # Attached::Storage.medium(:aws)
11
12
 
12
13
  def self.storage(medium = :aws, credentials = nil)
13
14
 
14
15
  case medium
15
- when :aws then return Attached::Storage::AWS.new(credentials)
16
+ when :aws then return Attached::Storage::AWS.new credentials
17
+ else raise "Undefined storage medium '#{medium}'."
16
18
  end
17
19
 
18
20
  end
@@ -12,6 +12,7 @@ module Attached
12
12
  class AWS < Base
13
13
 
14
14
 
15
+ attr_reader :access
15
16
  attr_reader :bucket
16
17
  attr_reader :access_key_id
17
18
  attr_reader :secret_access_key
@@ -27,6 +28,7 @@ module Attached
27
28
  def initialize(credentials)
28
29
  credentials = parse(credentials)
29
30
 
31
+ @access = :public_read
30
32
  @bucket = credentials[:bucket] || credentials['bucket']
31
33
  @access_key_id = credentials[:access_key_id] || credentials['access_key_id']
32
34
  @secret_access_key = credentials[:secret_access_key] || credentials['secret_access_key']
@@ -54,7 +56,7 @@ module Attached
54
56
  def save(file, path)
55
57
  connect()
56
58
  begin
57
- ::AWS::S3::S3Object.store(path, file, bucket, :access => :public_read)
59
+ ::AWS::S3::S3Object.store(path, file, bucket, :access => access)
58
60
  rescue AWS::S3::NoSuchBucket => e
59
61
  ::AWS::S3::Bucket.create(bucket)
60
62
  retry
@@ -71,10 +73,8 @@ module Attached
71
73
  def destroy(path)
72
74
  connect()
73
75
  begin
74
- ::AWS::S3::S3Object.delete(path, bucket, :access => :authenticated_read)
76
+ ::AWS::S3::S3Object.delete(path, bucket)
75
77
  rescue AWS::S3::NoSuchBucket => e
76
- ::AWS::S3::Bucket.create(bucket)
77
- retry
78
78
  end
79
79
  end
80
80
 
@@ -0,0 +1,3 @@
1
+ module Attached
2
+ VERSION = "0.1.8"
3
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 7
9
- version: 0.1.7
8
+ - 8
9
+ version: 0.1.8
10
10
  platform: ruby
11
11
  authors:
12
12
  - Kevin Sylvestre
@@ -14,11 +14,11 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-20 00:00:00 -05:00
17
+ date: 2010-12-22 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- name: guid
21
+ name: aws-s3
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
24
  none: false
@@ -31,7 +31,7 @@ dependencies:
31
31
  type: :runtime
32
32
  version_requirements: *id001
33
33
  - !ruby/object:Gem::Dependency
34
- name: aws-s3
34
+ name: guid
35
35
  prerelease: false
36
36
  requirement: &id002 !ruby/object:Gem::Requirement
37
37
  none: false
@@ -60,6 +60,7 @@ files:
60
60
  - lib/attached/storage/aws.rb
61
61
  - lib/attached/storage/base.rb
62
62
  - lib/attached/storage.rb
63
+ - lib/attached/version.rb
63
64
  - lib/attached.rb
64
65
  - lib/tasks/attached.rake
65
66
  - README.rdoc