<commit_before>module Seek
  module ContentTypeDetection

    include Seek::MimeTypes

    MAX_EXTRACTABLE_SPREADSHEET_SIZE=1*1024*1024
    PDF_CONVERTABLE_FORMAT = %w[doc docx ppt pptx odt odp rtf txt]
    IMAGE_VIEWABLE_FORMAT = %w[gif jpg jpeg png]

    def is_excel? blob=self
      is_xls?(blob) || is_xlsx?(blob)
    end

    def is_extractable_spreadsheet? blob=self
      within_size_limit(blob) && is_excel?(blob)
    end

    def is_xlsx? blob=self
      mime_extension(blob.content_type) == "xlsx"
    end

    def is_xls? blob=self
      mime_extension(blob.content_type) == "xls"
    end

    def is_jws_dat? blob=self
      check_content blob,"begin name",25000
    end

    def is_sbml? blob=self
      check_content blob,"<sbml"
    end

    def is_xgmml? blob=self
      check_content(blob,"<graph") && check_content(blob,"<node")
    end

    def is_pdf? blob=self
      mime_extension(blob.content_type) == 'pdf'
    end

    def is_image? blob=self
      blob.content_type.try(:split, '/').try(:first) == 'image'
    end

    def is_pdf_convertable? blob=self
      PDF_CONVERTABLE_FORMAT.include?(mime_extension(blob.content_type)) && Seek::Config.pdf_conversion_enabled
    end

    def is_viewable_format? blob=self
      if Seek::Config.pdf_conversion_enabled
        ((PDF_CONVERTABLE_FORMAT + IMAGE_VIEWABLE_FORMAT) << 'pdf').include?(mime_extension(blob.content_type))
      else
        (IMAGE_VIEWABLE_FORMAT << 'pdf').include?(mime_extension(blob.content_type))
      end
    end

    def is_content_viewable? blob=self
       blob.asset.is_downloadable_asset? && !blob.filesize.nil? && (blob.is_viewable_format? || File.exist?(blob.filepath('pdf')))
    end

    private

    def within_size_limit blob
      !blob.filesize.nil? && blob.filesize < MAX_EXTRACTABLE_SPREADSHEET_SIZE
    end

    def check_content blob, str, max_length=1500
      char_count=0
      filepath=blob.filepath
      begin
        f = File.open(filepath, "r")
        f.each_line do |line|
          char_count += line.length
          return true if line.downcase.include?(str)
          break if char_count>=max_length
        end
      rescue Exception=>e
        Rails.logger.error("Error reading content_blob contents #{e.class.name}:#{e.message}")
      end
      false
    end

  end
end<commit_msg>remove jpg for now, coz the content_type sending from browser is image/jpeg<commit_after>module Seek
  module ContentTypeDetection

    include Seek::MimeTypes

    MAX_EXTRACTABLE_SPREADSHEET_SIZE=1*1024*1024
    PDF_CONVERTABLE_FORMAT = %w[doc docx ppt pptx odt odp rtf txt]
    IMAGE_VIEWABLE_FORMAT = %w[gif jpeg png]

    def is_excel? blob=self
      is_xls?(blob) || is_xlsx?(blob)
    end

    def is_extractable_spreadsheet? blob=self
      within_size_limit(blob) && is_excel?(blob)
    end

    def is_xlsx? blob=self
      mime_extension(blob.content_type) == "xlsx"
    end

    def is_xls? blob=self
      mime_extension(blob.content_type) == "xls"
    end

    def is_jws_dat? blob=self
      check_content blob,"begin name",25000
    end

    def is_sbml? blob=self
      check_content blob,"<sbml"
    end

    def is_xgmml? blob=self
      check_content(blob,"<graph") && check_content(blob,"<node")
    end

    def is_pdf? blob=self
      mime_extension(blob.content_type) == 'pdf'
    end

    def is_image? blob=self
      blob.content_type.try(:split, '/').try(:first) == 'image'
    end

    def is_pdf_convertable? blob=self
      PDF_CONVERTABLE_FORMAT.include?(mime_extension(blob.content_type)) && Seek::Config.pdf_conversion_enabled
    end

    def is_viewable_format? blob=self
      if Seek::Config.pdf_conversion_enabled
        ((PDF_CONVERTABLE_FORMAT + IMAGE_VIEWABLE_FORMAT) << 'pdf').include?(mime_extension(blob.content_type))
      else
        (IMAGE_VIEWABLE_FORMAT << 'pdf').include?(mime_extension(blob.content_type))
      end
    end

    def is_content_viewable? blob=self
       blob.asset.is_downloadable_asset? && !blob.filesize.nil? && (blob.is_viewable_format? || File.exist?(blob.filepath('pdf')))
    end

    private

    def within_size_limit blob
      !blob.filesize.nil? && blob.filesize < MAX_EXTRACTABLE_SPREADSHEET_SIZE
    end

    def check_content blob, str, max_length=1500
      char_count=0
      filepath=blob.filepath
      begin
        f = File.open(filepath, "r")
        f.each_line do |line|
          char_count += line.length
          return true if line.downcase.include?(str)
          break if char_count>=max_length
        end
      rescue Exception=>e
        Rails.logger.error("Error reading content_blob contents #{e.class.name}:#{e.message}")
      end
      false
    end

  end
end
{'fraction_non_alphanumeric': 0.10162254483347566, 'fraction_numerical': 0.008112724167378309, 'mean_word_length': 3.124119718309859, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 7, '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': '4606622', 'n_tokens_mistral': 1652, 'n_tokens_neox': 1588, 'n_words': 305}