erikhansson-paperclip_cloudfiles 0.0.2

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.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Erik Hansson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,71 @@
1
+ = paperclip_cloudfiles
2
+
3
+ Enables CloudFiles storage for Paperclip. Also provides a simple mechanism
4
+ for defining default settings for Paperclip attachments. Be warned that this
5
+ project is largely untested. It has worked for me in a limited number of
6
+ specific cases, but as experience shows, this is no guarantee of general
7
+ soundness. You've been warned.
8
+
9
+
10
+ == Installation
11
+
12
+ sudo gem install erikhansson-paperclip_cloudfiles
13
+
14
+
15
+ == Setup
16
+
17
+ environment.rb
18
+
19
+ config.gem 'erikhansson-paperclip_cloudfiles', :lib => 'paperclip_cloudfiles', :source => 'http://gems.github.com'
20
+
21
+ production.rb (or some other source of configuration)
22
+
23
+ Paperclip::WithDefault.use_cloudfiles_options :cloudfiles => {
24
+ :credentials => {
25
+ :username => 'username',
26
+ :api_key => 'abcdefghijklmnopqrstuvxyz1234567'
27
+ },
28
+ :container => 'container_name',
29
+ :base_url => 'http://xxxxyyyy.cdn.cloudfiles.rackspacecloud.com'
30
+ }
31
+
32
+
33
+ == Usage
34
+
35
+ class Model < ActiveRecord::Base
36
+
37
+ has_attached_file_with_defaults :image, :styles => { :display => "675>" }
38
+
39
+ end
40
+
41
+
42
+ == What's going on?
43
+
44
+ has_attached_file_with_defaults simply calls the familiar Paperclip
45
+ has_attached_file method with a set of default options, found in
46
+ Paperclip::WithDefault.options.
47
+
48
+ Paperclip::WithDefault.use_cloudfiles_options, sets this default
49
+ options hash to some convenient default settings for CloudFiles. You
50
+ can also set it directly using Paperclip::WithDefault.options=
51
+
52
+ You can also set different default settings for the test and development
53
+ environments. In fact, the test environment automagically gets new
54
+ default settings { :url => '/system_test/:attachment/:id/:style/:filename' }
55
+ to keep files uploaded during tests to comingle with actual uploaded files.
56
+
57
+
58
+ == Note on Patches/Pull Requests
59
+
60
+ * Fork the project.
61
+ * Make your feature addition or bug fix.
62
+ * Add tests for it. This is important so I don't break it in a
63
+ future version unintentionally.
64
+ * Commit, do not mess with rakefile, version, or history.
65
+ (if you want to have your own version, that is fine but
66
+ bump version in a commit by itself I can ignore when I pull)
67
+ * Send me a pull request. Bonus points for topic branches.
68
+
69
+ == Copyright
70
+
71
+ Copyright (c) 2009 Erik Hansson. See LICENSE for details.
@@ -0,0 +1,49 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "paperclip_cloudfiles"
8
+ gem.summary = %Q{Cloudfiles storage for Paperclip}
9
+ gem.description = %Q{Provides a :cloudfiles storage options for Paperclip.}
10
+ gem.email = "erik@bits2life.com"
11
+ gem.homepage = "http://github.com/erikhansson/paperclip_cloudfiles"
12
+ gem.authors = ["Erik Hansson"]
13
+ gem.add_dependency "rackspace-cloudfiles"
14
+ gem.add_development_dependency "rspec"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ if File.exist?('VERSION')
40
+ version = File.read('VERSION')
41
+ else
42
+ version = ""
43
+ end
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "paperclip_cloudfiles #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.2
@@ -0,0 +1,103 @@
1
+
2
+ module Paperclip
3
+ module Storage
4
+
5
+ # Alternative Storage for Paperclip, using Rackspace's CloudFiles storage
6
+ # service. All files will be saved to a specified CDN-enabled container.
7
+ module Cloudfiles
8
+
9
+ def self.extended base
10
+ begin
11
+ require 'cloudfiles'
12
+ rescue LoadError => e
13
+ e.message << " (You may need to install the rackspace-cloudfiles gem)"
14
+ raise e
15
+ end
16
+
17
+ base.instance_eval do
18
+ @cf_credentials = @options[:cloudfiles][:credentials]
19
+ @cf_container_name = @options[:cloudfiles][:container]
20
+ @cf_base_url = @options[:cloudfiles][:base_url]
21
+ end
22
+
23
+ Paperclip.interpolates(:cf_base_url) do |attachment, style|
24
+ "#{cf_base_url}"
25
+ end
26
+ end
27
+
28
+ def cf_connection
29
+ # TODO: Ensure that this works acceptably. A new CloudFiles::Connection and
30
+ # container instance will be created for each attachment when the corresponding
31
+ # methods are called. Those could probably be shared for all instances of the
32
+ # same attachment for the process.
33
+
34
+ @cf_connection ||= CloudFiles::Connection.new(@cf_credentials[:username], @cf_credentials[:api_key])
35
+ end
36
+
37
+ def cf_container
38
+ @cf_container ||= cf_connection.container(cf_container_name)
39
+ end
40
+
41
+ def cf_container_name
42
+ @cf_container_name
43
+ end
44
+
45
+ def cf_base_url
46
+ @cf_base_url ||= cf_container.cdn_url
47
+ end
48
+
49
+ # Checks wether this attachment exists in the given style.
50
+ def exists?(style = default_style)
51
+ if original_filename
52
+ cf_container.object_exists?(path(style))
53
+ else
54
+ false
55
+ end
56
+ end
57
+
58
+ # Returns representation of the data of the file assigned to the given
59
+ # style, in the format most representative of the current storage.
60
+ def to_file style = default_style
61
+ return @queued_for_write[style] if @queued_for_write[style]
62
+ file = Tempfile.new(path(style))
63
+ file.write(cf_container.object(path(style)).value)
64
+ file.rewind
65
+ return file
66
+ end
67
+
68
+ def flush_writes #:nodoc:
69
+ @queued_for_write.each do |style, file|
70
+ begin
71
+ log("saving to cloudfiles #{path(style)}")
72
+ obj = cf_container.create_object(path(style))
73
+ obj.write(file, cf_headers)
74
+ rescue StandardError => e
75
+ raise
76
+ end
77
+ end
78
+ @queued_for_write = {}
79
+ end
80
+
81
+ # Headers sent to Cloudfiles on upload. Currently used to provide
82
+ # a Content-Type header if one is available.
83
+ def cf_headers #:nodoc:
84
+ result = {}
85
+ if content_type = instance_read(:content_type)
86
+ result['Content-Type'] = content_type
87
+ end
88
+ result
89
+ end
90
+
91
+ def flush_deletes #:nodoc:
92
+ @queued_for_delete.each do |path|
93
+ begin
94
+ log("deleting from cloudfiles #{path}")
95
+ cf_container.delete(path)
96
+ end
97
+ end
98
+ @queued_for_delete = []
99
+ end
100
+
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,31 @@
1
+ module Paperclip
2
+ class WithDefault
3
+ cattr_accessor :options
4
+
5
+ # Default options for CloudFiles.
6
+ def self.cloudfiles_options
7
+ @cloudfiles_options ||= {
8
+ :url => ":cf_base_url/:path",
9
+ :path => "attachments/:class/:attachment/:id/:style/:filename",
10
+ :storage => :cloudfiles
11
+ }
12
+ end
13
+
14
+ # Sets Paperclip::WithDefault.options to the given option
15
+ # hash merged with Paperclip::WithDefault.cloudfiles_options
16
+ def self.use_cloudfiles_options(options)
17
+ Paperclip::WithDefault.options = cloudfiles_options.merge(options)
18
+ end
19
+
20
+
21
+ module ClassMethods
22
+
23
+ # Simply calls has_attached_file with the default values found in
24
+ # Paperclip::WitDefault.options merged into options.
25
+ def has_attached_file_with_defaults(name, options = {})
26
+ has_attached_file name, Paperclip::WithDefault.options.merge(options)
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,17 @@
1
+ require 'paperclip/storage/cloudfiles'
2
+ require 'paperclip/with_default'
3
+
4
+
5
+ # Make has_attached_file_with_defaults available in ActiveRecord::Base
6
+ if Object.const_defined?("ActiveRecord")
7
+ ActiveRecord::Base.extend Paperclip::WithDefault::ClassMethods
8
+ end
9
+
10
+ # Provide default options, primarily for development and test environments.
11
+ if Rails.env == 'development' || Rails.env == 'production'
12
+ Paperclip::WithDefault.options ||= {}
13
+ elsif Rails.env == 'test'
14
+ Paperclip::WithDefault.options ||= {
15
+ :url => '/system_test/:attachment/:id/:style/:filename'
16
+ }
17
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "PaperclipCloudfiles" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'paperclip_cloudfiles'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: erikhansson-paperclip_cloudfiles
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Erik Hansson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-22 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rackspace-cloudfiles
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ description: Provides a :cloudfiles storage options for Paperclip.
36
+ email: erik@bits2life.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.rdoc
44
+ files:
45
+ - .document
46
+ - .gitignore
47
+ - LICENSE
48
+ - README.rdoc
49
+ - Rakefile
50
+ - VERSION
51
+ - lib/paperclip/storage/cloudfiles.rb
52
+ - lib/paperclip/with_default.rb
53
+ - lib/paperclip_cloudfiles.rb
54
+ - spec/paperclip_cloudfiles_spec.rb
55
+ - spec/spec_helper.rb
56
+ has_rdoc: true
57
+ homepage: http://github.com/erikhansson/paperclip_cloudfiles
58
+ licenses:
59
+ post_install_message:
60
+ rdoc_options:
61
+ - --charset=UTF-8
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: "0"
75
+ version:
76
+ requirements: []
77
+
78
+ rubyforge_project:
79
+ rubygems_version: 1.3.5
80
+ signing_key:
81
+ specification_version: 2
82
+ summary: Cloudfiles storage for Paperclip
83
+ test_files:
84
+ - spec/paperclip_cloudfiles_spec.rb
85
+ - spec/spec_helper.rb