Skip to content

Commit dc083f7

Browse files
authored
Merge pull request #7140 from LibreSign/backport/7139/stable33
[stable33] fix: orphan file delete null nodeid
2 parents 1c9a61d + 3cab110 commit dc083f7

File tree

2 files changed

+64
-16
lines changed

2 files changed

+64
-16
lines changed

lib/Service/FileService.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
use OCP\IUserManager;
4747
use Psr\Log\LoggerInterface;
4848
use stdClass;
49+
use TypeError;
4950

5051
/**
5152
* @psalm-import-type LibresignEnvelopeChildFile from ResponseDefinitions
@@ -660,22 +661,22 @@ public function delete(int $fileId, bool $deleteFile = true): void {
660661
$this->idDocsMapper->deleteByFileId($file->getId());
661662
$this->fileMapper->delete($file);
662663
if ($deleteFile) {
663-
if ($file->getSignedNodeId()) {
664-
try {
665-
$signedNextcloudFile = $this->folderService->getFileByNodeId($file->getSignedNodeId());
666-
$parentFolder = $signedNextcloudFile->getParent();
667-
$signedNextcloudFile->delete();
668-
$this->deleteEmptyFolder($parentFolder);
669-
} catch (NotFoundException) {
670-
}
671-
}
672-
try {
673-
$nextcloudFile = $this->folderService->getFileByNodeId($file->getNodeId());
674-
$parentFolder = $nextcloudFile->getParent();
675-
$nextcloudFile->delete();
676-
$this->deleteEmptyFolder($parentFolder);
677-
} catch (NotFoundException) {
678-
}
664+
$this->deletePhysicalFileIfPossible($file->getSignedNodeId());
665+
$this->deletePhysicalFileIfPossible($file->getNodeId());
666+
}
667+
}
668+
669+
private function deletePhysicalFileIfPossible(?int $nodeId): void {
670+
if ($nodeId === null) {
671+
return;
672+
}
673+
674+
try {
675+
$nextcloudFile = $this->folderService->getFileByNodeId($nodeId);
676+
$parentFolder = $nextcloudFile->getParent();
677+
$nextcloudFile->delete();
678+
$this->deleteEmptyFolder($parentFolder);
679+
} catch (NotFoundException|TypeError) {
679680
}
680681
}
681682

tests/php/Unit/Service/FileServiceTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,53 @@ public function testDeleteWithoutDeletingFile(): void {
340340
$service->delete(1, false);
341341
}
342342

343+
public function testDeleteWithNullNodeIdDoesNotCallFolderService(): void {
344+
$file = new \OCA\Libresign\Db\File();
345+
$file->setId(1);
346+
$file->setNodeType('single');
347+
$file->setParentFileId(null);
348+
$file->setNodeId(null);
349+
$file->setSignedNodeId(null);
350+
351+
$this->fileMapper->method('getById')->willReturn($file);
352+
$this->fileMapper->expects($this->once())->method('delete')->with($file);
353+
354+
$this->folderService->expects($this->never())->method('getFileByNodeId');
355+
356+
$this->signRequestMapper->method('getByFileId')->willReturn([]);
357+
$this->fileElementService->expects($this->once())->method('deleteVisibleElements');
358+
$this->idDocsMapper->expects($this->once())->method('deleteByFileId');
359+
360+
$service = $this->createFileService();
361+
362+
$service->delete(1, true);
363+
}
364+
365+
public function testDeleteIgnoresMissingPhysicalFile(): void {
366+
$file = new \OCA\Libresign\Db\File();
367+
$file->setId(1);
368+
$file->setNodeId(100);
369+
$file->setSignedNodeId(null);
370+
$file->setNodeType('single');
371+
$file->setParentFileId(null);
372+
373+
$this->fileMapper->method('getById')->willReturn($file);
374+
$this->fileMapper->expects($this->once())->method('delete')->with($file);
375+
376+
$this->folderService->expects($this->once())
377+
->method('getFileByNodeId')
378+
->with(100)
379+
->willThrowException(new \OCP\Files\NotFoundException());
380+
381+
$this->signRequestMapper->method('getByFileId')->willReturn([]);
382+
$this->fileElementService->expects($this->once())->method('deleteVisibleElements');
383+
$this->idDocsMapper->expects($this->once())->method('deleteByFileId');
384+
385+
$service = $this->createFileService();
386+
387+
$service->delete(1, true);
388+
}
389+
343390
#[DataProvider('providerTestVisibleElements')]
344391
public function testVisibleElements(bool $showVisibleElementsFlag, bool $expectedInResult): void {
345392
$file = new \OCA\Libresign\Db\File();

0 commit comments

Comments
 (0)