Class: Spree::DigitalAsset

Inherits:
Object
  • Object
show all
Includes:
Metadata
Defined in:
app/models/spree/digital_asset.rb

Constant Summary collapse

PROVIDER_SETTINGS_KEY =

Provider config lives under one key in metadata so a provider owns its own namespace without colliding with other developer metadata. The values match the provider's settings_schema; the provider reads them in deliver.

'provider'.freeze
DEFAULT_PROVIDER =

A blank provider_type is the uploaded-file default; the provider class says whether an attachment is required, so file assets keep today's rule and provider-backed assets (which carry no file) are exempt.

'Spree::DigitalAssetProvider::File'.freeze
DEFAULT_SELLER_CONTENT_TYPES =

What a seller may upload unless the marketplace says otherwise: the formats digital goods usually come in. Executables, scripts and unrecognised binaries are left out on purpose, as is SVG, which can carry scripts. A marketplace selling a niche format adds it in settings. The text formats have no signature, so they are taken at the type the upload declared.

%w[
  application/pdf application/epub+zip application/zip application/x-7z-compressed application/gzip
  application/json text/plain text/csv
  audio/mpeg audio/mp4 audio/aac audio/flac audio/ogg audio/wav audio/x-wav audio/midi
  video/mp4 video/webm video/quicktime
  image/jpeg image/png image/gif image/webp image/tiff
].freeze

Instance Method Summary collapse

Methods included from Metadata

#private_metadata, #private_metadata=

Instance Method Details

#deliver(digital_link, expires_in:) ⇒ Spree::DigitalDelivery?

Produces the deliverable for one authorized download — a redirect URL or an inline value — via the provider. Callers stay out of the storage layer so a provider-backed asset can answer without an attachment.

Parameters:

Returns:



110
111
112
# File 'app/models/spree/digital_asset.rb', line 110

def deliver(digital_link, expires_in:)
  provider_class.new(self).deliver(digital_link, expires_in: expires_in)
end

#download_url(expires_in:) ⇒ String?

Returns nil when there is nothing to download.

Parameters:

  • expires_in (ActiveSupport::Duration) —

    lifetime of the signed URL

Returns:

  • (String, nil) —

    nil when there is nothing to download



121
122
123
124
125
# File 'app/models/spree/digital_asset.rb', line 121

def download_url(expires_in:)
  return unless downloadable?

  attachment.url(expires_in: expires_in, disposition: :attachment)
end

#downloadable? ⇒ Boolean

Returns whether there is anything to hand to a customer.

Returns:

  • (Boolean) —

    whether there is anything to hand to a customer



115
116
117
# File 'app/models/spree/digital_asset.rb', line 115

def downloadable?
  attachment.attached?
end

#effective_authorized_clicks(store = self.store) ⇒ Integer

Download allowances fall back to the owning store's settings when the asset leaves them blank, so a catalog can mix evergreen files with time-limited ones without changing the store default.

The store is passed in by the caller, which already holds it — resolving it here would walk variant → product → store for every link checked.

Parameters:

Returns:

  • (Integer)


66
67
68
# File 'app/models/spree/digital_asset.rb', line 66

def effective_authorized_clicks(store = self.store)
  authorized_clicks || store.preferred_digital_asset_authorized_clicks
end

#effective_authorized_days(store = self.store) ⇒ Integer

Parameters:

Returns:

  • (Integer)


72
73
74
# File 'app/models/spree/digital_asset.rb', line 72

def effective_authorized_days(store = self.store)
  authorized_days || store.preferred_digital_asset_authorized_days
end

#provider_class ⇒ Class

The strategy that produces this asset's deliverable. A blank provider_type resolves to the uploaded-file default, exactly as DeliveryMethod resolves its provider — so an unset column is a File asset, not an error.

Returns:

  • (Class)


86
87
88
# File 'app/models/spree/digital_asset.rb', line 86

def provider_class
  (provider_type.presence || DEFAULT_PROVIDER).safe_constantize || DEFAULT_PROVIDER.constantize
end

#provider_settings ⇒ Hash

The provider's per-asset configuration, as declared by its settings_schema. Stored under one key in metadata.

Returns:

  • (Hash)


94
95
96
# File 'app/models/spree/digital_asset.rb', line 94

def provider_settings
  .fetch(PROVIDER_SETTINGS_KEY, {}) || {}
end

#provider_settings=(values) ⇒ Object

Parameters:

  • values (Hash)


99
100
101
# File 'app/models/spree/digital_asset.rb', line 99

def provider_settings=(values)
  self. = .merge(PROVIDER_SETTINGS_KEY => (values || {}))
end

#store ⇒ Spree::Store

Returns the store this asset's product belongs to.

Returns:

  • (Spree::Store) —

    the store this asset's product belongs to



77
78
79
# File 'app/models/spree/digital_asset.rb', line 77

def store
  product.store
end