Class: Dato::Local::FieldType::File
- Inherits:
-
Object
- Object
- Dato::Local::FieldType::File
- Defined in:
- lib/dato/local/field_type/file.rb
Defined Under Namespace
Classes: VideoAttributes
Class Method Summary collapse
Instance Method Summary collapse
- #alt ⇒ Object
- #author ⇒ Object
- #basename ⇒ Object
- #blurhash ⇒ Object
- #colors ⇒ Object
- #copyright ⇒ Object
- #custom_data ⇒ Object
- #exif_info ⇒ Object
- #file ⇒ Object
- #filename ⇒ Object
- #focal_point ⇒ Object
- #format ⇒ Object
- #height ⇒ Object
- #id ⇒ Object
-
#initialize(upload, alt, title, custom_data, focal_point, imgix_host) ⇒ File
constructor
A new instance of File.
- #is_image ⇒ Object
- #lqip_data_url(opts = {}) ⇒ Object
- #mime_type ⇒ Object
- #notes ⇒ Object
- #path ⇒ Object
- #size ⇒ Object
- #smart_tags ⇒ Object
- #tags ⇒ Object
- #title ⇒ Object
- #to_hash(*_args) ⇒ Object
- #url(query = {}) ⇒ Object
- #video ⇒ Object
- #width ⇒ Object
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
#alt ⇒ Object
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 |
#author ⇒ Object
68 69 70 |
# File 'lib/dato/local/field_type/file.rb', line 68 def @upload. end |
#basename ⇒ Object
84 85 86 |
# File 'lib/dato/local/field_type/file.rb', line 84 def basename @upload.basename end |
#blurhash ⇒ Object
136 137 138 |
# File 'lib/dato/local/field_type/file.rb', line 136 def blurhash @upload.blurhash end |
#colors ⇒ Object
132 133 134 |
# File 'lib/dato/local/field_type/file.rb', line 132 def colors @upload.colors.map { |color| Color.parse(color, nil) } end |
#copyright ⇒ Object
76 77 78 |
# File 'lib/dato/local/field_type/file.rb', line 76 def copyright @upload.copyright end |
#custom_data ⇒ Object
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_info ⇒ Object
124 125 126 |
# File 'lib/dato/local/field_type/file.rb', line 124 def exif_info @upload.exif_info end |
#file ⇒ Object
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 |
#filename ⇒ Object
80 81 82 |
# File 'lib/dato/local/field_type/file.rb', line 80 def filename @upload.filename end |
#focal_point ⇒ Object
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 |
#format ⇒ Object
52 53 54 |
# File 'lib/dato/local/field_type/file.rb', line 52 def format @upload.format end |
#height ⇒ Object
64 65 66 |
# File 'lib/dato/local/field_type/file.rb', line 64 def height @upload.height end |
#id ⇒ Object
44 45 46 |
# File 'lib/dato/local/field_type/file.rb', line 44 def id @upload.id end |
#is_image ⇒ Object
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_type ⇒ Object
128 129 130 |
# File 'lib/dato/local/field_type/file.rb', line 128 def mime_type @upload.mime_type end |
#notes ⇒ Object
72 73 74 |
# File 'lib/dato/local/field_type/file.rb', line 72 def notes @upload.notes end |
#path ⇒ Object
48 49 50 |
# File 'lib/dato/local/field_type/file.rb', line 48 def path @upload.path end |
#size ⇒ Object
56 57 58 |
# File 'lib/dato/local/field_type/file.rb', line 56 def size @upload.size end |
#smart_tags ⇒ Object
116 117 118 |
# File 'lib/dato/local/field_type/file.rb', line 116 def @upload. end |
#tags ⇒ Object
112 113 114 |
# File 'lib/dato/local/field_type/file.rb', line 112 def @upload. end |
#title ⇒ Object
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: , 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 |
#video ⇒ Object
212 213 214 |
# File 'lib/dato/local/field_type/file.rb', line 212 def video VideoAttributes.new(@upload) if @upload.mux_playback_id end |
#width ⇒ Object
60 61 62 |
# File 'lib/dato/local/field_type/file.rb', line 60 def width @upload.width end |