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
Class Method Summary collapse
-
.create(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?
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).
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/lws/apps/generic.rb', line 223 def self.create(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 = @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 = @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 |