Skip to content

Commit

Permalink
Calculate Fog expiration taking DST into account
Browse files Browse the repository at this point in the history
  • Loading branch information
mshibuya authored and joemsak committed Mar 27, 2021
1 parent 10556ce commit f53f15a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/carrierwave/storage/fog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def authenticated_url(options = {})
# avoid a get by using local references
local_directory = connection.directories.new(:key => @uploader.fog_directory)
local_file = local_directory.files.new(:key => path)
expire_at = options[:expire_at] || ::Fog::Time.now + @uploader.fog_authenticated_url_expiration
expire_at = options[:expire_at] || ::Fog::Time.now.since(@uploader.fog_authenticated_url_expiration.to_i)
case @uploader.fog_credentials[:provider]
when 'AWS', 'Google'
# Older versions of fog-google do not support options as a parameter
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
require 'open-uri'
require "webmock/rspec"
require 'mini_magick'
require "vips"
require 'active_support/core_ext'

I18n.enforce_available_locales = false

Expand Down
28 changes: 28 additions & 0 deletions spec/storage/fog_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,34 @@ def check_file
end
end

context 'in a timezone with DST' do
before do
@prev_tz = ENV['TZ']
ENV['TZ'] = 'US/Pacific'
end
after { ENV['TZ'] = @prev_tz }

it "should generate a proper X-Amz-Expires when expires spans a move to DST" do
if @provider == 'AWS'
Timecop.freeze(Time.at(1477932000)) do |now|
expiration = 7 * 24 * 60 * 60 # 1 week
allow(@uploader).to receive(:fog_authenticated_url_expiration).and_return(expiration)
expect(@fog_file.authenticated_url).to include("X-Amz-Expires=#{expiration.to_s}")
end
end
end

it "should generate a proper X-Amz-Expires when expires spans a move to DST and an ActiveRecord::Duration is provided" do
if @provider == 'AWS'
Timecop.freeze(Time.at(1477932000)) do |now|
expiration = 1.week
allow(@uploader).to receive(:fog_authenticated_url_expiration).and_return(expiration)
expect(@fog_file.authenticated_url).to include("X-Amz-Expires=#{expiration.to_s}")
end
end
end
end

it 'should generate correct filename' do
expect(@fog_file.filename).to eq('private.txt')
end
Expand Down

0 comments on commit f53f15a

Please sign in to comment.