class VideosController < ApplicationController before_action :set_video, only: [:show, :stream, :playback_position, :retry_processing] def show @work = @video.work @last_position = get_last_playback_position end def stream file_path = @video.web_stream_path unless file_path && File.exist?(file_path) head :not_found return end send_file file_path, filename: @video.filename, type: 'video/mp4', disposition: 'inline', stream: true, buffer_size: 4096 end def playback_position position = params[:position].to_i session = get_or_create_playback_session session.update!(position: position, last_played_at: Time.current) head :ok end def retry_processing VideoProcessorJob.perform_later(@video.id) redirect_to @video, notice: 'Video processing has been queued.' end private def set_video @video = Video.find(params[:id]) end def get_last_playback_position # Get from current user's session or cookie session_key = "video_position_#{@video.id}" session[session_key] || 0 end def get_or_create_playback_session # For Phase 1, we'll use a simple session-based approach # Phase 2 will use proper user authentication PlaybackSession.find_or_initialize_by( video: @video, session_id: session.id.to_s, user_id: nil # Will be populated in Phase 2 ) end end