shrine-url 0.2.1 → 0.3.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a8b08fda0c471e0bd77b59fbd0b0c7dcef319c45
4
- data.tar.gz: e7bf1aa59cc2890f564404a70692acd56a2a4c8a
3
+ metadata.gz: 05b3f6c5186e58f0cf26662a88f33a956d17d2fc
4
+ data.tar.gz: 321ba3cf1043b2913a6dccf3056d024582072f69
5
5
  SHA512:
6
- metadata.gz: 6d798794e1e07afcfb5ea292424307bd35a456e1cbe470409c234fd046f1818b7c733e1309e1b1f8ea48e60bd366789efe6e126800f9b40966740c36c84c97b4
7
- data.tar.gz: 85fb9f5f702e3a53bf815537d32a614a8f9527dbb1ecd6fdc2e2a702dd758c839a1404dd54d87d5cbd21568d46f51a4a5068a8f417488407838bfe2a96537975
6
+ metadata.gz: c99cdb69217eef960d553f69d1d7b4191c527bf599eeb05432e80d093d3d26325a7807b9797cc7df9021e2acd99193e41cbd8431ad0c139b812d735572d9d0a5
7
+ data.tar.gz: 6e9589ed93f23b8e578177b1b7e67e87241374b3c424259c0458af361309530190b1141b091c0656e62911d87d9997ab31f2ed0ee7fbb02693508c4e8c6f8263
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Shrine::Storage::Url
2
2
 
3
- Provides a "storage" which allows you to save uploaded files defined by a
3
+ Provides a "storage" which allows you to attach uploaded files defined by a
4
4
  custom URL.
5
5
 
6
6
  ## Installation
@@ -17,16 +17,23 @@ require "shrine/storage/url"
17
17
  Shrine.storages[:cache] = Shrine::Storage::Url.new
18
18
  ```
19
19
 
20
- ```rb
21
- photo = Photo.new
20
+ The custom URL should be assigned to the `id` field in the Shrine uploaded file
21
+ JSON representation:
22
22
 
23
- attachment_data = {
24
- id: "http://example.com/image.jpg",
25
- storage: "cache",
26
- metadata: {...}
23
+ ```rb
24
+ {
25
+ "id": "http://example.com/image.jpg",
26
+ "storage": "cache",
27
+ "metadata": {
28
+ # ...
29
+ }
27
30
  }
31
+ ```
28
32
 
29
- photo.image = attachment_data.to_json
33
+ Now you can assign this data as the cached attachment:
34
+
35
+ ```rb
36
+ photo = Photo.new(image: data)
30
37
  photo.image #=> #<Shrine::UploadedFile>
31
38
 
32
39
  photo.image.url #=> "http://example.com/image.jpg"
@@ -36,12 +43,34 @@ photo.image.exists? # Sends a HEAD request and returns true if it's 2xx
36
43
  photo.image.delete # Sends a DELETE request
37
44
  ```
38
45
 
39
- The custom URL can be saved to `id`, and `#url` will simply read that field.
40
- When this `Shrine::UploadedFile` is uploaded to another storage (e.g. permanent
41
- storage), if the storage doesn't support upload from URL the file will simply
42
- be downloaded from the custom URL, just like for any other storage.
46
+ No HTTP requests are made at this point. When the cached file is uploaded to
47
+ permanent storage, `shrine-url` will by default use [Down] to download the file
48
+ from the given URL, before uploading it to permanent storage.
49
+
50
+ Note that Down doesn't yet support resuming the download in case of network
51
+ failures, so if you're expecting large files to be attached, you might want to
52
+ tell `Shrine::Storage::Url` to use `wget` instead of Down.
53
+
54
+ ```rb
55
+ Shrine::Storage::Url.new(downloader: :wget)
56
+ ```
57
+
58
+ Just note that using `wget` won't work well with the `restore_cached_data`
59
+ Shrine plugin from the performance standpoint, because `wget` doesn't support
60
+ partial downloads, so the file would first be fully downloaded before
61
+ extracting metadata.
62
+
63
+ If you're using permanent storage that supports uploading from remote URL (like
64
+ [shrine-cloudinary] or [shrine-uploadcare]), downloading will be completely
65
+ skipped and the permanent storage will use only the custom URL for uploading
66
+ the file.
67
+
68
+ ## Advantages and Use Cases
43
69
 
