<commit_before># frozen_string_literal: true

class Folio::CreateFileFromS3Job < ApplicationJob
  include Folio::S3Client

  queue_as :default
  sidekiq_options retry: false

  def perform(s3_path:, type:, existing_id: nil)
    return unless s3_path
    return unless type
    klass = type.safe_constantize
    return unless Rails.application.config.folio_direct_s3_upload_class_names.any? { |class_name| klass <= class_name.constantize }

    unless test_aware_s3_exists?(s3_path)
      # probably handled it already in another job
      return
    end

    broadcast_start(s3_path:, file_type: type)

    if existing_id
      file = klass.find(existing_id)
      replacing_file = true
      if file.try(:thumbnail_sizes).is_a?(Hash)
        thumbnail_keys_to_recreate = file.thumbnail_sizes.keys
      else
        thumbnail_keys_to_recreate = []
      end
    else
      file = klass.new
      replacing_file = false
      thumbnail_keys_to_recreate = []
    end

    Dir.mktmpdir("folio-file-s3") do |tmpdir|
      tmp_file_path = "#{tmpdir}/#{s3_path.split("/").pop}"

      test_aware_download_from_s3(s3_path, tmp_file_path)
      test_aware_s3_delete(s3_path)

      file.file = File.open(tmp_file_path)

      if file.save
        file.try(:admin_thumb, immediate: true)

        thumbnail_keys_to_recreate.each { |thumbnail_key| file.thumb(thumbnail_key) }

        if replacing_file
          broadcast_replace_success(file: file.reload, file_type: type)<commit_msg>respect thumbnailable? in create_file_from_s3_job<commit_after># frozen_string_literal: true

class Folio::CreateFileFromS3Job < ApplicationJob
  include Folio::S3Client

  queue_as :default
  sidekiq_options retry: false

  def perform(s3_path:, type:, existing_id: nil)
    return unless s3_path
    return unless type
    klass = type.safe_constantize
    return unless Rails.application.config.folio_direct_s3_upload_class_names.any? { |class_name| klass <= class_name.constantize }

    unless test_aware_s3_exists?(s3_path)
      # probably handled it already in another job
      return
    end

    broadcast_start(s3_path:, file_type: type)

    if existing_id
      file = klass.find(existing_id)
      replacing_file = true

      if file.try(:thumbnailable?) && file.try(:thumbnail_sizes).is_a?(Hash)
        thumbnail_keys_to_recreate = file.thumbnail_sizes.keys
      else
        thumbnail_keys_to_recreate = []
      end
    else
      file = klass.new
      replacing_file = false
      thumbnail_keys_to_recreate = []
    end

    Dir.mktmpdir("folio-file-s3") do |tmpdir|
      tmp_file_path = "#{tmpdir}/#{s3_path.split("/").pop}"

      test_aware_download_from_s3(s3_path, tmp_file_path)
      test_aware_s3_delete(s3_path)

      file.file = File.open(tmp_file_path)

      if file.save
        if file.try(:thumbnailable?)
          file.try(:admin_thumb, immediate: true)

          thumbnail_keys_to_recreate.each { |thumbnail_key| file.thumb(thumbnail_key) }
        end

        if replacing_file
          broadcast_replace_success(file: file.reload, file_type: type)
{'fraction_non_alphanumeric': 0.08493328994467947, 'fraction_numerical': 0.009437032216075496, 'mean_word_length': 3.34180790960452, 'pattern_counts': {'":': 0, '<': 7, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3420730', 'n_tokens_mistral': 1034, 'n_tokens_neox': 1029, 'n_words': 186}