<commit_before>class UploadsController < ApplicationController
  before_filter :find_upload, :only => [:destroy,:update,:thumbnail,:show]
  skip_before_filter :load_project, :only => [:download]
  before_filter :set_page_title
  before_filter :load_folder, :only => :index
  before_filter :check_private_download_access, :only => :download
  before_filter :check_public_download_access, :only => :tokenized_download
  
  rescue_from CanCan::AccessDenied do |exception|
    respond_to do |f|
      error_message = "You are not allowed to do that!"
      f.js             { render :text => "alert('#{error_message}')" }
      f.any(:html, :m) { render :text => "alert('#{error_message}')" }
    end
  end

  def tokenized_download
    @extra_sendfile_options = {:disposition => 'inline'}
    download
  end

  def download
    
    if !!Teambox.config.amazon_s3
      unless @upload.asset.exists?(params[:style])
        head(:bad_request)
        raise "Unable to download file"
      end
      redirect_to @upload.s3_url(params[:style])
    else
      path = @upload.asset.path(params[:style])
      unless File.exist?(path)
        head(:bad_request)
        raise "Unable to download file"
      end  

      mime_type = File.mime_type?(@upload.asset_file_name)

      mime_type = 'application/octet-stream' if mime_type == 'unknown/unknown'

      send_file_options = {:type => mime_type}.merge(@extra_sendfile_options || {})

      response.headers['Cache-Control'] = 'private, max-age=31557600'

      send_file(path, send_file_options)
    end
  end

  def index

    # TODO: Ensure you are able to view this folder
    @uploads = @current_project.uploads.
                       where(['uploads.is_private = ? OR (uploads.is_private = ? AND watchers.user_id = ?)', false, true, current_user.id]).
                       where(:parent_folder_id => @current_folder).
                       joins("LEFT JOIN comments ON comments.id = uploads.comment_id").
                       joins("LEFT JOIN watchers ON (comments.target_id = watchers.watchable_id AND watchers.watchable_type = comments.target_type) AND watchers.user_id = #{current_user.id}").
                       includes(:user).
                       order('updated_at DESC')
    @folders = @current_project.folders.
                       where(:parent_folder_id => @current_folder, :deleted => false).
                       order('name DESC')
    @upload ||= @current_project.uploads.new

    unless params[:extractparts]
      respond_to do |format|
        format.js   { render "browsing", :layout => false }
      end
    end

  end

  def show
    authorize! :show, @upload
    redirect_to @upload.url
  end

  def create
    authorize! :upload_files, @current_project
    authorize! :update, @page if @page
    @upload = @current_project.uploads.new params[:upload]
    @upload.user = current_user
    @page = @upload.page
    calculate_position(@upload) if @page
    
    @upload.save!

    respond_to do |wants|
      wants.any(:html, :m) {
        if @upload.new_record?
          flash.now[:error] = "There was an error uploading the file"
          render :new
        elsif @upload.page
          if iframe?<commit_msg>Another fix for ajax folders browsing<commit_after>class UploadsController < ApplicationController
  before_filter :find_upload, :only => [:destroy,:update,:thumbnail,:show]
  skip_before_filter :load_project, :only => [:download]
  before_filter :set_page_title
  before_filter :load_folder, :only => :index
  before_filter :check_private_download_access, :only => :download
  before_filter :check_public_download_access, :only => :tokenized_download
  
  rescue_from CanCan::AccessDenied do |exception|
    respond_to do |f|
      error_message = "You are not allowed to do that!"
      f.js             { render :text => "alert('#{error_message}')" }
      f.any(:html, :m) { render :text => "alert('#{error_message}')" }
    end
  end

  def tokenized_download
    @extra_sendfile_options = {:disposition => 'inline'}
    download
  end

  def download
    
    if !!Teambox.config.amazon_s3
      unless @upload.asset.exists?(params[:style])
        head(:bad_request)
        raise "Unable to download file"
      end
      redirect_to @upload.s3_url(params[:style])
    else
      path = @upload.asset.path(params[:style])
      unless File.exist?(path)
        head(:bad_request)
        raise "Unable to download file"
      end  

      mime_type = File.mime_type?(@upload.asset_file_name)

      mime_type = 'application/octet-stream' if mime_type == 'unknown/unknown'

      send_file_options = {:type => mime_type}.merge(@extra_sendfile_options || {})

      response.headers['Cache-Control'] = 'private, max-age=31557600'

      send_file(path, send_file_options)
    end
  end

  def index

    # TODO: Ensure you are able to view this folder
    @uploads = @current_project.uploads.
                       where(['uploads.is_private = ? OR (uploads.is_private = ? AND watchers.user_id = ?)', false, true, current_user.id]).
                       where(:parent_folder_id => @current_folder).
                       joins("LEFT JOIN comments ON comments.id = uploads.comment_id").
                       joins("LEFT JOIN watchers ON (comments.target_id = watchers.watchable_id AND watchers.watchable_type = comments.target_type) AND watchers.user_id = #{current_user.id}").
                       includes(:user).
                       order('updated_at DESC')
    @folders = @current_project.folders.
                       where(:parent_folder_id => @current_folder, :deleted => false).
                       order('name DESC')
    @upload ||= @current_project.uploads.new

    unless params[:extractparts]
      respond_to do |format|
        format.js   { render "browsing", :layout => false }
        format.any(:html, :m) {}
      end
    end

  end

  def show
    authorize! :show, @upload
    redirect_to @upload.url
  end

  def create
    authorize! :upload_files, @current_project
    authorize! :update, @page if @page
    @upload = @current_project.uploads.new params[:upload]
    @upload.user = current_user
    @page = @upload.page
    calculate_position(@upload) if @page
    
    @upload.save!

    respond_to do |wants|
      wants.any(:html, :m) {
        if @upload.new_record?
          flash.now[:error] = "There was an error uploading the file"
          render :new
        elsif @upload.page
          if iframe?
{'fraction_non_alphanumeric': 0.1128157446149078, 'fraction_numerical': 0.003099333643266698, 'mean_word_length': 2.7049368541905854, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1993511', 'n_tokens_mistral': 1996, 'n_tokens_neox': 1921, 'n_words': 444}