<commit_before>module Attachs
  class Attachment

    attr_reader :record, :attribute, :filename, :content_type, :size, :updated_at, :upload, :options

    delegate :basename, :extension, :image?, :url, :process, :destroy, :update, to: :type

    def initialize(record, attribute, options, source=false)
      @record = record
      @attribute = attribute
      @options = options
      case source
      when nil
        %w(filename content_type size updated_at).each do |name|
          record.send "#{attribute}_#{name}=", nil
        end
      when false
        %w(filename content_type size updated_at).each do |name|
          instance_variable_set :"@#{name}", record.send("#{attribute}_#{name}")
        end
      else
        load source
        %w(filename content_type size updated_at).each do |name|
          record.send "#{attribute}_#{name}=", send(name)
        end
      end
    end

    def uploaded!
      @upload = nil
    end

    def styles
      @styles ||= ((options[:styles] || []) | Attachs.config.global_styles)
    end

    def private?
      @private ||= options[:private] == true
    end

    def processors
      @processors ||= (options[:processors] || Attachs.config.default_processors).map do |processor|
        "Attachs::Processors::#{processor.to_s.classify}".constantize
      end
    end

    def public?
      !private?
    end

    def upload?
      !upload.nil?
    end

    def processed?
      exists? and record.persisted? and !(
        record.send("#{attribute}_filename_changed?") or
        record.send("#{attribute}_content_type_changed?") or
        record.send("#{attribute}_size_changed?") or
        record.send("#{attribute}_updated_at_changed?")
      )
    end

    def exists?
      filename and content_type and size and updated_at
    end

    def blank?
      !exists?
    end

    def default?
      !options[:default_path].nil?
    end

    def url?
      public? and (default? or processed?)
    end

    protected

    def load(source)
      if source.is_a? URI
        download = Tempfile.new('external')
        File.open(download.path, 'wb') do |file|
          Net::HTTP.start(source.host) do |http|
            file.write http.get(source.path).body
          end
        end
        type = `file --mime-type -b '#{download.path}'`.strip
        source = ActionDispatch::Http::UploadedFile.new(
          tempfile: download,
          filename: File.basename(source.path),
          type: type
        )
      end
      if source.is_a? Attachs::Attachment
        @filename = source.filename
        @updated_at = source.updated_at
      else
        @upload = source
        @filename = source.original_filename.downcase
        @updated_at = Time.zone.now
      end
      @content_type = source.content_type
      @size = source.size
    end

    def type
      @type ||= begin
        if exists?
          Attachs::Types::Regular.new(self)
        else
          Attachs::Types::Default.new(self)
        end
      end
    end

  end
end<commit_msg>Add debug information<commit_after>module Attachs
  class Attachment

    attr_reader :record, :attribute, :filename, :content_type, :size, :updated_at, :upload, :options

    delegate :basename, :extension, :image?, :url, :process, :destroy, :update, to: :type

    def initialize(record, attribute, options, source=false)
      @record = record
      @attribute = attribute
      @options = options
      case source
      when nil
        %w(filename content_type size updated_at).each do |name|
          record.send "#{attribute}_#{name}=", nil
        end
      when false
        %w(filename content_type size updated_at).each do |name|
          instance_variable_set :"@#{name}", record.send("#{attribute}_#{name}")
        end
      else
        load source
        %w(filename content_type size updated_at).each do |name|
          record.send "#{attribute}_#{name}=", send(name)
        end
      end
    end

    def uploaded!
      @upload = nil
    end

    def styles
      @styles ||= ((options[:styles] || []) | Attachs.config.global_styles)
    end

    def private?
      @private ||= options[:private] == true
    end

    def processors
      @processors ||= (options[:processors] || Attachs.config.default_processors).map do |processor|
        "Attachs::Processors::#{processor.to_s.classify}".constantize
      end
    end

    def public?
      !private?
    end

    def upload?
      !upload.nil?
    end

    def processed?
      exists? and record.persisted? and !(
        record.send("#{attribute}_filename_changed?") or
        record.send("#{attribute}_content_type_changed?") or
        record.send("#{attribute}_size_changed?") or
        record.send("#{attribute}_updated_at_changed?")
      )
    end

    def exists?
      filename and content_type and size and updated_at
    end

    def blank?
      !exists?
    end

    def default?
      !options[:default_path].nil?
    end

    def url?
      public? and (default? or processed?)
    end

    protected

    def load(source)
      if source.is_a? URI
        download = Tempfile.new('external')
        File.open(download.path, 'wb') do |file|
          Net::HTTP.start(source.host) do |http|
            file.write http.get(source.path).body
          end
        end
        type = `file --mime-type -b '#{download.path}'`.strip
        source = ActionDispatch::Http::UploadedFile.new(
          tempfile: download,
          filename: File.basename(source.path),
          type: type
        )
      end
      if source.is_a? Attachs::Attachment
        @filename = source.filename
        @updated_at = source.updated_at
      else
        @upload = source
        @filename = source.original_filename.downcase
        @updated_at = Time.zone.now
        Rails.logger.info "Setting time #{@updated_at}"
      end
      @content_type = source.content_type
      @size = source.size
    end

    def type
      @type ||= begin
        if exists?
          Attachs::Types::Regular.new(self)
        else
          Attachs::Types::Default.new(self)
        end
      end
    end

  end
end
{'fraction_non_alphanumeric': 0.11362148002619515, 'fraction_numerical': 0.0, 'mean_word_length': 2.2237467018469657, 'pattern_counts': {'":': 0, '<': 3, '<?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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6242054', 'n_tokens_mistral': 1816, 'n_tokens_neox': 1791, 'n_words': 439}