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 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).
230 231 232 |
# File 'lib/lws/apps/generic.rb', line 230 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
.
283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/lws/apps/generic.rb', line 283 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).
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/lws/apps/generic.rb', line 243 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.length, 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 |