-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
eldertek
committed
Dec 17, 2024
1 parent
77d3017
commit 3287483
Showing
31 changed files
with
1,514 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
js/duplicatefinder-node_modules_nextcloud_dialogs_dist_chunks_index-CYiQsZoY_mjs.js
This file was deleted.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
js/duplicatefinder-node_modules_nextcloud_dialogs_dist_chunks_index-Kg2hZgGF_mjs.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
...icatefinder-vendors-node_modules_nextcloud_dialogs_dist_chunks_FilePicker-DUbP4INd_mjs.js
This file was deleted.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
...icatefinder-vendors-node_modules_nextcloud_dialogs_dist_chunks_FilePicker-ajWx2Snh_mjs.js
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OCA\DuplicateFinder\Controller; | ||
|
||
use Exception; | ||
use OCA\DuplicateFinder\Service\FileService; | ||
use OCP\AppFramework\Controller; | ||
use OCP\AppFramework\Http; | ||
use OCP\AppFramework\Http\JSONResponse; | ||
use OCP\IRequest; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* @package OCA\DuplicateFinder\Controller | ||
*/ | ||
class FileApiController extends Controller { | ||
private FileService $service; | ||
private string $userId; | ||
private LoggerInterface $logger; | ||
|
||
public function __construct( | ||
string $AppName, | ||
IRequest $request, | ||
FileService $service, | ||
string $userId, | ||
LoggerInterface $logger | ||
) { | ||
parent::__construct($AppName, $request); | ||
$this->service = $service; | ||
$this->userId = $userId; | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* Delete a file | ||
* | ||
* @NoAdminRequired | ||
* @NoCSRFRequired | ||
* @return JSONResponse | ||
*/ | ||
public function delete(): JSONResponse { | ||
$path = $this->request->getParam('path'); | ||
if (empty($path)) { | ||
return new JSONResponse( | ||
['error' => 'Path parameter is required'], | ||
Http::STATUS_BAD_REQUEST | ||
); | ||
} | ||
|
||
try { | ||
$this->logger->debug('Attempting to delete file: {path}', ['path' => $path]); | ||
$this->service->deleteFile($this->userId, $path); | ||
$this->logger->info('Successfully deleted file: {path}', ['path' => $path]); | ||
|
||
return new JSONResponse(['status' => 'success']); | ||
} catch (Exception $e) { | ||
$this->logger->error('Error deleting file: {error}', [ | ||
'error' => $e->getMessage(), | ||
'trace' => $e->getTraceAsString() | ||
]); | ||
|
||
$status = match (true) { | ||
$e instanceof \OCA\DuplicateFinder\Exception\OriginFolderProtectionException => Http::STATUS_FORBIDDEN, | ||
$e instanceof \OCP\Files\NotFoundException => Http::STATUS_NOT_FOUND, | ||
$e instanceof \OCP\Files\NotPermittedException => Http::STATUS_FORBIDDEN, | ||
default => Http::STATUS_INTERNAL_SERVER_ERROR, | ||
}; | ||
|
||
$message = match (true) { | ||
$e instanceof \OCA\DuplicateFinder\Exception\OriginFolderProtectionException => [ | ||
'error' => 'ORIGIN_FOLDER_PROTECTED', | ||
'message' => $e->getMessage() | ||
], | ||
$e instanceof \OCP\Files\NotFoundException => [ | ||
'error' => 'FILE_NOT_FOUND', | ||
'message' => 'File not found: ' . $path | ||
], | ||
$e instanceof \OCP\Files\NotPermittedException => [ | ||
'error' => 'PERMISSION_DENIED', | ||
'message' => 'Permission denied to delete file: ' . $path | ||
], | ||
default => [ | ||
'error' => 'INTERNAL_ERROR', | ||
'message' => 'An unexpected error occurred' | ||
], | ||
}; | ||
|
||
return new JSONResponse($message, $status); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OCA\DuplicateFinder\Controller; | ||
|
||
use Exception; | ||
use OCA\DuplicateFinder\Service\OriginFolderService; | ||
use OCP\AppFramework\Controller; | ||
use OCP\AppFramework\Http; | ||
use OCP\AppFramework\Http\JSONResponse; | ||
use OCP\IRequest; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class OriginFolderApiController extends Controller { | ||
private OriginFolderService $service; | ||
private string $userId; | ||
private LoggerInterface $logger; | ||
|
||
public function __construct( | ||
string $AppName, | ||
IRequest $request, | ||
OriginFolderService $service, | ||
string $userId, | ||
LoggerInterface $logger | ||
) { | ||
parent::__construct($AppName, $request); | ||
$this->service = $service; | ||
$this->userId = $userId; | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* @NoAdminRequired | ||
* @NoCSRFRequired | ||
*/ | ||
public function index(): JSONResponse { | ||
try { | ||
$folders = $this->service->findAll(); | ||
return new JSONResponse([ | ||
'folders' => array_map(function ($folder) { | ||
return [ | ||
'id' => $folder->getId(), | ||
'path' => $folder->getFolderPath() | ||
]; | ||
}, $folders) | ||
]); | ||
} catch (Exception $e) { | ||
return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
|
||
/** | ||
* @NoAdminRequired | ||
* @NoCSRFRequired | ||
*/ | ||
public function create(array $folders): JSONResponse { | ||
try { | ||
$this->logger->debug('Creating origin folders: {folders}', ['folders' => json_encode($folders)]); | ||
$createdFolders = []; | ||
$errors = []; | ||
|
||
foreach ($folders as $folderPath) { | ||
try { | ||
$this->logger->debug('Attempting to create origin folder: {path}', ['path' => $folderPath]); | ||
$this->service->create($folderPath); | ||
$createdFolders[] = $folderPath; | ||
$this->logger->debug('Successfully created origin folder: {path}', ['path' => $folderPath]); | ||
} catch (Exception $e) { | ||
$this->logger->error('Failed to create origin folder: {path}, error: {error}', [ | ||
'path' => $folderPath, | ||
'error' => $e->getMessage() | ||
]); | ||
$errors[] = [ | ||
'path' => $folderPath, | ||
'error' => $e->getMessage() | ||
]; | ||
} | ||
} | ||
|
||
$this->logger->debug('Creation complete. Created: {created}, Errors: {errors}', [ | ||
'created' => count($createdFolders), | ||
'errors' => count($errors) | ||
]); | ||
|
||
return new JSONResponse([ | ||
'created' => $createdFolders, | ||
'errors' => $errors | ||
], empty($errors) ? Http::STATUS_CREATED : Http::STATUS_MULTI_STATUS); | ||
} catch (Exception $e) { | ||
return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
|
||
/** | ||
* @NoAdminRequired | ||
* @NoCSRFRequired | ||
*/ | ||
public function destroy(int $id): JSONResponse { | ||
try { | ||
$folder = $this->service->delete($id); | ||
return new JSONResponse($folder); | ||
} catch (Exception $e) { | ||
return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_NOT_FOUND); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OCA\DuplicateFinder\Db; | ||
|
||
use JsonSerializable; | ||
use OCP\AppFramework\Db\Entity; | ||
|
||
/** | ||
* @method string getUserId() | ||
* @method void setUserId(string $userId) | ||
* @method string getFolderPath() | ||
* @method void setFolderPath(string $folderPath) | ||
* @method string getCreatedAt() | ||
* @method void setCreatedAt(string $createdAt) | ||
*/ | ||
class OriginFolder extends Entity implements JsonSerializable { | ||
protected string $userId = ''; | ||
protected string $folderPath = ''; | ||
protected string $createdAt = ''; | ||
|
||
public function __construct() { | ||
$this->addType('userId', 'string'); | ||
$this->addType('folderPath', 'string'); | ||
$this->addType('createdAt', 'string'); | ||
} | ||
|
||
public function jsonSerialize(): array { | ||
return [ | ||
'id' => $this->id, | ||
'userId' => $this->userId, | ||
'folderPath' => $this->folderPath, | ||
'createdAt' => $this->createdAt, | ||
]; | ||
} | ||
} |
Oops, something went wrong.