@@ -352,10 +352,10 @@ public function createAttachmentFile(int $documentId, string $newFileName, strin
352352 $ fileName = self ::getUniqueFileName ($ saveDir , $ newFileName );
353353 $ newFile = $ saveDir ->newFile ($ fileName );
354354 return [
355- 'name ' => $ fileName ,
355+ 'name ' => $ newFile -> getName () ,
356356 'dirname ' => $ saveDir ->getName (),
357357 'id ' => $ newFile ->getId (),
358- 'documentId ' => $ newFile ->getId (),
358+ 'documentId ' => $ textFile ->getId (),
359359 'mimetype ' => $ newFile ->getMimetype (),
360360 ];
361361 }
@@ -581,23 +581,44 @@ public function cleanupAttachments(int $fileId): int {
581581 // this only happens if the attachment dir was deleted by the user while editing the document
582582 return 0 ;
583583 }
584- $ attachmentsByName = [];
585- foreach ($ attachmentDir ->getDirectoryListing () as $ attNode ) {
586- $ attachmentsByName [$ attNode ->getName ()] = $ attNode ;
587- }
588-
584+ $ contentAttachmentFileIds = self ::getAttachmentIdsFromContent ($ textFile ->getContent ());
589585 $ contentAttachmentNames = self ::getAttachmentNamesFromContent ($ textFile ->getContent (), $ fileId );
590586
591- $ toDelete = array_diff (array_keys ($ attachmentsByName ), $ contentAttachmentNames );
592- foreach ($ toDelete as $ name ) {
593- $ attachmentsByName [$ name ]->delete ();
587+ $ toDelete = array_filter ($ attachmentDir ->getDirectoryListing (),
588+ function ($ node ) use ($ contentAttachmentFileIds , $ contentAttachmentNames ) {
589+ return !in_array ($ node ->getName (), $ contentAttachmentNames ) &&
590+ !in_array ($ node ->getId (), $ contentAttachmentFileIds );
591+ }
592+ );
593+ foreach ($ toDelete as $ node ) {
594+ $ node ->delete ();
594595 }
595596 return count ($ toDelete );
596597 }
597598 }
598599 return 0 ;
599600 }
600601
602+ /**
603+ * Get attachment file ids listed in the markdown file content
604+ *
605+ * @param string $content
606+ *
607+ * @return array
608+ */
609+ public static function getAttachmentIdsFromContent (string $ content ): array {
610+ $ matches = [];
611+ // matches [ANY_CONSIDERED_CORRECT_BY_PHP-MARKDOWN](ANY_URL/f/FILE_ID[ (preview)]) and captures FILE_ID
612+ preg_match_all (
613+ '/\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[\])*\])*\])*\])*\])*\])*\]\(\S+\/f\/(\d+)(?: \(preview\))?\)/ ' ,
614+ $ content ,
615+ $ matches ,
616+ PREG_SET_ORDER
617+ );
618+ return array_map (static function (array $ match ) {
619+ return intval ($ match [1 ]);
620+ }, $ matches );
621+ }
601622
602623 /**
603624 * Get attachment file names listed in the markdown file content
0 commit comments