class Dato::Local::FieldType::File

Public Class Methods

new( upload, alt, title, custom_data, focal_point, imgix_host ) click to toggle source
# File lib/dato/local/field_type/file.rb, line 28
def initialize(
  upload,
  alt,
  title,
  custom_data,
  focal_point,
  imgix_host
)
  @upload = upload
  @alt = alt
  @title = title
  @custom_data = custom_data
  @focal_point = focal_point
  @imgix_host = imgix_host
end
parse(value, repo) click to toggle source
# File lib/dato/local/field_type/file.rb, line 9
def self.parse(value, repo)
  if value
    v = value.with_indifferent_access

    upload = repo.entities_repo.find_entity("upload", v[:upload_id])

    if upload
      new(
        upload,
        v[:alt],
        v[:title],
        v[:custom_data],
        v[:focal_point],
        repo.site.entity.imgix_host,
      )
    end
  end
end

Public Instance Methods

alt() click to toggle source
# File lib/dato/local/field_type/file.rb, line 88
def alt
  default_metadata = @upload.default_field_metadata.deep_stringify_keys
                            .fetch(I18n.locale.to_s, {})
  @alt || default_metadata["alt"]
end
author() click to toggle source
# File lib/dato/local/field_type/file.rb, line 68
def author
  @upload.author
end
basename() click to toggle source
# File lib/dato/local/field_type/file.rb, line 84
def basename
  @upload.basename
end
blurhash() click to toggle source
# File lib/dato/local/field_type/file.rb, line 136
def blurhash
  @upload.blurhash
end
colors() click to toggle source
# File lib/dato/local/field_type/file.rb, line 132
def colors
  @upload.colors.map { |color| Color.parse(color, nil) }
end
custom_data() click to toggle source
# File lib/dato/local/field_type/file.rb, line 100
def custom_data
  default_metadata = @upload.default_field_metadata.deep_stringify_keys
                            .fetch(I18n.locale.to_s, {})
  @custom_data.merge(default_metadata.fetch("custom_data", {}))
end
exif_info() click to toggle source
# File lib/dato/local/field_type/file.rb, line 124
def exif_info
  @upload.exif_info
end
file() click to toggle source
# File lib/dato/local/field_type/file.rb, line 216
def file
  Imgix::Client.new(
    domain: @imgix_host,
    secure: true,
    include_library_param: false,
  ).path(path)
end
filename() click to toggle source
# File lib/dato/local/field_type/file.rb, line 80
def filename
  @upload.filename
end
focal_point() click to toggle source
# File lib/dato/local/field_type/file.rb, line 106
def focal_point
  default_metadata = @upload.default_field_metadata.deep_stringify_keys
                            .fetch(I18n.locale.to_s, {})
  @focal_point || default_metadata["focal_point"]
end
format() click to toggle source
# File lib/dato/local/field_type/file.rb, line 52
def format
  @upload.format
end
height() click to toggle source
# File lib/dato/local/field_type/file.rb, line 64
def height
  @upload.height
end
id() click to toggle source
# File lib/dato/local/field_type/file.rb, line 44
def id
  @upload.id
end
is_image() click to toggle source
# File lib/dato/local/field_type/file.rb, line 120
def is_image
  @upload.is_image
end
lqip_data_url(opts = {}) click to toggle source
# File lib/dato/local/field_type/file.rb, line 245
def lqip_data_url(opts = {})
  @imgix_host != "www.datocms-assets.com" and
    raise "#lqip_data_url can only be used with www.datocms-assets.com domain"

  response = Faraday.get(file.to_url(opts.merge(lqip: "blurhash")))

  "data:image/jpeg;base64,#{Base64.strict_encode64(response.body)}" if response.status == 200
end
mime_type() click to toggle source
# File lib/dato/local/field_type/file.rb, line 128
def mime_type
  @upload.mime_type
end
notes() click to toggle source
# File lib/dato/local/field_type/file.rb, line 72
def notes
  @upload.notes
end
path() click to toggle source
# File lib/dato/local/field_type/file.rb, line 48
def path
  @upload.path
end
size() click to toggle source
# File lib/dato/local/field_type/file.rb, line 56
def size
  @upload.size
end
smart_tags() click to toggle source
# File lib/dato/local/field_type/file.rb, line 116
def smart_tags
  @upload.smart_tags
end
tags() click to toggle source
# File lib/dato/local/field_type/file.rb, line 112
def tags
  @upload.tags
end
title() click to toggle source
# File lib/dato/local/field_type/file.rb, line 94
def title
  default_metadata = @upload.default_field_metadata.deep_stringify_keys
                            .fetch(I18n.locale.to_s, {})
  @title || default_metadata["title"]
end
to_hash(*_args) click to toggle source
# File lib/dato/local/field_type/file.rb, line 254
def to_hash(*_args)
  {
    id: id,
    format: format,
    size: size,
    width: width,
    height: height,
    alt: alt,
    title: title,
    custom_data: custom_data,
    focal_point: focal_point,
    url: url,
    copyright: copyright,
    tags: tags,
    smart_tags: smart_tags,
    filename: filename,
    basename: basename,
    is_image: is_image,
    exif_info: exif_info,
    mime_type: mime_type,
    colors: colors.map(&:to_hash),
    blurhash: blurhash,
    video: video && video.to_hash,
  }
end
url(query = {}) click to toggle source
# File lib/dato/local/field_type/file.rb, line 224
def url(query = {})
  query.deep_stringify_keys!

  if focal_point &&
     query["fit"] == "crop" &&
     (query["h"] || query["height"]) &&
     (query["w"] || query["width"]) &&
     [nil, "focalpoint"].include?(query["crop"]) &&
     query["fp-x"].nil? &&
     query["fp-y"].nil?

    query.merge!(
      "crop" => "focalpoint",
      "fp-x" => focal_point[:x],
      "fp-y" => focal_point[:y],
    )
  end

  file.to_url(query)
end
video() click to toggle source
# File lib/dato/local/field_type/file.rb, line 212
def video
  VideoAttributes.new(@upload) if @upload.mux_playback_id
end
width() click to toggle source
# File lib/dato/local/field_type/file.rb, line 60
def width
  @upload.width
end