tus-client 0.0.1

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: fb181b60fb3adfe8144c63058f5b8041aff99adcc0951c956dd8d33891c7a7b6
4
+ data.tar.gz: 98f4f9fccf403e70a49dd3d6ddaa15c9877956db3c43ca69ad0795e90cdfb1ff
5
+ SHA512:
6
+ metadata.gz: 05c74eaf8f6bbdcc875488dc5b802c910365beacea32cd8ee41135e5560ab89cd121fc565e693d991fbbefbe75226f287c6a6df90b03d3220cc5903395219701
7
+ data.tar.gz: fc4f57f6b8ef4c16f4216ad119d49d1f938f7e213081304c444ae32a6d67549be19bfb2fb9b7d6e159956c03d70da3a9fa06ca806a38cb35227d76871e6110e7
@@ -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
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at factyy@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in tus-client.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 12.0'
9
+ gem 'rspec', '~> 3.0'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Andrei Mochalov (factyy)
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,32 @@
1
+ # Tus::Client
2
+
3
+ This project is aimed to provide a client tus.io implementation (protocol version 1.0.0)
4
+
5
+ TODO: Write the additional info
6
+
7
+ ## Installation
8
+
9
+ TODO: this gem is not published yet!
10
+
11
+ Just install it from sources
12
+
13
+ ## Usage
14
+
15
+ TODO: Write usage instructions here
16
+
17
+ ## Development
18
+
19
+ TODO: Write the info
20
+
21
+ ## Contributing
22
+
23
+ Bug reports and pull requests are welcome on GitHub at https://github.com/factyy/tus-tuby-client. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/factyy/tus-ruby-client/blob/master/CODE_OF_CONDUCT.md).
24
+
25
+
26
+ ## License
27
+
28
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
29
+
30
+ ## Code of Conduct
31
+
32
+ Everyone interacting in the Tus::Client project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/factyy/tus-ruby-client/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,126 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'net/http'
4
+ require 'base64'
5
+
6
+ require 'tus/client/version'
7
+
8
+ module Tus
9
+ class Client
10
+ # 100 MiB is ok for now...
11
+ CHUNK_SIZE = 100 * 1024 * 1024
12
+ TUS_VERSION = '1.0.0'
13
+ NUM_RETRIES = 5
14
+
15
+ def initialize(server_url)
16
+ @server_uri = URI.parse(server_url)
17
+
18
+ # better to open the connection now
19
+ @http = Net::HTTP.start(@server_uri.host, @server_uri.port)
20
+ # we cache this value for further use
21
+ @capabilities = capabilities
22
+ end
23
+
24
+ def upload(file_path)
25
+ raise 'No such file!' unless File.file?(file_path)
26
+
27
+ io = File.open(file_path, 'rb')
28
+
29
+ uri = create_remote(File.basename(file_path), File.size(file_path))
30
+ # we use only parameters that are known to the server
31
+ offset, length = upload_parameters(uri)
32
+
33
+ chunks = Enumerator.new do |yielder|
34
+ yielder << io.read(CHUNK_SIZE)
35
+ end
36
+
37
+ begin
38
+ offset = chunks.lazy.inject(offset) do |current_offset, chunk|
39
+ upload_chunk(uri, current_offset, chunk)
40
+ end
41
+ rescue StandardError
42
+ raise 'Broken upload! Cannot send a chunk!'
43
+ end
44
+
45
+ raise 'Broken upload!' unless offset == length
46
+
47
+ io.close
48
+ end
49
+
50
+ private
51
+
52
+ def capabilities
53
+ raise 'Uninitialized connection!' unless @http
54
+
55
+ response = @http.options(@server_uri.request_uri)
56
+
57
+ response['Tus-Extension']&.split(',')
58
+ end
59
+
60
+ def create_remote(file_name, file_size)
61
+ unless @capabilities.include?('creation')
62
+ raise 'New file uploading is not supported!'
63
+ end
64
+
65
+ request = Net::HTTP::Post.new(@server_uri.request_uri)
66
+ request['Content-Length'] = 0
67
+ request['Upload-Length'] = file_size
68
+ request['Tus-Resumable'] = TUS_VERSION
69
+ request['Upload-Metadata'] = "filename: #{Base64.strict_encode64(file_name)},is_confidential"
70
+
71
+ response = nil
72
+
73
+ NUM_RETRIES.times do
74
+ begin
75
+ response = @http.request(request)
76
+ break
77
+ rescue StandardError
78
+ next
79
+ end
80
+ end
81
+
82
+ unless response.is_a?(Net::HTTPCreated)
83
+ raise 'Cannot create a remote file!'
84
+ end
85
+
86
+ response['Location']
87
+ end
88
+
89
+ def upload_parameters(uri)
90
+ request = Net::HTTP::Head.new(uri)
91
+ request['Tus-Resumable'] = TUS_VERSION
92
+
93
+ response = @http.request(request)
94
+
95
+ [response['Upload-Offset'], response['Upload-Length']].map(&:to_i)
96
+ end
97
+
98
+ def upload_chunk(uri, offset, chunk)
99
+ request = Net::HTTP::Patch.new(uri)
100
+ request['Content-Type'] = 'application/offset+octet-stream'
101
+ request['Upload-Offset'] = offset
102
+ request['Tus-Resumable'] = TUS_VERSION
103
+ request.body = chunk
104
+
105
+ response = nil
106
+
107
+ NUM_RETRIES.times do
108
+ begin
109
+ response = @http.request(request)
110
+ break
111
+ rescue StandardError
112
+ next
113
+ end
114
+ end
115
+
116
+ raise 'Cannot upload a chunk!' unless response.is_a?(Net::HTTPNoContent)
117
+
118
+ resulting_offset = response['Upload-Offset'].to_i
119
+ unless resulting_offset == offset + chunk.size
120
+ raise 'Chunk upload is broken!'
121
+ end
122
+
123
+ resulting_offset
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tus
4
+ class Client
5
+ VERSION = '0.0.1'
6
+ end
7
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/tus/client/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'tus-client'
7
+ spec.version = Tus::Client::VERSION
8
+ spec.authors = ['Andrei Mochalov (factyy)']
9
+ spec.email = ['factyy@gmail.com']
10
+
11
+ spec.summary = 'Quick and dirty tus.io protocol ruby client'
12
+ spec.homepage = 'https://github.com/factyy/tus-ruby-client'
13
+ spec.license = 'MIT'
14
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+
22
+ spec.require_paths = ['lib']
23
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tus-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andrei Mochalov (factyy)
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-06-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - factyy@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - CODE_OF_CONDUCT.md
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - lib/tus/client.rb
27
+ - lib/tus/client/version.rb
28
+ - tus-client.gemspec
29
+ homepage: https://github.com/factyy/tus-ruby-client
30
+ licenses:
31
+ - MIT
32
+ metadata: {}
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: 2.3.0
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubygems_version: 3.1.2
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: Quick and dirty tus.io protocol ruby client
52
+ test_files: []