Class: Protege::Gateway::AttachmentPolicy

Inherits:
Data
  • Object
show all
Defined in:
lib/protege/gateway/attachment_policy.rb

Overview

Immutable attachment limits + the policy that enforces them. A Gateway value type consulted at the mail edges — built through the Gateway.build_attachment_policy factory and assigned as config.attachment_policy:

config.attachment_policy = Protege::Gateway.build_attachment_policy(max_bytes: 5.megabytes)

A Data value with the engine's default limits, consulted at the edges: the inbound mailbox bounces a violation, and the send_email tool refuses one. enabled is the master switch — when false, any attachment is rejected (inbound mail with attachments bounces; the agent can't attach files), while plain mail is unaffected. When enabled, limits are checked in a fixed order — count, then per-attachment size, then total size — and the first breach wins, so #violation returns a single human-readable reason (or nil when the set is acceptable).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enabled: true, max_count: 10, max_bytes: 10 * 1024 * 1024, max_total_bytes: 25 * 1024 * 1024) ⇒ AttachmentPolicy

Returns a new instance of AttachmentPolicy.

Parameters:

  • enabled (Boolean) (defaults to: true)

    whether attachments are accepted at all (default true)

  • max_count (Integer) (defaults to: 10)

    maximum attachments per message (default 10)

  • max_bytes (Integer) (defaults to: 10 * 1024 * 1024)

    maximum bytes per attachment (default 10 MB)

  • max_total_bytes (Integer) (defaults to: 25 * 1024 * 1024)

    maximum combined bytes (default 25 MB)



22
23
24
25
26
27
28
29
# File 'lib/protege/gateway/attachment_policy.rb', line 22

def initialize(
  enabled: true,
  max_count: 10,
  max_bytes: 10 * 1024 * 1024,
  max_total_bytes: 25 * 1024 * 1024
)
  super
end

Instance Attribute Details

#enabledObject (readonly)

Returns the value of attribute enabled

Returns:

  • (Object)

    the current value of enabled



17
18
19
# File 'lib/protege/gateway/attachment_policy.rb', line 17

def enabled
  @enabled
end

#max_bytesObject (readonly)

Returns the value of attribute max_bytes

Returns:

  • (Object)

    the current value of max_bytes



17
18
19
# File 'lib/protege/gateway/attachment_policy.rb', line 17

def max_bytes
  @max_bytes
end

#max_countObject (readonly)

Returns the value of attribute max_count

Returns:

  • (Object)

    the current value of max_count



17
18
19
# File 'lib/protege/gateway/attachment_policy.rb', line 17

def max_count
  @max_count
end

#max_total_bytesObject (readonly)

Returns the value of attribute max_total_bytes

Returns:

  • (Object)

    the current value of max_total_bytes



17
18
19
# File 'lib/protege/gateway/attachment_policy.rb', line 17

def max_total_bytes
  @max_total_bytes
end

Instance Method Details

#permits?(byte_sizes:) ⇒ Boolean

Report whether the given attachment sizes are acceptable.

Parameters:

  • byte_sizes (Array<Integer>)

    each attachment's size in bytes

Returns:

  • (Boolean)

    true when the set is acceptable



35
36
37
# File 'lib/protege/gateway/attachment_policy.rb', line 35

def permits?(byte_sizes:)
  violation(byte_sizes:).nil?
end

#violation(byte_sizes:) ⇒ String?

Return the first reason the set is rejected, or nil when acceptable. A message with no attachments is always acceptable, even when attachments are disabled.

Parameters:

  • byte_sizes (Array<Integer>)

    each attachment's size in bytes

Returns:

  • (String, nil)

    the rejection reason, or nil



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/protege/gateway/attachment_policy.rb', line 44

def violation(byte_sizes:)
  sizes = Array(byte_sizes)

  return nil if sizes.empty?

  %i[
    attachments_disabled?
    too_many_attachments?
    any_attachment_too_large?
    total_size_exceeded?
  ].each do |validator|
    if (reason = send(validator, sizes))
      return reason
    end
  end

  nil
end