shrine-tus 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 88569c9725438f4ec79a2e475952cf7ba968ce2c
4
+ data.tar.gz: d5811879b451a6ec5bead0540a2d1582a5b9f5d2
5
+ SHA512:
6
+ metadata.gz: f2ea7ef342d68564e10f5831e65804106bc8af0b1f53f352d658f340c9bae6de15d18d7dfe375695869b7d7a5934787f9d8e491d1b3a6897690de0aa8bd70b03
7
+ data.tar.gz: d0874c8447a1b1053bbb4b64f8d5435f9bcd232b0092b9caf4c679f7da91d6a318e29ad9d674d758f23418531b73d0fa99da68120144fa47f2d97cf39ebf71ba
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Janko Marohnić
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.
data/README.md ADDED
@@ -0,0 +1,98 @@
1
+ # Shrine::Tus
2
+
3
+ Provides tools for integrating [Shrine] with [tus-ruby-server].
4
+
5
+ ## Installation
6
+
7
+ ```ruby
8
+ gem "shrine-tus"
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ Once the file has been uploaded to `tus-ruby-server`, you want to attach it to
14
+ a database record and move it to permanent Shrine storage.
15
+
16
+ ```rb
17
+ class VideoUploader < Shrine
18
+ plugin :default_storage, cache: :tus # set Shrine::Storage::Tus as temporary storage
19
+ end
20
+ ```
21
+ ```rb
22
+ class Movie < Sequel::Model
23
+ include VideoUploader::Attachment.new(:video)
24
+ end
25
+ ```
26
+ ```rb
27
+ file_data #=> {"id":"http://tus-server.org/68db42638388ae645ab747b36a837a79", "storage":"cache", "metadata":{...}}
28
+ Movie.create(video: file_data)
29
+ ```
30
+
31
+ The simplest setup is to have Shrine download the file uploaded to
32
+ `tus-ruby-server` through the app, which you can do by setting
33
+ `Shrine::Storage::Tus` as the temporary Shrine storage.
34
+
35
+ ```rb
36
+ gem "shrine-url", "~> 0.3" # dependency of Shrine::Storage::Tus
37
+ ```
38
+ ```rb
39
+ require "shrine/storage/tus"
40
+
41
+ Shrine.storages[:tus] = Shrine::Storage::Tus.new
42
+ ```
43
+ ```rb
44
+ class VideoUploader < Shrine
45
+ plugin :default_storage, cache: :tus # set Shrine::Storage::Tus as temporary storage
46
+ end
47
+ ```
48
+
49
+ By default `wget` will be used for downloading, but if you need support for
50
+ partial downloads (e.g. you want to use `restore_cached_data`), you can switch
51
+ to using [Down] for downloads.
52
+
53
+ ```rb
54
+ Shrine::Storage::Tus.new(downloader: :down)
55
+ ```
56
+
57
+ If you want to Shrine, instead of downloading through the `tus-ruby-server`
58
+ app, to download directly from the tus storage, you can assign the tus storage
59
+ instance to `Shrine::Storage::Tus`:
60
+
61
+ ```rb
62
+ Shrine::Storage::Tus.new(tus_storage: Tus::Server.opts[:storage])
63
+ ```
64
+
65
+ Finally, if you want to use the same kind of permanent storage as your
66
+ `tus-ruby-server` uses, you can setup your temporary Shrine storage to match
67
+ the one your tus server uses, and load the `tus` plugin which will translate
68
+ the assigned tus URL to the corresponding storage ID.
69
+
70
+ ```rb
71
+ Shrine.storages = {
72
+ cache: Shrine::Storage::YourTemporaryStorage.new(...),
73
+ store: Shrine::Storage::YourPermanentStorage.new(...),
74
+ }
75
+ ```
76
+ ```rb
77
+ class VideoUploader < Shrine
78
+ plugin :tus
79
+ end
80
+ ```
81
+
82
+ For more details, and an explanation of pros and cons for each of the
83
+ approaches, see [shrine-tus-demo].
84
+
85
+ ## Contributing
86
+
87
+ ```sh
88
+ $ rake test
89
+ ```
90
+
91
+ ## License
92
+
93
+ [MIT](/LICENSE.txt)
94
+
95
+ [Shrine]: https://github.com/janko-m/shrine
96
+ [tus-ruby-server]: https://github.com/janko-m/tus-ruby-server
97
+ [Down]: https://github.com/janko-m/down
98
+ [shrine-tus-demo]: https://github.com/janko-m/shrine-tus-demo
@@ -0,0 +1,38 @@
1
+ require "json"
2
+
3
+ class Shrine
4
+ module Plugins
5
+ module Tus
6
+ module AttacherMethods
7
+ def assign(value)
8
+ if value.is_a?(String) && value != ""
9
+ data = JSON.parse(value)
10
+ data["id"] = tus_url_to_storage_id(data["id"], cache.storage)
11
+ super(data.to_json)
12
+ else
13
+ super
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def tus_url_to_storage_id(tus_url, storage)
20
+ tus_uid = tus_url.split("/").last
21
+
22
+ if defined?(Storage::FileSystem) && storage.is_a?(Storage::FileSystem)
23
+ "#{tus_uid}.file"
24
+ elsif defined?(Storage::Gridfs) && storage.is_a?(Storage::Gridfs)
25
+ grid_info = storage.bucket.find(filename: tus_uid).limit(1).first
26
+ grid_info[:_id].to_s
27
+ elsif defined?(Storage::S3) && storage.is_a?(Storage::S3)
28
+ tus_uid
29
+ else
30
+ raise Error, "undefined conversion of tus URL to storage id for storage #{storage.inspect}"
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ register_plugin(:tus, Tus)
37
+ end
38
+ end
@@ -0,0 +1,71 @@
1
+ require "shrine/storage/url"
2
+ require "down/chunked_io"
3
+ require "tempfile"
4
+
5
+ class Shrine
6
+ module Storage
7
+ class Tus < Url
8
+ attr_reader :tus_storage
9
+
10
+ def initialize(downloader: :wget, tus_storage: nil)
11
+ @tus_storage = tus_storage
12
+
13
+ super(downloader: downloader)
14
+ end
15
+
16
+ def download(id)
17
+ if tus_storage
18
+ download_from_tus_storage(tus_uid(id))
19
+ else
20
+ super
21
+ end
22
+ end
23
+
24
+ def open(id)
25
+ if tus_storage
26
+ open_from_tus_storage(tus_uid(id))
27
+ else
28
+ super
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def download_from_tus_storage(uid)
35
+ tempfile = Tempfile.new("shrine-tus", binmode: true)
36
+
37
+ response = get_tus_file(uid)
38
+ response.each { |chunk| tempfile << chunk }
39
+ response.close
40
+
41
+ tempfile.open
42
+ tempfile
43
+ end
44
+
45
+ def open_from_tus_storage(uid)
46
+ response = get_tus_file(uid)
47
+
48
+ Down::ChunkedIO.new(
49
+ size: response.length,
50
+ chunks: response.each,
51
+ on_close: ->{response.close},
52
+ )
53
+ end
54
+
55
+ def get_tus_file(uid)
56
+ tus_storage.get_file(uid)
57
+ end
58
+
59
+ def tus_uid(url)
60
+ url.split("/").last
61
+ end
62
+
63
+ # Add "Tus-Resumable" header to HEAD and DELETE requests.
64
+ def request(*args)
65
+ super do |req|
66
+ req["Tus-Resumable"] = "1.0.0"
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,23 @@
1
+ Gem::Specification.new do |gem|
2
+ gem.name = "shrine-tus"
3
+ gem.version = "0.1.0"
4
+
5
+ gem.required_ruby_version = ">= 2.1"
6
+
7
+ gem.summary = "Provides storage and plugin for easier integration between Shrine and tus-ruby-server."
8
+ gem.homepage = "https://github.com/janko-m/shrine-tus"
9
+ gem.authors = ["Janko Marohnić"]
10
+ gem.email = ["janko.marohnic@gmail.com"]
11
+ gem.license = "MIT"
12
+
13
+ gem.files = Dir["README.md", "LICENSE.txt", "lib/**/*.rb", "*.gemspec"]
14
+ gem.require_path = "lib"
15
+
16
+ gem.add_development_dependency "rake"
17
+ gem.add_development_dependency "minitest"
18
+ gem.add_development_dependency "tus-server", "~> 0.10"
19
+ gem.add_development_dependency "shrine-url", "~> 0.3"
20
+ gem.add_development_dependency "shrine-gridfs"
21
+ gem.add_development_dependency "aws-sdk", "~> 2.1"
22
+ gem.add_development_dependency "webmock"
23
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shrine-tus
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Janko Marohnić
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-04-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: tus-server
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: shrine-url
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: shrine-gridfs
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: aws-sdk
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '2.1'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '2.1'
97
+ - !ruby/object:Gem::Dependency
98
+ name: webmock
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description:
112
+ email:
113
+ - janko.marohnic@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - LICENSE.txt
119
+ - README.md
120
+ - lib/shrine/plugins/tus.rb
121
+ - lib/shrine/storage/tus.rb
122
+ - shrine-tus.gemspec
123
+ homepage: https://github.com/janko-m/shrine-tus
124
+ licenses:
125
+ - MIT
126
+ metadata: {}
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '2.1'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubyforge_project:
143
+ rubygems_version: 2.5.1
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: Provides storage and plugin for easier integration between Shrine and tus-ruby-server.
147
+ test_files: []