Add new endpoints to PartiesController

* `preview` shows the preview and queues it up for generation if it doesn't exist yet
* `regenerate_preview` allows the party owner to force regeneration of previews
This commit is contained in:
Justin Edmund 2025-01-18 08:54:04 -08:00
parent 8d047be149
commit 0257469582
2 changed files with 42 additions and 0 deletions

View file

@ -116,6 +116,30 @@ module Api
render_party_json(@parties, count, total_pages)
end
def preview
party = Party.find_by!(shortcode: params[:id])
preview_service = PreviewService::Coordinator.new(party)
redirect_to preview_service.preview_url
end
def regenerate_preview
party = Party.find_by!(shortcode: params[:id])
# Ensure only party owner can force regeneration
unless current_user && party.user_id == current_user.id
return render_unauthorized_response
end
preview_service = PreviewService::Coordinator.new(party)
if preview_service.force_regenerate
render json: { status: 'Preview regeneration started' }
else
render json: { error: 'Preview regeneration failed' },
status: :unprocessable_entity
end
end
private
def authorize

View file

@ -24,6 +24,8 @@ Rails.application.routes.draw do
get 'parties/favorites', to: 'parties#favorites'
get 'parties/:id', to: 'parties#show'
get 'parties/:id/preview', to: 'parties#preview'
post 'parties/:id/regenerate_preview', to: 'parties#regenerate_preview'
post 'parties/:id/remix', to: 'parties#remix'
put 'parties/:id/jobs', to: 'jobs#update_job'
@ -72,4 +74,20 @@ Rails.application.routes.draw do
delete 'favorites', to: 'favorites#destroy'
end
end
if Rails.env.development?
get '/party-previews/*filename', to: proc { |env|
filename = env['action_dispatch.request.path_parameters'][:filename]
path = Rails.root.join('storage', 'party-previews', filename)
if File.exist?(path)
[200, {
'Content-Type' => 'image/png',
'Cache-Control' => 'no-cache' # Prevent caching during development
}, [File.read(path)]]
else
[404, { 'Content-Type' => 'text/plain' }, ['Preview not found']]
end
}
end
end