@@ -570,15 +570,15 @@ private function computeEnvelopeSignersProgress(): void {
570570 );
571571 }
572572
573- public function delete (int $ fileId ): void {
573+ public function delete (int $ fileId, bool $ deleteFile = true ): void {
574574 $ file = $ this ->fileMapper ->getById ($ fileId );
575575
576576 $ this ->decrementEnvelopeFilesCountIfNeeded ($ file );
577577
578578 if ($ file ->getNodeType () === 'envelope ' ) {
579579 $ childrenFiles = $ this ->fileMapper ->getChildrenFiles ($ file ->getId ());
580580 foreach ($ childrenFiles as $ childFile ) {
581- $ this ->delete ($ childFile ->getId ());
581+ $ this ->delete ($ childFile ->getId (), $ deleteFile );
582582 }
583583 }
584584
@@ -590,14 +590,33 @@ public function delete(int $fileId): void {
590590 }
591591 $ this ->idDocsMapper ->deleteByFileId ($ file ->getId ());
592592 $ this ->fileMapper ->delete ($ file );
593- if ($ file ->getSignedNodeId ()) {
594- $ signedNextcloudFile = $ this ->folderService ->getFileByNodeId ($ file ->getSignedNodeId ());
595- $ signedNextcloudFile ->delete ();
593+ if ($ deleteFile ) {
594+ if ($ file ->getSignedNodeId ()) {
595+ $ signedNextcloudFile = $ this ->folderService ->getFileByNodeId ($ file ->getSignedNodeId ());
596+ $ parentFolder = $ signedNextcloudFile ->getParent ();
597+ $ signedNextcloudFile ->delete ();
598+ $ this ->deleteEmptyFolder ($ parentFolder );
599+ }
600+ try {
601+ $ nextcloudFile = $ this ->folderService ->getFileByNodeId ($ file ->getNodeId ());
602+ $ parentFolder = $ nextcloudFile ->getParent ();
603+ $ nextcloudFile ->delete ();
604+ $ this ->deleteEmptyFolder ($ parentFolder );
605+ } catch (NotFoundException ) {
606+ }
596607 }
608+ }
609+
610+ private function deleteEmptyFolder (\OCP \Files \Folder $ folder ): void {
597611 try {
598- $ nextcloudFile = $ this ->folderService ->getFileByNodeId ($ file ->getNodeId ());
599- $ nextcloudFile ->delete ();
600- } catch (NotFoundException ) {
612+ $ contents = $ folder ->getDirectoryListing ();
613+ if (count ($ contents ) === 0 ) {
614+ $ folder ->delete ();
615+ }
616+ } catch (\Exception $ e ) {
617+ $ this ->logger ->debug ('Could not delete empty folder: ' . $ e ->getMessage (), [
618+ 'exception ' => $ e ,
619+ ]);
601620 }
602621 }
603622
0 commit comments