paperclip_cloud_storage 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +24 -0
- data/README.rdoc +58 -0
- data/Rakefile +2 -0
- data/init.rb +2 -0
- data/lib/cloudfile.rb +6 -0
- data/lib/paperclip/storage/cloudfile.rb +140 -0
- metadata +95 -0
data/LICENSE
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
|
2
|
+
LICENSE
|
3
|
+
|
4
|
+
The MIT License
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
8
|
+
in the Software without restriction, including without limitation the rights
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
11
|
+
furnished to do so, subject to the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be included in
|
14
|
+
all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22
|
+
THE SOFTWARE.
|
23
|
+
|
24
|
+
|
data/README.rdoc
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
==Paperclip Cloud Storage
|
2
|
+
|
3
|
+
Paperclips is a great way to save files in a rails app. With this gem you
|
4
|
+
can have the files saved to your Rackspace Cloud account.
|
5
|
+
|
6
|
+
==Credits
|
7
|
+
|
8
|
+
Most of the cloudfile.rb code was written by http://github.com/minter/paperclip.
|
9
|
+
|
10
|
+
==Usage
|
11
|
+
|
12
|
+
In your model:
|
13
|
+
|
14
|
+
class User < ActiveRecord::Base
|
15
|
+
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" },
|
16
|
+
:storage => :cloudfile
|
17
|
+
end
|
18
|
+
|
19
|
+
== Configuration
|
20
|
+
|
21
|
+
In your config directory make a file called paperclip_cloud_storage.yml
|
22
|
+
|
23
|
+
You can 'environment-space' this just like you do to your database.yml file, so different environments can use different accounts:
|
24
|
+
|
25
|
+
development:
|
26
|
+
username: hayley
|
27
|
+
api_key: a7f...
|
28
|
+
test:
|
29
|
+
username: katherine
|
30
|
+
api_key: 7fa...
|
31
|
+
production:
|
32
|
+
username: minter
|
33
|
+
api_key: 87k...
|
34
|
+
servicenet: true
|
35
|
+
|
36
|
+
This is not required to, however, and the file may simply look like this:
|
37
|
+
username: minter...
|
38
|
+
api_key: 11q...
|
39
|
+
|
40
|
+
In which case, those access keys will be used in all environments. You can also
|
41
|
+
put your container name in this file, instead of adding it to the code directly.
|
42
|
+
This is useful when you want the same account but a different container for
|
43
|
+
development versus production.
|
44
|
+
|
45
|
+
* +container+: This is the name of the Cloud Files container that will store your files.
|
46
|
+
This container should be marked "public" so that the files are available to the world at large.
|
47
|
+
If the container does not exist, it will be created and marked public.
|
48
|
+
|
49
|
+
* +path+: This is the path under the container in which the file will be stored. The
|
50
|
+
CDN URL will be constructed from the CDN identifier for the container and the path. This is what
|
51
|
+
you will want to interpolate. Keys should be unique, like filenames, and despite the fact that
|
52
|
+
Cloud Files (strictly speaking) does not support directories, you can still use a / to
|
53
|
+
separate parts of your file name, and they will show up in the URL structure.
|
54
|
+
|
55
|
+
== Test
|
56
|
+
|
57
|
+
Coming soon needed for project now. If you would like to contribute super, and thanks.
|
58
|
+
|
data/Rakefile
ADDED
data/init.rb
ADDED
data/lib/cloudfile.rb
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
module Paperclip
|
2
|
+
module Storage
|
3
|
+
# Rackspace's Cloud Files service is a scalable, easy place to store files for
|
4
|
+
# distribution, and is integrated into the Limelight CDN. You can find out more about
|
5
|
+
# it at http://www.rackspacecloud.com/cloud_hosting_products/files
|
6
|
+
#
|
7
|
+
#
|
8
|
+
# To install the Cloud Files gem, add the Gemcutter gem source ("gem sources -a http://gemcutter.org"), then
|
9
|
+
# do a "gem install paperclip_cloud_storage". For more information, see the github repository at http://github.com/ciddennis/cloudfiles-paperclip-storage
|
10
|
+
#
|
11
|
+
# There are a few Cloud Files-specific options for has_attached_file:
|
12
|
+
# * +cloudfiles_credentials+: Takes a path, a File, or a Hash. The path (or File) must point
|
13
|
+
# to a YAML file containing the +username+ and +api_key+ that Rackspace
|
14
|
+
# gives you. Rackspace customers using the cloudfiles gem >= 1.4.1 can also set a servicenet
|
15
|
+
# variable to true to send traffic over the unbilled internal Rackspace service network.
|
16
|
+
# You can 'environment-space' this just like you do to your
|
17
|
+
# database.yml file, so different environments can use different accounts:
|
18
|
+
# development:
|
19
|
+
# username: hayley
|
20
|
+
# api_key: a7f...
|
21
|
+
# test:
|
22
|
+
# username: katherine
|
23
|
+
# api_key: 7fa...
|
24
|
+
# production:
|
25
|
+
# username: minter
|
26
|
+
# api_key: 87k...
|
27
|
+
# servicenet: true
|
28
|
+
# This is not required, however, and the file may simply look like this:
|
29
|
+
# username: minter...
|
30
|
+
# api_key: 11q...
|
31
|
+
# In which case, those access keys will be used in all environments. You can also
|
32
|
+
# put your container name in this file, instead of adding it to the code directly.
|
33
|
+
# This is useful when you want the same account but a different container for
|
34
|
+
# development versus production.
|
35
|
+
# * +container+: This is the name of the Cloud Files container that will store your files.
|
36
|
+
# This container should be marked "public" so that the files are available to the world at large.
|
37
|
+
# If the container does not exist, it will be created and marked public.
|
38
|
+
# * +path+: This is the path under the container in which the file will be stored. The
|
39
|
+
# CDN URL will be constructed from the CDN identifier for the container and the path. This is what
|
40
|
+
# you will want to interpolate. Keys should be unique, like filenames, and despite the fact that
|
41
|
+
# Cloud Files (strictly speaking) does not support directories, you can still use a / to
|
42
|
+
# separate parts of your file name, and they will show up in the URL structure.
|
43
|
+
module Cloudfile
|
44
|
+
def self.extended base
|
45
|
+
begin
|
46
|
+
require 'cloudfiles'
|
47
|
+
rescue LoadError => e
|
48
|
+
e.message << " (You may need to install the cloudfiles gem)"
|
49
|
+
raise e
|
50
|
+
end
|
51
|
+
@@container ||= {}
|
52
|
+
base.instance_eval do
|
53
|
+
# If they were not passed in then default to yml config file.
|
54
|
+
if(!@options[:cloudfiles_credentials])
|
55
|
+
@options[:cloudfiles_credentials] = Rails.root.to_s + "/config/paperclip_cloud_storage.yml"
|
56
|
+
end
|
57
|
+
|
58
|
+
@cloudfiles_credentials = parse_credentials(@options[:cloudfiles_credentials])
|
59
|
+
@container_name = @options[:container] || @cloudfiles_credentials[:container]
|
60
|
+
@container_name = @container_name.call(self) if @container_name.is_a?(Proc)
|
61
|
+
@@cdn_url = cloudfiles_container.cdn_url
|
62
|
+
@path_filename = ":cf_path_filename" unless @url.to_s.match(/^:cf.*filename$/)
|
63
|
+
@url = @@cdn_url + "/#{URI.encode(@path_filename).gsub(/&/, '%26')}"
|
64
|
+
@path = (Paperclip::Attachment.default_options[:path] == @options[:path]) ? ":attachment/:id/:style/:basename.:extension" : @options[:path]
|
65
|
+
end
|
66
|
+
Paperclip.interpolates(:cf_path_filename) do |attachment, style|
|
67
|
+
attachment.path(style)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def cloudfiles
|
72
|
+
@@cf ||= CloudFiles::Connection.new(:username => @cloudfiles_credentials[:username], :api_key => @cloudfiles_credentials[:api_key], :snet => @cloudfiles_credentials[:servicenet])
|
73
|
+
@@cf
|
74
|
+
end
|
75
|
+
|
76
|
+
def create_container
|
77
|
+
container = cloudfiles.create_container(@container_name)
|
78
|
+
container.make_public
|
79
|
+
container
|
80
|
+
end
|
81
|
+
|
82
|
+
def cloudfiles_container
|
83
|
+
@@container[@container_name] ||= create_container
|
84
|
+
end
|
85
|
+
|
86
|
+
def container_name
|
87
|
+
@container_name
|
88
|
+
end
|
89
|
+
|
90
|
+
def parse_credentials creds
|
91
|
+
creds = find_credentials(creds).stringify_keys
|
92
|
+
(creds[Rails.env] || creds).symbolize_keys
|
93
|
+
end
|
94
|
+
|
95
|
+
def exists?(style = default_style)
|
96
|
+
cloudfiles_container.object_exists?(path(style))
|
97
|
+
end
|
98
|
+
|
99
|
+
# Returns representation of the data of the file assigned to the given
|
100
|
+
# style, in the format most representative of the current storage.
|
101
|
+
def to_file style = default_style
|
102
|
+
@queued_for_write[style] || cloudfiles_container.create_object(path(style))
|
103
|
+
end
|
104
|
+
|
105
|
+
alias_method :to_io, :to_file
|
106
|
+
|
107
|
+
def flush_writes #:nodoc:
|
108
|
+
@queued_for_write.each do |style, file|
|
109
|
+
object = cloudfiles_container.create_object(path(style), false)
|
110
|
+
object.load_from_filename(file)
|
111
|
+
end
|
112
|
+
@queued_for_write = {}
|
113
|
+
end
|
114
|
+
|
115
|
+
def flush_deletes #:nodoc:
|
116
|
+
@queued_for_delete.each do |path|
|
117
|
+
cloudfiles_container.delete_object(path)
|
118
|
+
end
|
119
|
+
@queued_for_delete = []
|
120
|
+
end
|
121
|
+
|
122
|
+
def find_credentials creds
|
123
|
+
case creds
|
124
|
+
when File
|
125
|
+
YAML.load_file(creds.path)
|
126
|
+
when String
|
127
|
+
YAML.load_file(creds)
|
128
|
+
when Hash
|
129
|
+
creds
|
130
|
+
else
|
131
|
+
raise ArgumentError, "Credentials are not a path, file, or hash."
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
private :find_credentials
|
136
|
+
|
137
|
+
end
|
138
|
+
|
139
|
+
end
|
140
|
+
end
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: paperclip_cloud_storage
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
version: "0.1"
|
9
|
+
platform: ruby
|
10
|
+
authors:
|
11
|
+
- Cid Dennis
|
12
|
+
autorequire:
|
13
|
+
bindir: bin
|
14
|
+
cert_chain: []
|
15
|
+
|
16
|
+
date: 2010-09-27 00:00:00 -06:00
|
17
|
+
default_executable:
|
18
|
+
dependencies:
|
19
|
+
- !ruby/object:Gem::Dependency
|
20
|
+
name: cloudfiles
|
21
|
+
prerelease: false
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
type: :runtime
|
31
|
+
version_requirements: *id001
|
32
|
+
- !ruby/object:Gem::Dependency
|
33
|
+
name: paperclip
|
34
|
+
prerelease: false
|
35
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
segments:
|
41
|
+
- 0
|
42
|
+
version: "0"
|
43
|
+
type: :runtime
|
44
|
+
version_requirements: *id002
|
45
|
+
description: Store Paperclip files on Rackspace CloudFiles
|
46
|
+
email: cid.dennis@gmail.com
|
47
|
+
executables: []
|
48
|
+
|
49
|
+
extensions: []
|
50
|
+
|
51
|
+
extra_rdoc_files:
|
52
|
+
- README.rdoc
|
53
|
+
files:
|
54
|
+
- README.rdoc
|
55
|
+
- LICENSE
|
56
|
+
- Rakefile
|
57
|
+
- init.rb
|
58
|
+
- lib/cloudfile.rb
|
59
|
+
- lib/paperclip/storage/cloudfile.rb
|
60
|
+
has_rdoc: true
|
61
|
+
homepage: http://github.com/ciddennis/cloudfiles-paperclip-storage
|
62
|
+
licenses: []
|
63
|
+
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options:
|
66
|
+
- --line-numbers
|
67
|
+
- --inline-source
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
segments:
|
76
|
+
- 0
|
77
|
+
version: "0"
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
segments:
|
84
|
+
- 0
|
85
|
+
version: "0"
|
86
|
+
requirements:
|
87
|
+
- paperclip
|
88
|
+
- cloudfiles
|
89
|
+
rubyforge_project: paperclip_cloud_storage
|
90
|
+
rubygems_version: 1.3.7
|
91
|
+
signing_key:
|
92
|
+
specification_version: 3
|
93
|
+
summary: Store Paperclip files on Rackspace CloudFiles
|
94
|
+
test_files: []
|
95
|
+
|