qoobaa-s3 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/extras/s3_backend.rb +127 -0
- data/lib/s3/object.rb +1 -1
- data/s3.gemspec +2 -1
- metadata +2 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.4
|
@@ -0,0 +1,127 @@
|
|
1
|
+
require "singleton"
|
2
|
+
require "s3"
|
3
|
+
|
4
|
+
module Technoweenie
|
5
|
+
module AttachmentFu
|
6
|
+
module Backends
|
7
|
+
module S3Backend
|
8
|
+
class Configuration
|
9
|
+
include Singleton
|
10
|
+
|
11
|
+
ATTRIBUTES = [:access_key_id, :secret_access_key, :use_ssl, :bucket_name]
|
12
|
+
|
13
|
+
attr_accessor *ATTRIBUTES
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.configuration
|
17
|
+
if block_given?
|
18
|
+
yield Configuration.instance
|
19
|
+
end
|
20
|
+
Configuration.instance
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.included(base)
|
24
|
+
include S3
|
25
|
+
|
26
|
+
service = Service.new(:access_key_id => configuration.access_key_id,
|
27
|
+
:secret_access_key => configuration.secret_access_key,
|
28
|
+
:use_ssl => configuration.use_ssl)
|
29
|
+
|
30
|
+
bucket_name = base.attachment_options[:bucket_name] || configuration.bucket_name
|
31
|
+
|
32
|
+
base.cattr_accessor :bucket
|
33
|
+
base.bucket = service.buckets.find(bucket_name)
|
34
|
+
|
35
|
+
base.before_update :rename_file
|
36
|
+
end
|
37
|
+
|
38
|
+
# The attachment ID used in the full path of a file
|
39
|
+
def attachment_path_id
|
40
|
+
((respond_to?(:parent_id) && parent_id) || id).to_s
|
41
|
+
end
|
42
|
+
|
43
|
+
# The pseudo hierarchy containing the file relative to the bucket name
|
44
|
+
# Example: <tt>:table_name/:id</tt>
|
45
|
+
def base_path
|
46
|
+
[attachment_options[:path_prefix], attachment_path_id].join("/")
|
47
|
+
end
|
48
|
+
|
49
|
+
# The full path to the file relative to the bucket name
|
50
|
+
# Example: <tt>:table_name/:id/:filename</tt>
|
51
|
+
def full_filename(thumbnail = nil)
|
52
|
+
[base_path, thumbnail_name_for(thumbnail)].join("/")
|
53
|
+
end
|
54
|
+
|
55
|
+
# All public objects are accessible via a GET request to the S3 servers. You can generate a
|
56
|
+
# url for an object using the s3_url method.
|
57
|
+
#
|
58
|
+
# @photo.s3_url
|
59
|
+
#
|
60
|
+
# The resulting url is in the form: <tt>http(s)://:server/:bucket_name/:table_name/:id/:file</tt> where
|
61
|
+
# the <tt>:server</tt> variable defaults to <tt>AWS::S3 URL::DEFAULT_HOST</tt> (s3.amazonaws.com) and can be
|
62
|
+
# set using the configuration parameters in <tt>RAILS_ROOT/config/amazon_s3.yml</tt>.
|
63
|
+
#
|
64
|
+
# The optional thumbnail argument will output the thumbnail's filename (if any).
|
65
|
+
def s3_url(thumbnail = nil)
|
66
|
+
if attachment_options[:cname]
|
67
|
+
["#{s3_protocol}#{bucket.name}", full_filename(thumbnail)].join("/")
|
68
|
+
else
|
69
|
+
["#{s3_protocol}#{s3_hostname}#{bucket.path_prefix}", full_filename(thumbnail)].join("/")
|
70
|
+
end
|
71
|
+
end
|
72
|
+
alias :public_url :s3_url
|
73
|
+
alias :public_filename :s3_url
|
74
|
+
|
75
|
+
def bucket_name
|
76
|
+
self.class.bucket.name
|
77
|
+
end
|
78
|
+
|
79
|
+
def create_temp_file
|
80
|
+
write_to_temp_file current_data
|
81
|
+
end
|
82
|
+
|
83
|
+
def current_data
|
84
|
+
S3Object.value full_filename, bucket_name
|
85
|
+
end
|
86
|
+
|
87
|
+
def s3_protocol
|
88
|
+
attachment_options[:use_ssl] ? "https://" : "http://"
|
89
|
+
end
|
90
|
+
|
91
|
+
def s3_hostname
|
92
|
+
attachment_options[:cname] ? self.class.bucket.name : self.class.bucket.host
|
93
|
+
end
|
94
|
+
|
95
|
+
protected
|
96
|
+
|
97
|
+
def destroy_file
|
98
|
+
object = self.class.bucket.find(full_filename)
|
99
|
+
object.destroy
|
100
|
+
end
|
101
|
+
|
102
|
+
def rename_file
|
103
|
+
return unless filename_changed?
|
104
|
+
|
105
|
+
old_full_filename = [base_path, filename_was].join("/")
|
106
|
+
|
107
|
+
object = self.class.bucket.find(old_full_filename)
|
108
|
+
new_object = object.copy(:key => full_filename, :acl => attachment_options[:acl])
|
109
|
+
object.destroy
|
110
|
+
true
|
111
|
+
end
|
112
|
+
|
113
|
+
def save_to_storage
|
114
|
+
if save_attachment?
|
115
|
+
object = self.class.bucket.objects.build(full_filename)
|
116
|
+
|
117
|
+
object.content_type = content_type
|
118
|
+
object.acl = attachment_options[:acl]
|
119
|
+
object.content = temp_path ? File.open(temp_path) : temp_data
|
120
|
+
object.save
|
121
|
+
end
|
122
|
+
true
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
data/lib/s3/object.rb
CHANGED
data/s3.gemspec
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{s3}
|
5
|
-
s.version = "0.0.
|
5
|
+
s.version = "0.0.4"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Jakub Kuźma", "Mirosław Boruta"]
|
@@ -22,6 +22,7 @@ Gem::Specification.new do |s|
|
|
22
22
|
"Rakefile",
|
23
23
|
"VERSION",
|
24
24
|
"bin/s3cmd.rb",
|
25
|
+
"extras/s3_backend.rb",
|
25
26
|
"lib/s3.rb",
|
26
27
|
"lib/s3/bucket.rb",
|
27
28
|
"lib/s3/connection.rb",
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: qoobaa-s3
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "Jakub Ku\xC5\xBAma"
|
@@ -31,6 +31,7 @@ files:
|
|
31
31
|
- Rakefile
|
32
32
|
- VERSION
|
33
33
|
- bin/s3cmd.rb
|
34
|
+
- extras/s3_backend.rb
|
34
35
|
- lib/s3.rb
|
35
36
|
- lib/s3/bucket.rb
|
36
37
|
- lib/s3/connection.rb
|