Class: Spree::AgentTools::UploadFile

Inherits:
Spree::AgentTool show all
Defined in:
app/tools/spree/agent_tools/upload_file.rb

Overview

Stores a file the merchant provided, through the Admin API's own upload endpoint, and answers with the id every other write accepts.

The dashboard uploads in two steps: ask for a storage target, then PUT the bytes to it. An agent has no second request to make, so this sends the bytes with the request — the multipart shape POST /files already accepts — and the endpoint owns the type allowlist, the size cap and which store the file belongs to.

Storing a file is not yet using it: the id is attached by whichever tool the merchant asked for next, under that tool's own permission.

Constant Summary collapse

PERMITTED_CONTENT_TYPES =

A private upload accepts any type, which is right for the dashboard — a merchant picking a file has already decided. A model repeating a filename back has decided nothing, so an agent is held to the kinds it has a reason to send.

%w[
  text/csv
  text/plain
  application/pdf
  image/jpeg
  image/png
  image/webp
  image/gif
  application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
].freeze

Constants inherited from Spree::AgentTool

Spree::AgentTool::PARENT_ID_DESCRIPTION

Instance Attribute Summary

Attributes inherited from Spree::AgentTool

#context

Instance Method Summary collapse

Methods inherited from Spree::AgentTool

additive!, audience, description, destructive?, #initialize, input_schema, mutating!, mutating?, param, params, permission, #permitted?, tool_name, #unknown_resource

Constructor Details

This class inherits a constructor from Spree::AgentTool

Instance Method Details

#call(filename:, content:, encoding: nil, content_type: nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/tools/spree/agent_tools/upload_file.rb', line 47

def call(filename:, content:, encoding: nil, content_type: nil)
  bytes, error = decode(content, encoding)
  return { error: error } if error

  # Read from the bytes, not from what the caller named it: Marcel
  # places the content first, so a renamed script is not a CSV.
  kind = Marcel::MimeType.for(StringIO.new(bytes), name: filename,
                                                   declared_type: content_type.presence)
  unless PERMITTED_CONTENT_TYPES.include?(kind)
    return { error: "Files of type #{kind.inspect} are not accepted here.",
             supported_content_types: PERMITTED_CONTENT_TYPES }
  end

  response = ApiDispatch.new(context).call(
    method: :post,
    path: '/api/v3/admin/files',
    body: {
      'filename' => filename,
      'content_type' => kind,
      # Agent uploads are private: a purchase order or a customer list
      # is not something to put behind a public URL, and a private
      # upload is the one that accepts a document at all.
      'visibility' => 'private',
      'file' => StringIO.new(bytes)
    }.compact
  )
  return { error: response.error_message } unless response.success?

  data = response.data.to_h

  {
    ok: true,
    file_id: data['signed_id'],
    filename: data['filename'],
    content_type: data['content_type'],
    byte_size: data['byte_size']
  }
end

#summary(arguments) ⇒ Object



86
87
88
# File 'app/tools/spree/agent_tools/upload_file.rb', line 86

def summary(arguments)
  "Store the file #{arguments[:filename]}"
end