expiring_asset_links
CarrierWave FOG Expiring Asset Links
Handles storing and generating CarrierWave FOG expiring asset link stored in string and text attributes of ActiveRecord objects
This is helpful when using a WYSIWYG that inserts and previews assets directly from S3 using full S3 URLs. These URLs need to be updated each time the object is reloaded. When used in conjunction with CarrierWave and FOG gems, this gem will automatically update these links for you.
Dependencies
the following gems are required
gem 'rails', '>= 4.0.0'
gem 'carrierwave'
gem 'fog'
Installation
install manually
gem install expiring_asset_links
or add it to your Gemfile
gem 'carrierwave'
Usage
configure CarrierWave
your configuration must satisfy the following requirements:
- define
storage :fog
- set
Expires
ins3_headers
class AssetUploader < CarrierWave::Uploader::Base
storage :fog
def s3_headers
{ "Expires" => 5.minutes.from_how.httpdate }
end
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
add an initializer with your CarrierWave Uploader store_dir
matching Regexp
if you wish to change the default store_dir
in your CarrierWave Uploader you will need to create a Regexp that captures the object class name and id from the path
create an initializer, such as config/initializers/expiring_asset_links.rb
and include your configuration with Regexp
ExpiringAssetLinks.configure do |conf|
conf.fog_directory = /\/\S+\/(?<name>[a-z_]+)\/[a-z_]+\/(?<id>\d+)/
end
the path defined in CarrierWave Uploader store_dir
, such as uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}
, must match the Regexp in your configuration once generated by CarrierWave and must capture the class name as <name>
and the object id as <id>
/\/\S+\/(?<name>[a-z_]+)\/[a-z_]+\/(?<id>\d+)/.match("uploads/class_name/uploader_name/100")
#=> #<MatchData "uploads/class_name/uploader_name/100" name:"model_class_name" id:"100">
mount an uploader for any class containing an assets
you may use as many different classes as you would like, but each may only contain one mounted uploader
class Image
mount_uploader :image, ImageUploader
end
class Attachment
mount_uploader :file, FileUploader
end
specify the attributes that will contain asset links
the attributes specified should contain fully qualified asset URLs generated by CarrierWave with the Expires
value specified when saved, these URLs will be automatically updated each time the attribute is accessed
class Document < ActiveRecord::Base
attr_expiring_asset_links :body
end
class Article < ActiveRecord::Base
attr_expiring_asset_links :summary, :body
end
Notes
the url that is generated by CarrierWave may not immediately follow a letter or underscore in the content of the attribute specified as attr_expiring_asset_links
, this will prevent the gem from properly parsing the data
example of proper placement (the double quote character is no a letter or an underscore)
<h2>
Section One
</h2>
<p>
This is the first section in the body of the document.
It includes an image.
</p>
<img src="https://test.s3-us-east-1.amazonaws.com/uploads/test/99/asset/file_attachment/sample.jpg">
example of improper placement (no space between 'at' and 'https')
<h2>
Section One
</h2>
<p>
This is the first section in the body of the document.
It includes an image, you may view it athttps://test.s3-us-east-1.amazonaws.com/uploads/test/99/asset/file_attachment/sample.jpg
</p>