1818use OCA \Mail \Db \LocalAttachment ;
1919use OCA \Mail \Db \LocalAttachmentMapper ;
2020use OCA \Mail \Db \LocalMessage ;
21+ use OCA \Mail \Db \Mailbox ;
22+ use OCA \Mail \Db \Message ;
2123use OCA \Mail \Exception \AttachmentNotFoundException ;
24+ use OCA \Mail \Exception \ServiceException ;
2225use OCA \Mail \Exception \UploadException ;
2326use OCA \Mail \IMAP \MessageMapper ;
2427use OCP \AppFramework \Db \DoesNotExistException ;
2528use OCP \Files \File ;
2629use OCP \Files \Folder ;
2730use OCP \Files \NotFoundException ;
2831use OCP \Files \NotPermittedException ;
32+ use OCP \ICacheFactory ;
2933use Psr \Log \LoggerInterface ;
3034
3135class AttachmentService implements IAttachmentService {
@@ -51,6 +55,10 @@ class AttachmentService implements IAttachmentService {
5155 * @var LoggerInterface
5256 */
5357 private $ logger ;
58+ /**
59+ * @var ICache
60+ */
61+ private $ cache ;
5462
5563 /**
5664 * @param Folder $userFolder
@@ -60,13 +68,15 @@ public function __construct($userFolder,
6068 AttachmentStorage $ storage ,
6169 IMailManager $ mailManager ,
6270 MessageMapper $ imapMessageMapper ,
71+ ICacheFactory $ cacheFactory ,
6372 LoggerInterface $ logger ) {
6473 $ this ->mapper = $ mapper ;
6574 $ this ->storage = $ storage ;
6675 $ this ->mailManager = $ mailManager ;
6776 $ this ->messageMapper = $ imapMessageMapper ;
6877 $ this ->userFolder = $ userFolder ;
6978 $ this ->logger = $ logger ;
79+ $ this ->cache = $ cacheFactory ->createLocal ('mail.attachment_names ' );
7080 }
7181
7282 /**
@@ -249,6 +259,31 @@ public function handleAttachments(Account $account, array $attachments, \Horde_I
249259 return array_values (array_filter ($ attachmentIds ));
250260 }
251261
262+ public function getAttachmentNames (Account $ account , Mailbox $ mailbox , Message $ message , \Horde_Imap_Client_Socket $ client ): array {
263+ $ attachments = [];
264+ $ cached = $ this ->cache ->get ($ message ->getUid ());
265+ if ($ cached ) {
266+ return $ cached ;
267+ }
268+ try {
269+ $ imapMessage = $ this ->mailManager ->getImapMessage (
270+ $ client ,
271+ $ account ,
272+ $ mailbox ,
273+ $ message ->getUid (),
274+ true
275+ );
276+ $ attachments = $ imapMessage ->getAttachments ();
277+ } catch (ServiceException $ e ) {
278+ $ this ->logger ->error ('Could not get attachment names ' , ['exception ' => $ e , 'messageId ' => $ message ->getUid ()]);
279+ }
280+ $ result = array_map (static function (array $ attachment ) {
281+ return $ attachment ['fileName ' ];
282+ }, $ attachments );
283+ $ this ->cache ->set ($ message ->getUid (), $ result );
284+ return $ result ;
285+ }
286+
252287 /**
253288 * Add a message as attachment
254289 *
0 commit comments