Class: Spree::FileUpload
- Inherits:
-
Object
- Object
- Spree::FileUpload
- Includes:
- ActiveModel::Attributes, ActiveModel::Model
- Defined in:
- app/models/spree/file_upload.rb
Overview
A file sent to POST /files (docs/plans/6.0-uploads-and-file-ownership.md),
before anything uses it.
Two ways in, one result. A presigned upload carries only the file's
metadata and gets back a storage target the client sends the bytes to; a
multipart upload carries the bytes, which are stored here. Either way the
result is a blob owned by store and an expiring, opaque signed_id the
client passes to whichever endpoint uses the file.
Never persisted: the blob is the record.
Defined Under Namespace
Classes: Target
Constant Summary collapse
- VISIBILITIES =
%w[public private].freeze
Instance Attribute Summary collapse
-
#blob ⇒ Object
readonly
Returns the value of attribute blob.
-
#expires_at ⇒ Object
readonly
Returns the value of attribute expires_at.
-
#signed_id ⇒ Object
readonly
Returns the value of attribute signed_id.
Class Method Summary collapse
-
.public_content_types ⇒ Array<String>
The content types a public upload may have.
Instance Method Summary collapse
-
#multipart? ⇒ Boolean
Whether the bytes travel in the request.
-
#private? ⇒ Boolean
Whether the file lands on private storage.
-
#save ⇒ Boolean
Validates and creates the blob.
-
#upload_target ⇒ Spree::FileUpload::Target?
Where a presigned upload's bytes go, or nil once the file is stored.
Instance Attribute Details
#blob ⇒ Object (readonly)
Returns the value of attribute blob.
31 32 33 |
# File 'app/models/spree/file_upload.rb', line 31 def blob @blob end |
#expires_at ⇒ Object (readonly)
Returns the value of attribute expires_at.
31 32 33 |
# File 'app/models/spree/file_upload.rb', line 31 def expires_at @expires_at end |
#signed_id ⇒ Object (readonly)
Returns the value of attribute signed_id.
31 32 33 |
# File 'app/models/spree/file_upload.rb', line 31 def signed_id @signed_id end |
Class Method Details
.public_content_types ⇒ Array<String>
The content types a public upload may have. Private uploads accept any type: digital products are ebooks, archives and audio, and each attachment validates its own file.
46 47 48 49 50 |
# File 'app/models/spree/file_upload.rb', line 46 def self.public_content_types Rails.application.config.active_storage.web_image_content_types + Spree::Config.video_content_types + %w[text/csv] end |
Instance Method Details
#multipart? ⇒ Boolean
Returns whether the bytes travel in the request.
53 54 55 |
# File 'app/models/spree/file_upload.rb', line 53 def multipart? file.respond_to?(:read) end |
#private? ⇒ Boolean
Returns whether the file lands on private storage.
58 59 60 |
# File 'app/models/spree/file_upload.rb', line 58 def private? visibility == 'private' end |
#save ⇒ Boolean
Validates and creates the blob. A multipart upload is stored here; a presigned one waits for the client to send its bytes to #upload_target.
66 67 68 69 70 71 72 73 74 |
# File 'app/models/spree/file_upload.rb', line 66 def save read_file_facts if multipart? return false unless valid? @blob = multipart? ? store_bytes : reserve_blob @expires_at = Spree::Uploads::SIGNED_ID_EXPIRY.from_now @signed_id = Spree::Uploads.signed_id_for(@blob) true end |
#upload_target ⇒ Spree::FileUpload::Target?
Where a presigned upload's bytes go, or nil once the file is stored.
79 80 81 82 83 84 85 86 87 |
# File 'app/models/spree/file_upload.rb', line 79 def upload_target return if multipart? || blob.nil? Target.new( http_method: 'PUT', url: blob.service_url_for_direct_upload, headers: blob.service_headers_for_direct_upload ) end |