Class: Dato::Local::FieldType::File

Inherits:
Object
  • Object
show all
Defined in:
lib/dato/local/field_type/file.rb

Defined Under Namespace

Classes: VideoAttributes

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(upload, alt, title, custom_data, focal_point, imgix_host) ⇒ File

Returns a new instance of File.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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

Class Method Details

.parse(value, repo) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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

Instance Method Details

#altObject



88
89
90
91
92
# File 'lib/dato/local/field_type/file.rb', line 88

def alt
   = @upload..deep_stringify_keys
                            .fetch(I18n.locale.to_s, {})
  @alt || ["alt"]
end

#authorObject



68
69
70
# File 'lib/dato/local/field_type/file.rb', line 68

def author
  @upload.author
end

#basenameObject



84
85
86
# File 'lib/dato/local/field_type/file.rb', line 84

def basename
  @upload.basename
end

#blurhashObject



136
137
138
# File 'lib/dato/local/field_type/file.rb', line 136

def blurhash
  @upload.blurhash
end

#colorsObject



132
133
134
# File 'lib/dato/local/field_type/file.rb', line 132

def colors
  @upload.colors.map { |color| Color.parse(color, nil) }
end


76
77
78
# File 'lib/dato/local/field_type/file.rb', line 76

def copyright
  @upload.copyright
end

#custom_dataObject



100
101
102
103
104
# File 'lib/dato/local/field_type/file.rb', line 100

def custom_data
   = @upload..deep_stringify_keys
                            .fetch(I18n.locale.to_s, {})
  @custom_data.merge(.fetch("custom_data", {}))
end

#exif_infoObject



124
125
126
# File 'lib/dato/local/field_type/file.rb', line 124

def exif_info
  @upload.exif_info
end

#fileObject



216
217
218
219
220
221
222
# 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

#filenameObject



80
81
82
# File 'lib/dato/local/field_type/file.rb', line 80

def filename
  @upload.filename
end

#focal_pointObject



106
107
108
109
110
# File 'lib/dato/local/field_type/file.rb', line 106

def focal_point
   = @upload..deep_stringify_keys
                            .fetch(I18n.locale.to_s, {})
  @focal_point || ["focal_point"]
end

#formatObject



52
53
54
# File 'lib/dato/local/field_type/file.rb', line 52

def format
  @upload.format
end

#heightObject



64
65
66
# File 'lib/dato/local/field_type/file.rb', line 64

def height
  @upload.height
end

#idObject



44
45
46
# File 'lib/dato/local/field_type/file.rb', line 44

def id
  @upload.id
end

#is_imageObject



120
121
122
# File 'lib/dato/local/field_type/file.rb', line 120

def is_image
  @upload.is_image
end

#lqip_data_url(opts = {}) ⇒ Object



245
246
247
248
249
250
251
252
# 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_typeObject



128
129
130
# File 'lib/dato/local/field_type/file.rb', line 128

def mime_type
  @upload.mime_type
end

#notesObject



72
73
74
# File 'lib/dato/local/field_type/file.rb', line 72

def notes
  @upload.notes
end

#pathObject



48
49
50
# File 'lib/dato/local/field_type/file.rb', line 48

def path
  @upload.path
end

#sizeObject



56
57
58
# File 'lib/dato/local/field_type/file.rb', line 56

def size
  @upload.size
end

#smart_tagsObject



116
117
118
# File 'lib/dato/local/field_type/file.rb', line 116

def smart_tags
  @upload.smart_tags
end

#tagsObject



112
113
114
# File 'lib/dato/local/field_type/file.rb', line 112

def tags
  @upload.tags
end

#titleObject



94
95
96
97
98
# File 'lib/dato/local/field_type/file.rb', line 94

def title
   = @upload..deep_stringify_keys
                            .fetch(I18n.locale.to_s, {})
  @title || ["title"]
end

#to_hash(*_args) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# 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 = {}) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# 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

#videoObject



212
213
214
# File 'lib/dato/local/field_type/file.rb', line 212

def video
  VideoAttributes.new(@upload) if @upload.mux_playback_id
end

#widthObject



60
61
62
# File 'lib/dato/local/field_type/file.rb', line 60

def width
  @upload.width
end