This repository has been archived by the owner on Apr 4, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
68 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
CDN asset URL helpers | ||
===================== | ||
|
||
This library contains a Rubygem and Django custom templatetag library to do the following: | ||
|
||
turn an asset path like | ||
|
||
/images/thing.png | ||
|
||
into a CDN-pointed URL like | ||
|
||
http://cdn.name.com/images/thing.png/{sha1}.png | ||
|
||
Rails | ||
----- | ||
|
||
Add `cdn_helpers` to your `Gemfile`: | ||
|
||
gem 'cdn_helpers', :git => 'git@github.com:alphagov/cdn_helpers.git' | ||
|
||
In your `environments/production.rb`: | ||
|
||
require 'cdn_helpers' | ||
config.action_controller.asset_host = "http://cdn.url.gov.uk" | ||
config.action_controller.asset_path = CdnHelpers::AssetPath | ||
|
||
Django | ||
------ | ||
|
||
Add `cdn_helpers` to your `requirements.txt`: | ||
|
||
-e git+ssh://git@github.com/alphagov/cdn_helpers.git#egg=cdn_helpers | ||
|
||
in `settings.py` add cdn_helpers to your list of installed apps: | ||
|
||
INSTALLED_APPS = ( | ||
'django.contrib.auth', | ||
... | ||
'cdn_helpers' | ||
) | ||
|
||
in the appropriate `local_settings.py`: | ||
|
||
APP_DEPLOYMENT_ENV = 'dev' # (or staging, production) | ||
CDN_HOSTS = ['cdn1', 'cdn2'] # (as appropriate for the environment) | ||
|
||
|
||
in templates use the `asset_url` helper: | ||
|
||
<img src="{% asset_url "/images/thing.png" %}"> | ||
<img src="{% asset_url thing.image.src %}"> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,18 @@ | ||
require 'digest/sha1' | ||
module CdnHelpers | ||
# Your code goes here... | ||
module AssetPath | ||
def self.asset_cache | ||
@@asset_cache ||= {} | ||
end | ||
|
||
def self.call(path) | ||
unless asset_cache.has_key?(path) | ||
file_path = Rails.root.join('public', path.sub(/^\//, '')).to_s | ||
sha1 = Digest::SHA1.file(file_path).hexdigest | ||
extension = File.extname(path) | ||
return asset_cache[path] = path + "/#{sha1[0..7]}#{extension}" | ||
end | ||
asset_cache[path] | ||
end | ||
end | ||
end |