<commit_before>require 'htmlentities'

class RunsController < ApplicationController
  def create
    splits = params[:file]
    run = Run.create(nick: new_nick, user: nil)
    if splits.present?
      File.open(Rails.root.join('private', 'runs', run.nick), 'wb') do |file|
        file.write splits.read
      end
      respond_to do |format|
        format.html { redirect_to run_path run.nick }
        format.json { render text: run.nick }
      end
    else
      respond_to do |format|
        format.html { redirect_to upload_path, alert: "Didn't receive any uploaded file, try again." }
      end
    end
  end
  def show
    @random = session[:random] or false
    session[:random] = false

    @run_record = Run.find_by nick: params[:nick]
    render :bad_url and return if @run_record.nil?

    @run_record.hits += 1 and @run_record.save
    @run = parse @run_record
    if @run.present?
      respond_to do |format|
        format.html { render :show }
        format.json { render @run_record }
      end
    else
      if @run_record.hits > 1 then render :cant_parse
      else @run_record.destroy and redirect_to cant_parse_path end
    end
  end
  def download
    @run_record = Run.find_by nick: params[:nick]
    render :bad_url and return if @run_record.nil?

    @run_record.hits += 1 and @run_record.save
    @run = parse @run_record
    if @run.present?
      file_extension = params[:format] == 'livesplit' ? '.lss' : params[:format]
      send_data(HTMLEntities.new.decode(render_to_string(params[:format], layout: false)), filename: @run_record.nick + "." + params[:format], content_type: "text/html", layout: false)
    else
      if @run_record.hits > 1 then render :cant_parse
      else @run_record.destroy and redirect_to cant_parse_path end
    end
  end
  def download_original
    @run_record = Run.find_by nick: params[:nick]
    render :bad_url and return if @run_record.nil?

    @run_record.hits += 1 and @run_record.save
    
  end
  def random
    # Find a random run. If we can't parse it, find another, and so on.
    run = nil
    if request.env["HTTP_REFERER"].present?
      # Don't use the run that we just came from (i.e. we're spam clicking random)
      last_run = URI.parse(URI.encode request.env["HTTP_REFERER"].strip).path<commit_msg>LiveSplit downloads now have the correct extension<commit_after>require 'htmlentities'

class RunsController < ApplicationController
  def create
    splits = params[:file]
    run = Run.create(nick: new_nick, user: nil)
    if splits.present?
      File.open(Rails.root.join('private', 'runs', run.nick), 'wb') do |file|
        file.write splits.read
      end
      respond_to do |format|
        format.html { redirect_to run_path run.nick }
        format.json { render text: run.nick }
      end
    else
      respond_to do |format|
        format.html { redirect_to upload_path, alert: "Didn't receive any uploaded file, try again." }
      end
    end
  end
  def show
    @random = session[:random] or false
    session[:random] = false

    @run_record = Run.find_by nick: params[:nick]
    render :bad_url and return if @run_record.nil?

    @run_record.hits += 1 and @run_record.save
    @run = parse @run_record
    if @run.present?
      respond_to do |format|
        format.html { render :show }
        format.json { render @run_record }
      end
    else
      if @run_record.hits > 1 then render :cant_parse
      else @run_record.destroy and redirect_to cant_parse_path end
    end
  end
  def download
    @run_record = Run.find_by nick: params[:nick]
    render :bad_url and return if @run_record.nil?

    @run_record.hits += 1 and @run_record.save
    @run = parse @run_record
    if @run.present?
      file_extension = params[:format] == 'livesplit' ? '.lss' : params[:format]
      send_data(HTMLEntities.new.decode(render_to_string(params[:format], layout: false)), filename: @run_record.nick + file_extension, content_type: "text/html", layout: false)
    else
      if @run_record.hits > 1 then render :cant_parse
      else @run_record.destroy and redirect_to cant_parse_path end
    end
  end
  def download_original
    @run_record = Run.find_by nick: params[:nick]
    render :bad_url and return if @run_record.nil?

    @run_record.hits += 1 and @run_record.save
    
  end
  def random
    # Find a random run. If we can't parse it, find another, and so on.
    run = nil
    if request.env["HTTP_REFERER"].present?
      # Don't use the run that we just came from (i.e. we're spam clicking random)
      last_run = URI.parse(URI.encode request.env["HTTP_REFERER"].strip).path
{'fraction_non_alphanumeric': 0.10200086994345367, 'fraction_numerical': 0.002174858634188778, 'mean_word_length': 2.964655172413793, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4918727', 'n_tokens_mistral': 1504, 'n_tokens_neox': 1469, 'n_words': 429}