Cleanup MIME part FileName handling

This commit is contained in:
the-djmaze 2023-01-22 16:07:49 +01:00
parent 65bf9be580
commit 3d63eeb79e
3 changed files with 45 additions and 57 deletions

View file

@ -55,9 +55,34 @@ class BodyStructure
return $this->sPartID; return $this->sPartID;
} }
public function FileName() : string public function FileName(bool $bCalculateOnEmpty = false) : string
{ {
return $this->sFileName; $sFileName = \trim($this->sFileName);
if (\strlen($sFileName) || !$bCalculateOnEmpty) {
return $sFileName;
}
$sIdx = '-' . $this->PartID();
$sMimeType = \strtolower(\trim($this->ContentType()));
if ('message/rfc822' === $sMimeType) {
return "message{$sIdx}.eml";
}
if ('text/calendar' === $sMimeType) {
return "calendar{$sIdx}.ics";
}
if ('text/plain' === $sMimeType) {
return "part{$sIdx}.txt";
}
if (\preg_match('@text/(vcard|html|csv|xml|css|asp)@', $sMimeType, $aMatch)
|| \preg_match('@image/(png|jpeg|gif|bmp|cgm|ief|tiff|webp)@', $sMimeType, $aMatch)) {
return "part{$sIdx}.{$aMatch[1]}";
}
if (\strlen($sMimeType)) {
return \str_replace('/', $sIdx.'.', $sMimeType);
}
return ($this->IsInline() ? 'inline' : 'part' ) . $sIdx;
} }
public function ContentType() : string public function ContentType() : string

View file

