18
18
use OCA \Mail \Db \LocalAttachment ;
19
19
use OCA \Mail \Db \LocalAttachmentMapper ;
20
20
use OCA \Mail \Db \LocalMessage ;
21
+ use OCA \Mail \Db \Mailbox ;
22
+ use OCA \Mail \Db \Message ;
21
23
use OCA \Mail \Exception \AttachmentNotFoundException ;
24
+ use OCA \Mail \Exception \ServiceException ;
22
25
use OCA \Mail \Exception \UploadException ;
23
26
use OCA \Mail \IMAP \MessageMapper ;
24
27
use OCP \AppFramework \Db \DoesNotExistException ;
25
28
use OCP \Files \File ;
26
29
use OCP \Files \Folder ;
27
30
use OCP \Files \NotFoundException ;
28
31
use OCP \Files \NotPermittedException ;
32
+ use OCP \ICacheFactory ;
29
33
use Psr \Log \LoggerInterface ;
30
34
31
35
class AttachmentService implements IAttachmentService {
@@ -51,6 +55,10 @@ class AttachmentService implements IAttachmentService {
51
55
* @var LoggerInterface
52
56
*/
53
57
private $ logger ;
58
+ /**
59
+ * @var ICache
60
+ */
61
+ private $ cache ;
54
62
55
63
/**
56
64
* @param Folder $userFolder
@@ -60,13 +68,15 @@ public function __construct($userFolder,
60
68
AttachmentStorage $ storage ,
61
69
IMailManager $ mailManager ,
62
70
MessageMapper $ imapMessageMapper ,
71
+ ICacheFactory $ cacheFactory ,
63
72
LoggerInterface $ logger ) {
64
73
$ this ->mapper = $ mapper ;
65
74
$ this ->storage = $ storage ;
66
75
$ this ->mailManager = $ mailManager ;
67
76
$ this ->messageMapper = $ imapMessageMapper ;
68
77
$ this ->userFolder = $ userFolder ;
69
78
$ this ->logger = $ logger ;
79
+ $ this ->cache = $ cacheFactory ->createLocal ('mail.attachment_names ' );
70
80
}
71
81
72
82
/**
@@ -249,6 +259,31 @@ public function handleAttachments(Account $account, array $attachments, \Horde_I
249
259
return array_values (array_filter ($ attachmentIds ));
250
260
}
251
261
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
+
252
287
/**
253
288
* Add a message as attachment
254
289
*
0 commit comments