Skip to content

Commit

Permalink
Merge pull request #8597 from nextcloud/fix/noid/attachments-fclose
Browse files Browse the repository at this point in the history
fix: handle attachments without transfer encoding properly
  • Loading branch information
kesselb authored Jul 6, 2023
2 parents e5cefed + 8c3c248 commit 44a6aed
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions lib/IMAP/MessageMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -682,13 +682,36 @@ public function getAttachments(Horde_Imap_Client_Socket $client,
if ($enc = $mimeHeaders->getValue('content-transfer-encoding')) {
$part->setTransferEncoding($enc);
}

/*
* usestream depends on transfer encoding
*
* Case 1: base64 encoded file
* Base64 stream is copied to a new stream and decoded using a convert.base64-decode stream filter.
*
* Case 2: text file (no transfer encoding)
* Existing stream is reused in Horde_Mime_Part.
*
* To handle both cases:
*
* 1) Horde_Mime_Part.clearContents to close the internal stream (Horde_Mime_Part._contents)
* 2) If $stream is still open, the data was transfer encoded, close it.
*
* Attachment.fromMimePart uses Horde_Mime_Part.getContents and Horde_Mime_Part.getBytes
* and therefore needs an open input stream.
*/
$part->setContents($stream, [
'usestream' => true,
'usestream' => true
]);
fclose($stream);
$attachments[] = Attachment::fromMimePart($part);
$part->clearContents();
if (is_resource($stream)) {
fclose($stream);
}
} else {
$attachments[] = Attachment::fromMimePart($part);
$part->clearContents();
}

$attachments[] = Attachment::fromMimePart($part);
}
return $attachments;
}
Expand Down

0 comments on commit 44a6aed

Please sign in to comment.