active-storage-ftp 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +35 -0
- data/README.md +46 -0
- data/Rakefile +6 -0
- data/active_storage_ftp.gemspec +39 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/active-storage-ftp.rb +2 -0
- data/lib/active_storage/service/ftp_service.rb +183 -0
- data/lib/active_storage_ftp/enhance_ftp.rb +12 -0
- data/lib/active_storage_ftp/version.rb +3 -0
- data/lib/active_storage_ftp.rb +6 -0
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 17d6911595715168ddebd3539f62b8e201d09d9acf98d9d6d30c19aff431149d
|
4
|
+
data.tar.gz: 4415e8df88899c04e4a78c45cb1117c7a6577c089c9cf4c81ed7731e70eadef1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e4ad459cc21fb0fa91a7a65142754565559d876e481fe65572f3e9893db659425a91256093fe18739d9332d8d552143483a699c53cda2c3cc6bfc7098bc543fa
|
7
|
+
data.tar.gz: a69367e3bada7f7c61eab4a9e3b018793b50aac3dde0477b8cf675346fb50adbbc82a5109e87aaa25440775c3ce6908d051314313512e02ca4cebf86e6c69f1d
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
active-storage-ftp (0.1.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.3)
|
10
|
+
rake (10.5.0)
|
11
|
+
rspec (3.8.0)
|
12
|
+
rspec-core (~> 3.8.0)
|
13
|
+
rspec-expectations (~> 3.8.0)
|
14
|
+
rspec-mocks (~> 3.8.0)
|
15
|
+
rspec-core (3.8.0)
|
16
|
+
rspec-support (~> 3.8.0)
|
17
|
+
rspec-expectations (3.8.3)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.8.0)
|
20
|
+
rspec-mocks (3.8.0)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.8.0)
|
23
|
+
rspec-support (3.8.0)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
active-storage-ftp!
|
30
|
+
bundler (~> 2.0)
|
31
|
+
rake (~> 10.0)
|
32
|
+
rspec (~> 3.0)
|
33
|
+
|
34
|
+
BUNDLED WITH
|
35
|
+
2.0.1
|
data/README.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# Activestorage::Ftp
|
2
|
+
|
3
|
+
Active Storage FTP Service, This project only support FTP service not SSH
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'actives-storage-ftp'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install active-storage-ftp
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```yml
|
24
|
+
production:
|
25
|
+
service: FTP
|
26
|
+
host: <%= ENV["FTP_HOST"] %>
|
27
|
+
port: <%= ENV["FTP_PORT"] %>
|
28
|
+
username: <%= ENV["FTP_USERNAME"] %>
|
29
|
+
password: <%= ENV["FTP_PASSWORD"] %>
|
30
|
+
folder: <%= ENV["FTP_FOLDER"] %>
|
31
|
+
url: <%= ENV["FTP_URL"] %>
|
32
|
+
# optional
|
33
|
+
passive: true
|
34
|
+
ssl: false
|
35
|
+
debug_mode: false
|
36
|
+
```
|
37
|
+
|
38
|
+
## Development
|
39
|
+
|
40
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
41
|
+
|
42
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
43
|
+
|
44
|
+
## Contributing
|
45
|
+
|
46
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/iscreen/actives-torage-ftp.
|
data/Rakefile
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'active_storage_ftp/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'active-storage-ftp'
|
8
|
+
spec.version = ActiveStorageFtp::VERSION
|
9
|
+
spec.authors = ["Dean Lin"]
|
10
|
+
spec.email = ["iscreen@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Active Storage FTP Service.}
|
13
|
+
spec.description = %q{Active Storage FTP Service.}
|
14
|
+
spec.homepage = 'https://api.genbagames.com/doc/'
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
|
+
if spec.respond_to?(:metadata)
|
19
|
+
spec.metadata['allowed_push_host"'] = 'https://rubygems.org'
|
20
|
+
|
21
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
22
|
+
else
|
23
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
24
|
+
'public gem pushes.'
|
25
|
+
end
|
26
|
+
|
27
|
+
# Specify which files should be added to the gem when it is released.
|
28
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
29
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
30
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
31
|
+
end
|
32
|
+
spec.bindir = 'exe'
|
33
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
34
|
+
spec.require_paths = ['lib']
|
35
|
+
|
36
|
+
spec.add_development_dependency 'bundler', '~> 2.0'
|
37
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
38
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
39
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "activestorage/ftp"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,183 @@
|
|
1
|
+
require 'net/ftp'
|
2
|
+
|
3
|
+
module ActiveStorage
|
4
|
+
class Service::FTPService < Service
|
5
|
+
def initialize(**config)
|
6
|
+
@config = config
|
7
|
+
end
|
8
|
+
|
9
|
+
def upload(key, io, checksum: nil, content_type: nil, disposition: nil, filename: nil)
|
10
|
+
instrument :upload, key: key, checksum: checksum do
|
11
|
+
connection do |ftp|
|
12
|
+
ftp.put(io, key)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def download(key, &block)
|
18
|
+
if block_given?
|
19
|
+
instrument :streaming_download, key: key do
|
20
|
+
stream(key, &block)
|
21
|
+
end
|
22
|
+
else
|
23
|
+
instrument :download, key: key do
|
24
|
+
stream(data) do |data|
|
25
|
+
result += data
|
26
|
+
end
|
27
|
+
result
|
28
|
+
rescue StandardError => _
|
29
|
+
raise ActiveStorage::FileNotFoundError
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def update_metadata(key, content_type:, disposition: nil, filename: nil)
|
35
|
+
instrument :update_metadata, key: key, content_type: content_type, disposition: disposition do
|
36
|
+
# Nothing need to do
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def download_chunk(key, range)
|
41
|
+
instrument :download_chunk, key: key, range: range do
|
42
|
+
connection do |ftp|
|
43
|
+
offset = range.begin
|
44
|
+
ftp.retrbinary("RETR #{key}", range.begin) do |data|
|
45
|
+
yield data
|
46
|
+
offset += data
|
47
|
+
end
|
48
|
+
end
|
49
|
+
rescue StandardError => _
|
50
|
+
raise ActiveStorage::FileNotFoundError
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def delete(key)
|
55
|
+
instrument :delete, key: key do
|
56
|
+
connection do |ftp|
|
57
|
+
ftp.delete(key)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def delete_prefixed(prefix)
|
63
|
+
instrument :delete_prefixed, prefix: prefix do
|
64
|
+
connection do |ftp|
|
65
|
+
files = ftp.list("#{prefix}*")
|
66
|
+
files.each { |f| f.delete(f) }
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def exist?(key)
|
72
|
+
instrument :exist, key: key do |payload|
|
73
|
+
connection do |ftp|
|
74
|
+
data = ftp.retrbinary("RETR #{key}")
|
75
|
+
answer = !data.nil?
|
76
|
+
payload[:exist] = answer
|
77
|
+
answer
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def url(key, expires_in:, filename:, content_type:, disposition:)
|
83
|
+
instrument :url, key: key do |payload|
|
84
|
+
generated_url = url_for(key)
|
85
|
+
payload[:url]
|
86
|
+
generated_url
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def url_for_direct_upload(key, expires_in:, checksum:, **)
|
91
|
+
instrument :url, key: key do |payload|
|
92
|
+
raise 'not support direct upload'
|
93
|
+
# generated_url = bucket.signed_url key, method: "PUT", expires: expires_in, content_md5: checksum
|
94
|
+
|
95
|
+
# payload[:url] = generated_url
|
96
|
+
|
97
|
+
# generated_url
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def headers_for_direct_upload(key, checksum:, **)
|
102
|
+
{ 'Content-MD5' => checksum }
|
103
|
+
end
|
104
|
+
|
105
|
+
private
|
106
|
+
|
107
|
+
attr_reader :config
|
108
|
+
|
109
|
+
def connection
|
110
|
+
EnhanceFtp.open(
|
111
|
+
ftp_host,
|
112
|
+
username: ftp_username,
|
113
|
+
password: ftp_password,
|
114
|
+
port: ftp_port,
|
115
|
+
passive: ftp_passive,
|
116
|
+
ssl: ftp_ssl,
|
117
|
+
debug_mode: ftp_debug_mode
|
118
|
+
) do |ftp|
|
119
|
+
begin
|
120
|
+
ftp.chdir(ftp_folder)
|
121
|
+
rescue StandardError => _
|
122
|
+
ftp.mkdir_p(ftp_folder)
|
123
|
+
ftp.chdir(ftp_folder)
|
124
|
+
end
|
125
|
+
yield ftp
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def path_for(key) #:nodoc:
|
130
|
+
File.join(ftp_folder, key)
|
131
|
+
end
|
132
|
+
|
133
|
+
def url_for(key)
|
134
|
+
file_url = [ftp_url, ftp_folder, key].join('/')
|
135
|
+
file_url
|
136
|
+
end
|
137
|
+
|
138
|
+
# Reads the file for the given key in chunks, yielding each to the block.
|
139
|
+
def stream(key)
|
140
|
+
connection do |ftp|
|
141
|
+
ftp.retrbinary("RETR #{key}", 5.megabytes) do |data|
|
142
|
+
yield data
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def ftp_host
|
148
|
+
config.fetch(:host)
|
149
|
+
end
|
150
|
+
|
151
|
+
def ftp_username
|
152
|
+
config.fetch(:username)
|
153
|
+
end
|
154
|
+
|
155
|
+
def ftp_password
|
156
|
+
config.fetch(:password)
|
157
|
+
end
|
158
|
+
|
159
|
+
def ftp_port
|
160
|
+
config.fetch(:port)
|
161
|
+
end
|
162
|
+
|
163
|
+
def ftp_folder
|
164
|
+
config.fetch(:folder)
|
165
|
+
end
|
166
|
+
|
167
|
+
def ftp_ssl
|
168
|
+
config.fetch(:ssl, false)
|
169
|
+
end
|
170
|
+
|
171
|
+
def ftp_passive
|
172
|
+
config.fetch(:passive, true)
|
173
|
+
end
|
174
|
+
|
175
|
+
def ftp_url
|
176
|
+
config.fetch(:url)
|
177
|
+
end
|
178
|
+
|
179
|
+
def ftp_debug_mode
|
180
|
+
config.fetch(:debug_mode, false)
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module ActiveStorageFtp
|
2
|
+
class EnhanceFtp < Net::Ftp
|
3
|
+
def mkdir_p(remove_path)
|
4
|
+
paths = remove_path.split('/')
|
5
|
+
full_path = paths.first == '~' ? '' : '/'
|
6
|
+
paths.each do |path|
|
7
|
+
full_path = File.join(full_path, path)
|
8
|
+
mkdir(full_path)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active-storage-ftp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dean Lin
|
@@ -58,7 +58,22 @@ email:
|
|
58
58
|
executables: []
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
61
|
-
files:
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- Gemfile.lock
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- active_storage_ftp.gemspec
|
70
|
+
- bin/console
|
71
|
+
- bin/setup
|
72
|
+
- lib/active-storage-ftp.rb
|
73
|
+
- lib/active_storage/service/ftp_service.rb
|
74
|
+
- lib/active_storage_ftp.rb
|
75
|
+
- lib/active_storage_ftp/enhance_ftp.rb
|
76
|
+
- lib/active_storage_ftp/version.rb
|
62
77
|
homepage: https://api.genbagames.com/doc/
|
63
78
|
licenses: []
|
64
79
|
metadata:
|