ftp_multipart_download 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c4379d1fc77c729e633e71db43bb0d15073733499196b3b5d17879003bbd0f0a
4
+ data.tar.gz: f3b4d39c38786ee80860eb5fcf77848087a5fe70c92db7a6e7b5cf043019c10e
5
+ SHA512:
6
+ metadata.gz: 2ae6a727e6ba89a6547b26beb194fb786725db887cfd8f452da3468c8327f7155797b7eb8898941e1a0a5c543ff21f958dbc908a5ebcbbd8404a656d2867e356
7
+ data.tar.gz: 8aa22cac4da25975aa817fc4478ca85401dffb9e522cc4a9f90f87ac971b3f539b6ab9fe5197b1f48b8800344b36ab91fb61c9a856d046dff00d625717771d5a
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,2 @@
1
+ Metrics/LineLength:
2
+ Max: 128
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.1
5
+ before_install: gem install bundler -v 1.16.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in ftp_multipart_download.gemspec
6
+ gemspec
@@ -0,0 +1,59 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ftp_multipart_download (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.0)
10
+ coderay (1.1.2)
11
+ diff-lcs (1.3)
12
+ jaro_winkler (1.5.1)
13
+ method_source (0.9.0)
14
+ parallel (1.12.1)
15
+ parser (2.5.1.0)
16
+ ast (~> 2.4.0)
17
+ powerpack (0.1.2)
18
+ pry (0.11.3)
19
+ coderay (~> 1.1.0)
20
+ method_source (~> 0.9.0)
21
+ rainbow (3.0.0)
22
+ rake (10.5.0)
23
+ rspec (3.7.0)
24
+ rspec-core (~> 3.7.0)
25
+ rspec-expectations (~> 3.7.0)
26
+ rspec-mocks (~> 3.7.0)
27
+ rspec-core (3.7.1)
28
+ rspec-support (~> 3.7.0)
29
+ rspec-expectations (3.7.0)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.7.0)
32
+ rspec-mocks (3.7.0)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.7.0)
35
+ rspec-support (3.7.1)
36
+ rubocop (0.57.2)
37
+ jaro_winkler (~> 1.5.1)
38
+ parallel (~> 1.10)
39
+ parser (>= 2.5)
40
+ powerpack (~> 0.1)
41
+ rainbow (>= 2.2.2, < 4.0)
42
+ ruby-progressbar (~> 1.7)
43
+ unicode-display_width (~> 1.0, >= 1.0.1)
44
+ ruby-progressbar (1.9.0)
45
+ unicode-display_width (1.4.0)
46
+
47
+ PLATFORMS
48
+ ruby
49
+
50
+ DEPENDENCIES
51
+ bundler (~> 1.16)
52
+ ftp_multipart_download!
53
+ pry (~> 0.11.3)
54
+ rake (~> 10.0)
55
+ rspec (~> 3.0)
56
+ rubocop (~> 0.57.2)
57
+
58
+ BUNDLED WITH
59
+ 1.16.2
@@ -0,0 +1,35 @@
1
+ # FtpMultipartDownload
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'ftp_multipart_download'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ftp_multipart_download
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ $ bin/console
23
+ downloader = FtpMultipartDownload::Downloader.new('ftp.riken.go.jp', username: 'anonymous', debug_mode: true)
24
+ downloader.download('/Linux/centos/7/isos/x86_64/CentOS-7-x86_64-NetInstall-1804.iso')
25
+ ```
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/koshigoe/ftp_multipart_download.
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'ftp_multipart_download'
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 'pry'
14
+ Pry.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'ftp_multipart_download/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'ftp_multipart_download'
7
+ spec.version = FtpMultipartDownload::VERSION
8
+ spec.authors = %w[koshigoe]
9
+ spec.email = %w[koshigoeb@gmail.com]
10
+
11
+ spec.summary = 'This gem allows to concurrent multipart download via FTP.'
12
+ spec.description = 'This gem allows to concurrent multipart download via FTP.'
13
+ spec.homepage = 'https://github.com/koshigoe/ftp_multipart_download'
14
+
15
+ # Specify which files should be added to the gem when it is released.
16
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
17
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = %w[lib]
23
+
24
+ spec.required_ruby_version = '~> 2.5.1'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.16'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'rspec', '~> 3.0'
29
+ spec.add_development_dependency 'rubocop', '~> 0.57.2'
30
+ spec.add_development_dependency 'pry', '~> 0.11.3'
31
+ end
@@ -0,0 +1,20 @@
1
+ require 'ftp_multipart_download/version'
2
+ require 'ftp_multipart_download/connection_pool'
3
+ require 'ftp_multipart_download/downloader'
4
+ require 'ftp_multipart_download/part'
5
+ require 'ftp_multipart_download/scheduler'
6
+ require 'net/ftp'
7
+
8
+ module FtpMultipartDownload
9
+ class Error < StandardError; end
10
+ class ConnectionError < Error
11
+ attr_reader :errors
12
+
13
+ def initialize(error_message = nil, errors: [])
14
+ super(error_message)
15
+ @errors = errors
16
+ end
17
+ end
18
+
19
+ DEFAULT_BLOCKSIZE = Net::FTP::DEFAULT_BLOCKSIZE
20
+ end
@@ -0,0 +1,29 @@
1
+ require 'net/ftp'
2
+
3
+ module FtpMultipartDownload
4
+ class ConnectionPool
5
+ attr_reader :errors
6
+
7
+ # @param [Integer] size number of connections.
8
+ # @param [Array] argv argument values for Net::FTP#initialize.
9
+ #
10
+ def initialize(size, *argv)
11
+ @errors = []
12
+ @connection_pool = Array.new(size) do
13
+ Net::FTP.new(*argv).yield_self { |c| c.closed? ? nil : c }
14
+ rescue => e
15
+ errors << e
16
+ nil
17
+ end.compact
18
+ end
19
+
20
+ include ::Enumerable
21
+
22
+ # @yield [connection] gives an instance of Net::FTP which connected
23
+ # @yieldparam [Net::FTP] an instance of Net::FTP which connected
24
+ #
25
+ def each(&block)
26
+ @connection_pool.each(&block)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,33 @@
1
+ module FtpMultipartDownload
2
+ class Downloader
3
+ attr_reader :argv
4
+
5
+ # see Net::FTP#initialize
6
+ #
7
+ # @param [String] host Host name.
8
+ # @param [Hash, String] user_or_options username or option as Hash.
9
+ # @param [String] passwd password
10
+ # @param [String] Account information for ACCT.
11
+ #
12
+ def initialize(host = nil, user_or_options = {}, passwd = nil, acct = nil)
13
+ @argv = [host, user_or_options, passwd, acct]
14
+ end
15
+
16
+ # concurrent multipart downloading.
17
+ #
18
+ # @param [Integer] concurrent number of concurrent download sessions.
19
+ # @param [String] remotefile remote file path.
20
+ # @param [String] localfile local file path.
21
+ # @param [Integer] blocksize block size.
22
+ # @raise [ConnectionError]
23
+ #
24
+ def download(concurrent, remotefile, localfile = File.basename(remotefile), blocksize = DEFAULT_BLOCKSIZE)
25
+ connection_pool = ConnectionPool.new(concurrent, *argv)
26
+ if connection_pool.count == 0
27
+ raise ConnectionError.new('Could not establish any connection.', errors: connection_pool.errors)
28
+ end
29
+
30
+ Scheduler.new(connection_pool).schedule(remotefile, localfile, blocksize)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,35 @@
1
+ module FtpMultipartDownload
2
+ class Part
3
+ attr_reader :connection
4
+
5
+ # @param [Net::FTP] connection an instance of Net::FTP.
6
+ #
7
+ def initialize(connection)
8
+ @connection = connection
9
+ end
10
+
11
+ # download a part of file.
12
+ #
13
+ # @param [String] remotefile remote file path.
14
+ # @param [String] localfile local file path.
15
+ # @param [Integer] blocksize block size.
16
+ # @param [Integer] offset offset
17
+ # @param [Integer] limit limit
18
+ #
19
+ def download(remotefile, localfile, blocksize, offset, limit)
20
+ old_resume, connection.resume = connection.resume, true
21
+
22
+ File.open(localfile, 'wb') do |f|
23
+ retrieved = 0
24
+ connection.retrbinary("RETR #{remotefile}", blocksize, offset) do |chunk|
25
+ retrievable = [limit - retrieved, chunk.bytesize].min
26
+ break if retrievable <= 0
27
+
28
+ retrieved += f.write(chunk[0, retrievable])
29
+ end
30
+ end
31
+ ensure
32
+ connection.resume = old_resume
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,55 @@
1
+ require 'thwait'
2
+ require 'tmpdir'
3
+
4
+ module FtpMultipartDownload
5
+ class Scheduler
6
+ attr_reader :connection_pool
7
+
8
+ # @param [Array<Net::FTP>] connection_pool an array of instance of Net::FTP.
9
+ #
10
+ def initialize(connection_pool)
11
+ @connection_pool = connection_pool
12
+ end
13
+
14
+ # concurrent multipart downloading.
15
+ #
16
+ # @param [String] remotefile remote file path.
17
+ # @param [String] localfile local file path.
18
+ # @param [Integer] blocksize block size.
19
+ #
20
+ def schedule(remotefile, localfile, blocksize)
21
+ total_size = connection_pool.first.size(remotefile)
22
+ part_size = (total_size.to_f / connection_pool.count).ceil
23
+
24
+ Dir.mktmpdir do |dir|
25
+ part_files = []
26
+ threads = connection_pool.map.with_index do |connection, i|
27
+ offset = part_size * i
28
+ next if offset >= total_size
29
+ part_files << "#{dir}/#{i}"
30
+ thread(connection, remotefile, part_files.last, blocksize, offset, part_size)
31
+ end.compact
32
+ ThreadsWait.all_waits(*threads)
33
+
34
+ concat_files(localfile, *part_files)
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def thread(*args)
41
+ Thread.start(*args) do |connection, remotefile, partfile, blocksize, offset, limit|
42
+ Part.new(connection).download(remotefile, partfile, blocksize, offset, limit)
43
+ end
44
+ end
45
+
46
+ def concat_files(dest, *parts)
47
+ File.open(dest, 'wb') do |output|
48
+ parts.each do |part|
49
+ next unless File.exist?(part)
50
+ IO.copy_stream(part, output)
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ module FtpMultipartDownload
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ftp_multipart_download
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - koshigoe
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-06-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.57.2
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.57.2
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.11.3
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.11.3
83
+ description: This gem allows to concurrent multipart download via FTP.
84
+ email:
85
+ - koshigoeb@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".rubocop.yml"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - Gemfile.lock
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - ftp_multipart_download.gemspec
101
+ - lib/ftp_multipart_download.rb
102
+ - lib/ftp_multipart_download/connection_pool.rb
103
+ - lib/ftp_multipart_download/downloader.rb
104
+ - lib/ftp_multipart_download/part.rb
105
+ - lib/ftp_multipart_download/scheduler.rb
106
+ - lib/ftp_multipart_download/version.rb
107
+ homepage: https://github.com/koshigoe/ftp_multipart_download
108
+ licenses: []
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - "~>"
117
+ - !ruby/object:Gem::Version
118
+ version: 2.5.1
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.7.6
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: This gem allows to concurrent multipart download via FTP.
130
+ test_files: []