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)



301
302
303
# File 'lib/lws/apps/generic.rb', line 301

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



354
355
356
357
358
359
360
361
362
363
364
# File 'lib/lws/apps/generic.rb', line 354

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)



314
315
316
317
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
# File 'lib/lws/apps/generic.rb', line 314

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