1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
# frozen_string_literal: true
module Sentry
class Attachment
PathNotFoundError = Class.new(StandardError)
attr_reader :bytes, :filename, :path, :content_type
def initialize(bytes: nil, filename: nil, content_type: nil, path: nil)
@bytes = bytes
@filename = filename || infer_filename(path)
@path = path
@content_type = content_type
end
def to_envelope_headers
{ type: "attachment", filename: filename, content_type: content_type, length: payload.bytesize }
end
def payload
@payload ||= if bytes
bytes
else
File.binread(path)
end
rescue Errno::ENOENT
raise PathNotFoundError, "Failed to read attachment file, file not found: #{path}"
end
private
def infer_filename(path)
if path
File.basename(path)
else
raise ArgumentError, "filename or path is required"
end
end
end
end
|