- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 463
 
feat: add attachment support #1925
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            7 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      69803b7
              
                feat: add attachment support
              
              
                Litarnus 9bf4dba
              
                remove attachment event type
              
              
                Litarnus 276d203
              
                remove attachment event type
              
              
                Litarnus 8633944
              
                fix tests in windows
              
              
                Litarnus 9b2e0b2
              
                Merge branch '5.x' into martinl/attachments
              
              
                Litarnus 5d8bdef
              
                review
              
              
                Litarnus 3b97289
              
                lints
              
              
                Litarnus File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| <?php | ||
| 
     | 
||
| declare(strict_types=1); | ||
| 
     | 
||
| namespace Sentry\Attachment; | ||
| 
     | 
||
| abstract class Attachment | ||
| { | ||
| private const DEFAULT_CONTENT_TYPE = 'application/octet-stream'; | ||
| 
     | 
||
| /** | ||
| * @var string | ||
| */ | ||
| private $filename; | ||
| 
     | 
||
| /** | ||
| * @var string | ||
| */ | ||
| private $contentType; | ||
| 
     | 
||
| public function __construct(string $filename, string $contentType) | ||
| { | ||
| $this->filename = $filename; | ||
| $this->contentType = $contentType; | ||
| } | ||
| 
     | 
||
| public function getFilename(): string | ||
| { | ||
| return $this->filename; | ||
| } | ||
| 
     | 
||
| public function getContentType(): string | ||
| { | ||
| return $this->contentType; | ||
| } | ||
| 
     | 
||
| /** | ||
| * Returns the size in bytes for the attachment. This method should aim to use a low overhead | ||
| * way of determining the size because it will be called more than once. | ||
| * For example, for file attachments it should read the file size from the filesystem instead of | ||
| * reading the file in memory and then calculating the length. | ||
| * If no low overhead way exists, then the result should be cached so that calling it multiple times | ||
| * does not decrease performance. | ||
| * | ||
| * @return int the size in bytes or null if the length could not be determined, for example if the file | ||
| * does not exist | ||
| */ | ||
| abstract public function getSize(): ?int; | ||
| 
     | 
||
| /** | ||
| * Fetches and returns the data. Calling this can have a non-trivial impact on memory usage, depending | ||
| * on the type and size of attachment. | ||
| * | ||
| * @return string the content as bytes or null if the content could not be retrieved, for example if the file | ||
| * does not exist | ||
| */ | ||
| abstract public function getData(): ?string; | ||
| 
     | 
||
| /** | ||
| * Creates a new attachment representing a file referenced by a path. | ||
| * The file is not validated and the content is not read when creating the attachment. | ||
| */ | ||
| public static function fromFile(string $path, string $contentType = self::DEFAULT_CONTENT_TYPE): Attachment | ||
| { | ||
| return new FileAttachment($path, $contentType); | ||
| } | ||
| 
     | 
||
| /** | ||
| * Creates a new attachment representing a slice of bytes that lives in memory. | ||
| */ | ||
| public static function fromBytes(string $filename, string $data, string $contentType = self::DEFAULT_CONTENT_TYPE): Attachment | ||
| { | ||
| return new ByteAttachment($filename, $contentType, $data); | ||
| } | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?php | ||
| 
     | 
||
| declare(strict_types=1); | ||
| 
     | 
||
| namespace Sentry\Attachment; | ||
| 
     | 
||
| /** | ||
| * Represents an attachment that is stored in memory and will not be read from the filesystem. | ||
| */ | ||
| class ByteAttachment extends Attachment | ||
| { | ||
| /** | ||
| * @var string | ||
| */ | ||
| private $data; | ||
| 
     | 
||
| public function __construct(string $filename, string $contentType, string $data) | ||
| { | ||
| parent::__construct($filename, $contentType); | ||
| $this->data = $data; | ||
| } | ||
| 
     | 
||
| public function getSize(): ?int | ||
| { | ||
| return \strlen($this->data); | ||
| } | ||
| 
     | 
||
| public function getData(): ?string | ||
| { | ||
| return $this->data; | ||
| } | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?php | ||
| 
     | 
||
| declare(strict_types=1); | ||
| 
     | 
||
| namespace Sentry\Attachment; | ||
| 
     | 
||
| /** | ||
| * Represents a file that is readable by using a path. | ||
| */ | ||
| class FileAttachment extends Attachment | ||
| { | ||
| /** | ||
| * @var string | ||
| */ | ||
| private $path; | ||
| 
     | 
||
| public function __construct(string $path, string $contentType) | ||
| { | ||
| parent::__construct(basename($path), $contentType); | ||
| $this->path = $path; | ||
| } | ||
| 
     | 
||
| public function getSize(): ?int | ||
| { | ||
| return @filesize($this->path) ?: null; | ||
| } | ||
| 
     | 
||
| public function getData(): ?string | ||
| { | ||
| return @file_get_contents($this->path) ?: null; | ||
| } | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| <?php | ||
| 
     | 
||
| declare(strict_types=1); | ||
| 
     | 
||
| namespace Sentry\Serializer\EnvelopItems; | ||
| 
     | 
||
| use Sentry\Attachment\Attachment; | ||
| use Sentry\Util\JSON; | ||
| 
     | 
||
| class AttachmentItem | ||
| { | ||
| public static function toAttachmentItem(Attachment $attachment): ?string | ||
| { | ||
| $data = $attachment->getData(); | ||
| if ($data === null) { | ||
| return null; | ||
| } | ||
| 
     | 
||
| $header = [ | ||
| 'type' => 'attachment', | ||
| 'filename' => $attachment->getFilename(), | ||
| 'content_type' => $attachment->getContentType(), | ||
| 'attachment_type' => 'event.attachment', | ||
| 'length' => $attachment->getSize(), | ||
| ]; | ||
| 
     | 
||
| return \sprintf("%s\n%s", JSON::encode($header), $data); | ||
| } | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Null Handling Bug in File Operations
The
?: nulloperator ingetSize()andgetData()incorrectly handles empty files. It convertsfilesize()'s0andfile_get_contents()'s empty string''intonull. This causes empty files to be treated as unreadable, conflicting with tests expecting0for size and''for data.