attached 0.1.4 → 0.1.5
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/README.rdoc +10 -11
- data/lib/attached/attachment.rb +36 -20
- data/lib/attached/storage.rb +4 -4
- data/lib/attached/storage/{s3.rb → aws.rb} +6 -6
- metadata +4 -4
data/README.rdoc
CHANGED
|
@@ -5,6 +5,7 @@ Attached is a Ruby on Rails file attachment tool that lets users upload to the c
|
|
|
5
5
|
== Installation
|
|
6
6
|
|
|
7
7
|
gem install attached
|
|
8
|
+
gem install rmagick
|
|
8
9
|
|
|
9
10
|
== Examples
|
|
10
11
|
|
|
@@ -43,7 +44,6 @@ Form:
|
|
|
43
44
|
|
|
44
45
|
<%= form_for @video, :html => { :multipart => true } do |form| %>
|
|
45
46
|
<%= form.file_field :encoding %>
|
|
46
|
-
<p class="errors"><%= @video.errors[:encoding] %></p>
|
|
47
47
|
<% end %>
|
|
48
48
|
|
|
49
49
|
View:
|
|
@@ -57,12 +57,7 @@ View:
|
|
|
57
57
|
|
|
58
58
|
=== Storage
|
|
59
59
|
|
|
60
|
-
has_attached :avatar, :provider => :
|
|
61
|
-
:secret_access_key => "*****",
|
|
62
|
-
:access_key_id => "*****",
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
has_attached :avatar, :provider => :google, :credentials => {
|
|
60
|
+
has_attached :avatar, :provider => :aws, :credentials => {
|
|
66
61
|
:secret_access_key => "*****",
|
|
67
62
|
:access_key_id => "*****",
|
|
68
63
|
}
|
|
@@ -74,10 +69,14 @@ View:
|
|
|
74
69
|
:large => { :size => "400x400#" }
|
|
75
70
|
}
|
|
76
71
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
72
|
+
=== Alias
|
|
73
|
+
|
|
74
|
+
Attached::Attachment.options[:aliases] = %w(
|
|
75
|
+
d1kf5um3mxa8kv.cloudfront.net
|
|
76
|
+
d3d5wf186mp1rv.cloudfront.net
|
|
77
|
+
d2wmfkw6rydl1g.cloudfront.net
|
|
78
|
+
d1gqpdc40l7s16.cloudfront.net
|
|
79
|
+
)
|
|
81
80
|
|
|
82
81
|
== Copyright
|
|
83
82
|
|
data/lib/attached/attachment.rb
CHANGED
|
@@ -12,7 +12,6 @@ module Attached
|
|
|
12
12
|
attr_reader :file
|
|
13
13
|
attr_reader :name
|
|
14
14
|
attr_reader :instance
|
|
15
|
-
attr_reader :options
|
|
16
15
|
attr_reader :queue
|
|
17
16
|
attr_reader :path
|
|
18
17
|
attr_reader :styles
|
|
@@ -21,6 +20,10 @@ module Attached
|
|
|
21
20
|
attr_reader :credentials
|
|
22
21
|
attr_reader :processors
|
|
23
22
|
attr_reader :processor
|
|
23
|
+
attr_reader :aliases
|
|
24
|
+
attr_reader :alias
|
|
25
|
+
attr_reader :storage
|
|
26
|
+
attr_reader :host
|
|
24
27
|
|
|
25
28
|
|
|
26
29
|
# A default set of options that can be extended to customize the path, storage or credentials.
|
|
@@ -31,10 +34,12 @@ module Attached
|
|
|
31
34
|
|
|
32
35
|
def self.options
|
|
33
36
|
@options ||= {
|
|
34
|
-
:path
|
|
35
|
-
:default
|
|
36
|
-
:
|
|
37
|
-
:
|
|
37
|
+
:path => "/:name/:style/:identifier:extension",
|
|
38
|
+
:default => :original,
|
|
39
|
+
:credentials => {},
|
|
40
|
+
:styles => {},
|
|
41
|
+
:processors => [],
|
|
42
|
+
:aliases => [],
|
|
38
43
|
}
|
|
39
44
|
end
|
|
40
45
|
|
|
@@ -54,21 +59,29 @@ module Attached
|
|
|
54
59
|
# * :styles - A hash containing optional parameters including extension and identifier
|
|
55
60
|
|
|
56
61
|
def initialize(name, instance, options = {})
|
|
62
|
+
options = self.class.options.merge(options)
|
|
63
|
+
|
|
57
64
|
@name = name
|
|
58
65
|
@instance = instance
|
|
59
|
-
@options = self.class.options.merge(options)
|
|
60
66
|
|
|
61
67
|
@queue = {}
|
|
62
68
|
|
|
63
|
-
@path =
|
|
64
|
-
@styles =
|
|
65
|
-
@default =
|
|
66
|
-
@medium =
|
|
67
|
-
@credentials =
|
|
68
|
-
@processors =
|
|
69
|
-
@processor =
|
|
69
|
+
@path = options[:path]
|
|
70
|
+
@styles = options[:styles]
|
|
71
|
+
@default = options[:default]
|
|
72
|
+
@medium = options[:medium]
|
|
73
|
+
@credentials = options[:credentials]
|
|
74
|
+
@processors = options[:processors]
|
|
75
|
+
@processor = options[:processor]
|
|
76
|
+
@aliases = options[:aliases]
|
|
77
|
+
@alias = options[:alias]
|
|
78
|
+
|
|
79
|
+
@processors = self.processors + [self.processor] if self.processor
|
|
80
|
+
@aliases = self.aliases + [self.alias] if self.alias
|
|
70
81
|
|
|
71
|
-
@
|
|
82
|
+
@storage = Attached::Storage.storage(self.medium, self.credentials)
|
|
83
|
+
|
|
84
|
+
@host = self.storage.host
|
|
72
85
|
end
|
|
73
86
|
|
|
74
87
|
|
|
@@ -82,6 +95,10 @@ module Attached
|
|
|
82
95
|
instance.changed.include? "#{name}_identifier"
|
|
83
96
|
end
|
|
84
97
|
|
|
98
|
+
def file?
|
|
99
|
+
not identifier.blank?
|
|
100
|
+
end
|
|
101
|
+
|
|
85
102
|
|
|
86
103
|
# Assign an attachment to a file.
|
|
87
104
|
#
|
|
@@ -111,8 +128,6 @@ module Attached
|
|
|
111
128
|
# @object.avatar.save
|
|
112
129
|
|
|
113
130
|
def save
|
|
114
|
-
@storage ||= Attached::Storage.storage(self.medium, self.credentials)
|
|
115
|
-
|
|
116
131
|
@queue.each do |style, file|
|
|
117
132
|
@storage.save(file, self.path(style)) if file and self.path(style)
|
|
118
133
|
end
|
|
@@ -128,8 +143,6 @@ module Attached
|
|
|
128
143
|
# @object.avatar.destroy
|
|
129
144
|
|
|
130
145
|
def destroy
|
|
131
|
-
@storage ||= Attached::Storage.storage(self.medium, self.credentials)
|
|
132
|
-
|
|
133
146
|
@storage.destroy(self.path) if self.path
|
|
134
147
|
end
|
|
135
148
|
|
|
@@ -143,9 +156,12 @@ module Attached
|
|
|
143
156
|
# @object.avatar.url(:large)
|
|
144
157
|
|
|
145
158
|
def url(style = self.default)
|
|
146
|
-
|
|
159
|
+
path = self.path(style)
|
|
160
|
+
|
|
161
|
+
host = self.host
|
|
162
|
+
host = self.aliases[path.hash % self.aliases.count] unless self.aliases.empty?
|
|
147
163
|
|
|
148
|
-
return "#{
|
|
164
|
+
return "#{host}#{path}"
|
|
149
165
|
end
|
|
150
166
|
|
|
151
167
|
|
data/lib/attached/storage.rb
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
require 'attached/storage/
|
|
1
|
+
require 'attached/storage/aws'
|
|
2
2
|
|
|
3
3
|
module Attached
|
|
4
4
|
module Storage
|
|
@@ -7,12 +7,12 @@ module Attached
|
|
|
7
7
|
#
|
|
8
8
|
# Usage:
|
|
9
9
|
#
|
|
10
|
-
# Attached::Storage.medium(
|
|
10
|
+
# Attached::Storage.medium(aws)
|
|
11
11
|
|
|
12
|
-
def self.storage(medium = :
|
|
12
|
+
def self.storage(medium = :aws, credentials = nil)
|
|
13
13
|
|
|
14
14
|
case medium
|
|
15
|
-
when :
|
|
15
|
+
when :aws then return Attached::Storage::AWS.new(credentials)
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
end
|
|
@@ -5,7 +5,7 @@ require 'aws/s3'
|
|
|
5
5
|
|
|
6
6
|
module Attached
|
|
7
7
|
module Storage
|
|
8
|
-
class
|
|
8
|
+
class AWS < Base
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
attr_reader :bucket
|
|
@@ -50,9 +50,9 @@ module Attached
|
|
|
50
50
|
def save(file, path)
|
|
51
51
|
connect()
|
|
52
52
|
begin
|
|
53
|
-
AWS::S3::S3Object.store(path, file, bucket, :access => :public_read)
|
|
53
|
+
::AWS::S3::S3Object.store(path, file, bucket, :access => :public_read)
|
|
54
54
|
rescue AWS::S3::NoSuchBucket => e
|
|
55
|
-
AWS::S3::Bucket.create(bucket)
|
|
55
|
+
::AWS::S3::Bucket.create(bucket)
|
|
56
56
|
retry
|
|
57
57
|
end
|
|
58
58
|
end
|
|
@@ -67,9 +67,9 @@ module Attached
|
|
|
67
67
|
def destroy(path)
|
|
68
68
|
connect()
|
|
69
69
|
begin
|
|
70
|
-
AWS::S3::S3Object.delete(path, bucket, :access => :authenticated_read)
|
|
70
|
+
::AWS::S3::S3Object.delete(path, bucket, :access => :authenticated_read)
|
|
71
71
|
rescue AWS::S3::NoSuchBucket => e
|
|
72
|
-
AWS::S3::Bucket.create(bucket)
|
|
72
|
+
::AWS::S3::Bucket.create(bucket)
|
|
73
73
|
retry
|
|
74
74
|
end
|
|
75
75
|
end
|
|
@@ -81,7 +81,7 @@ module Attached
|
|
|
81
81
|
# Connect to an AWS S3 server.
|
|
82
82
|
|
|
83
83
|
def connect()
|
|
84
|
-
@connection ||= AWS::S3::Base.establish_connection!(
|
|
84
|
+
@connection ||= ::AWS::S3::Base.establish_connection!(
|
|
85
85
|
:access_key_id => access_key_id, :secret_access_key => secret_access_key
|
|
86
86
|
)
|
|
87
87
|
end
|
metadata
CHANGED
|
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
|
5
5
|
segments:
|
|
6
6
|
- 0
|
|
7
7
|
- 1
|
|
8
|
-
-
|
|
9
|
-
version: 0.1.
|
|
8
|
+
- 5
|
|
9
|
+
version: 0.1.5
|
|
10
10
|
platform: ruby
|
|
11
11
|
authors:
|
|
12
12
|
- Kevin Sylvestre
|
|
@@ -14,7 +14,7 @@ autorequire:
|
|
|
14
14
|
bindir: bin
|
|
15
15
|
cert_chain: []
|
|
16
16
|
|
|
17
|
-
date: 2010-12-
|
|
17
|
+
date: 2010-12-17 00:00:00 -05:00
|
|
18
18
|
default_executable:
|
|
19
19
|
dependencies:
|
|
20
20
|
- !ruby/object:Gem::Dependency
|
|
@@ -57,8 +57,8 @@ files:
|
|
|
57
57
|
- lib/attached/image.rb
|
|
58
58
|
- lib/attached/processor.rb
|
|
59
59
|
- lib/attached/railtie.rb
|
|
60
|
+
- lib/attached/storage/aws.rb
|
|
60
61
|
- lib/attached/storage/base.rb
|
|
61
|
-
- lib/attached/storage/s3.rb
|
|
62
62
|
- lib/attached/storage.rb
|
|
63
63
|
- lib/attached.rb
|
|
64
64
|
- README.rdoc
|