<commit_before>require_dependency 'stash_engine/application_controller'

module StashEngine
  class FileUploadsController < ApplicationController
    before_action :require_login

    before_action :set_file, only: [:destroy]

    before_action :require_file_owner, except: [:create]

    def destroy
      respond_to do |format|
        format.js do
          File.delete(@file.temp_file_path) if File.exist?(@file.temp_file_path)
          @file_id = @file.id
          @file.destroy
        end
      end
    end

    def create
      respond_to do |format|
        format.js do
          @temp_id = params[:temp_id]
          fu = params[:upload][:upload]
          @my_file = FileUpload.new(
            upload_file_name: fu.original_filename,
            temp_file_path: fu.tempfile.path,
            upload_content_type: fu.content_type,
            upload_file_size: File.size(fu.tempfile.path),
            resource_id: params[:resource_id],
            upload_updated_at: Time.new.utc)
          @my_file.save
        end
      end
    end

    private

    def require_file_owner
      return if current_user.id = @file.resource.user_id
      redirect_to tenants_path && return
    end

    def set_file
      @file = FileUpload.find(params[:id])
    end
  end
end<commit_msg>Some changes to persist files better rather than using temp files for storage which seem to disappear quickly.<commit_after>require_dependency 'stash_engine/application_controller'
require 'fileutils'

module StashEngine
  class FileUploadsController < ApplicationController
    before_action :require_login

    before_action :set_file, only: [:destroy]

    before_action :require_file_owner, except: [:create]

    def destroy
      respond_to do |format|
        format.js do
          File.delete(@file.temp_file_path) if File.exist?(@file.temp_file_path)
          @file_id = @file.id
          @file.destroy
        end
      end
    end

    def create
      respond_to do |format|
        format.js do
          @temp_id = params[:temp_id]
          fu = params[:upload][:upload]

          dir = File.join(Rails.root, "uploads", params[:resource_id])
          FileUtils.mkdir_p dir unless File.exists?(dir)
          new_fn = File.join(dir, fu.original_filename)
          FileUtils.mv(fu.tempfile.path, new_fn)

          @my_file = FileUpload.new(
            upload_file_name: fu.original_filename,
            temp_file_path: new_fn,
            upload_content_type: fu.content_type,
            upload_file_size: File.size(new_fn),
            resource_id: params[:resource_id],
            upload_updated_at: Time.new.utc)
          @my_file.save
        end
      end
    end

    private

    def require_file_owner
      return if current_user.id = @file.resource.user_id
      redirect_to tenants_path && return
    end

    def set_file
      @file = FileUpload.find(params[:id])
    end
  end
end
{'fraction_non_alphanumeric': 0.07983482450103235, 'fraction_numerical': 0.0, 'mean_word_length': 2.3920653442240374, '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': '2399512', 'n_tokens_mistral': 883, 'n_tokens_neox': 860, 'n_words': 191}