Class: M365ActiveStorage::BlobsController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
lib/m365_active_storage/controllers/blobs_controller.rb

Overview

SharePoint Blob Download Controller

Handles serving of attached files stored in SharePoint through a Rails action.

Responsibilities

  • Serve blobs securely using signed URLs

  • Validate blob access through ActiveStorage signatures

  • Handle Download requests with appropriate content headers

  • Recover from authentication errors (e.g., expired tokens)

  • Provide appropriate HTTP error responses

Route

This controller is automatically registered with Rails via the Railtie. Typically used with ActiveStorage’s url_for helper:

url_for(@document.file)  # => /rails/active_storage/blobs/signed_id/document.pdf

Security

Access is secured through ActiveStorage signed IDs. The signature ensures:

  • Only authorized users can download blobs

  • URLs are time-limited if configured

  • Tampering with signed IDs is detected

Example Usage

In your Rails view:

<%= link_to "Download", @document.file, download: true %>
<%= image_tag @document.image %>

Error Handling

  • Invalid signatures return 404 Not Found

  • Authentication failures trigger token refresh and retry

  • Other errors return 500 Internal Server Error

See Also:

  • ActiveStorage::Blob
  • ActiveStorage::Service::SharepointService

Instance Method Summary collapse

Instance Method Details

#showvoid

This method returns an undefined value.

Display/download a blob

Retrieves a blob by its signed ID and sends it to the client with appropriate Content-Type and disposition headers.

This action:

  1. Extracts the signed blob ID from the URL

  2. Validates the signature using ActiveStorage’s signing mechanism

  3. Downloads the file from SharePoint

  4. Returns it with appropriate HTTP headers

  5. Handles token expiration by retrying once

Examples:

# GET /rails/active_storage/blobs/:signed_id/document.pdf
# Returns the document with:
# Content-Type: application/pdf
# Content-Disposition: inline; filename="document.pdf"

Raises:

  • (ActiveSupport::MessageVerifier::InvalidSignature)

    if signature is invalid

  • (StandardError)

    if download fails after retry

See Also:

  • #find_blob
  • #download


71
72
73
74
75
76
77
78
79
80
# File 'lib/m365_active_storage/controllers/blobs_controller.rb', line 71

def show
  # Skip sharepoint authentication - blob is secured via signed ID
  signed_id = params[:signed_id]
  blob = find_blob(signed_id)
  return head :not_found unless blob

  download(blob)
rescue StandardError
  head :internal_server_error
end