paperclipftp 0.1.0

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) 2009 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 = "paperclipftp"
8
+ gem.summary = %Q{Ftp storage support for paperclip file attachment}
9
+ gem.description = %Q{Ftp storage support for paperclip file attachment}
10
+ gem.email = "damian.caruso@gmail.com"
11
+ gem.homepage = "http://github.com/cdamian/paperclipftp"
12
+ gem.authors = ["Damian Caruso"]
13
+ gem.files = FileList['lib/**/*.rb', '[A-Z]*', 'test/**/*'].to_a
14
+ gem.add_dependency "paperclip", ">= 2.3.0"
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.0
@@ -0,0 +1,91 @@
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
+ if @ftp.nil? || @ftp.closed?
13
+ @ftp = Net::FTP.new(@ftp_credentials[:host], @ftp_credentials[:username], @ftp_credentials[:password])
14
+ end
15
+ @ftp
16
+ end
17
+
18
+ def exists?(style = default_style)
19
+ move_to_remote_path(File.dirname(path(style)))
20
+ ftp.size(File.basename(path(style))) > 0 ? true : false
21
+ rescue Net::FTPPermError => e
22
+ #File not exists
23
+ false
24
+ rescue Net::FTPReplyError => e
25
+ ftp.close
26
+ raise e
27
+ end
28
+
29
+ def to_file style = default_style
30
+ @queued_for_write[style] || "ftp://#{path(style)}"
31
+ end
32
+ alias_method :to_io, :to_file
33
+
34
+ def flush_writes
35
+ @queued_for_write.each do |style, file|
36
+ file.close
37
+ move_to_remote_path(File.dirname(path(style)))
38
+ log("uploading #{path(style)}")
39
+ ftp.putbinaryfile(file.path, File.basename(path(style)))
40
+ end
41
+ @queued_for_write = {}
42
+ rescue Net::FTPReplyError => e
43
+ raise e
44
+ rescue Net::FTPPermError => e
45
+ raise e
46
+ ensure
47
+ ftp.close
48
+ end
49
+
50
+ def flush_deletes
51
+ @queued_for_delete.each do |path|
52
+ begin
53
+ move_to_remote_path(File.dirname(path))
54
+ log("deleting #{path}")
55
+ ftp.delete(File.basename(path))
56
+ rescue Net::FTPPermError, Net::FTPReplyError
57
+ end
58
+ end
59
+ @queued_for_delete = []
60
+ rescue Net::FTPReplyError => e
61
+ raise e
62
+ rescue Net::FTPPermError => e
63
+ raise e
64
+ ensure
65
+ ftp.close
66
+ end
67
+
68
+ def move_to_remote_path(rpath)
69
+ ftp.chdir("/")
70
+ rpath.split(File::SEPARATOR).each do |rdir|
71
+ rdir = rdir.strip
72
+ unless rdir.blank?
73
+ list = ftp.ls.collect { |f| f.split.last }
74
+ unless list.include?(rdir)
75
+ ftp.mkdir(rdir)
76
+ end
77
+ ftp.chdir(rdir)
78
+ end
79
+ end
80
+ end
81
+
82
+ def parse_credentials
83
+ creds = YAML.load_file(File.join(RAILS_ROOT,"config","paperclipftp.yml"))
84
+ creds = creds.stringify_keys
85
+ (creds[RAILS_ENV] || creds).symbolize_keys
86
+ end
87
+ private :parse_credentials
88
+
89
+ end
90
+ end
91
+ 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,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paperclipftp
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Damian Caruso
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-03-08 00:00:00 -03:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: paperclip
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 3
30
+ - 0
31
+ version: 2.3.0
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: yard
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :development
45
+ version_requirements: *id002
46
+ description: Ftp storage support for paperclip file attachment
47
+ email: damian.caruso@gmail.com
48
+ executables: []
49
+
50
+ extensions: []
51
+
52
+ extra_rdoc_files:
53
+ - LICENSE
54
+ - README.rdoc
55
+ files:
56
+ - LICENSE
57
+ - README.rdoc
58
+ - Rakefile
59
+ - VERSION
60
+ - lib/paperclipftp.rb
61
+ - test/paperclipftp_test.rb
62
+ - test/test_helper.rb
63
+ has_rdoc: true
64
+ homepage: http://github.com/cdamian/paperclipftp
65
+ licenses: []
66
+
67
+ post_install_message:
68
+ rdoc_options:
69
+ - --charset=UTF-8
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ requirements: []
87
+
88
+ rubyforge_project:
89
+ rubygems_version: 1.3.6
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: Ftp storage support for paperclip file attachment
93
+ test_files:
94
+ - test/paperclipftp_test.rb
95
+ - test/test_helper.rb