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 /**
@@ -247,6 +257,32 @@ public function handleAttachments(Account $account, array $attachments, \Horde_I
247257 return array_values (array_filter ($ attachmentIds ));
248258 }
249259
260+ public function getAttachmentNames (Account $ account , Mailbox $ mailbox , Message $ message , \Horde_Imap_Client_Socket $ client ): array {
261+ $ attachments = [];
262+ $ uniqueCacheId = $ account ->getUserId () . $ account ->getId () . $ mailbox ->getId () . $ message ->getUid ();
263+ $ cached = $ this ->cache ->get ($ uniqueCacheId );
264+ if ($ cached ) {
265+ return $ cached ;
266+ }
267+ try {
268+ $ imapMessage = $ this ->mailManager ->getImapMessage (
269+ $ client ,
270+ $ account ,
271+ $ mailbox ,
272+ $ message ->getUid (),
273+ true
274+ );
275+ $ attachments = $ imapMessage ->getAttachments ();
276+ } catch (ServiceException $ e ) {
277+ $ this ->logger ->error ('Could not get attachment names ' , ['exception ' => $ e , 'messageId ' => $ message ->getUid ()]);
278+ }
279+ $ result = array_map (static function (array $ attachment ) {
280+ return ['name ' => $ attachment ['fileName ' ],'mime ' => $ attachment ['mime ' ]];
281+ }, $ attachments );
282+ $ this ->cache ->set ($ uniqueCacheId , $ result );
283+ return $ result ;
284+ }
285+
250286 /**
251287 * Add a message as attachment
252288 *
0 commit comments