DataPart.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Mime\Part;
  11. use Symfony\Component\Mime\Exception\InvalidArgumentException;
  12. use Symfony\Component\Mime\Header\Headers;
  13. /**
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class DataPart extends TextPart
  17. {
  18. private ?string $filename = null;
  19. private string $mediaType;
  20. private ?string $cid = null;
  21. /**
  22. * @param resource|string|File $body Use a File instance to defer loading the file until rendering
  23. */
  24. public function __construct($body, ?string $filename = null, ?string $contentType = null, ?string $encoding = null)
  25. {
  26. if ($body instanceof File && !$filename) {
  27. $filename = $body->getFilename();
  28. }
  29. $contentType ??= $body instanceof File ? $body->getContentType() : 'application/octet-stream';
  30. [$this->mediaType, $subtype] = explode('/', $contentType);
  31. parent::__construct($body, null, $subtype, $encoding);
  32. if (null !== $filename) {
  33. $this->filename = $filename;
  34. $this->setName($filename);
  35. }
  36. $this->setDisposition('attachment');
  37. }
  38. public static function fromPath(string $path, ?string $name = null, ?string $contentType = null): self
  39. {
  40. return new self(new File($path), $name, $contentType);
  41. }
  42. /**
  43. * @return $this
  44. */
  45. public function asInline(): static
  46. {
  47. return $this->setDisposition('inline');
  48. }
  49. /**
  50. * @return $this
  51. */
  52. public function setContentId(string $cid): static
  53. {
  54. if (!str_contains($cid, '@')) {
  55. throw new InvalidArgumentException(\sprintf('The "%s" CID is invalid as it doesn\'t contain an "@".', $cid));
  56. }
  57. $this->cid = $cid;
  58. return $this;
  59. }
  60. public function getContentId(): string
  61. {
  62. return $this->cid ?: $this->cid = $this->generateContentId();
  63. }
  64. public function hasContentId(): bool
  65. {
  66. return null !== $this->cid;
  67. }
  68. public function getMediaType(): string
  69. {
  70. return $this->mediaType;
  71. }
  72. public function getPreparedHeaders(): Headers
  73. {
  74. $headers = parent::getPreparedHeaders();
  75. if (null !== $this->cid) {
  76. $headers->setHeaderBody('Id', 'Content-ID', $this->cid);
  77. }
  78. if (null !== $this->filename) {
  79. $headers->setHeaderParameter('Content-Disposition', 'filename', $this->filename);
  80. }
  81. return $headers;
  82. }
  83. public function asDebugString(): string
  84. {
  85. $str = parent::asDebugString();
  86. if (null !== $this->filename) {
  87. $str .= ' filename: '.$this->filename;
  88. }
  89. return $str;
  90. }
  91. public function getFilename(): ?string
  92. {
  93. return $this->filename;
  94. }
  95. public function getContentType(): string
  96. {
  97. return implode('/', [$this->getMediaType(), $this->getMediaSubtype()]);
  98. }
  99. private function generateContentId(): string
  100. {
  101. return bin2hex(random_bytes(16)).'@symfony';
  102. }
  103. public function __serialize(): array
  104. {
  105. $parent = parent::__serialize();
  106. $headers = $parent['_headers'];
  107. unset($parent['_headers']);
  108. return [
  109. '_headers' => $headers,
  110. '_parent' => $parent,
  111. 'filename' => $this->filename,
  112. 'mediaType' => $this->mediaType,
  113. 'cid' => $this->cid,
  114. ];
  115. }
  116. public function __unserialize(array $data): void
  117. {
  118. parent::__unserialize(['_headers' => $data['_headers'] ?? $data["\0*\0_headers"], ...$data['_parent'] ?? $data["\0*\0_parent"]]);
  119. $this->filename = $data['filename'] ?? $data["\0".self::class."\0filename"] ?? null;
  120. $this->mediaType = $data['mediaType'] ?? $data["\0".self::class."\0mediaType"];
  121. $this->cid = $data['cid'] ?? $data["\0".self::class."\0cid"] ?? null;
  122. }
  123. }