22
33import io .sentry .protocol .ViewHierarchy ;
44import java .io .File ;
5+ import java .util .concurrent .Callable ;
56import org .jetbrains .annotations .NotNull ;
67import org .jetbrains .annotations .Nullable ;
78
@@ -10,6 +11,7 @@ public final class Attachment {
1011
1112 private @ Nullable byte [] bytes ;
1213 private final @ Nullable JsonSerializable serializable ;
14+ private final @ Nullable Callable <byte []> byteProvider ;
1315 private @ Nullable String pathname ;
1416 private final @ NotNull String filename ;
1517 private final @ Nullable String contentType ;
@@ -84,6 +86,7 @@ public Attachment(
8486 final boolean addToTransactions ) {
8587 this .bytes = bytes ;
8688 this .serializable = null ;
89+ this .byteProvider = null ;
8790 this .filename = filename ;
8891 this .contentType = contentType ;
8992 this .attachmentType = attachmentType ;
@@ -109,6 +112,33 @@ public Attachment(
109112 final boolean addToTransactions ) {
110113 this .bytes = null ;
111114 this .serializable = serializable ;
115+ this .byteProvider = null ;
116+ this .filename = filename ;
117+ this .contentType = contentType ;
118+ this .attachmentType = attachmentType ;
119+ this .addToTransactions = addToTransactions ;
120+ }
121+
122+ /**
123+ * Initializes an Attachment with bytes factory, a filename, a content type, and
124+ * addToTransactions.
125+ *
126+ * @param byteProvider A provider holding the attachment payload
127+ * @param filename The name of the attachment to display in Sentry.
128+ * @param contentType The content type of the attachment.
129+ * @param attachmentType the attachment type.
130+ * @param addToTransactions <code>true</code> if the SDK should add this attachment to every
131+ * {@link ITransaction} or set to <code>false</code> if it shouldn't.
132+ */
133+ public Attachment (
134+ final @ NotNull Callable <byte []> byteProvider ,
135+ final @ NotNull String filename ,
136+ final @ Nullable String contentType ,
137+ final @ Nullable String attachmentType ,
138+ final boolean addToTransactions ) {
139+ this .bytes = null ;
140+ this .serializable = null ;
141+ this .byteProvider = byteProvider ;
112142 this .filename = filename ;
113143 this .contentType = contentType ;
114144 this .attachmentType = attachmentType ;
@@ -186,6 +216,7 @@ public Attachment(
186216 this .pathname = pathname ;
187217 this .filename = filename ;
188218 this .serializable = null ;
219+ this .byteProvider = null ;
189220 this .contentType = contentType ;
190221 this .attachmentType = attachmentType ;
191222 this .addToTransactions = addToTransactions ;
@@ -212,6 +243,7 @@ public Attachment(
212243 this .pathname = pathname ;
213244 this .filename = filename ;
214245 this .serializable = null ;
246+ this .byteProvider = null ;
215247 this .contentType = contentType ;
216248 this .addToTransactions = addToTransactions ;
217249 }
@@ -240,6 +272,7 @@ public Attachment(
240272 this .pathname = pathname ;
241273 this .filename = filename ;
242274 this .serializable = null ;
275+ this .byteProvider = null ;
243276 this .contentType = contentType ;
244277 this .addToTransactions = addToTransactions ;
245278 this .attachmentType = attachmentType ;
@@ -310,16 +343,35 @@ boolean isAddToTransactions() {
310343 return attachmentType ;
311344 }
312345
346+ public @ Nullable Callable <byte []> getByteProvider () {
347+ return byteProvider ;
348+ }
349+
313350 /**
314351 * Creates a new Screenshot Attachment
315352 *
316- * @param screenshotBytes the array bytes
353+ * @param screenshotBytes the array bytes of the PNG screenshot
317354 * @return the Attachment
318355 */
319356 public static @ NotNull Attachment fromScreenshot (final byte [] screenshotBytes ) {
320357 return new Attachment (screenshotBytes , "screenshot.png" , "image/png" , false );
321358 }
322359
360+ /**
361+ * Creates a new Screenshot Attachment
362+ *
363+ * @param provider the mechanism providing the screenshot payload
364+ * @return the Attachment
365+ */
366+ public static @ NotNull Attachment fromByteProvider (
367+ final @ NotNull Callable <byte []> provider ,
368+ final @ NotNull String filename ,
369+ final @ Nullable String contentType ,
370+ final boolean addToTransactions ) {
371+ return new Attachment (
372+ provider , filename , contentType , DEFAULT_ATTACHMENT_TYPE , addToTransactions );
373+ }
374+
323375 /**
324376 * Creates a new View Hierarchy Attachment
325377 *
0 commit comments