Class: LWS::Generic::Storage
- Inherits:
-
Object
- Object
- LWS::Generic::Storage
- Defined in:
- lib/lws/apps/generic.rb
Overview
The storage class
This class can be used to download and upload files for this app module.
Direct Known Subclasses
DigitalSignage::Storage, Presence::Storage, Resource::Storage
Class Method Summary collapse
-
.create(file_or_io, filename, content_type = "application/octet-stream") ⇒ String?
deprecated
Deprecated.
To have a consistent upload/download naming counterpart, this method has been deprecated. Use Storage.upload instead.
-
.download(url) ⇒ String
Downloads a file at the provided URL from the storage using the appropriate API tokens.
-
.upload(file_or_io, filename, content_type = "application/octet-stream") ⇒ String?
Uploads a file (or IO object) to the storage of the app module.
Class Method Details
.create(file_or_io, filename, content_type = "application/octet-stream") ⇒ String?
To have a consistent upload/download naming counterpart, this method has been deprecated. Use upload instead.
Uploads a file (or IO object) to the storage of the app module.
The resulting storage ID can be used in model attributes that refer to storage IDs (e.g. DigitalSignage::Layout::Element#asset_storage_ids).
305 306 307 |
# File 'lib/lws/apps/generic.rb', line 305 def self.create(file_or_io, filename, content_type = "application/octet-stream") upload(file_or_io, filename, content_type) end |
.download(url) ⇒ String
Downloads a file at the provided URL from the storage using the appropriate API tokens.
This method can be used to download images/assets/etc. from LWS using URLs provided by attributes that end with _url
.
358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/lws/apps/generic.rb', line 358 def self.download(url) return nil if url.nil? unless url.start_with? @lws_connection.url_prefix.to_s raise ArgumentError, "URL does not belong to this LWS app (endpoint: #{@lws_connection.url_prefix})" end res = @lws_connection.get(url) res.body end |
.upload(file_or_io, filename, content_type = "application/octet-stream") ⇒ String?
Uploads a file (or IO object) to the storage of the app module.
The resulting storage ID can be used in model attributes that refer to storage IDs (e.g. DigitalSignage::Layout::Element#asset_storage_ids).
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/lws/apps/generic.rb', line 318 def self.upload(file_or_io, filename, content_type = "application/octet-stream") return nil if file_or_io.closed? data = file_or_io.read checksum = Digest::MD5.base64digest(data) body = { blob: { filename: filename, content_type: content_type, byte_size: data.bytesize, checksum: checksum } } res = @as_connection.post do |req| req.url "rails/active_storage/direct_uploads" req.headers["Accept"] = "application/json" req.headers["Content-Type"] = "application/json" req.body = body.to_json end if res.success? result = JSON.parse(res.body) res = @as_connection.put do |req| req.url result.dig("direct_upload", "url") result.dig("direct_upload", "headers").each do |hdr, val| req.headers[hdr] = val end req.body = data end if res.success? result["signed_id"] end else nil end end |