Active Storage:在元数据上查询

问题描述 投票:1回答:1

我在元数据中添加了“纬度,经度,geo_location_name,image_taken_time”属性。因此,当我对其进行分析时,将添加此属性以及高度和宽度。

在我的项目中,我想获取那些花费了特定年份的记录。

require 'exifr/jpeg'
require 'geocoder'

module ActiveStorage
  class Analyzer::ImageAnalyzer < Analyzer
    def metadata
      read_image do |image|
        if rotated_image?(image)
          { width: image.height, height: image.width }
        else
          { width: image.width, height: image.height }
        end.merge(gps_from_exif(image) || {})
      end
    rescue LoadError
      logger.info "Skipping image analysis because the mini_magick gem isn't installed"
      {}
    end

    private

    def gps_from_exif(image)
      return unless image.type == 'JPEG'
      if exif = EXIFR::JPEG.new(image.path).exif
        if gps = exif.fields[:gps]
          result = {
            latitude:  gps.fields[:gps_latitude].to_f,
            longitude: gps.fields[:gps_longitude].to_f,
            altitude:  gps.fields[:gps_altitude].to_f
          }
          result[:image_taken_time] =  exif.date_time_original
          query        = "#{result[:latitude]},#{result[:longitude]}"
          geo_location = Geocoder.search(query).first
          result[:geo_location_name]       = geo_location.address if geo_location.present?
          return result
        end
      end
    rescue EXIFR::MalformedImage, EXIFR::MalformedJPEG
    end
  end
end

如何查询Blob(文本数据类型)的元数据字段?

提前感谢

mysql ruby-on-rails rails-activestorage
1个回答
-1
投票

如果要查询元数据,可以将索引添加到活动存储表中

class AddSearchToActiveStorageBlobs < ActiveRecord::Migration[5.2]
  def change
    add_index :active_storage_blobs,
      "(to_tsvector('english', coalesce(\"active_storage_blobs\".\"metadata\"::text, '')))",
      using: :gin,
      name: 'metadata_search_idx'
  end
end
© www.soinside.com 2019 - 2024. All rights reserved.