<commit_before>class DatasetsController < ApplicationController
  before_action :authenticate_user_from_token!, only: [ :json_manifest, :manifest, :files ]
  before_action :authenticate_user!,        only: [ :new, :edit, :create, :update, :destroy, :audits, :requests, :request_access, :set_access, :create_access, :new_page, :create_page, :edit_page, :update_page, :pull_changes, :sync, :set_public_file ]
  before_action :check_system_admin,        only: [ :new, :create, :destroy, :pull_changes, :sync ]
  before_action :set_viewable_dataset,      only: [ :show, :json_manifest, :manifest, :logo, :images, :files, :pages, :request_access, :search ]
  before_action :set_editable_dataset,      only: [ :edit, :update, :destroy, :audits, :requests, :set_access, :create_access, :new_page, :create_page, :edit_page, :update_page, :pull_changes, :sync, :set_public_file ]
  before_action :redirect_without_dataset,  only: [ :show, :json_manifest, :manifest, :logo, :images, :files, :pages, :request_access, :set_access, :create_access, :search, :edit, :update, :destroy, :audits, :requests, :new_page, :create_page, :edit_page, :update_page, :pull_changes, :sync, :set_public_file ]

  # Concerns
  include Pageable

  def set_public_file
    file = @dataset.find_file( params[:path] )
    if file
      if params[:public] == '1'
        @dataset.public_files.where( file_path: @dataset.file_path(file) ).first_or_create( user_id: current_user.id )
      else
        @dataset.public_files.where( file_path: @dataset.file_path(file) ).destroy_all
      end
    end

    redirect_to files_dataset_path(@dataset, path: @dataset.find_file_folder(params[:path]))
  end

  def request_access
    if @dataset_user = @dataset.dataset_users.where( user_id: current_user.id ).first
      # Dataset access has already been requested
    else
      @dataset_user = @dataset.dataset_users.create( user_id: current_user.id, editor: false, approved: nil )
      @dataset.editors.each do |editor|
        UserMailer.dataset_access_requested(@dataset_user, editor).deliver if Rails.env.production?
      end
    end
    if params[:path]
      redirect_to files_dataset_path(@dataset, path: params[:path])
    else
      redirect_to daua_path
    end
  end

  def set_access
    if @dataset_user = @dataset.dataset_users.find_by_id(params[:dataset_user_id])
      @dataset_user.update( editor: params[:editor], approved: params[:approved] )
      if @dataset_user.approved? and not @dataset_user.email_sent?
        UserMailer.dataset_access_approved(@dataset_user, current_user).deliver if Rails.env.production?
        @dataset_user.update email_sent: true
      end
    end
    redirect_to requests_dataset_path(@dataset, dataset_user_id: @dataset_user ? @dataset_user.id : nil)
  end

  def create_access
    @dataset_user = @dataset.dataset_users.where( user_id: params[:user_id] ).first_or_create
    redirect_to requests_dataset_path(@dataset, dataset_user_id: @dataset_user ? @dataset_user.id : nil)
  end

  # GET /datasets/1/file_audits
  def audits
    audit_scope = @dataset.dataset_file_audits.order( created_at: :desc )
    audit_scope = audit_scope.where( user_id: params[:user_id].blank? ? nil : params[:user_id] ) if params.has_key?(:user_id)
    audit_scope = audit_scope.where( medium: params[:medium].blank? ? nil : params[:medium] ) if params.has_key?(:medium)
    audit_scope = audit_scope.where( remote_ip: params[:remote_ip].blank? ? nil : params[:remote_ip] ) if params.has_key?(:remote_ip)
    @audits = audit_scope
  end

  # GET /datasets/1/search
  def search
    @term = params[:s].to_s.gsub(/[^\w]/, '')
    @results = []
    @results = `grep -i -R #{@term} #{@dataset.pages_folder}`.split("\n") unless @term.blank?
  end

  # GET /datasets/1/json_manifest
  def json_manifest
    @folder_path = @dataset.find_file_folder(params[:path])
    render json: @dataset.indexed_files_with_md5(@folder_path, -1).collect{ |folder, file_name, is_file, file_size, file_time, file_checksum| { file_name: file_name, checksum: file_checksum, is_file: is_file, file_size: file_size, dataset: @dataset.slug, file_path: folder } }
  end

  # GET /datasets/1/manifest.txt
  def manifest
    @folder_path = @dataset.find_file_folder(params[:path])
    render text: @dataset.indexed_files(@folder_path, -1).select{|folder, file_name, is_file, file_size, file_time| is_file}.collect{|folder, file_name, is_file, file_size, file_time| site_prefix + files_dataset_path(@dataset, path: folder, auth_token: (current_user ? current_user.id_and_auth_token : nil ), medium: 'wget')}.join("\n\r")
  end

  def logo
    send_file File.join( CarrierWave::Uploader::Base.root, @dataset.logo.url )
  end

  def files
    file = @dataset.find_file( params[:path] )
    if file and File.file?(file) and [@dataset.find_file_folder(params[:path]), File.basename(file)].compact.join('/') == params[:path] and (@dataset.public_file?(params[:path]) or @dataset.grants_file_access_to?(current_user))
      @dataset.dataset_file_audits.create( user_id: (current_user ? current_user.id : nil), file_path: @dataset.file_path(file), medium: params[:medium], file_size: File.size(file), remote_ip: request.remote_ip )
      send_file file
    elsif file and File.directory?(file) and @dataset.find_file_folder(params[:path]) == params[:path]
      store_location_in_session
      render 'files'
    elsif not File.directory?(@dataset.files_folder)
      redirect_to @dataset
    else<commit_msg>The JSON manifest file no longer friendly redirects when a non-existent path is referenced and instead returns a blank file response, fixes nsrr/nsrr-gem#1.<commit_after>class DatasetsController < ApplicationController
  before_action :authenticate_user_from_token!, only: [ :json_manifest, :manifest, :files ]
  before_action :authenticate_user!,        only: [ :new, :edit, :create, :update, :destroy, :audits, :requests, :request_access, :set_access, :create_access, :new_page, :create_page, :edit_page, :update_page, :pull_changes, :sync, :set_public_file ]
  before_action :check_system_admin,        only: [ :new, :create, :destroy, :pull_changes, :sync ]
  before_action :set_viewable_dataset,      only: [ :show, :json_manifest, :manifest, :logo, :images, :files, :pages, :request_access, :search ]
  before_action :set_editable_dataset,      only: [ :edit, :update, :destroy, :audits, :requests, :set_access, :create_access, :new_page, :create_page, :edit_page, :update_page, :pull_changes, :sync, :set_public_file ]
  before_action :redirect_without_dataset,  only: [ :show, :json_manifest, :manifest, :logo, :images, :files, :pages, :request_access, :set_access, :create_access, :search, :edit, :update, :destroy, :audits, :requests, :new_page, :create_page, :edit_page, :update_page, :pull_changes, :sync, :set_public_file ]

  # Concerns
  include Pageable

  def set_public_file
    file = @dataset.find_file( params[:path] )
    if file
      if params[:public] == '1'
        @dataset.public_files.where( file_path: @dataset.file_path(file) ).first_or_create( user_id: current_user.id )
      else
        @dataset.public_files.where( file_path: @dataset.file_path(file) ).destroy_all
      end
    end

    redirect_to files_dataset_path(@dataset, path: @dataset.find_file_folder(params[:path]))
  end

  def request_access
    if @dataset_user = @dataset.dataset_users.where( user_id: current_user.id ).first
      # Dataset access has already been requested
    else
      @dataset_user = @dataset.dataset_users.create( user_id: current_user.id, editor: false, approved: nil )
      @dataset.editors.each do |editor|
        UserMailer.dataset_access_requested(@dataset_user, editor).deliver if Rails.env.production?
      end
    end
    if params[:path]
      redirect_to files_dataset_path(@dataset, path: params[:path])
    else
      redirect_to daua_path
    end
  end

  def set_access
    if @dataset_user = @dataset.dataset_users.find_by_id(params[:dataset_user_id])
      @dataset_user.update( editor: params[:editor], approved: params[:approved] )
      if @dataset_user.approved? and not @dataset_user.email_sent?
        UserMailer.dataset_access_approved(@dataset_user, current_user).deliver if Rails.env.production?
        @dataset_user.update email_sent: true
      end
    end
    redirect_to requests_dataset_path(@dataset, dataset_user_id: @dataset_user ? @dataset_user.id : nil)
  end

  def create_access
    @dataset_user = @dataset.dataset_users.where( user_id: params[:user_id] ).first_or_create
    redirect_to requests_dataset_path(@dataset, dataset_user_id: @dataset_user ? @dataset_user.id : nil)
  end

  # GET /datasets/1/file_audits
  def audits
    audit_scope = @dataset.dataset_file_audits.order( created_at: :desc )
    audit_scope = audit_scope.where( user_id: params[:user_id].blank? ? nil : params[:user_id] ) if params.has_key?(:user_id)
    audit_scope = audit_scope.where( medium: params[:medium].blank? ? nil : params[:medium] ) if params.has_key?(:medium)
    audit_scope = audit_scope.where( remote_ip: params[:remote_ip].blank? ? nil : params[:remote_ip] ) if params.has_key?(:remote_ip)
    @audits = audit_scope
  end

  # GET /datasets/1/search
  def search
    @term = params[:s].to_s.gsub(/[^\w]/, '')
    @results = []
    @results = `grep -i -R #{@term} #{@dataset.pages_folder}`.split("\n") unless @term.blank?
  end

  # GET /datasets/1/json_manifest
  def json_manifest
    @folder_path = @dataset.find_file_folder(params[:path])
    if @folder_path == params[:path]
      render json: @dataset.indexed_files_with_md5(@folder_path, -1).collect{ |folder, file_name, is_file, file_size, file_time, file_checksum| { file_name: file_name, checksum: file_checksum, is_file: is_file, file_size: file_size, dataset: @dataset.slug, file_path: folder } }
    else
      render json: []
    end
  end

  # GET /datasets/1/manifest.txt
  def manifest
    @folder_path = @dataset.find_file_folder(params[:path])
    render text: @dataset.indexed_files(@folder_path, -1).select{|folder, file_name, is_file, file_size, file_time| is_file}.collect{|folder, file_name, is_file, file_size, file_time| site_prefix + files_dataset_path(@dataset, path: folder, auth_token: (current_user ? current_user.id_and_auth_token : nil ), medium: 'wget')}.join("\n\r")
  end

  def logo
    send_file File.join( CarrierWave::Uploader::Base.root, @dataset.logo.url )
  end

  def files
    file = @dataset.find_file( params[:path] )
    if file and File.file?(file) and [@dataset.find_file_folder(params[:path]), File.basename(file)].compact.join('/') == params[:path] and (@dataset.public_file?(params[:path]) or @dataset.grants_file_access_to?(current_user))
      @dataset.dataset_file_audits.create( user_id: (current_user ? current_user.id : nil), file_path: @dataset.file_path(file), medium: params[:medium], file_size: File.size(file), remote_ip: request.remote_ip )
      send_file file
    elsif file and File.directory?(file) and @dataset.find_file_folder(params[:path]) == params[:path]
      store_location_in_session
      render 'files'
    elsif not File.directory?(@dataset.files_folder)
      redirect_to @dataset
    else
{'fraction_non_alphanumeric': 0.12714221624046657, 'fraction_numerical': 0.001525347689546882, 'mean_word_length': 4.823406478578892, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '915903', 'n_tokens_mistral': 3740, 'n_tokens_neox': 3684, 'n_words': 780}