Class: Spree::Api::V3::Admin::ExportsController

Inherits:
ResourceController show all
Includes:
ActiveStorage::SetCurrent
Defined in:
app/controllers/spree/api/v3/admin/exports_controller.rb

Overview

See ‘docs/plans/5.5-admin-spa-csv-export.md`.

Constant Summary

Constants inherited from BaseController

BaseController::RATE_LIMIT_RESPONSE

Constants included from Idempotent

Idempotent::IDEMPOTENCY_HEADER, Idempotent::IDEMPOTENCY_TTL, Idempotent::MAX_KEY_LENGTH, Idempotent::MUTATING_METHODS

Constants included from ErrorHandler

ErrorHandler::ERROR_CODES

Constants included from JwtAuthentication

JwtAuthentication::JWT_AUDIENCE_ADMIN, JwtAuthentication::JWT_AUDIENCE_STORE, JwtAuthentication::JWT_ISSUER, JwtAuthentication::USER_TYPE_ADMIN, JwtAuthentication::USER_TYPE_CUSTOMER

Instance Method Summary collapse

Methods inherited from ResourceController

#create, #destroy, #index, #show, #update

Methods included from Spree::Api::V3::ApiKeyAuthentication

#authenticate_api_key!, #authenticate_secret_key!

Methods included from JwtAuthentication

#authenticate_user, #require_authentication!

Instance Method Details

#downloadObject

We stream the CSV inline rather than redirecting to ActiveStorage’s signed-URL endpoint because the SPA’s Vite proxy only forwards ‘/api/*`. A cross-origin redirect to `/rails/active_storage/…` strips the Authorization header and the download fails silently.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/controllers/spree/api/v3/admin/exports_controller.rb', line 15

def download
  @resource = find_resource
  authorize_resource!(@resource, :show)

  unless @resource.done?
    return render_error(
      code: Spree::Api::V3::ErrorHandler::ERROR_CODES[:export_not_ready],
      message: 'Export is not ready yet',
      status: :unprocessable_content
    )
  end

  attachment = @resource.attachment
  send_data(
    attachment.download,
    filename: attachment.filename.to_s,
    type: attachment.content_type || 'text/csv',
    disposition: 'attachment'
  )
end