Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/glacials/splits-io. a speedrunning data store and analysis engine splits.io
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193require 'htmlentities'require 'uri'
require 'speedrundotcom'
class RunsController < ApplicationController before_action :set_run, only: [:show, :download, :destroy, :compare, :edit, :update] before_action :set_comparison, only: :compare
before_action :handle_first_visit, only: [:show, :edit, :update], unless: Proc.new { @run.visited? } before_action :warn_about_deprecated_url, only: [:show], if: Proc.new { request.path == "/#{@run.nick}" } before_action :reject_as_unparsable, only: [:show], unless: Proc.new { @run.parses? }
before_action :attempt_to_claim, only: [:show]
def show if params['reparse'] == '1' @run.clear_dynamodb_rows @run.parse_into_dynamodb redirect_to run_path(@run) return end end
def index end
def new end
def edit if cannot?(:edit, @run) render :forbidden, status: 403 return end end
def update if cannot?(:edit, @run) render :forbidden, status: 403 return end
unless params[@run.id36].respond_to?(:[]) redirect_to edit_run_path(@run), alert: "There was an error saving that data. Please try again." end
if params[@run.id36][:category] @run.update(category: Category.find(params[@run.id36][:category])) redirect_to edit_run_path(@run), notice: 'Game/category updated.' return end
if params[@run.id36][:disown] @run.update(user: nil) redirect_to run_path(@run), notice: 'Run disowned.' return end
if params[@run.id36][:srdc_url] srdc_id = SpeedrunDotCom::Run.id_from_url(params[@run.id36][:srdc_url]) if !srdc_id redirect_params = {alert: 'Your speedrun.com URL must have the format http://www.speedrun.com/run/6yjoqgzd.'} else if !@run.update(srdc_id: srdc_id) redirect_params = {alert: "There was an error updating your SRDC link. Please try again."} else redirect_params = {notice: "Link with Speedrun.com updated."} end end redirect_to edit_run_path(@run), redirect_params return end
if params[@run.id36][:video_url] if @run.update(video_url: params[@run.id36][:video_url]) redirect_to edit_run_path(@run), notice: 'Proof saved.' else redirect_to edit_run_path(@run), alert: @run.errors.full_messages.join(' ') end end end
def download # Enable CORS for this endpoint so clients can download files; this should be an API endpoint but I'm not sure how I # want the timer-specific views to belong to both an API namespace and a user namespace headers['Access-Control-Allow-Origin'] = '*' headers['Access-Control-Allow-Methods'] = 'POST, PUT, DELETE, GET, OPTIONS' headers['Access-Control-Request-Method'] = '*' headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Authorization'
timer = Run.program(params[:timer]) if timer.nil? redirect_to run_path(@run), alert: 'Unrecognized timer.' return end
begin s3_file = $s3_bucket.object("splits/#{@run.s3_filename}")
if timer == Run.program(@run.timer) && s3_file.exists? redirect_to s3_file.presigned_url(:get, response_content_disposition: "attachment; filename=\"#{@run.filename}\"" ) return end rescue Aws::S3::Errors::Forbidden end
send_data( if timer == Run.program(@run.timer) @run.original_file else render_to_string(params[:timer], layout: false) end, filename: "#{@run.filename(timer: timer)}", layout: false ) end
def random redirect_to Run.find_by(run_file: RunFile.random) end
def destroy if cannot?(:destroy, @run) render :forbidden, status: 403 return end
if @run.destroy redirect_to root_path, notice: 'Run deleted.' else redirect_to root_path, alert: 'There was an error deleting your run. Please try again.' end end
private
def set_run @run = Run.includes(:user, :category, :game, :segments).find_by(id: params[:run].to_i(36)) || Run.find_by!(nick: params[:run]) gon.run = {id: @run.id36, splits: @run.collapsed_segments} gon.scale_to = @run.duration_milliseconds rescue ActionController::UnknownFormat, ActiveRecord::RecordNotFound render :not_found, status: 404 end
def set_comparison return if params[:comparison_run].blank? @comparison_run = Run.find_by(id: params[:comparison_run].to_i(36)) || Run.find_by!(nick: params[:comparison_run]) gon.comparison_run = { id: @comparison_run.id36, splits: @comparison_run.collapsed_segments } gon.scale_to = [@run.duration_milliseconds, @comparison_run.duration_milliseconds].max rescue ActiveRecord::RecordNotFound render :not_found, status: 404 end
def warn_about_deprecated_url redirect_to run_path(@run), flash: { icon: 'warning-sign', alert: "This run's permalink has changed. You have been redirected to the newer one. \ <a href='#{why_permalinks_path}'>More info</a>.".html_safe } end
def reject_as_unparsable if @run.visited? render :cant_parse, status: 500 else @run.destroy redirect_to cant_parse_path end end
def attempt_to_claim if @run.user == nil && @run.claim_token.present? && @run.claim_token == params[:claim_token] @run.update(user: current_user) redirect_to run_path(@run) end end
def handle_first_visit if @run.parses? @run.update(visited: true) else @run.destroy redirect_to cant_parse_path end endend