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;
}
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

View file

@ -53,36 +53,7 @@ class Attachment implements \JsonSerializable
public function FileName(bool $bCalculateOnEmpty = false) : string
{
if (!$this->oBodyStructure) {
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;
return $this->oBodyStructure ? $this->oBodyStructure->FileName($bCalculateOnEmpty) : '';
}
public function __call(string $name, array $arguments) //: mixed
@ -96,8 +67,8 @@ class Attachment implements \JsonSerializable
return array(
'@Object' => 'Object/Attachment',
'Folder' => $this->sFolder,
'Uid' => (string) $this->iUid,
'MimeIndex' => (string) $this->oBodyStructure->PartID(),
'Uid' => $this->iUid,
'MimeIndex' => $this->oBodyStructure->PartID(),
'MimeType' => $this->oBodyStructure->ContentType(),
'MimeTypeParams' => $this->oBodyStructure->ContentTypeParameters(),
'FileName' => \MailSo\Base\Utils::SecureFileName($this->FileName(true)),

View file

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