Class: LWS::Generic::Storage

Inherits:
Object
  • Object
show all
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.

Class Method Summary collapse

Class Method Details

.create(file_or_io, filename, content_type = "application/octet-stream") ⇒ String?

Deprecated.

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).

Parameters:

  • file_or_io (File, IO)

    the file (or IO object) to upload

  • filename (String)

    the filename to use for the uploaded file/IO object

  • content_type (String) (defaults to: "application/octet-stream")

    the content type of the uploaded file/IO object

Returns:

  • (String, nil)

    the storage ID (if successful)



273
274
275
# File 'lib/lws/apps/generic.rb', line 273

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.

Parameters:

  • url (String)

    the URL to download the file from

Returns:

  • (String)

    the contents of the file



326
327
328
329
330
331
332
333
334
335
336
# File 'lib/lws/apps/generic.rb', line 326

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).

Parameters:

  • file_or_io (File, IO)

    the file (or IO object) to upload

  • filename (String)

    the filename to use for the uploaded file/IO object

  • content_type (String) (defaults to: "application/octet-stream")

    the content type of the uploaded file/IO object

Returns:

  • (String, nil)

    the storage ID (if successful)



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/lws/apps/generic.rb', line 286

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