diff --git a/app/controllers/api/v1/parties_controller.rb b/app/controllers/api/v1/parties_controller.rb index 35a620d..409abf3 100644 --- a/app/controllers/api/v1/parties_controller.rb +++ b/app/controllers/api/v1/parties_controller.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 4a7e1dc..9ec0148 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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