Skip to content

Commit 5034534

Browse files
authored
Merge pull request #6349 from LibreSign/backport/6347/stable32
[stable32] feat: delete empty folders and respect checkbox
2 parents ff7c805 + d14585d commit 5034534

File tree

8 files changed

+269
-80
lines changed

8 files changed

+269
-80
lines changed

lib/Controller/FileController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,7 @@ private function extractFileName(array $fileData): string {
668668
* This will delete the file and all data
669669
*
670670
* @param integer $fileId LibreSign file ID
671+
* @param boolean $deleteFile Whether to delete the physical file from Nextcloud (default: true)
671672
* @return DataResponse<Http::STATUS_OK, array{message: string}, array{}>|DataResponse<Http::STATUS_UNAUTHORIZED, array{message: string}, array{}>|DataResponse<Http::STATUS_UNPROCESSABLE_ENTITY, array{action: integer, errors: list<array{message: string, title?: string}>}, array{}>
672673
*
673674
* 200: OK
@@ -678,7 +679,7 @@ private function extractFileName(array $fileData): string {
678679
#[NoCSRFRequired]
679680
#[RequireManager]
680681
#[ApiRoute(verb: 'DELETE', url: '/api/{apiVersion}/file/file_id/{fileId}', requirements: ['apiVersion' => '(v1)'])]
681-
public function deleteAllRequestSignatureUsingFileId(int $fileId): DataResponse {
682+
public function deleteAllRequestSignatureUsingFileId(int $fileId, bool $deleteFile = true): DataResponse {
682683
try {
683684
$data = [
684685
'userManager' => $this->userSession->getUser(),
@@ -687,7 +688,7 @@ public function deleteAllRequestSignatureUsingFileId(int $fileId): DataResponse
687688
]
688689
];
689690
$this->validateHelper->validateExistingFile($data);
690-
$this->fileService->delete($fileId);
691+
$this->fileService->delete($fileId, $deleteFile);
691692
} catch (\Throwable $th) {
692693
return new DataResponse(
693694
[

lib/Service/FileService.php

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

openapi-full.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5201,6 +5201,19 @@
52015201
"format": "int64"
52025202
}
52035203
},
5204+
{
5205+
"name": "deleteFile",
5206+
"in": "query",
5207+
"description": "Whether to delete the physical file from Nextcloud (default: true)",
5208+
"schema": {
5209+
"type": "integer",
5210+
"default": 1,
5211+
"enum": [
5212+
0,
5213+
1
5214+
]
5215+
}
5216+
},
52045217
{
52055218
"name": "OCS-APIRequest",
52065219
"in": "header",

openapi.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5051,6 +5051,19 @@
50515051
"format": "int64"
50525052
}
50535053
},
5054+
{
5055+
"name": "deleteFile",
5056+
"in": "query",
5057+
"description": "Whether to delete the physical file from Nextcloud (default: true)",
5058+
"schema": {
5059+
"type": "integer",
5060+
"default": 1,
5061+
"enum": [
5062+
0,
5063+
1
5064+
]
5065+
}
5066+
},
50545067
{
50555068
"name": "OCS-APIRequest",
50565069
"in": "header",

src/store/files.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,10 @@ export const useFilesStore = function(...args) {
354354
const url = deleteFile
355355
? '/apps/libresign/api/v1/file/file_id/{fileId}'
356356
: '/apps/libresign/api/v1/sign/file_id/{fileId}'
357+
const params = deleteFile ? { deleteFile: true } : {}
357358
await axios.delete(generateOcsUrl(url, {
358359
fileId: file.id,
359-
}))
360+
}), { params })
360361
.then(() => {
361362
if (this.selectedNodeId === file.nodeId) {
362363
const sidebarStore = useSidebarStore()

src/types/openapi/openapi-full.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3473,7 +3473,10 @@ export interface operations {
34733473
};
34743474
"file-delete-all-request-signature-using-file-id": {
34753475
parameters: {
3476-
query?: never;
3476+
query?: {
3477+
/** @description Whether to delete the physical file from Nextcloud (default: true) */
3478+
deleteFile?: 0 | 1;
3479+
};
34773480
header: {
34783481
/** @description Required to be true for the API request to pass */
34793482
"OCS-APIRequest": boolean;

src/types/openapi/openapi.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2995,7 +2995,10 @@ export interface operations {
29952995
};
29962996
"file-delete-all-request-signature-using-file-id": {
29972997
parameters: {
2998-
query?: never;
2998+
query?: {
2999+
/** @description Whether to delete the physical file from Nextcloud (default: true) */
3000+
deleteFile?: 0 | 1;
3001+
};
29993002
header: {
30003003
/** @description Required to be true for the API request to pass */
30013004
"OCS-APIRequest": boolean;

0 commit comments

Comments
 (0)