Support downloading asciicast JSON file as "attachment"

This commit is contained in:
Marcin Kulik 2016-01-10 18:42:13 +00:00
parent e29f755cbc
commit b8f90166c1
4 changed files with 15 additions and 9 deletions

View File

@ -28,11 +28,13 @@ class AsciicastsController < ApplicationController
end
format.json do
if url = asciicast.file_url
redirect_to url
else
render nothing: true, status: 404
end
opts = if params[:dl]
{ query: { "response-content-disposition" => "attachment; filename=#{asciicast.download_filename}" } }
else
{}
end
redirect_to asciicast.data_url(opts)
end
format.png do

View File

@ -85,8 +85,12 @@ class Asciicast < ActiveRecord::Base
end
end
def data_url
file_url || stdout_frames_url
def data_url(options = {})
file_url(options) || stdout_frames_url(options)
end
def download_filename
"asciicast-#{id}.json"
end
def stdout

View File

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

View File

@ -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.to_param}.json")
end
it 'includes snapshot' do