Update parties_controller.rb
* Adds retry header if generation is still in progress * Streams S3 content instead of redirecting to prevent 302
This commit is contained in:
parent
8207b3a09a
commit
8f2a7fe96e
1 changed files with 34 additions and 3 deletions
|
|
@ -117,10 +117,41 @@ module Api
|
||||||
end
|
end
|
||||||
|
|
||||||
def preview
|
def preview
|
||||||
party = Party.find_by!(shortcode: params[:id])
|
coordinator = PreviewService::Coordinator.new(@party)
|
||||||
|
|
||||||
preview_service = PreviewService::Coordinator.new(party)
|
if coordinator.generation_in_progress?
|
||||||
redirect_to preview_service.preview_url
|
response.headers['Retry-After'] = '2'
|
||||||
|
default_path = Rails.root.join('public', 'default-previews', "#{@party.element || 'default'}.png")
|
||||||
|
send_file default_path,
|
||||||
|
type: 'image/png',
|
||||||
|
disposition: 'inline'
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
# Try to get the preview or send default
|
||||||
|
begin
|
||||||
|
if Rails.env.production?
|
||||||
|
# Stream S3 content instead of redirecting
|
||||||
|
s3_object = coordinator.get_s3_object
|
||||||
|
send_data s3_object.body.read,
|
||||||
|
filename: "#{@party.shortcode}.png",
|
||||||
|
type: 'image/png',
|
||||||
|
disposition: 'inline'
|
||||||
|
else
|
||||||
|
# In development, serve from local filesystem
|
||||||
|
send_file coordinator.local_preview_path,
|
||||||
|
type: 'image/png',
|
||||||
|
disposition: 'inline'
|
||||||
|
end
|
||||||
|
rescue Aws::S3::Errors::NoSuchKey
|
||||||
|
# Schedule generation if needed
|
||||||
|
coordinator.schedule_generation unless coordinator.generation_in_progress?
|
||||||
|
|
||||||
|
# Return default preview while generating
|
||||||
|
send_file Rails.root.join('public', 'default-previews', "#{@party.element || 'default'}.png"),
|
||||||
|
type: 'image/png',
|
||||||
|
disposition: 'inline'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def regenerate_preview
|
def regenerate_preview
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue