refactor: rework controller (1)
Signed-off-by: Varun Patil <varunpatil@ucla.edu>pull/563/head
parent
bd6aaeee3a
commit
0e385d2283
|
@ -24,8 +24,7 @@ declare(strict_types=1);
|
|||
namespace OCA\Memories\ClustersBackend;
|
||||
|
||||
use OCA\Memories\Db\TimelineQuery;
|
||||
use OCA\Memories\Errors;
|
||||
use OCA\Memories\HttpResponseException;
|
||||
use OCA\Memories\Exceptions;
|
||||
use OCP\App\IAppManager;
|
||||
use OCP\IUserSession;
|
||||
|
||||
|
@ -95,7 +94,7 @@ class AlbumsBackend extends Backend
|
|||
// Get album
|
||||
$album = $this->timelineQuery->getAlbumIfAllowed($this->userId, $name);
|
||||
if (null === $album) {
|
||||
throw new HttpResponseException(Errors::NotFound("album {$name}"));
|
||||
throw Exceptions::NotFound("album {$name}");
|
||||
}
|
||||
|
||||
// Get files
|
||||
|
|
|
@ -23,8 +23,9 @@ declare(strict_types=1);
|
|||
|
||||
namespace OCA\Memories\Controller;
|
||||
|
||||
use OCA\Memories\Errors;
|
||||
use OCA\Memories\Exceptions;
|
||||
use OCA\Memories\Exif;
|
||||
use OCA\Memories\Util;
|
||||
use OCP\AppFramework\Http;
|
||||
use OCP\AppFramework\Http\JSONResponse;
|
||||
use OCP\Files\Folder;
|
||||
|
@ -40,23 +41,20 @@ class ArchiveController extends GenericApiController
|
|||
*/
|
||||
public function archive(string $id): Http\Response
|
||||
{
|
||||
$user = $this->userSession->getUser();
|
||||
if (null === $user) {
|
||||
return Errors::NotLoggedIn();
|
||||
}
|
||||
$uid = $user->getUID();
|
||||
$userFolder = $this->rootFolder->getUserFolder($uid);
|
||||
return Util::guardEx(function () use ($id) {
|
||||
$uid = Util::getUID();
|
||||
$userFolder = Util::getUserFolder();
|
||||
|
||||
// Check for permissions and get numeric Id
|
||||
$file = $userFolder->getById((int) $id);
|
||||
if (0 === \count($file)) {
|
||||
return Errors::NotFound("file id {$id}");
|
||||
throw Exceptions::NotFound("file id {$id}");
|
||||
}
|
||||
$file = $file[0];
|
||||
|
||||
// Check if user has permissions
|
||||
if (!$file->isUpdateable()) {
|
||||
return Errors::ForbiddenFileUpdate($file->getName());
|
||||
throw Exceptions::ForbiddenFileUpdate($file->getName());
|
||||
}
|
||||
|
||||
// Create archive folder in the root of the user's configured timeline
|
||||
|
@ -72,7 +70,7 @@ class ArchiveController extends GenericApiController
|
|||
$timelineFolders[] = $f;
|
||||
$timelinePaths[] = $f->getPath();
|
||||
} catch (\OCP\Files\NotFoundException $e) {
|
||||
return new JSONResponse(['message' => 'Timeline folder not found'], Http::STATUS_NOT_FOUND);
|
||||
throw Exceptions::NotFound("timeline folder {$path}");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -121,10 +119,10 @@ class ArchiveController extends GenericApiController
|
|||
$body = $this->request->getParams();
|
||||
$unarchive = isset($body['archive']) && false === $body['archive'];
|
||||
if ($isArchived && !$unarchive) {
|
||||
return new JSONResponse(['message' => 'File already archived'], Http::STATUS_BAD_REQUEST);
|
||||
throw Exceptions::BadRequest('File already archived');
|
||||
}
|
||||
if (!$isArchived && $unarchive) {
|
||||
return new JSONResponse(['message' => 'File not archived'], Http::STATUS_BAD_REQUEST);
|
||||
throw Exceptions::BadRequest('File not archived');
|
||||
}
|
||||
|
||||
// Final path of the file including the file name
|
||||
|
@ -151,31 +149,22 @@ class ArchiveController extends GenericApiController
|
|||
try {
|
||||
$existingFolder = $folder->get($folderName.'/');
|
||||
if (!$existingFolder instanceof Folder) {
|
||||
throw new \OCP\Files\NotFoundException('Not a folder');
|
||||
throw Exceptions::NotFound('Not a folder: '.$existingFolder->getPath());
|
||||
}
|
||||
$folder = $existingFolder;
|
||||
} catch (\OCP\Files\NotFoundException $e) {
|
||||
try {
|
||||
$folder = $folder->newFolder($folderName);
|
||||
} catch (\OCP\Files\NotPermittedException $e) {
|
||||
return new JSONResponse(['message' => 'Failed to create folder'], Http::STATUS_FORBIDDEN);
|
||||
throw Exceptions::ForbiddenFileUpdate($folder->getPath().' [create]');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Move file to archive folder
|
||||
try {
|
||||
$file->move($folder->getPath().'/'.$file->getName());
|
||||
} catch (\OCP\Files\NotPermittedException $e) {
|
||||
return new JSONResponse(['message' => 'Failed to move file'], Http::STATUS_FORBIDDEN);
|
||||
} catch (\OCP\Files\NotFoundException $e) {
|
||||
return new JSONResponse(['message' => 'File not found'], Http::STATUS_INTERNAL_SERVER_ERROR);
|
||||
} catch (\OCP\Files\InvalidPathException $e) {
|
||||
return new JSONResponse(['message' => 'Invalid path'], Http::STATUS_INTERNAL_SERVER_ERROR);
|
||||
} catch (\OCP\Lock\LockedException $e) {
|
||||
return new JSONResponse(['message' => 'File is locked'], Http::STATUS_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
return new JSONResponse([], Http::STATUS_OK);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,8 +24,8 @@ declare(strict_types=1);
|
|||
namespace OCA\Memories\Controller;
|
||||
|
||||
use OCA\Memories\ClustersBackend\Backend;
|
||||
use OCA\Memories\Errors;
|
||||
use OCA\Memories\HttpResponseException;
|
||||
use OCA\Memories\Exceptions;
|
||||
use OCA\Memories\Util;
|
||||
use OCP\AppFramework\Http;
|
||||
use OCP\AppFramework\Http\DataDisplayResponse;
|
||||
use OCP\AppFramework\Http\JSONResponse;
|
||||
|
@ -42,7 +42,7 @@ class ClustersController extends GenericApiController
|
|||
*/
|
||||
public function list(string $backend): Http\Response
|
||||
{
|
||||
return $this->guardEx(function () use ($backend) {
|
||||
return Util::guardEx(function () use ($backend) {
|
||||
$this->init($backend);
|
||||
|
||||
$list = $this->backend->getClusters();
|
||||
|
@ -60,7 +60,7 @@ class ClustersController extends GenericApiController
|
|||
*/
|
||||
public function preview(string $backend, string $name): Http\Response
|
||||
{
|
||||
return $this->guardEx(function () use ($backend, $name) {
|
||||
return Util::guardEx(function () use ($backend, $name) {
|
||||
$this->init($backend);
|
||||
|
||||
// Get list of some photos in this cluster
|
||||
|
@ -88,7 +88,7 @@ class ClustersController extends GenericApiController
|
|||
*/
|
||||
public function download(string $backend, string $name): Http\Response
|
||||
{
|
||||
return $this->guardEx(function () use ($backend, $name) {
|
||||
return Util::guardEx(function () use ($backend, $name) {
|
||||
$this->init($backend);
|
||||
|
||||
// Get list of all files in this cluster
|
||||
|
@ -111,7 +111,7 @@ class ClustersController extends GenericApiController
|
|||
{
|
||||
$user = $this->userSession->getUser();
|
||||
if (null === $user) {
|
||||
throw new HttpResponseException(Errors::NotLoggedIn());
|
||||
throw Exceptions::NotLoggedIn();
|
||||
}
|
||||
|
||||
if (\array_key_exists($backend, Backend::$backends)) {
|
||||
|
@ -121,13 +121,13 @@ class ClustersController extends GenericApiController
|
|||
}
|
||||
|
||||
if (!$this->backend->isEnabled()) {
|
||||
throw new HttpResponseException(Errors::NotEnabled($this->backend->appName()));
|
||||
throw Exceptions::NotEnabled($this->backend->appName());
|
||||
}
|
||||
|
||||
if (property_exists($this->backend, 'root')) {
|
||||
$this->backend->root = $this->getRequestRoot();
|
||||
if ($this->backend->root->isEmpty()) {
|
||||
throw new HttpResponseException(Errors::NoRequestRoot());
|
||||
throw Exceptions::NoRequestRoot();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -141,7 +141,7 @@ class ClustersController extends GenericApiController
|
|||
$previewManager = \OC::$server->get(\OCP\IPreview::class);
|
||||
|
||||
// Try to get a preview
|
||||
$userFolder = $this->rootFolder->getUserFolder($this->getUID());
|
||||
$userFolder = Util::getUserFolder();
|
||||
foreach ($photos as $img) {
|
||||
// Get the file
|
||||
$files = $userFolder->getById($this->backend->getFileId($img));
|
||||
|
@ -172,6 +172,6 @@ class ClustersController extends GenericApiController
|
|||
}
|
||||
}
|
||||
|
||||
return Errors::NotFound('preview from photos list');
|
||||
throw Exceptions::NotFound('preview from photos list');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,7 +24,8 @@ declare(strict_types=1);
|
|||
namespace OCA\Memories\Controller;
|
||||
|
||||
use OCA\Memories\Db\TimelineRoot;
|
||||
use OCA\Memories\Errors;
|
||||
use OCA\Memories\Exceptions;
|
||||
use OCA\Memories\Util;
|
||||
use OCP\AppFramework\Http;
|
||||
use OCP\AppFramework\Http\JSONResponse;
|
||||
|
||||
|
@ -39,24 +40,10 @@ class DaysController extends GenericApiController
|
|||
*/
|
||||
public function days(): Http\Response
|
||||
{
|
||||
// Get the folder to show
|
||||
try {
|
||||
$uid = $this->getUID();
|
||||
} catch (\Exception $e) {
|
||||
return Errors::NotLoggedIn();
|
||||
}
|
||||
|
||||
// Get the folder to show
|
||||
$root = null;
|
||||
|
||||
try {
|
||||
return Util::guardEx(function () {
|
||||
$uid = $this->getShareToken() ? '' : Util::getUID();
|
||||
$root = $this->getRequestRoot();
|
||||
} catch (\Exception $e) {
|
||||
return Errors::Generic($e);
|
||||
}
|
||||
|
||||
// Run actual query
|
||||
try {
|
||||
$list = $this->timelineQuery->getDays(
|
||||
$root,
|
||||
$uid,
|
||||
|
@ -84,9 +71,7 @@ class DaysController extends GenericApiController
|
|||
}
|
||||
|
||||
return new JSONResponse($list, Http::STATUS_OK);
|
||||
} catch (\Exception $e) {
|
||||
return new JSONResponse(['message' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -96,8 +81,8 @@ class DaysController extends GenericApiController
|
|||
*/
|
||||
public function day(string $id): Http\Response
|
||||
{
|
||||
// Get user
|
||||
$uid = $this->getUID();
|
||||
return Util::guardEx(function () use ($id) {
|
||||
$uid = $this->getShareToken() ? '' : Util::getUID();
|
||||
|
||||
// Check for wildcard
|
||||
$dayIds = [];
|
||||
|
@ -105,9 +90,7 @@ class DaysController extends GenericApiController
|
|||
$dayIds = null;
|
||||
} else {
|
||||
// Split at commas and convert all parts to int
|
||||
$dayIds = array_map(function ($part) {
|
||||
return (int) $part;
|
||||
}, explode(',', $id));
|
||||
$dayIds = array_map(fn ($p) => (int) $p, explode(',', $id));
|
||||
}
|
||||
|
||||
// Check if $dayIds is empty
|
||||
|
@ -116,13 +99,7 @@ class DaysController extends GenericApiController
|
|||
}
|
||||
|
||||
// Get the folder to show
|
||||
$root = null;
|
||||
|
||||
try {
|
||||
$root = $this->getRequestRoot();
|
||||
} catch (\Exception $e) {
|
||||
return Errors::Generic($e);
|
||||
}
|
||||
|
||||
// Convert to actual dayIds if month view
|
||||
if ($this->isMonthView()) {
|
||||
|
@ -130,7 +107,6 @@ class DaysController extends GenericApiController
|
|||
}
|
||||
|
||||
// Run actual query
|
||||
try {
|
||||
$list = $this->timelineQuery->getDay(
|
||||
$root,
|
||||
$uid,
|
||||
|
@ -153,9 +129,7 @@ class DaysController extends GenericApiController
|
|||
}
|
||||
|
||||
return new JSONResponse($list, Http::STATUS_OK);
|
||||
} catch (\Exception $e) {
|
||||
return new JSONResponse(['message' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -165,12 +139,14 @@ class DaysController extends GenericApiController
|
|||
*/
|
||||
public function dayPost(): Http\Response
|
||||
{
|
||||
return Util::guardEx(function () {
|
||||
$id = $this->request->getParam('body_ids');
|
||||
if (null === $id) {
|
||||
return Errors::MissingParameter('body_ids');
|
||||
throw Exceptions::MissingParameter('body_ids');
|
||||
}
|
||||
|
||||
return $this->day($id);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -24,7 +24,8 @@ declare(strict_types=1);
|
|||
namespace OCA\Memories\Controller;
|
||||
|
||||
use bantu\IniGetWrapper\IniGetWrapper;
|
||||
use OCA\Memories\Errors;
|
||||
use OCA\Memories\Exceptions;
|
||||
use OCA\Memories\Util;
|
||||
use OCP\AppFramework\Http;
|
||||
use OCP\AppFramework\Http\JSONResponse;
|
||||
use OCP\ISession;
|
||||
|
@ -44,16 +45,18 @@ class DownloadController extends GenericApiController
|
|||
*/
|
||||
public function request(): Http\Response
|
||||
{
|
||||
return Util::guardEx(function () {
|
||||
// Get ids from body
|
||||
$files = $this->request->getParam('files');
|
||||
if (null === $files || !\is_array($files)) {
|
||||
return Errors::MissingParameter('files');
|
||||
throw Exceptions::MissingParameter('files');
|
||||
}
|
||||
|
||||
// Return id
|
||||
$handle = self::createHandle('memories', $files);
|
||||
|
||||
return new JSONResponse(['handle' => $handle]);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -83,6 +86,7 @@ class DownloadController extends GenericApiController
|
|||
*/
|
||||
public function file(string $handle): Http\Response
|
||||
{
|
||||
return Util::guardEx(function () use ($handle) {
|
||||
// Get ids from request
|
||||
$session = \OC::$server->get(ISession::class);
|
||||
$key = "memories_download_{$handle}";
|
||||
|
@ -90,20 +94,18 @@ class DownloadController extends GenericApiController
|
|||
$session->remove($key);
|
||||
|
||||
if (null === $info) {
|
||||
return Errors::NotFound('handle');
|
||||
return Exceptions::NotFound('handle');
|
||||
}
|
||||
|
||||
$name = $info[0].'-'.date('YmdHis');
|
||||
$fileIds = $info[1];
|
||||
|
||||
/** @var int[] $fileIds */
|
||||
$fileIds = array_filter(array_map('intval', $fileIds), function (int $id): bool {
|
||||
return $id > 0;
|
||||
});
|
||||
$fileIds = array_filter(array_map('intval', $fileIds), fn ($id) => $id > 0);
|
||||
|
||||
// Check if we have any valid ids
|
||||
if (0 === \count($fileIds)) {
|
||||
return Errors::NotFound('file IDs');
|
||||
return Exceptions::NotFound('file IDs');
|
||||
}
|
||||
|
||||
// Download single file
|
||||
|
@ -113,6 +115,7 @@ class DownloadController extends GenericApiController
|
|||
|
||||
// Download multiple files
|
||||
$this->multiple($name, $fileIds); // exits
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -124,31 +127,28 @@ class DownloadController extends GenericApiController
|
|||
*/
|
||||
public function one(int $fileid): Http\Response
|
||||
{
|
||||
return Util::guardEx(function () use ($fileid) {
|
||||
$file = $this->getUserFile($fileid);
|
||||
if (null === $file) {
|
||||
return Errors::NotFoundFile($fileid);
|
||||
return Exceptions::NotFoundFile($fileid);
|
||||
}
|
||||
|
||||
// Get the owner's root folder
|
||||
$owner = $file->getOwner()->getUID();
|
||||
$userFolder = $this->rootFolder->getUserFolder($owner);
|
||||
$userFolder = Util::getUserFolder($owner);
|
||||
|
||||
// Get the file in the context of the owner
|
||||
$ownerFile = $userFolder->getById($fileid);
|
||||
if (0 === \count($ownerFile)) {
|
||||
// This should never happen, since the file was already found earlier
|
||||
// Except if it was deleted in the meantime ...
|
||||
return new JSONResponse([
|
||||
'message' => 'File not found in owner\'s root folder',
|
||||
], Http::STATUS_INTERNAL_SERVER_ERROR);
|
||||
throw new \Exception('File not found in owner\'s root folder');
|
||||
}
|
||||
|
||||
// Get DAV path of file relative to owner's root folder
|
||||
$path = $userFolder->getRelativePath($ownerFile[0]->getPath());
|
||||
if (null === $path) {
|
||||
return new JSONResponse([
|
||||
'message' => 'File path not found in owner\'s root folder',
|
||||
], Http::STATUS_INTERNAL_SERVER_ERROR);
|
||||
throw new \Exception('File path not found in owner\'s root folder');
|
||||
}
|
||||
|
||||
// Setup filesystem for owner
|
||||
|
@ -165,6 +165,7 @@ class DownloadController extends GenericApiController
|
|||
\OC_Files::get(\dirname($path), basename($path), $server_params);
|
||||
|
||||
exit;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -173,7 +174,7 @@ class DownloadController extends GenericApiController
|
|||
* @param string $name Name of zip file
|
||||
* @param int[] $fileIds
|
||||
*/
|
||||
private function multiple(string $name, array &$fileIds)
|
||||
private function multiple(string $name, array $fileIds)
|
||||
{
|
||||
// Disable time limit
|
||||
$executionTime = (int) \OC::$server->get(IniGetWrapper::class)->getNumeric('max_execution_time');
|
||||
|
|
|
@ -26,9 +26,7 @@ trait FoldersTrait
|
|||
$folders = $view->getDirectoryContent($folder->getPath(), FileInfo::MIMETYPE_FOLDER, $folder);
|
||||
|
||||
// Sort by name
|
||||
usort($folders, function ($a, $b) {
|
||||
return strnatcmp($a->getName(), $b->getName());
|
||||
});
|
||||
usort($folders, fn ($a, $b) => strnatcmp($a->getName(), $b->getName()));
|
||||
|
||||
// Process to response type
|
||||
return [
|
||||
|
|
|
@ -23,6 +23,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace OCA\Memories\Controller;
|
||||
|
||||
use OCA\Memories\Util;
|
||||
use OCP\App\IAppManager;
|
||||
use OCP\IConfig;
|
||||
|
||||
|
@ -31,41 +32,12 @@ trait GenericApiControllerUtils
|
|||
protected IAppManager $appManager;
|
||||
protected IConfig $config;
|
||||
|
||||
/** Get logged in user's UID or throw exception */
|
||||
protected function getUID(): string
|
||||
{
|
||||
$user = $this->userSession->getUser();
|
||||
if ($this->getShareToken()) {
|
||||
$user = null;
|
||||
} elseif (null === $user) {
|
||||
throw new \Exception('User not logged in');
|
||||
}
|
||||
|
||||
return $user ? $user->getUID() : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Runa function and catch exceptions to return HTTP response.
|
||||
*
|
||||
* @param mixed $function
|
||||
*/
|
||||
protected function guardEx($function): \OCP\AppFramework\Http\Response
|
||||
{
|
||||
try {
|
||||
return $function();
|
||||
} catch (\OCA\Memories\HttpResponseException $e) {
|
||||
return $e->response;
|
||||
} catch (\Exception $e) {
|
||||
return \OCA\Memories\Errors::Generic($e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if albums are enabled for this user.
|
||||
*/
|
||||
protected function albumsIsEnabled(): bool
|
||||
{
|
||||
return \OCA\Memories\Util::albumsIsEnabled($this->appManager);
|
||||
return Util::albumsIsEnabled($this->appManager);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -73,7 +45,7 @@ trait GenericApiControllerUtils
|
|||
*/
|
||||
protected function tagsIsEnabled(): bool
|
||||
{
|
||||
return \OCA\Memories\Util::tagsIsEnabled($this->appManager);
|
||||
return Util::tagsIsEnabled($this->appManager);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -81,13 +53,13 @@ trait GenericApiControllerUtils
|
|||
*/
|
||||
protected function recognizeIsEnabled(): bool
|
||||
{
|
||||
return \OCA\Memories\Util::recognizeIsEnabled($this->appManager);
|
||||
return Util::recognizeIsEnabled($this->appManager);
|
||||
}
|
||||
|
||||
// Check if facerecognition is installed and enabled for this user.
|
||||
protected function facerecognitionIsInstalled(): bool
|
||||
{
|
||||
return \OCA\Memories\Util::facerecognitionIsInstalled($this->appManager);
|
||||
return Util::facerecognitionIsInstalled($this->appManager);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -95,7 +67,7 @@ trait GenericApiControllerUtils
|
|||
*/
|
||||
protected function facerecognitionIsEnabled(): bool
|
||||
{
|
||||
return \OCA\Memories\Util::facerecognitionIsEnabled($this->config, $this->getUID());
|
||||
return Util::facerecognitionIsEnabled($this->config, Util::getUID());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -103,6 +75,6 @@ trait GenericApiControllerUtils
|
|||
*/
|
||||
protected function placesIsEnabled(): bool
|
||||
{
|
||||
return \OCA\Memories\Util::placesGISType() > 0;
|
||||
return Util::placesGISType() > 0;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,8 +24,9 @@ declare(strict_types=1);
|
|||
namespace OCA\Memories\Controller;
|
||||
|
||||
use OCA\Memories\AppInfo\Application;
|
||||
use OCA\Memories\Errors;
|
||||
use OCA\Memories\Exceptions;
|
||||
use OCA\Memories\Exif;
|
||||
use OCA\Memories\Util;
|
||||
use OCP\AppFramework\Http;
|
||||
use OCP\AppFramework\Http\FileDisplayResponse;
|
||||
use OCP\AppFramework\Http\JSONResponse;
|
||||
|
@ -49,16 +50,16 @@ class ImageController extends GenericApiController
|
|||
bool $a = false,
|
||||
string $mode = 'fill'
|
||||
) {
|
||||
return Util::guardEx(function () use ($id, $x, $y, $a, $mode) {
|
||||
if (-1 === $id || 0 === $x || 0 === $y) {
|
||||
return Errors::MissingParameter('id, x, y');
|
||||
throw Exceptions::MissingParameter('id, x, y');
|
||||
}
|
||||
|
||||
$file = $this->getUserFile($id);
|
||||
if (!$file) {
|
||||
return Errors::NotFoundFile($id);
|
||||
throw Exceptions::NotFoundFile($id);
|
||||
}
|
||||
|
||||
try {
|
||||
$preview = \OC::$server->get(\OCP\IPreview::class)->getPreview($file, $x, $y, !$a, $mode);
|
||||
$response = new FileDisplayResponse($preview, Http::STATUS_OK, [
|
||||
'Content-Type' => $preview->getMimeType(),
|
||||
|
@ -66,11 +67,7 @@ class ImageController extends GenericApiController
|
|||
$response->cacheFor(3600 * 24, false, true);
|
||||
|
||||
return $response;
|
||||
} catch (\OCP\Files\NotFoundException $e) {
|
||||
return Errors::NotFound('preview');
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
return Errors::Generic($e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -84,13 +81,10 @@ class ImageController extends GenericApiController
|
|||
*/
|
||||
public function multipreview()
|
||||
{
|
||||
return Util::guardEx(function () {
|
||||
// read body to array
|
||||
try {
|
||||
$body = file_get_contents('php://input');
|
||||
$files = json_decode($body, true);
|
||||
} catch (\Exception $e) {
|
||||
return new JSONResponse([], Http::STATUS_BAD_REQUEST);
|
||||
}
|
||||
|
||||
/** @var \OCP\IPreview $previewManager */
|
||||
$previewManager = \OC::$server->get(\OCP\IPreview::class);
|
||||
|
@ -165,6 +159,7 @@ class ImageController extends GenericApiController
|
|||
}
|
||||
|
||||
exit;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -182,9 +177,10 @@ class ImageController extends GenericApiController
|
|||
bool $current = false,
|
||||
bool $tags = false
|
||||
): Http\Response {
|
||||
return Util::guardEx(function () use ($id, $basic, $current, $tags) {
|
||||
$file = $this->getUserFile((int) $id);
|
||||
if (!$file) {
|
||||
return Errors::NotFoundFile($id);
|
||||
throw Exceptions::NotFoundFile($id);
|
||||
}
|
||||
|
||||
// Get the image info
|
||||
|
@ -212,6 +208,7 @@ class ImageController extends GenericApiController
|
|||
$info['basename'] = $file->getName();
|
||||
|
||||
return new JSONResponse($info, Http::STATUS_OK);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -224,19 +221,15 @@ class ImageController extends GenericApiController
|
|||
*/
|
||||
public function setExif(string $id, array $raw): Http\Response
|
||||
{
|
||||
return Util::guardEx(function () use ($id, $raw) {
|
||||
$file = $this->getUserFile((int) $id);
|
||||
if (!$file) {
|
||||
return Errors::NotFoundFile($id);
|
||||
throw Exceptions::NotFoundFile($id);
|
||||
}
|
||||
|
||||
// Check if user has permissions
|
||||
if (!$file->isUpdateable()) {
|
||||
return Errors::ForbiddenFileUpdate($file->getName());
|
||||
}
|
||||
|
||||
// Check for end-to-end encryption
|
||||
if (\OCA\Memories\Util::isEncryptionEnabled()) {
|
||||
return new JSONResponse(['message' => 'Cannot change encrypted file'], Http::STATUS_PRECONDITION_FAILED);
|
||||
if (!$file->isUpdateable() || Util::isEncryptionEnabled()) {
|
||||
throw Exceptions::ForbiddenFileUpdate($file->getName());
|
||||
}
|
||||
|
||||
// Check if allowed to edit file
|
||||
|
@ -244,16 +237,14 @@ class ImageController extends GenericApiController
|
|||
if (!\in_array($mime, Exif::allowedEditMimetypes(), true)) {
|
||||
$name = $file->getName();
|
||||
|
||||
return new JSONResponse(['message' => "Cannot edit file {$name} (blacklisted type {$mime})"], Http::STATUS_PRECONDITION_FAILED);
|
||||
throw Exceptions::Forbidden("Cannot edit file {$name} (blacklisted type {$mime})");
|
||||
}
|
||||
|
||||
try {
|
||||
// Set the exif data
|
||||
Exif::setFileExif($file, $raw);
|
||||
} catch (\Exception $e) {
|
||||
return Errors::Generic($e);
|
||||
}
|
||||
|
||||
return new JSONResponse([], Http::STATUS_OK);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -269,15 +260,16 @@ class ImageController extends GenericApiController
|
|||
*/
|
||||
public function decodable(string $id): Http\Response
|
||||
{
|
||||
return Util::guardEx(function () use ($id) {
|
||||
$file = $this->getUserFile((int) $id);
|
||||
if (!$file) {
|
||||
return Errors::NotFoundFile($id);
|
||||
throw Exceptions::NotFoundFile($id);
|
||||
}
|
||||
|
||||
// Check if valid image
|
||||
$mimetype = $file->getMimeType();
|
||||
if (!\in_array($mimetype, Application::IMAGE_MIMES, true)) {
|
||||
return Errors::ForbiddenFileUpdate($file->getName());
|
||||
throw Exceptions::Forbidden('Not an image');
|
||||
}
|
||||
|
||||
/** @var string Blob of image */
|
||||
|
@ -298,6 +290,7 @@ class ImageController extends GenericApiController
|
|||
$response->cacheFor(3600 * 24, false, false);
|
||||
|
||||
return $response;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -306,7 +299,7 @@ class ImageController extends GenericApiController
|
|||
private function getTags(int $fileId): array
|
||||
{
|
||||
// Make sure tags are enabled
|
||||
if (!\OCA\Memories\Util::tagsIsEnabled($this->appManager)) {
|
||||
if (!Util::tagsIsEnabled($this->appManager)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
|
@ -320,10 +313,9 @@ class ImageController extends GenericApiController
|
|||
/** @var \OCP\SystemTag\ISystemTag[] */
|
||||
$tags = $tagManager->getTagsByIds($tagIds);
|
||||
|
||||
return array_map(function ($tag) {
|
||||
return $tag->getName();
|
||||
}, array_filter($tags, function ($tag) {
|
||||
return $tag->isUserVisible();
|
||||
}));
|
||||
$visible = array_filter($tags, fn ($t) => $t->isUserVisible());
|
||||
|
||||
// Get the tag names
|
||||
return array_map(fn ($t) => $t->getName(), $visible);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,7 +23,8 @@ declare(strict_types=1);
|
|||
|
||||
namespace OCA\Memories\Controller;
|
||||
|
||||
use OCA\Memories\Errors;
|
||||
use OCA\Memories\Exceptions;
|
||||
use OCA\Memories\Util;
|
||||
use OCP\AppFramework\Http;
|
||||
use OCP\AppFramework\Http\JSONResponse;
|
||||
|
||||
|
@ -34,21 +35,16 @@ class MapController extends GenericApiController
|
|||
*/
|
||||
public function clusters(): Http\Response
|
||||
{
|
||||
return Util::guardEx(function () {
|
||||
// Get the folder to show
|
||||
$root = null;
|
||||
|
||||
try {
|
||||
$root = $this->getRequestRoot();
|
||||
} catch (\Exception $e) {
|
||||
return Errors::NoRequestRoot();
|
||||
}
|
||||
|
||||
// Make sure we have bounds and zoom level
|
||||
// Zoom level is used to determine the grid length
|
||||
$bounds = $this->request->getParam('bounds');
|
||||
$zoomLevel = $this->request->getParam('zoom');
|
||||
if (!$bounds || !$zoomLevel || !is_numeric($zoomLevel)) {
|
||||
return Errors::MissingParameter('bounds or zoom');
|
||||
throw Exceptions::MissingParameter('bounds or zoom');
|
||||
}
|
||||
|
||||
// A tweakable parameter to determine the number of boxes in the map
|
||||
|
@ -56,13 +52,10 @@ class MapController extends GenericApiController
|
|||
$clusterDensity = 1;
|
||||
$gridLen = 180.0 / (2 ** $zoomLevel * $clusterDensity);
|
||||
|
||||
try {
|
||||
$clusters = $this->timelineQuery->getMapClusters($gridLen, $bounds, $root);
|
||||
|
||||
// Get previews for each cluster
|
||||
$clusterIds = array_map(function ($cluster) {
|
||||
return (int) $cluster['id'];
|
||||
}, $clusters);
|
||||
$clusterIds = array_map(fn ($cluster) => (int) $cluster['id'], $clusters);
|
||||
$previews = $this->timelineQuery->getMapClusterPreviews($clusterIds, $root);
|
||||
|
||||
// Merge the responses
|
||||
|
@ -75,8 +68,6 @@ class MapController extends GenericApiController
|
|||
}
|
||||
|
||||
return new JSONResponse($clusters);
|
||||
} catch (\Exception $e) {
|
||||
return Errors::Generic($e);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,7 +24,8 @@ declare(strict_types=1);
|
|||
namespace OCA\Memories\Controller;
|
||||
|
||||
use OCA\Memories\AppInfo\Application;
|
||||
use OCA\Memories\Errors;
|
||||
use OCA\Memories\Exceptions;
|
||||
use OCA\Memories\Util;
|
||||
use OCP\AppFramework\Http;
|
||||
use OCP\AppFramework\Http\JSONResponse;
|
||||
use OCP\AppFramework\Http\StreamResponse;
|
||||
|
@ -43,20 +44,18 @@ class OtherController extends GenericApiController
|
|||
*/
|
||||
public function setUserConfig(string $key, string $value): Http\Response
|
||||
{
|
||||
$user = $this->userSession->getUser();
|
||||
if (null === $user) {
|
||||
return Errors::NotLoggedIn();
|
||||
}
|
||||
return Util::guardEx(function () use ($key, $value) {
|
||||
$uid = Util::getUID();
|
||||
|
||||
// Make sure not running in read-only mode
|
||||
if ($this->config->getSystemValue('memories.readonly', false)) {
|
||||
return new JSONResponse(['message' => 'Cannot change settings in readonly mode'], Http::STATUS_FORBIDDEN);
|
||||
throw Exceptions::Forbidden('Cannot change settings in readonly mode');
|
||||
}
|
||||
|
||||
$userId = $user->getUID();
|
||||
$this->config->setUserValue($userId, Application::APPNAME, $key, $value);
|
||||
$this->config->setUserValue($uid, Application::APPNAME, $key, $value);
|
||||
|
||||
return new JSONResponse([], Http::STATUS_OK);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -23,7 +23,8 @@ declare(strict_types=1);
|
|||
|
||||
namespace OCA\Memories\Controller;
|
||||
|
||||
use OCA\Memories\Errors;
|
||||
use OCA\Memories\Exceptions;
|
||||
use OCA\Memories\Util;
|
||||
use OCP\AppFramework\Http;
|
||||
use OCP\AppFramework\Http\JSONResponse;
|
||||
|
||||
|
@ -39,22 +40,21 @@ class ShareController extends GenericApiController
|
|||
*/
|
||||
public function links($id, $path): Http\Response
|
||||
{
|
||||
return Util::guardEx(function () use ($id, $path) {
|
||||
$file = $this->getNodeByIdOrPath($id, $path);
|
||||
if (!$file) {
|
||||
return Errors::Forbidden('file');
|
||||
}
|
||||
|
||||
/** @var \OCP\Share\IManager $shareManager */
|
||||
$shareManager = \OC::$server->get(\OCP\Share\IManager::class);
|
||||
|
||||
$shares = $shareManager->getSharesBy($this->getUID(), \OCP\Share\IShare::TYPE_LINK, $file, true, 50, 0);
|
||||
$shares = $shareManager->getSharesBy(Util::getUID(), \OCP\Share\IShare::TYPE_LINK, $file, true, 50, 0);
|
||||
if (empty($shares)) {
|
||||
return Errors::NotFound('external links');
|
||||
throw Exceptions::NotFound('external links');
|
||||
}
|
||||
|
||||
$links = array_map([$this, 'makeShareResponse'], $shares);
|
||||
$links = array_map(fn ($s) => $this->makeShareResponse($s), $shares);
|
||||
|
||||
return new JSONResponse($links, Http::STATUS_OK);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -67,10 +67,8 @@ class ShareController extends GenericApiController
|
|||
*/
|
||||
public function createNode($id, $path): Http\Response
|
||||
{
|
||||
return Util::guardEx(function () use ($id, $path) {
|
||||
$file = $this->getNodeByIdOrPath($id, $path);
|
||||
if (!$file) {
|
||||
return Errors::Forbidden('You are not allowed to share this file');
|
||||
}
|
||||
|
||||
/** @var \OCP\Share\IManager $shareManager */
|
||||
$shareManager = \OC::$server->get(\OCP\Share\IManager::class);
|
||||
|
@ -85,6 +83,7 @@ class ShareController extends GenericApiController
|
|||
$share = $shareManager->createShare($share);
|
||||
|
||||
return new JSONResponse($this->makeShareResponse($share), Http::STATUS_OK);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -94,10 +93,8 @@ class ShareController extends GenericApiController
|
|||
*/
|
||||
public function deleteShare(string $id): Http\Response
|
||||
{
|
||||
$uid = $this->getUID();
|
||||
if (!$uid) {
|
||||
return Errors::NotLoggedIn();
|
||||
}
|
||||
return Util::guardEx(function () use ($id) {
|
||||
$uid = Util::getUID();
|
||||
|
||||
/** @var \OCP\Share\IManager $shareManager */
|
||||
$shareManager = \OC::$server->get(\OCP\Share\IManager::class);
|
||||
|
@ -105,35 +102,32 @@ class ShareController extends GenericApiController
|
|||
$share = $shareManager->getShareById($id);
|
||||
|
||||
if ($share->getSharedBy() !== $uid) {
|
||||
return Errors::Forbidden('You are not the owner of this share');
|
||||
throw Exceptions::Forbidden('You are not the owner of this share');
|
||||
}
|
||||
|
||||
$shareManager->deleteShare($share);
|
||||
|
||||
return new JSONResponse([], Http::STATUS_OK);
|
||||
});
|
||||
}
|
||||
|
||||
private function getNodeByIdOrPath($id, $path)
|
||||
{
|
||||
$uid = $this->getUID();
|
||||
if (!$uid) {
|
||||
return null;
|
||||
}
|
||||
$uid = Util::getUID();
|
||||
|
||||
try {
|
||||
$file = null;
|
||||
if ($id) {
|
||||
$file = $this->getUserFile($id);
|
||||
} elseif ($path) {
|
||||
try {
|
||||
$userFolder = $this->rootFolder->getUserFolder($uid);
|
||||
$file = $userFolder->get($path);
|
||||
} catch (\OCP\Files\NotFoundException $e) {
|
||||
return null;
|
||||
$file = Util::getUserFolder($uid)->get($path);
|
||||
}
|
||||
} catch (\OCP\Files\NotFoundException $e) {
|
||||
throw Exceptions::NotFoundFile($path ?? $id);
|
||||
}
|
||||
|
||||
if (!$file || !$file->isShareable()) {
|
||||
return null;
|
||||
throw Exceptions::Forbidden('File not sharable');
|
||||
}
|
||||
|
||||
return $file;
|
||||
|
|
|
@ -23,7 +23,8 @@ declare(strict_types=1);
|
|||
|
||||
namespace OCA\Memories\Controller;
|
||||
|
||||
use OCA\Memories\Errors;
|
||||
use OCA\Memories\Exceptions;
|
||||
use OCA\Memories\Util;
|
||||
use OCP\AppFramework\Http;
|
||||
use OCP\AppFramework\Http\JSONResponse;
|
||||
|
||||
|
@ -36,20 +37,21 @@ class TagsController extends GenericApiController
|
|||
*/
|
||||
public function set(int $id, array $add, array $remove): Http\Response
|
||||
{
|
||||
return Util::guardEx(function () use ($id, $add, $remove) {
|
||||
// Check tags enabled for this user
|
||||
if (!$this->tagsIsEnabled()) {
|
||||
return Errors::NotEnabled('Tags');
|
||||
throw Exceptions::NotEnabled('Tags');
|
||||
}
|
||||
|
||||
// Check the user is allowed to edit the file
|
||||
$file = $this->getUserFile($id);
|
||||
if (null === $file) {
|
||||
return Errors::NotFoundFile($id);
|
||||
throw Exceptions::NotFoundFile($id);
|
||||
}
|
||||
|
||||
// Check the user is allowed to edit the file
|
||||
if (!$file->isUpdateable() || !($file->getPermissions() & \OCP\Constants::PERMISSION_UPDATE)) {
|
||||
return Errors::ForbiddenFileUpdate($file->getName());
|
||||
throw Exceptions::ForbiddenFileUpdate($file->getName());
|
||||
}
|
||||
|
||||
// Get mapper from tags to objects
|
||||
|
@ -60,5 +62,6 @@ class TagsController extends GenericApiController
|
|||
$om->unassignTags((string) $id, 'files', $remove);
|
||||
|
||||
return new JSONResponse([], Http::STATUS_OK);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,8 +23,9 @@ declare(strict_types=1);
|
|||
|
||||
namespace OCA\Memories\Controller;
|
||||
|
||||
use OCA\Memories\Errors;
|
||||
use OCA\Memories\Exceptions;
|
||||
use OCA\Memories\Exif;
|
||||
use OCA\Memories\Util;
|
||||
use OCP\AppFramework\Http;
|
||||
use OCP\AppFramework\Http\DataDisplayResponse;
|
||||
use OCP\AppFramework\Http\JSONResponse;
|
||||
|
@ -43,37 +44,38 @@ class VideoController extends GenericApiController
|
|||
*/
|
||||
public function transcode(string $client, int $fileid, string $profile): Http\Response
|
||||
{
|
||||
return Util::guardEx(function () use ($client, $fileid, $profile) {
|
||||
// Make sure not running in read-only mode
|
||||
if (false !== $this->config->getSystemValue('memories.vod.disable', 'UNSET')) {
|
||||
return Errors::Forbidden('Transcoding disabled');
|
||||
throw Exceptions::Forbidden('Transcoding disabled');
|
||||
}
|
||||
|
||||
// Check client identifier is 8 characters or more
|
||||
if (\strlen($client) < 8) {
|
||||
return Errors::MissingParameter('client (invalid)');
|
||||
throw Exceptions::MissingParameter('client (invalid)');
|
||||
}
|
||||
|
||||
// Get file
|
||||
$file = $this->getUserFile($fileid);
|
||||
if (!$file || !$file->isReadable()) {
|
||||
return Errors::NotFoundFile($fileid);
|
||||
throw Exceptions::NotFoundFile($fileid);
|
||||
}
|
||||
|
||||
// Local files only for now
|
||||
if (!$file->getStorage()->isLocal()) {
|
||||
return Errors::Forbidden('External storage not supported');
|
||||
throw Exceptions::Forbidden('External storage not supported');
|
||||
}
|
||||
|
||||
// Get file path
|
||||
$path = $file->getStorage()->getLocalFile($file->getInternalPath());
|
||||
if (!$path || !file_exists($path)) {
|
||||
return Errors::NotFound('local file path');
|
||||
throw Exceptions::NotFound('local file path');
|
||||
}
|
||||
|
||||
// Check if file starts with temp dir
|
||||
$tmpDir = sys_get_temp_dir();
|
||||
if (0 === strpos($path, $tmpDir)) {
|
||||
return Errors::Forbidden('files in temp directory not supported');
|
||||
throw Exceptions::Forbidden('files in temp directory not supported');
|
||||
}
|
||||
|
||||
// Request and check data was received
|
||||
|
@ -90,11 +92,12 @@ class VideoController extends GenericApiController
|
|||
$msg = 'Transcode failed: '.$e->getMessage();
|
||||
$this->logger->error($msg, ['app' => 'memories']);
|
||||
|
||||
return Errors::Generic($e);
|
||||
throw $e;
|
||||
}
|
||||
|
||||
// The response was already streamed, so we have nothing to do here
|
||||
exit;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -112,14 +115,15 @@ class VideoController extends GenericApiController
|
|||
string $format = '',
|
||||
string $transcode = ''
|
||||
) {
|
||||
return Util::guardEx(function () use ($fileid, $liveid, $format, $transcode) {
|
||||
$file = $this->getUserFile($fileid);
|
||||
if (null === $file) {
|
||||
return Errors::NotFoundFile($fileid);
|
||||
throw Exceptions::NotFoundFile($fileid);
|
||||
}
|
||||
|
||||
// Check file liveid
|
||||
if (!$liveid) {
|
||||
return Errors::MissingParameter('liveid');
|
||||
throw Exceptions::MissingParameter('liveid');
|
||||
}
|
||||
|
||||
// Response data
|
||||
|
@ -141,19 +145,19 @@ class VideoController extends GenericApiController
|
|||
try { // Get trailer
|
||||
$blob = Exif::getBinaryExifProp($path, '-trailer');
|
||||
} catch (\Exception $e) {
|
||||
return Errors::NotFound('file trailer');
|
||||
throw Exceptions::NotFound('file trailer');
|
||||
}
|
||||
} elseif ('self__embeddedvideo' === $liveid) {
|
||||
try { // Get embedded video file
|
||||
$blob = Exif::getBinaryExifProp($path, '-EmbeddedVideoFile');
|
||||
} catch (\Exception $e) {
|
||||
return Errors::NotFound('embedded video');
|
||||
throw Exceptions::NotFound('embedded video');
|
||||
}
|
||||
} elseif (str_starts_with($liveid, 'self__traileroffset=')) {
|
||||
// Remove prefix
|
||||
$offset = (int) substr($liveid, \strlen('self__traileroffset='));
|
||||
if ($offset <= 0) {
|
||||
return new JSONResponse(['message' => 'Invalid offset'], Http::STATUS_BAD_REQUEST);
|
||||
throw Exceptions::BadRequest('Invalid offset');
|
||||
}
|
||||
|
||||
// Read file from offset to end
|
||||
|
@ -162,14 +166,14 @@ class VideoController extends GenericApiController
|
|||
// Get stored video file (Apple MOV)
|
||||
$lp = $this->timelineQuery->getLivePhoto($fileid);
|
||||
if (!$lp || $lp['liveid'] !== $liveid) {
|
||||
return Errors::NotFound('live video entry');
|
||||
throw Exceptions::NotFound('live video entry');
|
||||
}
|
||||
|
||||
// Get and return file
|
||||
$liveFileId = (int) $lp['fileid'];
|
||||
$files = $this->rootFolder->getById($liveFileId);
|
||||
if (0 === \count($files)) {
|
||||
return Errors::NotFound('live video file');
|
||||
throw Exceptions::NotFound('live video file');
|
||||
}
|
||||
$liveFile = $files[0];
|
||||
|
||||
|
@ -188,7 +192,7 @@ class VideoController extends GenericApiController
|
|||
|
||||
// Data not found
|
||||
if (!$blob) {
|
||||
return Errors::NotFound('live video data');
|
||||
throw Exceptions::NotFound('live video data');
|
||||
}
|
||||
|
||||
// Transcode video if allowed
|
||||
|
@ -217,6 +221,7 @@ class VideoController extends GenericApiController
|
|||
$response->cacheFor(3600 * 24, false, false);
|
||||
|
||||
return $response;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,74 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OCA\Memories;
|
||||
|
||||
use OCP\AppFramework\Http;
|
||||
use OCP\AppFramework\Http\DataResponse;
|
||||
|
||||
class Errors
|
||||
{
|
||||
public static function Generic(\Exception $e, int $status = Http::STATUS_INTERNAL_SERVER_ERROR): Http\Response
|
||||
{
|
||||
return new DataResponse([
|
||||
'message' => $e->getMessage(),
|
||||
], $status);
|
||||
}
|
||||
|
||||
public static function NotLoggedIn(): Http\Response
|
||||
{
|
||||
return new DataResponse([
|
||||
'message' => 'User not logged in',
|
||||
], Http::STATUS_PRECONDITION_FAILED);
|
||||
}
|
||||
|
||||
public static function NotEnabled(string $app): Http\Response
|
||||
{
|
||||
return new DataResponse([
|
||||
'message' => "{$app} app not enabled or not the required version.",
|
||||
], Http::STATUS_PRECONDITION_FAILED);
|
||||
}
|
||||
|
||||
public static function NoRequestRoot(): Http\Response
|
||||
{
|
||||
return new DataResponse([
|
||||
'message' => 'Request root could not be determined',
|
||||
], Http::STATUS_NOT_FOUND);
|
||||
}
|
||||
|
||||
public static function NotFound(string $ctx): Http\Response
|
||||
{
|
||||
return new DataResponse([
|
||||
'message' => "Not found ({$ctx})",
|
||||
], Http::STATUS_NOT_FOUND);
|
||||
}
|
||||
|
||||
public static function NotFoundFile($identifier): Http\Response
|
||||
{
|
||||
return new DataResponse([
|
||||
'message' => "File not found ({$identifier})",
|
||||
], Http::STATUS_NOT_FOUND);
|
||||
}
|
||||
|
||||
public static function Forbidden(string $ctx): Http\Response
|
||||
{
|
||||
return new DataResponse([
|
||||
'message' => "Forbidden ({$ctx})",
|
||||
], Http::STATUS_FORBIDDEN);
|
||||
}
|
||||
|
||||
public static function ForbiddenFileUpdate(string $name): Http\Response
|
||||
{
|
||||
return new DataResponse([
|
||||
'message' => "Forbidden ({$name} cannot be updated)",
|
||||
], Http::STATUS_FORBIDDEN);
|
||||
}
|
||||
|
||||
public static function MissingParameter(string $name): Http\Response
|
||||
{
|
||||
return new DataResponse([
|
||||
'message' => "Missing parameter ({$name})",
|
||||
], Http::STATUS_BAD_REQUEST);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,81 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OCA\Memories;
|
||||
|
||||
use OCP\AppFramework\Http;
|
||||
use OCP\AppFramework\Http\DataResponse;
|
||||
|
||||
class Exceptions
|
||||
{
|
||||
public static function Generic(\Exception $e, int $status = Http::STATUS_INTERNAL_SERVER_ERROR): HttpResponseException
|
||||
{
|
||||
return new HttpResponseException(new DataResponse([
|
||||
'message' => $e->getMessage(),
|
||||
], $status));
|
||||
}
|
||||
|
||||
public static function NotLoggedIn(): HttpResponseException
|
||||
{
|
||||
return new HttpResponseException(new DataResponse([
|
||||
'message' => 'User not logged in',
|
||||
], Http::STATUS_PRECONDITION_FAILED));
|
||||
}
|
||||
|
||||
public static function NotEnabled(string $app): HttpResponseException
|
||||
{
|
||||
return new HttpResponseException(new DataResponse([
|
||||
'message' => "{$app} app not enabled or not the required version.",
|
||||
], Http::STATUS_PRECONDITION_FAILED));
|
||||
}
|
||||
|
||||
public static function NoRequestRoot(): HttpResponseException
|
||||
{
|
||||
return new HttpResponseException(new DataResponse([
|
||||
'message' => 'Request root could not be determined',
|
||||
], Http::STATUS_NOT_FOUND));
|
||||
}
|
||||
|
||||
public static function NotFound(string $ctx): HttpResponseException
|
||||
{
|
||||
return new HttpResponseException(new DataResponse([
|
||||
'message' => "Not found ({$ctx})",
|
||||
], Http::STATUS_NOT_FOUND));
|
||||
}
|
||||
|
||||
public static function NotFoundFile($identifier): HttpResponseException
|
||||
{
|
||||
return new HttpResponseException(new DataResponse([
|
||||
'message' => "File not found ({$identifier})",
|
||||
], Http::STATUS_NOT_FOUND));
|
||||
}
|
||||
|
||||
public static function Forbidden(string $ctx): HttpResponseException
|
||||
{
|
||||
return new HttpResponseException(new DataResponse([
|
||||
'message' => "Forbidden ({$ctx})",
|
||||
], Http::STATUS_FORBIDDEN));
|
||||
}
|
||||
|
||||
public static function ForbiddenFileUpdate(string $name): HttpResponseException
|
||||
{
|
||||
return new HttpResponseException(new DataResponse([
|
||||
'message' => "Forbidden ({$name} cannot be updated)",
|
||||
], Http::STATUS_FORBIDDEN));
|
||||
}
|
||||
|
||||
public static function MissingParameter(string $name): HttpResponseException
|
||||
{
|
||||
return new HttpResponseException(new DataResponse([
|
||||
'message' => "Missing parameter ({$name})",
|
||||
], Http::STATUS_BAD_REQUEST));
|
||||
}
|
||||
|
||||
public static function BadRequest(string $ctx): HttpResponseException
|
||||
{
|
||||
return new HttpResponseException(new DataResponse([
|
||||
'message' => "Bad Request ({$ctx})",
|
||||
], Http::STATUS_BAD_REQUEST));
|
||||
}
|
||||
}
|
|
@ -16,6 +16,8 @@ use OCP\IConfig;
|
|||
|
||||
class Util
|
||||
{
|
||||
use UtilController;
|
||||
|
||||
public static $TAG_DAYID_START = -(1 << 30); // the world surely didn't exist
|
||||
public static $TAG_DAYID_FOLDERS = -(1 << 30) + 1;
|
||||
|
||||
|
|
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OCA\Memories;
|
||||
|
||||
use OCP\AppFramework\Http;
|
||||
use OCP\AppFramework\Http\DataResponse;
|
||||
|
||||
trait UtilController
|
||||
{
|
||||
/**
|
||||
* Run a function and catch exceptions to return HTTP response.
|
||||
*
|
||||
* @param Function $function
|
||||
*/
|
||||
public static function guardEx($function): Http\Response
|
||||
{
|
||||
try {
|
||||
return $function();
|
||||
} catch (\OCA\Memories\HttpResponseException $e) {
|
||||
return $e->response;
|
||||
} catch (\Exception $e) {
|
||||
return new DataResponse([
|
||||
'message' => $e->getMessage(),
|
||||
], Http::STATUS_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current user.
|
||||
*
|
||||
* @throws \OCA\Memories\HttpResponseException if the user is not logged in
|
||||
*/
|
||||
public static function getUser(): \OCP\IUser
|
||||
{
|
||||
$user = \OC::$server->get(\OCP\IUserSession::class)->getUser();
|
||||
if (null === $user) {
|
||||
throw Exceptions::NotLoggedIn();
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current user ID.
|
||||
*
|
||||
* @throws \OCA\Memories\HttpResponseException if the user is not logged in
|
||||
*/
|
||||
public static function getUID(): string
|
||||
{
|
||||
return self::getUser()->getUID();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a user's home folder.
|
||||
*
|
||||
* @param null|string $uid User ID, or null for current user
|
||||
*
|
||||
* @throws \OCA\Memories\HttpResponseException if the user is not logged in
|
||||
*/
|
||||
public static function getUserFolder(?string $uid = null): \OCP\Files\Folder
|
||||
{
|
||||
if (null === $uid) {
|
||||
$uid = self::getUID();
|
||||
}
|
||||
|
||||
return \OC::$server->get(\OCP\Files\IRootFolder::class)->getUserFolder($uid);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue