<commit_before># The public DocumentCloud API.
class ApiController < ApplicationController
  include DC::Access
  include DC::Search::Controller

  layout 'workspace'

  before_filter :bouncer if Rails.env.staging?
  before_filter :prefer_secure, :only => [:index]

  skip_before_filter :verify_authenticity_token

  before_filter :secure_only,        :only => [:upload, :project, :projects, :upload, :destroy, :create_project, :update_project, :destroy_project]
  before_filter :api_login_required, :only => [:upload, :project, :projects, :update, :destroy, :create_project, :update_project, :destroy_project]
  before_filter :api_login_optional, :only => [:documents, :search, :notes, :pending, :entities]

  def index
    redirect_to '/help/api'
  end

  def cors_options
    return bad_request unless params[:allowed_methods]
    headers['Access-Control-Allow-Origin'] = '*'
    headers['Access-Control-Allow-Methods'] = 'OPTIONS, ' + params[:allowed_methods].map(&:to_s).map(&:upcase).join(', ')
    headers['Access-Control-Allow-Headers'] = 'Authorization'
    headers['Access-Control-Allow-Credentials'] = 'true'
    render :nothing => true
  end

  def search
    opts = API_OPTIONS.merge(pick(params, :sections, :annotations, :entities, :mentions, :data))
    if opts[:mentions] &&= opts[:mentions].to_i
      opts[:mentions] = 10  if opts[:mentions] > 10
      opts[:mentions] = nil if opts[:mentions] < 1
    end
    respond_to do |format|
      format.any(:js, :json) do
        perform_search :mentions => opts[:mentions]
        @response = ActiveSupport::OrderedHash.new
        @response['total']     = @query.total
        @response['page']      = @query.page
        @response['per_page']  = @query.per_page
        @response['q']         = params[:q]
        @response['documents'] = @documents.map {|d| d.canonical(opts) }
        json_response
      end
    end
  end

  # Upload API, similar to our internal upload API for starters. Parameters:
  # file, title, access, source, description.
  # The `file` must either be a multipart file upload, or a URL to a remote doc.
  def upload
    secure_silence_logs do
      return bad_request unless params[:file] && params[:title] && current_account
      is_file = params[:file].respond_to?(:path)
      if !is_file && !(URI.parse(params[:file]) rescue nil)
        return render({
          :json => {:message => "The 'file' parameter must be the contents of a file or a URL."},
          :status => 400
        })
      end
      params[:url] = params[:file] unless is_file
      @response = Document.upload(params, current_account, current_organization).canonical
      json_response
    end
  end

  # Retrieve a document's canonical JSON.
  def documents
    return bad_request unless params[:id] and request.format.json? || request.format.js? || request.format.text?
    return not_found unless current_document<commit_msg>Allow API users to pass in a file hash that blocks duplicate uploads.<commit_after># The public DocumentCloud API.
class ApiController < ApplicationController
  include DC::Access
  include DC::Search::Controller

  layout 'workspace'

  before_filter :bouncer if Rails.env.staging?
  before_filter :prefer_secure, :only => [:index]

  skip_before_filter :verify_authenticity_token

  before_filter :secure_only,        :only => [:upload, :project, :projects, :upload, :destroy, :create_project, :update_project, :destroy_project]
  before_filter :api_login_required, :only => [:upload, :project, :projects, :update, :destroy, :create_project, :update_project, :destroy_project]
  before_filter :api_login_optional, :only => [:documents, :search, :notes, :pending, :entities]

  def index
    redirect_to '/help/api'
  end

  def cors_options
    return bad_request unless params[:allowed_methods]
    headers['Access-Control-Allow-Origin'] = '*'
    headers['Access-Control-Allow-Methods'] = 'OPTIONS, ' + params[:allowed_methods].map(&:to_s).map(&:upcase).join(', ')
    headers['Access-Control-Allow-Headers'] = 'Authorization'
    headers['Access-Control-Allow-Credentials'] = 'true'
    render :nothing => true
  end

  def search
    opts = API_OPTIONS.merge(pick(params, :sections, :annotations, :entities, :mentions, :data))
    if opts[:mentions] &&= opts[:mentions].to_i
      opts[:mentions] = 10  if opts[:mentions] > 10
      opts[:mentions] = nil if opts[:mentions] < 1
    end
    respond_to do |format|
      format.any(:js, :json) do
        perform_search :mentions => opts[:mentions]
        @response = ActiveSupport::OrderedHash.new
        @response['total']     = @query.total
        @response['page']      = @query.page
        @response['per_page']  = @query.per_page
        @response['q']         = params[:q]
        @response['documents'] = @documents.map {|d| d.canonical(opts) }
        json_response
      end
    end
  end

  # Upload API, similar to our internal upload API for starters. Parameters:
  # file, title, access, source, description.
  # The `file` must either be a multipart file upload, or a URL to a remote doc.
  def upload
    secure_silence_logs do
      return bad_request unless params[:file] && params[:title] && current_account
      is_file = params[:file].respond_to?(:path)
      if !is_file && !(URI.parse(params[:file]) rescue nil)
        return render({
          :json => {:message => "The 'file' parameter must be the contents of a file or a URL."},
          :status => 400
        })
      end
      
      if params[:file_hash] && Document.accessible(current_account, current_organization).exists?(:file_hash=>params[:file_hash])
        return render({ 
          :status=>409, 
          :json => "This file is a duplicate of an existing one you have access to" 
        })
      end
      params[:url] = params[:file] unless is_file
      @response = Document.upload(params, current_account, current_organization).canonical
      json_response
    end
  end

  # Retrieve a document's canonical JSON.
  def documents
    return bad_request unless params[:id] and request.format.json? || request.format.js? || request.format.text?
    return not_found unless current_document
{'fraction_non_alphanumeric': 0.12550870909978837, 'fraction_numerical': 0.003092951326713332, 'mean_word_length': 3.574832464631422, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 26, 'https://': 0, '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': '3289623', 'n_tokens_mistral': 1862, 'n_tokens_neox': 1806, 'n_words': 485}