Class: Scrivito::Binary
- Inherits:
-
Object
- Object
- Scrivito::Binary
- Defined in:
- app/cms/scrivito/binary.rb
Overview
Class Method Summary collapse
-
.upload(file_or_path, options = {}) ⇒ Scrivito::FutureBinary
Uploads a local file to the CMS.
Instance Method Summary collapse
-
#content_length ⇒ Integer
The length of this binary data, in bytes.
-
#content_type ⇒ String
The content type of this binary data, for example “image/jpeg”.
-
#copy(options = {}) ⇒ Scrivito::FutureBinary
Create a copy of this Binary with a different filename and/or content type.
-
#filename ⇒ String
The filename of this binary data, for example “my_image.jpg”.
-
#meta_data ⇒ Scrivito::MetaDataCollection
Returns the meta data for the given binary.
-
#original ⇒ Scrivito::Binary
Returns the original version of a transformed binary.
-
#private? ⇒ Boolean
Some Scrivito data is considered private, i.e.
-
#transform(definition) ⇒ Scrivito::Binary
Use this method to transform images, i.e.
-
#transformed? ⇒ Boolean
Returns whether a binary has been transformed.
-
#url ⇒ String
The URL for accessing the binary data and downloading it using an HTTP GET request.
Class Method Details
.upload(file_or_path, options = {}) ⇒ Scrivito::FutureBinary
Uploads a local file to the CMS.
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'app/cms/scrivito/binary.rb', line 58 def self.upload(file_or_path, = {}) () file_to_upload = case file_or_path when File, Tempfile then file_or_path else File.new(file_or_path) end FutureBinary.new(.reverse_merge( filename: File.basename(file_or_path), file_to_upload: file_to_upload, )) end |
Instance Method Details
#content_length ⇒ Integer
The length of this binary data, in bytes.
170 171 172 173 |
# File 'app/cms/scrivito/binary.rb', line 170 def content_length raise_field_not_available('Content length') if transformed? [:content_length] end |
#content_type ⇒ String
The content type of this binary data, for example “image/jpeg”.
160 161 162 163 |
# File 'app/cms/scrivito/binary.rb', line 160 def content_type raise_field_not_available('Content type') if transformed? [:content_type] end |
#copy(options = {}) ⇒ Scrivito::FutureBinary
Create a copy of this Binary with a different filename and/or content type.
91 92 93 94 |
# File 'app/cms/scrivito/binary.rb', line 91 def copy( = {}) self.class.() FutureBinary.new(.reverse_merge(filename: self.filename, id_to_copy: id)) end |
#filename ⇒ String
The filename of this binary data, for example “my_image.jpg”.
151 152 153 |
# File 'app/cms/scrivito/binary.rb', line 151 def filename File.basename(URI(url).path) end |
#meta_data ⇒ Scrivito::MetaDataCollection
Returns the meta data for the given binary.
318 319 320 321 322 323 324 325 |
# File 'app/cms/scrivito/binary.rb', line 318 def raise_field_not_available('Meta data') if transformed? @meta_data ||= begin = (CmsBackend.(id)) MetaDataCollection.new() end end |
#original ⇒ Scrivito::Binary
Returns the original version of a transformed binary.
If a binary is the result of a transformation, the original version of the binary is returned. Otherwise self
.
301 302 303 |
# File 'app/cms/scrivito/binary.rb', line 301 def original @original || self end |
#private? ⇒ Boolean
Some Scrivito data is considered private, i.e. it is not currently intended for the general public, for example content in a workspace that has not been published yet.
101 102 103 |
# File 'app/cms/scrivito/binary.rb', line 101 def private? !@is_public end |
#transform(definition) ⇒ Scrivito::Binary
Use this method to transform images, i.e. to scale down large images or to generate thumbnails of images. Only applicable if this Scrivito::Binary is an image.
This method does not change the binary. Instead, it returns a copy of it, transformed using the definition
.
If the original binary has already been transformed, the returned binary will be a combination of the transformations. Thus, the transformations can be chained (see examples).
The transformed data is calculated “lazily”, so calling #transform does not trigger any calculation. The calculation is triggered only when data is accessed, for example via #url.
Note that transforming images is slow and therefore should not be carried out inside a request. The #scrivito_image_tag and #scrivito_path helpers transform images asynchronously and don’t place additional load onto requests.
277 278 279 280 281 282 |
# File 'app/cms/scrivito/binary.rb', line 277 def transform(definition) self.class.new(id, public?, transformation_definition: (transformation_definition || {}).merge(definition), original: original, obj_id: obj_id) end |
#transformed? ⇒ Boolean
Returns whether a binary has been transformed.
288 289 290 |
# File 'app/cms/scrivito/binary.rb', line 288 def transformed? !!transformation_definition end |
#url ⇒ String
The URL is calculated on demand, i.e. if the URL has not been cached yet, this method calls the Scrivito API to retrieve the URL. If you want to link to a Binary, consider using ControllerHelper#scrivito_url instead. This is generally much faster, as it performs the Scrivito API call asynchronously.
URLs for private content have an expiration time in order to protect them. Therefore, the URL should be accessed immediately after it has been returned (i.e. within a couple of minutes). Accessing it after expiration causes an error.
The URL for accessing the binary data and downloading it using an HTTP GET request.
The URLs should not be used for long-term storage since they are no longer accessible hours or days after they have been generated.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'app/cms/scrivito/binary.rb', line 125 def url blob_data = CmsBackend.find_blob_data(id, access_type, 'get', transformation_definition: transformation_definition) blob_data['url'] rescue ClientError => e case e.backend_code when /\Abinary\.unprocessable\.image\.transform\.source\./ raise TransformationSourceError.new(e., e.backend_code) when 'binary.unprocessable.image.transform.config.invalid' raise TransformationDefinitionError.new(e., e.backend_code) else raise e end end |