aboutsummaryrefslogblamecommitdiffstats
path: root/activestorage/app/models/active_storage/blob/identifiable.rb
blob: 2c17ddc25f970a01d3bc9294b03f50c5398d0ee1 (plain) (tree)
1
2
3
4
5
6
7
8
9
10



                                        
                                                                                    




                 

         




                                                                                                        




                                                   
       
   
# frozen_string_literal: true

module ActiveStorage::Blob::Identifiable
  def identify
    update! content_type: identify_content_type, identified: true unless identified?
  end

  def identified?
    identified
  end

  private
    def identify_content_type
      Marcel::MimeType.for download_identifiable_chunk, name: filename.to_s, declared_type: content_type
    end

    def download_identifiable_chunk
      if byte_size.positive?
        service.download_chunk key, 0...4.kilobytes
      else
        ""
      end
    end
end