Provides a simple WebDAV storage for Shrine.
Add this line to your application's Gemfile:
gem 'shrine-webdav'
Suppose you have Report model which should be able to store data in a remote WebDAV storage.
class Report < ApplicationRecord
end
# == Schema Information
#
# Table name: reports
#
# id :uuid not null, primary key
# name :string not null
# created_at :datetime not null
# updated_at :datetime not null
Before you start add attributes pointing to remote files to your model you should describe Uploader class:
# app/models/reports/report_uploader.rb
class ReportUploader < Shrine
plugin :activerecord
plugin :logging, logger: Rails.logger
def generate_location(_io, context)
uuid = context[:record].id
"#{uuid}.#{context[:name]}"
end
end
You don't have to override method generate_location
, but if you didn't you would have random file names.
Now you can add the attributes:
class Report < ApplicationRecord
include ReportUploader::Attachment.new(:pdf)
include ReportUploader::Attachment.new(:xls)
end
Note corresponding migrations:
class AddFileAttributes < ActiveRecord::Migration[5.1]
def change
add_column :reports, :xls_data, :text
add_column :reports, :pdf_data, :text
end
end
Create file shrine.rb
in config/initializers/
to configure WebDAV storage:
# config/initializers/shrine.rb
require 'shrine'
require "shrine/storage/webdav"
Shrine.storages = {
cache: Shrine::Storage::WebDAV.new(host: 'http://webdav-server.com', prefix: 'your_project/cache'),
store: Shrine::Storage::WebDAV.new(host: 'http://webdav-server.com', prefix: 'your_project/store')
}
Now you can use your virtual attributes pdf
and xls
like this:
report = Report.new(name: 'Senseless report')
# here you are going to have file sample.pdf uploaded
# to "http://webdav-server.com/your_project/cache/#{report.id}.pdf"
report.pdf = File.open('sample.pdf')
# file sample.xls is being uploading
# to "http://webdav-server.com/your_project/cache/#{report.id}.xls"
report.xls = File.open('sample.xls')
# after committing in database both files sample.pdf and sample.xls have
# been uploaded to "http://webdav-server.com/your_project/store/..."
report.save
Gem also supports http options timeout
and basic auth
Basic usage:
Shrine::Storage::WebDAV.new(
host: 'http://webdav-server.com',
prefix: 'your_project/store',
http_options: {
basic_auth: { user: 'user', pass: 'pass' },
timeout: { connect: 5, write: 2, read: 10 }
}
)
You can also use global timeouts like this:
Shrine::Storage::WebDAV.new(
host: 'http://webdav-server.com',
prefix: 'your_project/store',
http_options: {
timeout: 3
}
)
After checking out the repo, run bundle install
to install dependencies. Then, run rake spec
to run the tests.
To install this gem onto your local machine, run bundle exec rake install
.
To release a new version, update the version number in shrine-webdav.gemspec
, 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.
Bug reports and pull requests are welcome on GitHub at https://github.com/funbox/shrine-webdav. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.