Module: Spree::Uploads

Defined in:
lib/spree/uploads.rb,
app/services/spree/uploads/assign_stores.rb,
app/jobs/spree/uploads/purge_unattached_job.rb

Overview

Store ownership of uploaded files (docs/plans/6.0-uploads-and-file-ownership.md).

Every ActiveStorage::Blob records the store that owns it, and attaching a blob to a record of another store is reported. Spree 6.0 only logs; 6.1 refuses, so one release of logs finds every path that still creates blobs without a store.

Defined Under Namespace

Modules: AttachmentStoreCheck, BlobStoreOwnership Classes: AssignStores, PurgeUnattachedJob

Constant Summary collapse

SIGNED_ID_EXPIRY =

How long an upload reference (signed_id) stays attachable.

1.day

Class Method Summary collapse

Class Method Details

.signed_id_for(blob) ⇒ String

Mints an expiring upload reference for blob.

Parameters:

  • blob (ActiveStorage::Blob)

Returns:

  • (String) —

    an opaque reference clients pass back unchanged



16
17
18
# File 'lib/spree/uploads.rb', line 16

def self.signed_id_for(blob)
  blob.signed_id(expires_in: SIGNED_ID_EXPIRY)
end

.store_id_for(record) ⇒ Integer, ...

The store owning record, or nil for records that belong to no store (admin users, customers).

Parameters:

  • record (ActiveRecord::Base)

Returns:

  • (Integer, String, nil)


25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/spree/uploads.rb', line 25

def self.store_id_for(record)
  return record.id if record.is_a?(Spree::Store)
  # A processed rendition belongs to whoever owns its original.
  return record.blob&.store_id if record.is_a?(ActiveStorage::VariantRecord)
  return record.store_id if record.has_attribute?(:store_id)

  record.try(:store)&.id
rescue Module::DelegationError
  # An owner whose parent is gone (a digital asset on a deleted variant)
  # has no store to compare against.
  nil
end