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).
251 252 253 |
# File 'lib/lws/apps/generic.rb', line 251 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
.
304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/lws/apps/generic.rb', line 304 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).
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/lws/apps/generic.rb', line 264 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 |