@ -53,36 +53,7 @@ class Attachment implements \JsonSerializable
public function FileName(bool $bCalculateOnEmpty = false) : string public function FileName(bool $bCalculateOnEmpty = false) : string
{ {
if (!$this->oBodyStructure) { return $this->oBodyStructure ? $this->oBodyStructure->FileName($bCalculateOnEmpty) : '';
return '';
}
$sFileName = \trim($this->oBodyStructure->FileName());
if (\strlen($sFileName) || !$bCalculateOnEmpty) {
return $sFileName;
}
$sIdx = '-' . $this->oBodyStructure->PartID();
$sMimeType = \strtolower(\trim($this->oBodyStructure->ContentType()));
if ('message/rfc822' === $sMimeType) {
return "message{$sIdx}.eml";
}
if ('text/calendar' === $sMimeType) {
return "calendar{$sIdx}.ics";
}
if ('text/plain' === $sMimeType) {
return "part{$sIdx}.txt";
}
if (\preg_match('@text/(vcard|html|csv|xml|css|asp)@', $sMimeType, $aMatch)
|| \preg_match('@image/(png|jpeg|gif|bmp|cgm|ief|tiff|webp)@', $sMimeType, $aMatch)) {
return "part{$sIdx}.{$aMatch[1]}";
}
if (\strlen($sMimeType)) {
return \str_replace('/', $sIdx.'.', $sMimeType);
}
return ($this->oBodyStructure->IsInline() ? 'inline' : 'part' ) . $sIdx;
} }
public function __call(string $name, array $arguments) //: mixed public function __call(string $name, array $arguments) //: mixed
@ -96,8 +67,8 @@ class Attachment implements \JsonSerializable
return array( return array(
'@Object' => 'Object/Attachment', '@Object' => 'Object/Attachment',
'Folder' => $this->sFolder, 'Folder' => $this->sFolder,
'Uid' => (string) $this->iUid, 'Uid' => $this->iUid,
'MimeIndex' => (string) $this->oBodyStructure->PartID(), 'MimeIndex' => $this->oBodyStructure->PartID(),
'MimeType' => $this->oBodyStructure->ContentType(), 'MimeType' => $this->oBodyStructure->ContentType(),
'MimeTypeParams' => $this->oBodyStructure->ContentTypeParameters(), 'MimeTypeParams' => $this->oBodyStructure->ContentTypeParameters(),
'FileName' => \MailSo\Base\Utils::SecureFileName($this->FileName(true)), 'FileName' => \MailSo\Base\Utils::SecureFileName($this->FileName(true)),

View file

@ -104,22 +104,22 @@ class Attachment
public function IsImage() : bool public function IsImage() : bool
{ {
return 'image' === \MailSo\Base\Utils::ContentTypeType($this->ContentType(), $this->FileName()); return 'image' === \MailSo\Base\Utils::ContentTypeType($this->ContentType(), $this->sFileName);
} }
public function IsArchive() : bool public function IsArchive() : bool
{ {
return 'archive' === \MailSo\Base\Utils::ContentTypeType($this->ContentType(), $this->FileName()); return 'archive' === \MailSo\Base\Utils::ContentTypeType($this->ContentType(), $this->sFileName);
} }
public function IsPdf() : bool public function IsPdf() : bool
{ {
return 'pdf' === \MailSo\Base\Utils::ContentTypeType($this->ContentType(), $this->FileName()); return 'pdf' === \MailSo\Base\Utils::ContentTypeType($this->ContentType(), $this->sFileName);
} }
public function IsDoc() : bool public function IsDoc() : bool
{ {
return 'doc' === \MailSo\Base\Utils::ContentTypeType($this->ContentType(), $this->FileName()); return 'doc' === \MailSo\Base\Utils::ContentTypeType($this->ContentType(), $this->sFileName);
} }
public function IsLinked() : bool public function IsLinked() : bool
@ -131,15 +131,14 @@ class Attachment
{ {
$oAttachmentPart = new Part; $oAttachmentPart = new Part;
$sFileName = $this->FileName(); $sFileName = \trim($this->sFileName);
$sCID = $this->CID(); $sCID = $this->CID();
$sContentLocation = $this->ContentLocation(); $sContentLocation = $this->ContentLocation();
$oContentTypeParameters = null; $oContentTypeParameters = null;
$oContentDispositionParameters = null; $oContentDispositionParameters = null;
if (\strlen(\trim($sFileName))) if (\strlen($sFileName)) {
{
$oContentTypeParameters = $oContentTypeParameters =
(new ParameterCollection)->Add(new Parameter( (new ParameterCollection)->Add(new Parameter(
Enumerations\Parameter::NAME, $sFileName)); Enumerations\Parameter::NAME, $sFileName));
@ -163,15 +162,13 @@ class Attachment
) )
); );
if (\strlen($sCID)) if (\strlen($sCID)) {
{
$oAttachmentPart->Headers->append( $oAttachmentPart->Headers->append(
new Header(Enumerations\Header::CONTENT_ID, $sCID) new Header(Enumerations\Header::CONTENT_ID, $sCID)
); );
} }
if (\strlen($sContentLocation)) if (\strlen($sContentLocation)) {
{
$oAttachmentPart->Headers->append( $oAttachmentPart->Headers->append(
new Header(Enumerations\Header::CONTENT_LOCATION, $sContentLocation) new Header(Enumerations\Header::CONTENT_LOCATION, $sContentLocation)
); );
@ -179,8 +176,7 @@ class Attachment
$oAttachmentPart->Body = $this->Resource(); $oAttachmentPart->Body = $this->Resource();
if ('message/rfc822' !== \strtolower($this->ContentType())) if ('message/rfc822' !== \strtolower($this->ContentType())) {
{
$oAttachmentPart->Headers->append( $oAttachmentPart->Headers->append(
new Header( new Header(
Enumerations\Header::CONTENT_TRANSFER_ENCODING, Enumerations\Header::CONTENT_TRANSFER_ENCODING,
@ -188,17 +184,13 @@ class Attachment
) )
); );
if (\is_resource($oAttachmentPart->Body)) if (\is_resource($oAttachmentPart->Body) && !\MailSo\Base\StreamWrappers\Binary::IsStreamRemembed($oAttachmentPart->Body)) {
{ $oAttachmentPart->Body =
if (!\MailSo\Base\StreamWrappers\Binary::IsStreamRemembed($oAttachmentPart->Body)) \MailSo\Base\StreamWrappers\Binary::CreateStream($oAttachmentPart->Body,
{ \MailSo\Base\StreamWrappers\Binary::GetInlineDecodeOrEncodeFunctionName(
$oAttachmentPart->Body = \MailSo\Base\Enumerations\Encoding::BASE64, false));
\MailSo\Base\StreamWrappers\Binary::CreateStream($oAttachmentPart->Body,
\MailSo\Base\StreamWrappers\Binary::GetInlineDecodeOrEncodeFunctionName(
\MailSo\Base\Enumerations\Encoding::BASE64, false));
\MailSo\Base\StreamWrappers\Binary::RememberStream($oAttachmentPart->Body); \MailSo\Base\StreamWrappers\Binary::RememberStream($oAttachmentPart->Body);
}
} }
} }