44
- ## Use cases
70
+ The main advantage of using `shrine-url` over the `remote_url` Shrine plugin is
71
+ that you can put downloading from the URL into a background job by loading the
72
+ `backgrounding` Shrine plugin. Another advantage is that you can assign
73
+ multiple remote URLs as multiple versions.
45
74
 
46
75
  This storage can be used with [shrine-transloadit] for direct uploads, where a
47
76
  temporary URL of the uploaded file is returned, and we want to use that URL for
@@ -64,3 +93,6 @@ $ rake test
64
93
 
65
94
  [shrine-transloadit]: https://github.com/janko-m/shrine-transloadit
66
95
  [shrine-tus-demo]: https://github.com/janko-m/shrine-tus-demo
96
+ [shrine-cloudinary]: https://github.com/janko-m/shrine-cloudinary
97
+ [shrine-uploadcare]: https://github.com/janko-m/shrine-uploadcare
98
+ [Down]: https://github.com/janko-m/down
@@ -1,20 +1,23 @@
1
1
  require "shrine"
2
- require "down"
3
2
  require "net/http"
4
3
 
5
4
  class Shrine
6
5
  module Storage
7
6
  class Url
7
+ def initialize(downloader: :down)
8
+ @downloader = Downloader.new(downloader)
9
+ end
10
+
8
11
  def upload(io, id, **)
9
12
  id.replace(io.url)
10
13
  end
11
14
 
12
15
  def download(id)
13
- Down.download(id)
16
+ @downloader.download(id)
14
17
  end
15
18
 
16
19
  def open(id)
17
- Down.open(id)
20
+ @downloader.open(id)
18
21
  end
19
22
 
20
23
  def exists?(id)
@@ -44,6 +47,58 @@ class Shrine
44
47
 
45
48
  response
46
49
  end
50
+
51
+ class Downloader
52
+ SUPPORTED_TOOLS = [:down, :wget]
53
+
54
+ def initialize(tool)
55
+ raise ArgumentError, "unsupported downloading tool: #{tool}" unless SUPPORTED_TOOLS.include?(tool)
56
+
57
+ @tool = tool
58
+ end
59
+
60
+ def download(url)
61
+ send(:"download_with_#{@tool}", url)
62
+ end
63
+
64
+ def open(url)
65
+ send(:"open_with_#{@tool}", url)
66
+ end
67
+
68
+ private
69
+
70
+ def download_with_down(url)
71
+ require "down"
72
+ Down.download(url)
73
+ end
74
+
75
+ def open_with_down(url)
76
+ require "down"
77
+ Down.open(url)
78
+ end
79
+
80
+ def download_with_wget(url)
81
+ require "tempfile"
82
+ require "open3"
83
+
84
+ tempfile = Tempfile.new("shrine-url", binmode: true)
85
+ cmd = %W[wget --no-verbose #{url} -O #{tempfile.path}]
86
+ stdout, stderr, status = Open3.capture3(*cmd)
87
+
88
+ if !status.success?
89
+ tempfile.close!
90
+ raise "downloading from #{url} failed: #{stderr}"
91
+ end
92
+
93
+ tempfile.tap(&:open)
94
+ end
95
+
96
+ def open_with_wget(url)
97
+ tempfile = download_with_wget(url)
98
+ tempfile.instance_eval { def close(unlink_now=true) super end } # delete tempfile when it's closed
99
+ tempfile
100
+ end
101
+ end
47
102
  end
48
103
  end
49
104
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = "shrine-url"
3
- gem.version = "0.2.1"
3
+ gem.version = "0.3.0"
4
4
 
5
5
  gem.required_ruby_version = ">= 2.1"
6
6
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shrine-url
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janko Marohnić
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-27 00:00:00.000000000 Z
11
+ date: 2017-04-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: shrine