https://github.com/shrinerb/shrine-url
Custom URL storage for Shrine
https://github.com/shrinerb/shrine-url
direct-upload download shrine url
Last synced: 19 days ago
JSON representation
Custom URL storage for Shrine
- Host: GitHub
- URL: https://github.com/shrinerb/shrine-url
- Owner: shrinerb
- License: mit
- Created: 2016-07-11T02:58:38.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2022-04-16T11:57:32.000Z (about 3 years ago)
- Last Synced: 2024-04-14T08:30:45.066Z (about 1 year ago)
- Topics: direct-upload, download, shrine, url
- Language: Ruby
- Homepage:
- Size: 51.8 KB
- Stars: 16
- Watchers: 5
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Shrine::Storage::Url
Provides a "storage" for [Shrine] for attaching uploaded files defined by a
custom URL.## Installation
```rb
gem "shrine-url", "~> 2.4"
```## Usage
```rb
require "shrine/storage/url"Shrine.storages[:cache] = Shrine::Storage::Url.new
```The custom URL should be assigned to the `id` field in the Shrine uploaded file
JSON representation:```rb
{
"id": "http://example.com/image.jpg",
"storage": "cache",
"metadata": {
# ...
}
}
```Now you can assign this data as the cached attachment:
```rb
photo = Photo.new(image: data)
photo.image #=> #
photo.image.url #=> "http://example.com/image.jpg"
```No HTTP requests are made when file is assigned (but you can load the
`restore_cached_data` Shrine plugin if you want metadata to be extracted on
assignment). When this "cached file" is about to be uploaded to a permanent
storage, `shrine-url` will download the file from the given URL using [Down].```rb
uploaded_file.download # Sends a GET request and streams body to Tempfile
uploaded_file.open { |io| } # Sends a GET request and yields `Down::ChunkedIO` ready for reading
uploaded_file.exists? # Sends a HEAD request and returns true if response status is 2xx
uploaded_file.delete # Sends a DELETE request if :delete is set to true
```By default the `Down::Http` backend will be used for downloading, which is
implemented using [HTTP.rb]. You can change the Down backend via the
`:downloader` option:```rb
Shrine::Storage::Url.new(downloader: :wget)
# or
require "down/http"
Shrine::Storage::Url.new(downloader: Down::Http)
# or
require "down/net_http"
Shrine::Storage::Url.new(downloader: Down::NetHttp.new("User-Agent" => "MyApp/1.0.0"))
```Note that if you're using permanent storage that supports uploading from a
remote URL (like [shrine-cloudinary] or [shrine-uploadcare]), downloading will
be completely skipped as the permanent storage will use only the URL for
uploading the file.## Deleting
Calling `Shrine::UploadedFile#delete` will call `Shrine::Storage::Url#delete`,
which for safety doesn't do anything by default. If you want it to make a
`DELETE` request to the URL, you can set `:delete` to `true` on initialization:```rb
Shrine::Storage::Url.new(delete: true)
```## Advantages and Use Cases
The main advantage of using `shrine-url` over the `remote_url` Shrine plugin is
that you can put downloading from the URL into a background job by loading the
`backgrounding` Shrine plugin. Another advantage is that you can assign
multiple remote URLs as multiple versions.This storage can be used with [shrine-transloadit] for direct uploads, where a
temporary URL of the uploaded file is returned, and we want to use that URL for
further background processing, eventually replacing the attachment with
processed files.It is also used in [shrine-tus-demo], where the files are uploaded to a
separate endpoint, and then its file URL is attached to a database record and
promoted to permanent storage.## Contributing
```sh
$ rake test
```The test suite pulls and runs [kennethreitz/httpbin] as a Docker container, so
you'll need to have Docker installed and running.## License
[MIT](/LICENSE.txt)
[Shrine]: https://github.com/shrinerb/shrine
[shrine-transloadit]: https://github.com/shrinerb/shrine-transloadit
[shrine-tus-demo]: https://github.com/shrinerb/shrine-tus-demo
[shrine-cloudinary]: https://github.com/shrinerb/shrine-cloudinary
[shrine-uploadcare]: https://github.com/shrinerb/shrine-uploadcare
[Down]: https://github.com/janko/down
[HTTP.rb]: https://github.com/httprb/http
[kennethreitz/httpbin]: https://github.com/kennethreitz/httpbin