clientus 0.1.0.dev

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e320aff4b0c5e9ead91a515f95dc708423b2e278
4
+ data.tar.gz: c7bdea9012df96ba9a3c0d44d7d0484eacce3988
5
+ SHA512:
6
+ metadata.gz: daec6499149ca9b627a41cb9b449ae144aca595dbe5cde6fc8f467898b3b106c5e9772ad0aaae173af6d7e4617c7e1d403ba31a8f0823577fd0918d6b4f74520
7
+ data.tar.gz: a702a56a0f2d5a6f945cd7840a2741bc8867c1f793ed66af3499fffda9b912d9b99da1721f98847ba325e7a6349dda1e6bd6792cc367413c209348522a30342a
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,7 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4.6
3
+ Exclude:
4
+ - bin/*
5
+ - clientus.gemspec
6
+ Layout/EmptyLineAfterGuardClause:
7
+ Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in clientus.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
@@ -0,0 +1,40 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ clientus (0.1.0.dev)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.1)
10
+ parallel (1.19.2)
11
+ parser (2.7.2.0)
12
+ ast (~> 2.4.1)
13
+ rainbow (3.0.0)
14
+ rake (12.3.3)
15
+ regexp_parser (1.8.2)
16
+ rexml (3.2.4)
17
+ rubocop (1.0.0)
18
+ parallel (~> 1.10)
19
+ parser (>= 2.7.1.5)
20
+ rainbow (>= 2.2.2, < 4.0)
21
+ regexp_parser (>= 1.8)
22
+ rexml
23
+ rubocop-ast (>= 0.6.0)
24
+ ruby-progressbar (~> 1.7)
25
+ unicode-display_width (>= 1.4.0, < 2.0)
26
+ rubocop-ast (1.0.0)
27
+ parser (>= 2.7.1.5)
28
+ ruby-progressbar (1.10.1)
29
+ unicode-display_width (1.7.0)
30
+
31
+ PLATFORMS
32
+ x64-mingw32
33
+
34
+ DEPENDENCIES
35
+ clientus!
36
+ rake (~> 12.0)
37
+ rubocop (~> 1.0)
38
+
39
+ BUNDLED WITH
40
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Oleg Ivanov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,86 @@
1
+ # Clientus
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/clientus`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'clientus'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install clientus
22
+
23
+ ## Usage
24
+
25
+ Case 1: Suppose you have an URL of the uploading interface
26
+ ```ruby
27
+ require 'clientus'
28
+
29
+ # simply create a client instance
30
+ client = Clientus::Client.new(url)
31
+
32
+ # then upload your file!
33
+ client.upload('foo/bar.txt')
34
+
35
+ # now bar.txt is somewhere on the server (or some exception appeared.. ouch!)
36
+ ```
37
+
38
+ Case 2: You have some headers you want to pass along (ex.: session sid)
39
+ ```ruby
40
+ require 'clientus'
41
+
42
+ # describe your headers
43
+ # NOTE: tus-specific headers will be rewritten or adjusted
44
+ # ex.: if you defined Tus-Resumable header, its value will be replaced with one from the server
45
+ custom_headers = {
46
+ 'Cookie' => 'sid=09876543211234567890'
47
+ }
48
+
49
+ # pass your headers when creating a client instance
50
+ client = Clientus::Client.new(url, additional_headers: custom_headers)
51
+
52
+ # upload a file!
53
+ client.upload('foo/bar.txt')
54
+ ```
55
+
56
+ Case 3: You want to use some custom http config (ex: you want to turn off the sertificate verification)
57
+ ```ruby
58
+ require 'clientus'
59
+
60
+ # describe a required http config
61
+ http_params = {
62
+ use_ssl: true,
63
+ verify_mode: OpenSSL::SSL::VERIFY_NONE
64
+ }
65
+
66
+ # pass your headers when creating a client instance
67
+ client = Clientus::Client.new(url, http_params: http_params)
68
+
69
+ # upload a file!
70
+ client.upload('foo/bar.txt')
71
+ ```
72
+
73
+ ## Development
74
+
75
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
76
+
77
+ 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).
78
+
79
+ ## Contributing
80
+
81
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/clientus.
82
+
83
+
84
+ ## License
85
+
86
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "clientus"
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__)
@@ -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
+ require_relative 'lib/clientus/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "clientus"
5
+ spec.version = Clientus::VERSION
6
+ spec.authors = ["Oleg Ivanov"]
7
+ spec.email = ["gluk.main+github@gmail.com"]
8
+
9
+ spec.summary = %q{A simple client for the Tus protocol}
10
+ spec.description = %q{A client for the Tus protocol enabling file uploads}
11
+ spec.homepage = "https://github.com/glUk-skywalker/clientus"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["allowed_push_host"] = "https://rubygems.org/"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/glUk-skywalker/clientus"
19
+ spec.metadata["changelog_uri"] = "<none yet>"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency 'rubocop', '~> 1.0'
31
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'net/http'
4
+
5
+ require 'clientus/error'
6
+ require 'clientus/version'
7
+ require 'clientus/client'
8
+ require 'clientus/server'
9
+ require 'clientus/upload_file'
10
+ require 'helpers'
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Clientus
4
+ # Client that transfers files. The main class.
5
+ class Client
6
+ CHUNK_SIZE = 100 * 1024 * 1024 # 100MB
7
+
8
+ def initialize(url, http_params: {}, additional_headers: {})
9
+ @server_uri = URI.parse url
10
+ @http = Net::HTTP.start(@server_uri.host, @server_uri.port, http_params)
11
+ @additional_headers = additional_headers
12
+
13
+ server_options = @http.options(@server_uri.request_uri)
14
+ @server = Server.new(server_options)
15
+ end
16
+
17
+ def upload(file_path)
18
+ # checking here instead of checking in 'create!' to avoid wasting time
19
+ # for needless file reading
20
+ @server.raise_if_unsupported(:creation)
21
+
22
+ file = read_file(file_path)
23
+
24
+ location = create!(file)
25
+
26
+ start_offset = 0
27
+ loop do
28
+ end_offset = start_offset + CHUNK_SIZE
29
+ chunk_data = file.data[start_offset..end_offset]
30
+
31
+ upload_chunk(location, start_offset, chunk_data)
32
+ break if end_offset >= file.size
33
+
34
+ start_offset = end_offset + 1
35
+ end
36
+ terminate!(location)
37
+ end
38
+
39
+ private
40
+
41
+ def read_file(file_path)
42
+ data = File.open(file_path, 'rb', &:read)
43
+ size = File.size(file_path)
44
+ name = File.basename(file_path)
45
+ UploadFile.new(name, size, data)
46
+ end
47
+
48
+ def create!(file)
49
+ # TODO: refactor this
50
+ request = default_request(:post, @server_uri.request_uri)
51
+ request['Content-Length'] = 0
52
+ request['Upload-Length'] = file.size
53
+ request['Upload-Metadata'] ||= "filename #{file.encoded_name}"
54
+ unless request['Upload-Metadata'].include?('filename ')
55
+ request['Upload-Metadata'] += ",filename #{file.encoded_name}"
56
+ end
57
+
58
+ res = @http.request(request)
59
+ return res['Location'] if res.is_a?(Net::HTTPCreated)
60
+
61
+ raise(Error, "failed to create remote file: bad resonse type: #{res.class}; body: #{res.body}")
62
+ end
63
+
64
+ def upload_chunk(uri, offset, chunk)
65
+ request = default_request(:patch, uri)
66
+ request['Content-Type'] = 'application/offset+octet-stream'
67
+ request['Upload-Offset'] = offset
68
+ request.body = chunk
69
+ @http.request(request)
70
+ end
71
+
72
+ def terminate!(uri)
73
+ @server.raise_if_unsupported(:termination)
74
+ request = default_request(:delete, uri)
75
+ @http.request(request)
76
+ end
77
+
78
+ def default_request(method, uri)
79
+ request_class = Object.const_get "Net::HTTP::#{method.to_s.capitalize}"
80
+ request = request_class.new(uri)
81
+ @additional_headers.each { |k, v| request[k] = v }
82
+ request['Tus-Resumable'] = @server.tus_version
83
+ request
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Clientus
4
+ class Error < StandardError; end
5
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Clientus
4
+ # Holds server info
5
+ class Server
6
+ attr_reader :tus_version
7
+
8
+ def initialize(http_options)
9
+ @operations = http_options['Tus-Extension'].split(',')
10
+ @tus_version = http_options['tus-version']
11
+ raise(Error, 'The server did not provide the tus version.') unless @tus_version
12
+ end
13
+
14
+ def raise_if_unsupported(extension)
15
+ ext = extension.to_s
16
+ return if @operations.include?(ext)
17
+ raise(Error, "The server does not support #{ext}")
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Clientus
4
+ # Holds file information
5
+ class UploadFile
6
+ attr_reader :name, :size, :data
7
+
8
+ def initialize(name, size, data)
9
+ @name = name
10
+ @data = data
11
+ @size = size
12
+ end
13
+
14
+ def encoded_name
15
+ # TODO: investigate why we need to exclude CR/LF in the end of encoded string
16
+ Base64.encode64(@name)[0..-2]
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Clientus
4
+ VERSION = '0.1.0.dev'
5
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: clientus
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.dev
5
+ platform: ruby
6
+ authors:
7
+ - Oleg Ivanov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-10-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rubocop
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ description: A client for the Tus protocol enabling file uploads
28
+ email:
29
+ - gluk.main+github@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".rubocop.yml"
36
+ - Gemfile
37
+ - Gemfile.lock
38
+ - LICENSE.txt
39
+ - README.md
40
+ - Rakefile
41
+ - bin/console
42
+ - bin/setup
43
+ - clientus.gemspec
44
+ - lib/clientus.rb
45
+ - lib/clientus/client.rb
46
+ - lib/clientus/error.rb
47
+ - lib/clientus/server.rb
48
+ - lib/clientus/upload_file.rb
49
+ - lib/clientus/version.rb
50
+ homepage: https://github.com/glUk-skywalker/clientus
51
+ licenses:
52
+ - MIT
53
+ metadata:
54
+ allowed_push_host: https://rubygems.org/
55
+ homepage_uri: https://github.com/glUk-skywalker/clientus
56
+ source_code_uri: https://github.com/glUk-skywalker/clientus
57
+ changelog_uri: "<none yet>"
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 2.3.0
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">"
70
+ - !ruby/object:Gem::Version
71
+ version: 1.3.1
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 2.6.14.4
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: A simple client for the Tus protocol
78
+ test_files: []