Skip to content

Commit

Permalink
Merge pull request #329 from lime/improve_gz_handling
Browse files Browse the repository at this point in the history
Set Expires and Cache-Control headers for .gz files [rebased]
  • Loading branch information
PikachuEXE authored Jul 5, 2016
2 parents d9a5921 + b6edf75 commit ac46e66
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/asset_sync/storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ def upload_file(f)
:content_type => mime
}

if /-[0-9a-fA-F]{32,}$/.match(File.basename(f,File.extname(f)))
uncompressed_filename = f.sub(/\.gz\z/, '')
basename = File.basename(uncompressed_filename, File.extname(uncompressed_filename))
if /-[0-9a-fA-F]{32,}$/.match(basename)
file.merge!({
:cache_control => "public, max-age=#{one_year}",
:expires => CGI.rfc1123_date(Time.now + one_year)
Expand Down
22 changes: 20 additions & 2 deletions spec/unit/storage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,20 @@


it 'should correctly set expire date' do
local_files = ['file1.jpg', 'file1-1234567890abcdef1234567890abcdef.jpg']
local_files += ['dir1/dir2/file2.jpg', 'dir1/dir2/file2-1234567890abcdef1234567890abcdef.jpg']
local_files = [
'file1.jpg',
'file1-1234567890abcdef1234567890abcdef.jpg',
'file1-1234567890abcdef1234567890abcdef.jpg.gz',
'file1-1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef.jpg',
'file1-1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef.jpg.gz'
]
local_files += [
'dir1/dir2/file2.jpg',
'dir1/dir2/file2-1234567890abcdef1234567890abcdef.jpg',
'dir1/dir2/file2-1234567890abcdef1234567890abcdef.jpg.gz',
'dir1/dir2/file2-1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef.jpg',
'dir1/dir2/file2-1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef.jpg.gz'
]
remote_files = []
storage = AssetSync::Storage.new(@config)
allow(storage).to receive(:local_files).and_return(local_files)
Expand All @@ -99,7 +111,13 @@ def check_file(file)
when 'dir1/dir2/file2.jpg'
!expect(file).not_to include(:cache_control, :expires)
when 'file1-1234567890abcdef1234567890abcdef.jpg'
when 'file1-1234567890abcdef1234567890abcdef.jpg.gz'
when 'file1-1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef.jpg'
when 'file1-1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef.jpg.gz'
when 'dir1/dir2/file2-1234567890abcdef1234567890abcdef.jpg'
when 'dir1/dir2/file2-1234567890abcdef1234567890abcdef.jpg.gz'
when 'dir1/dir2/file2-1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef.jpg'
when 'dir1/dir2/file2-1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef.jpg.gz'
expect(file).to include(:cache_control, :expires)
else
fail
Expand Down

0 comments on commit ac46e66

Please sign in to comment.