Skip to content

Commit

Permalink
Serve uploads from outside of public dir
Browse files Browse the repository at this point in the history
  • Loading branch information
ku1ik committed Apr 10, 2016
1 parent 2fb6194 commit 56cea2f
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 13 deletions.
25 changes: 17 additions & 8 deletions app/controllers/asciicasts_controller.rb
Expand Up @@ -28,18 +28,12 @@ def show
end

format.json do
opts = if params[:dl]
{ query: { "response-content-disposition" => "attachment; filename=#{asciicast.download_filename}" } }
else
{}
end

redirect_to asciicast.data_url(opts)
serve_file(asciicast.data, !!params[:dl])
end

format.png do
asciicast_image_generator.generate(asciicast) if asciicast.image_stale?
redirect_to asciicast.image_url
serve_file(asciicast.image)
end
end
end
Expand Down Expand Up @@ -98,4 +92,19 @@ def asciicast_image_generator
AsciicastImageGenerator.new(self)
end

def serve_file(uploader, as_attachment = false)
opts = if as_attachment
{ query: { "response-content-disposition" => "attachment; filename=#{asciicast.download_filename}" } }
else
{}
end

url = uploader.url(opts)

if url.starts_with?("/")
send_file uploader.path, disposition: as_attachment ? 'attachment' : 'inline'
else
redirect_to url
end
end
end
10 changes: 9 additions & 1 deletion app/models/asciicast.rb
Expand Up @@ -93,8 +93,16 @@ def to_param
end
end

def data
if file.present?
file
else
stdout_frames
end
end

def data_url(options = {})
file_url(options) || stdout_frames_url(options)
data.url(options)
end

def download_filename
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/asciicast_serializer.rb
Expand Up @@ -11,7 +11,7 @@ def url
if v0_url?
object.stdout_frames_url
else
object.data_url
asciicast_path(object, format: :json)
end
end

Expand Down
6 changes: 6 additions & 0 deletions config/initializers/carrierwave.rb
Expand Up @@ -10,5 +10,11 @@
}
config.fog_directory = CFG.aws_bucket
config.fog_public = false
elsif CFG.carrierwave_storage == 'file'
config.root = Rails.root
end
end

if File.exists?(Rails.root.to_s + "/public/uploads/asciicast")
raise "Please move all directories from ./public/uploads/ to ./uploads/"
end
2 changes: 1 addition & 1 deletion spec/features/png_spec.rb
Expand Up @@ -7,7 +7,7 @@
scenario "Requesting PNG" do
visit asciicast_path(asciicast, format: :png)

expect(current_path).to match(%r{/uploads/test/asciicast/image/\d+/\w+\.png$})
expect(current_path).to match(%r{/a/\d+\.png$})
end

end
2 changes: 1 addition & 1 deletion spec/serializers/asciicast_serializer_spec.rb
Expand Up @@ -13,7 +13,7 @@
end

it 'includes url' do
expect(subject['url']).to eq(asciicast.file_url)
expect(subject['url']).to eq("/a/#{asciicast.id}.json")
end

it 'includes snapshot' do
Expand Down
2 changes: 1 addition & 1 deletion spec/services/asciicast_image_generator_spec.rb
Expand Up @@ -17,7 +17,7 @@ def rgb(color)
end

it 'generates screenshot of "snapshot frame"' do
png = ChunkyPNG::Image.from_file("#{Rails.root}/public/#{asciicast.image_url}")
png = ChunkyPNG::Image.from_file(asciicast.image.path)

# make sure there are black-ish borders
expect(rgb(png[1, 1])).to eq([18, 19, 20])
Expand Down
1 change: 1 addition & 0 deletions uploads/.gitignore
@@ -0,0 +1 @@
*

0 comments on commit 56cea2f

Please sign in to comment.