attached 0.2.7 → 0.2.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -40,7 +40,7 @@ module Attached
40
40
  @options ||= {
41
41
  :path => ":name/:style/:identifier:extension",
42
42
  :default => :original,
43
- :medium => :aws,
43
+ :medium => :local,
44
44
  :credentials => {},
45
45
  :styles => {},
46
46
  :processors => [],
@@ -118,6 +118,11 @@ module Attached
118
118
  # @object.avatar.assign(...)
119
119
 
120
120
  def assign(file, identifier = "#{Guid.new}")
121
+
122
+ if file.is_a?(Attached::Attachment)
123
+ file = file.file
124
+ end
125
+
121
126
  @file = file.respond_to?(:tempfile) ? file.tempfile : file
122
127
 
123
128
  extension ||= File.extname(file.original_filename) if file.respond_to?(:original_filename)
@@ -130,6 +135,9 @@ module Attached
130
135
  instance_set :identifier, identifier
131
136
 
132
137
  process
138
+
139
+ ensure
140
+ file.close if file.respond_to?(:close)
133
141
  end
134
142
 
135
143
 
@@ -12,7 +12,7 @@ module Attached
12
12
  #
13
13
  # Attached::Processor.processor(:audio)
14
14
  # Attached::Processor.processor(:image)
15
- # Attached::Processor.processor(Attached::Processor::Video)
15
+ # Attached::Processor.processor(Attached::Processor::Custom.new)
16
16
 
17
17
  def self.processor(processor)
18
18
 
@@ -29,6 +29,13 @@ module Attached
29
29
 
30
30
  @extension ||= File.extname(self.file.path)
31
31
  end
32
+
33
+
34
+ # Redirect output path.
35
+
36
+ def redirect
37
+ ">/dev/null 2>&1" if File.exist?("/dev/null")
38
+ end
32
39
 
33
40
 
34
41
  # Helper function for calling processors.
@@ -53,7 +60,7 @@ module Attached
53
60
 
54
61
  parameters = parameters.join(" ").squeeze(" ")
55
62
 
56
- `lame #{parameters}`
63
+ `lame #{parameters} #{redirect}`
57
64
 
58
65
  raise Errno::ENOENT if $?.exitstatus == 127
59
66
 
@@ -62,7 +69,7 @@ module Attached
62
69
  end
63
70
 
64
71
  unless $?.exitstatus == 0
65
- raise Attached::Processor::Error, "attachment file must be an audio file"
72
+ raise Attached::Processor::Error, "must be an audio file"
66
73
  end
67
74
 
68
75
  return result
@@ -41,6 +41,13 @@ module Attached
41
41
  @width = Integer(self.width) if self.width
42
42
  @height = Integer(self.height) if self.height
43
43
  end
44
+
45
+
46
+ # Redirect output path.
47
+
48
+ def redirect
49
+ ">/dev/null 2>&1" if File.exist?("/dev/null")
50
+ end
44
51
 
45
52
 
46
53
  # Helper function for calling processors.
@@ -73,7 +80,7 @@ module Attached
73
80
 
74
81
  parameters = parameters.join(" ").squeeze(" ")
75
82
 
76
- `convert #{parameters}`
83
+ `convert #{parameters} #{redirect}`
77
84
 
78
85
  raise Errno::ENOENT if $?.exitstatus == 127
79
86
 
@@ -82,7 +89,7 @@ module Attached
82
89
  end
83
90
 
84
91
  unless $?.exitstatus == 0
85
- raise Attached::Processor::Error, "attachment file must be an image file"
92
+ raise Attached::Processor::Error, "must be an image file"
86
93
  end
87
94
 
88
95
  return result
@@ -1,6 +1,8 @@
1
+ require 'attached/storage/base'
1
2
  require 'attached/storage/aws'
2
3
  require 'attached/storage/google'
3
4
  require 'attached/storage/rackspace'
5
+ require 'attached/storage/local'
4
6
 
5
7
  module Attached
6
8
  module Storage
@@ -10,17 +12,22 @@ module Attached
10
12
  #
11
13
  # Usage:
12
14
  #
15
+ # Attached::Storage.storage(:local)
13
16
  # Attached::Storage.storage(:aws, "#{Rails.root}/config/aws.yml" )
14
17
  # Attached::Storage.storage(:google, "#{Rails.root}/config/google.yml" )
15
18
  # Attached::Storage.storage(:rackspace, "#{Rails.root}/config/rackspace.yml")
19
+ # Attached::Storage.storage(Attached::Storage::Custom.new)
16
20
 
17
- def self.storage(medium, credentials)
21
+ def self.storage(storage, credentials)
18
22
 
19
- case medium
23
+ return storage if storage.is_a? Attached::Storage::Base
24
+
25
+ case storage
20
26
  when :aws then return Attached::Storage::AWS.new credentials
21
27
  when :google then return Attached::Storage::Google.new credentials
22
28
  when :rackspace then return Attached::Storage::Rackspace.new credentials
23
- else raise "undefined storage medium '#{medium}'"
29
+ when :local then return Attached::Storage::Local.new
30
+ else raise "undefined storage '#{storage}'"
24
31
  end
25
32
 
26
33
  end
@@ -19,7 +19,7 @@ module Attached
19
19
  attr_reader :secret_access_key
20
20
 
21
21
 
22
- # Create a new AWS interface supporting save and destroy operations.
22
+ # Create a new interface supporting save and destroy operations.
23
23
  #
24
24
  # Usage:
25
25
  #
@@ -19,7 +19,7 @@ module Attached
19
19
  attr_reader :secret_access_key
20
20
 
21
21
 
22
- # Create a new AWS interface supporting save and destroy operations.
22
+ # Create a new interface supporting save and destroy operations.
23
23
  #
24
24
  # Usage:
25
25
  #
@@ -0,0 +1,72 @@
1
+ require 'attached/storage/base'
2
+
3
+ module Attached
4
+ module Storage
5
+ class Local < Base
6
+
7
+
8
+ attr_reader :mode
9
+
10
+
11
+ # Create a new interface supporting save and destroy operations.
12
+ #
13
+ # Usage:
14
+ #
15
+ # Attached::Storage::Local.new()
16
+
17
+ def initialize()
18
+ @mode = 0644
19
+ end
20
+
21
+
22
+ # Access the host (e.g. https://attached.commondatastorage.googleapis.com/) for a storage service.
23
+ #
24
+ # Usage:
25
+ #
26
+ # storage.host
27
+
28
+ def host()
29
+ "/system/"
30
+ end
31
+
32
+
33
+ # Save a file to a given path.
34
+ #
35
+ # Parameters:
36
+ #
37
+ # * file - The file to save.
38
+ # * path - The path to save.
39
+
40
+ def save(file, path)
41
+ path = "#{Rails.root}/public/system/#{path}"
42
+ dirname, basename = File.split(path)
43
+
44
+ begin
45
+ FileUtils.mkdir_p(dirname)
46
+ FileUtils.cp(file.path, path)
47
+ FileUtils.chmod(self.mode, path)
48
+ rescue Errno::ENOENT
49
+ end
50
+ end
51
+
52
+
53
+ # Destroy a file at a given path.
54
+ #
55
+ # Parameters:
56
+ #
57
+ # * path - The path to destroy.
58
+
59
+ def destroy(path)
60
+ path = "#{Rails.root}/public/system/#{path}"
61
+ dirname, basename = File.split(path)
62
+
63
+ begin
64
+ FileUtils.rm(path)
65
+ rescue Errno::ENOENT
66
+ end
67
+ end
68
+
69
+
70
+ end
71
+ end
72
+ end
@@ -18,7 +18,7 @@ module Attached
18
18
  attr_reader :api_key
19
19
 
20
20
 
21
- # Create a new AWS interface supporting save and destroy operations.
21
+ # Create a new interface supporting save and destroy operations.
22
22
  #
23
23
  # Usage:
24
24
  #
@@ -47,7 +47,7 @@ module Attached
47
47
  end
48
48
 
49
49
 
50
- # Save a file to a given path on AWS S3.
50
+ # Save a file to a given path.
51
51
  #
52
52
  # Parameters:
53
53
  #
@@ -64,7 +64,7 @@ module Attached
64
64
  end
65
65
 
66
66
 
67
- # Destroy a file at a given path on AWS S3.
67
+ # Destroy a file at a given path.
68
68
  #
69
69
  # Parameters:
70
70
  #
@@ -1,3 +1,3 @@
1
1
  module Attached
2
- VERSION = "0.2.7"
2
+ VERSION = "0.2.8"
3
3
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: attached
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.7
5
+ version: 0.2.8
6
6
  platform: ruby
7
7
  authors:
8
8
  - Kevin Sylvestre
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-02-21 00:00:00 -05:00
13
+ date: 2011-02-23 00:00:00 -05:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -56,6 +56,7 @@ files:
56
56
  - lib/attached/storage/base.rb
57
57
  - lib/attached/storage/error.rb
58
58
  - lib/attached/storage/google.rb
59
+ - lib/attached/storage/local.rb
59
60
  - lib/attached/storage/rackspace.rb
60
61
  - lib/attached/storage.rb
61
62
  - lib/attached/version.rb