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
41
42
43
44
45
46
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
85
86
87
88
89
90
91
92
93
94
use std::fmt;

use serde::Deserialize;

/// The different types an attachment can have.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Deserialize)]
pub enum AttachmentType {
    #[serde(rename = "event.attachment")]
    /// (default) A standard attachment without special meaning.
    Attachment,
    /// A minidump file that creates an error event and is symbolicated. The
    /// file should start with the `MDMP` magic bytes.
    #[serde(rename = "event.minidump")]
    Minidump,
    /// An Apple crash report file that creates an error event and is symbolicated.
    #[serde(rename = "event.applecrashreport")]
    AppleCrashReport,
    /// An XML file containing UE4 crash meta data. During event ingestion,
    /// event contexts and extra fields are extracted from this file.
    #[serde(rename = "unreal.context")]
    UnrealContext,
    /// A plain-text log file obtained from UE4 crashes. During event ingestion,
    /// the last logs are extracted into event breadcrumbs.
    #[serde(rename = "unreal.logs")]
    UnrealLogs,
}

impl Default for AttachmentType {
    fn default() -> Self {
        Self::Attachment
    }
}

impl AttachmentType {
    /// Gets the string value Sentry expects for the attachment type.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Attachment => "event.attachment",
            Self::Minidump => "event.minidump",
            Self::AppleCrashReport => "event.applecrashreport",
            Self::UnrealContext => "unreal.context",
            Self::UnrealLogs => "unreal.logs",
        }
    }
}

#[derive(Clone, PartialEq, Default)]
/// Represents an attachment item.
pub struct Attachment {
    /// The actual attachment data.
    pub buffer: Vec<u8>,
    /// The filename of the attachment.
    pub filename: String,
    /// The Content Type of the attachment
    pub content_type: Option<String>,
    /// The special type of this attachment.
    pub ty: Option<AttachmentType>,
}

impl Attachment {
    /// Writes the attachment and its headers to the provided `Writer`.
    pub fn to_writer<W>(&self, writer: &mut W) -> std::io::Result<()>
    where
        W: std::io::Write,
    {
        writeln!(
            writer,
            r#"{{"type":"attachment","length":{length},"filename":"{filename}","attachment_type":"{at}","content_type":"{ct}"}}"#,
            filename = self.filename,
            length = self.buffer.len(),
            at = self.ty.unwrap_or_default().as_str(),
            ct = self
                .content_type
                .as_ref()
                .unwrap_or(&"application/octet-stream".to_string())
        )?;

        writer.write_all(&self.buffer)?;
        Ok(())
    }
}

// Implement Debug manually, otherwise users will be sad when they get a dump
// of decimal encoded bytes to their console
impl fmt::Debug for Attachment {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Attachment")
            .field("buffer", &self.buffer.len())
            .field("filename", &self.filename)
            .field("content_type", &self.content_type)
            .field("type", &self.ty)
            .finish()
    }
}