Class: Spree::Api::V2::Storefront::DigitalsController

Inherits:
ResourceController show all
Defined in:
app/controllers/spree/api/v2/storefront/digitals_controller.rb

Instance Method Summary collapse

Methods inherited from ResourceController

#index, #show

Methods included from Caching

#collection_cache_key, #collection_cache_opts

Methods included from CollectionOptionsHelpers

#collection_links, #collection_meta, #collection_permitted_params

Methods inherited from BaseController

#content_type

Instance Method Details

#downloadObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/controllers/spree/api/v2/storefront/digitals_controller.rb', line 6

def download
  if attachment.present?
    if digital_link.authorize!
      if defined?(ActiveStorage::Service::DiskService) && ActiveStorage::Blob.service.instance_of?(ActiveStorage::Service::DiskService)
        # The asset is hosted on disk, use send_file.

        send_file(
          ActiveStorage::Blob.service.path_for(attachment.key),
          filename: attachment.filename.to_s,
          type: attachment.content_type.to_s,
          status: :ok
        ) and return

      else
        # The asset is hosted on a 3rd party service, use an expiring url with disposition: 'attachment'.

        redirect_to attachment.url(
          expires_in: current_store.digital_asset_link_expire_time.seconds,
          disposition: 'attachment',
          host: digital_attachment_host
        ) and return

      end
    end
  else
    Rails.logger.error I18n.t('spree.api.v2.digitals.missing_file')
  end

  render json: { error: I18n.t('spree.api.v2.digitals.unauthorized') }, status: 403
end