<commit_before>require 'datafy'
require 'open-uri'
require 'uri'

class TwitpicController < ApplicationController
  def callback
    session[:access_token] = request.env['omniauth.auth']['credentials']['token']
    session[:access_secret] = request.env['omniauth.auth']['credentials']['secret']
    redirect_to show_path
  end

  def show
    if !logged_in?
      if session[:tried_login]
        error
      else
        session[:tried_login] = true
        redirect_to '/auth/twitter'
      end
    end
  end

  def error
    flash[:error] = "Sign in with Twitter failed!"
    session[:tried_login] = false
    redirect_to root_path
  end

  def upload
    url = request.params['img'] || ''
    path = if url.starts_with?('data:')
      store_data_url(url)
    elsif url.starts_with?('http://') || url.starts_with?('https://')
      store_url(url)
    else
      raise 'Unsupported URL format'
    end
    begin
      render :json => twitpic_client.upload(path, '')
    ensure
      cleanup(path)
    end
  end

  def destroy
    reset_session
    redirect_to root_path, :notice => "Signed out!"
  end

  private
  def store_data_url(url)
    data, type = Datafy::decode_data_uri(url)
    suffix = get_suffix(type)
    return get_tempfile('image' + suffix) { |f|
      f.write(data)
    }
  end

  def store_url(url)
    return get_tempfile(File::basename(URI.split(url)[5])) { |f|
      f << open(url).read
    }
  end

  def get_tempfile(filename, &block)
    dir = File.join(Dir::tmpdir, "twitpic_#{Process.pid}_#{rand(100)}_#{Time.now.to_i}")
    Dir.mkdir(dir)
    filepath = File.join(dir, filename)
    File.open(filepath, "wb", &block)
    return filepath
  end

  def cleanup(filepath)
    File.unlink(filepath)
    Dir.rmdir(File::dirname(filepath))
  end

  def get_suffix(type)
    if (type == 'image/jpg' || type == 'image/jpeg')
      return '.jpg'
    elsif (type == 'image/png')
      return '.png'
    elsif (type == 'image/gif')
      return '.gif'
    else
      raise 'Unsupported image type'
    end
  end
end<commit_msg>extracted image fetching to library<commit_after>require 'datafy'
require 'image_fetch'
require 'open-uri'
require 'uri'

class TwitpicController < ApplicationController
  def callback
    session[:access_token] = request.env['omniauth.auth']['credentials']['token']
    session[:access_secret] = request.env['omniauth.auth']['credentials']['secret']
    redirect_to show_path
  end

  def show
    if !logged_in?
      if session[:tried_login]
        error
      else
        session[:tried_login] = true
        redirect_to '/auth/twitter'
      end
    end
  end

  def error
    flash[:error] = "Sign in with Twitter failed!"
    session[:tried_login] = false
    redirect_to root_path
  end

  def upload
    image = ImageFetch.new(request.params['img'])
    begin
      render :json => twitpic_client.upload(image.path, '')
    ensure
      image.cleanup
    end
  end

  def destroy
    reset_session
    redirect_to root_path, :notice => "Signed out!"
  end

  private
end
{'fraction_non_alphanumeric': 0.12363636363636364, 'fraction_numerical': 0.001322314049586777, 'mean_word_length': 3.0185922974767596, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3012921', 'n_tokens_mistral': 1014, 'n_tokens_neox': 994, 'n_words': 225}