olek-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) 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 = "olek-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 "olek-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,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: olek-paperclipftp
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Damian Caruso
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-04-20 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: olek-paperclip
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 2
32
+ - 3
33
+ - 0
34
+ version: 2.3.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: yard
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ type: :development
50
+ version_requirements: *id002
51
+ description: Ftp storage support for paperclip file attachment
52
+ email: damian.caruso@gmail.com
53
+ executables: []
54
+
55
+ extensions: []
56
+
57
+ extra_rdoc_files:
58
+ - LICENSE
59
+ - README.rdoc
60
+ files:
61
+ - LICENSE
62
+ - README.rdoc
63
+ - Rakefile
64
+ - VERSION
65
+ - lib/paperclipftp.rb
66
+ - test/paperclipftp_test.rb
67
+ - test/test_helper.rb
68
+ has_rdoc: true
69
+ homepage: http://github.com/cdamian/paperclipftp
70
+ licenses: []
71
+
72
+ post_install_message:
73
+ rdoc_options: []
74
+
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ hash: 3
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ hash: 3
92
+ segments:
93
+ - 0
94
+ version: "0"
95
+ requirements: []
96
+
97
+ rubyforge_project:
98
+ rubygems_version: 1.6.2
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: Ftp storage support for paperclip file attachment
102
+ test_files:
103
+ - test/paperclipftp_test.rb
104
+ - test/test_helper.rb