czj-paperclipftp 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Damian Caruso
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.
data/README.rdoc ADDED
@@ -0,0 +1,40 @@
1
+ = PaperclipFTP
2
+
3
+ Ftp storage support for paperclip file attachment plugin.
4
+ Useful for CDNs with ftp access method for file uploading.
5
+
6
+ == Install
7
+
8
+ gem install paperclipftp
9
+
10
+
11
+ == Usage
12
+
13
+ Create the file config/paperclipftp.yml:
14
+
15
+ development:
16
+ host: domain.com
17
+ username: user
18
+ password: password
19
+
20
+ test:
21
+ ...
22
+
23
+ production:
24
+ ...
25
+
26
+ In your model:
27
+
28
+ class User < ActiveRecord::Base
29
+ has_attached_file :avatar,
30
+ :styles => { :medium => "300x300>", :thumb => "100x100>" },
31
+ :storage => :ftp,
32
+ :path => "/:attachment/:attachment/:id/:style/:filename"
33
+ :url => "http://domain.com/:attachment/:attachment/:id/:style/:filename"
34
+ end
35
+
36
+ You must add in the url option the web access to that file.
37
+
38
+ == Copyright
39
+
40
+ Copyright (c) 2010 Damian Caruso. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "czj-paperclipftp"
8
+ gem.summary = %Q{FTP storage support for Paperclip gem}
9
+ gem.description = %Q{Paperclip storage using FTP servers as storage medium}
10
+ gem.email = "clement.joubert@gmail.com"
11
+ gem.homepage = "http://github.com/czj/paperclipftp"
12
+ gem.authors = ["Clément Joubert"]
13
+ gem.files = FileList['lib/**/*.rb', '[A-Z]*', 'test/**/*'].to_a
14
+ gem.add_dependency "paperclip", ">= 2.3.8"
15
+ # gem.add_development_dependency "yard", ">= 0"
16
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
21
+ end
22
+
23
+ require 'rake/testtask'
24
+ Rake::TestTask.new(:test) do |test|
25
+ test.libs << 'lib' << 'test'
26
+ test.pattern = 'test/**/*_test.rb'
27
+ test.verbose = true
28
+ end
29
+
30
+ begin
31
+ require 'rcov/rcovtask'
32
+ Rcov::RcovTask.new do |test|
33
+ test.libs << 'test'
34
+ test.pattern = 'test/**/*_test.rb'
35
+ test.verbose = true
36
+ end
37
+ rescue LoadError
38
+ task :rcov do
39
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
40
+ end
41
+ end
42
+
43
+ task :test => :check_dependencies
44
+
45
+ task :default => :test
46
+ #
47
+ # begin
48
+ # require 'yard'
49
+ # YARD::Rake::YardocTask.new
50
+ # rescue LoadError
51
+ # task :yardoc do
52
+ # abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
53
+ # end
54
+ # end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.2
@@ -0,0 +1,90 @@
1
+ module Paperclip
2
+ module Storage
3
+ module Ftp
4
+ def self.extended base
5
+ require 'net/ftp'
6
+ base.instance_eval do
7
+ @ftp_credentials = parse_credentials
8
+ end
9
+ end
10
+
11
+ def ftp
12
+ return @ftp unless @ftp.nil? || @ftp.closed?
13
+ @ftp = Net::FTP.new(@ftp_credentials[:host])
14
+ @ftp.login(@ftp_credentials[:username], @ftp_credentials[:password])
15
+ @ftp.passive = @ftp_credentials[:passive] || true
16
+ @ftp
17
+ end
18
+
19
+ def exists?(style = default_style)
20
+ move_to_remote_path(File.dirname(path(style)))
21
+ not ftp.size(File.basename(path(style))).zero?
22
+ rescue Net::FTPPermError => e
23
+ #File not exists
24
+ false
25
+ rescue Net::FTPReplyError => e
26
+ ftp.close
27
+ raise e
28
+ end
29
+
30
+ def to_file style = default_style
31
+ @queued_for_write[style] || "ftp://#{path(style)}"
32
+ end
33
+ alias_method :to_io, :to_file
34
+
35
+ def flush_writes
36
+ @queued_for_write.each do |style, file|
37
+ file.close
38
+ move_to_remote_path(File.dirname(path(style)))
39
+ log("FTP Uploading #{path(style)}")
40
+ ftp.putbinaryfile(file.path, File.basename(path(style)))
41
+ end
42
+ @queued_for_write = {}
43
+ rescue Net::FTPReplyError => e
44
+ raise e
45
+ rescue Net::FTPPermError => e
46
+ raise e
47
+ ensure
48
+ ftp.close
49
+ end
50
+
51
+ def flush_deletes
52
+ @queued_for_delete.each do |path|
53
+ begin
54
+ move_to_remote_path(File.dirname(path))
55
+ log("FTP deleting #{path}")
56
+ ftp.delete(File.basename(path))
57
+ rescue Net::FTPPermError, Net::FTPReplyError
58
+ end
59
+ end
60
+ @queued_for_delete = []
61
+ rescue Net::FTPReplyError => e
62
+ raise e
63
+ rescue Net::FTPPermError => e
64
+ raise e
65
+ ensure
66
+ ftp.close
67
+ end
68
+
69
+ def move_to_remote_path(rpath)
70
+ ftp.chdir("/")
71
+ rpath.split(File::SEPARATOR).each do |rdir|
72
+ rdir = rdir.strip
73
+ unless rdir.blank?
74
+ list = ftp.ls.collect { |f| f.split.last }
75
+ unless list.include?(rdir)
76
+ ftp.mkdir(rdir)
77
+ end
78
+ ftp.chdir(rdir)
79
+ end
80
+ end
81
+ end
82
+
83
+ def parse_credentials
84
+ creds = YAML.load_file(File.join(Rails.root,"config","paperclipftp.yml")).stringify_keys
85
+ (creds[Rails.env] || creds).symbolize_keys
86
+ end
87
+ private :parse_credentials
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class PaperclipftpTest < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'paperclipftp'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: czj-paperclipftp
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 2
10
+ version: 0.1.2
11
+ platform: ruby
12
+ authors:
13
+ - "Cl\xC3\xA9ment Joubert"
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-04 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: paperclip
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 19
30
+ segments:
31
+ - 2
32
+ - 3
33
+ - 8
34
+ version: 2.3.8
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ description: Paperclip storage using FTP servers as storage medium
38
+ email: clement.joubert@gmail.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - LICENSE
45
+ - README.rdoc
46
+ files:
47
+ - LICENSE
48
+ - README.rdoc
49
+ - Rakefile
50
+ - VERSION
51
+ - lib/paperclipftp.rb
52
+ - test/paperclipftp_test.rb
53
+ - test/test_helper.rb
54
+ has_rdoc: true
55
+ homepage: http://github.com/czj/paperclipftp
56
+ licenses: []
57
+
58
+ post_install_message:
59
+ rdoc_options: []
60
+
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ hash: 3
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ requirements: []
82
+
83
+ rubyforge_project:
84
+ rubygems_version: 1.5.0
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: FTP storage support for Paperclip gem
88
+ test_files:
89
+ - test/paperclipftp_test.rb
90
+ - test/test_helper.rb