Serve uploads from outside of public dir

rails
Marcin Kulik 8 years ago
parent 2fb6194db0
commit 56cea2fb39

@ -28,18 +28,12 @@ class AsciicastsController < ApplicationController
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
@ -98,4 +92,19 @@ class AsciicastsController < ApplicationController
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

@ -93,8 +93,16 @@ class Asciicast < ActiveRecord::Base
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

@ -11,7 +11,7 @@ class AsciicastSerializer < ActiveModel::Serializer
if v0_url?
object.stdout_frames_url
else
object.data_url
asciicast_path(object, format: :json)
end
end

@ -10,5 +10,11 @@ CarrierWave.configure do |config|
}
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

@ -7,7 +7,7 @@ feature "asciicast-as-png", needs_phantomjs_2_bin: true do
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

@ -13,7 +13,7 @@ describe AsciicastSerializer do
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

@ -17,7 +17,7 @@ describe AsciicastImageGenerator, needs_phantomjs_2_bin: true do
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])

@ -0,0 +1 @@
*
Loading…
Cancel
Save