Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/Controller/Implementation/ConfigImplementation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace OCA\Cookbook\Controller\Implementation;

use OCA\Cookbook\Helper\MyRecipesFolderHelper;
use OCA\Cookbook\Helper\RestParameterParser;
use OCA\Cookbook\Helper\SharedRecipesFolderHelper;
use OCA\Cookbook\Helper\UserFolderHelper;
use OCA\Cookbook\Service\DbCacheService;
use OCA\Cookbook\Service\RecipeService;
Expand All @@ -18,17 +20,25 @@ class ConfigImplementation {
private $restParser;
/** @var UserFolderHelper */
private $userFolder;
/** @var MyRecipesFolderHelper */
private $myRecipesFolder;
/** @var SharedRecipesFolderHelper */
private $sharedRecipesFolder;

public function __construct(
RecipeService $recipeService,
DbCacheService $dbCacheService,
RestParameterParser $restParser,
UserFolderHelper $userFolder,
MyRecipesFolderHelper $myRecipesFolder,
SharedRecipesFolderHelper $sharedRecipesFolder,
) {
$this->service = $recipeService;
$this->dbCacheService = $dbCacheService;
$this->restParser = $restParser;
$this->userFolder = $userFolder;
$this->myRecipesFolder = $myRecipesFolder;
$this->sharedRecipesFolder = $sharedRecipesFolder;
}

protected const KEY_VISIBLE_INFO_BLOCKS = 'visibleInfoBlocks';
Expand All @@ -43,6 +53,8 @@ public function list() {

return new JSONResponse([
'folder' => $this->userFolder->getPath(),
'my_recipes_folder' => $this->myRecipesFolder->getPath(),
'shared_recipes_folder' => $this->sharedRecipesFolder->getPath(),
'update_interval' => $this->dbCacheService->getSearchIndexUpdateInterval(),
'print_image' => $this->service->getPrintImage(),
self::KEY_VISIBLE_INFO_BLOCKS => $this->service->getVisibleInfoBlocks(),
Expand All @@ -67,6 +79,16 @@ public function config() {
$this->dbCacheService->updateCache();
}

if (isset($data['my_recipes_folder'])) {
$this->myRecipesFolder->setPath($data['my_recipes_folder']);
$this->dbCacheService->updateCache();
}

if (isset($data['shared_recipes_folder'])) {
$this->sharedRecipesFolder->setPath($data['shared_recipes_folder']);
$this->dbCacheService->updateCache();
}

if (isset($data['update_interval'])) {
$this->service->setSearchIndexUpdateInterval($data['update_interval']);
}
Expand Down
18 changes: 15 additions & 3 deletions lib/Controller/MainController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace OCA\Cookbook\Controller;

use OCA\Cookbook\Exception\UserFolderNotWritableException;
use OCA\Cookbook\Exception\FolderNotWritableException;
use OCA\Cookbook\Exception\UserNotLoggedInException;
use OCA\Cookbook\Helper\MyRecipesFolderHelper;
use OCA\Cookbook\Helper\SharedRecipesFolderHelper;
use OCA\Cookbook\Helper\UserFolderHelper;
use OCA\Cookbook\Service\DbCacheService;
use OCP\AppFramework\Controller;
Expand All @@ -20,18 +22,26 @@ class MainController extends Controller {
private $dbCacheService;
/** @var UserFolderHelper */
private $userFolder;
/** @var MyRecipesFolderHelper */
private $myRecipesFolder;
/** @var SharedRecipesFolderHelper */
private $sharedRecipesFolder;

public function __construct(
string $AppName,
IRequest $request,
DbCacheService $dbCacheService,
UserFolderHelper $userFolder,
MyRecipesFolderHelper $myRecipesFolder,
SharedRecipesFolderHelper $sharedRecipesFolder,
) {
parent::__construct($AppName, $request);

$this->appName = $AppName;
$this->dbCacheService = $dbCacheService;
$this->userFolder = $userFolder;
$this->myRecipesFolder = $myRecipesFolder;
$this->sharedRecipesFolder = $sharedRecipesFolder;
}

/**
Expand All @@ -43,9 +53,11 @@ public function __construct(
#[NoCSRFRequired]
public function index(): TemplateResponse {
try {
// Check if the user folder can be accessed
// Check if the user folders can be accessed
$this->userFolder->getFolder();
} catch (UserFolderNotWritableException $ex) {
$this->myRecipesFolder->getFolder();
$this->sharedRecipesFolder->getFolder();
} catch (FolderNotWritableException $ex) {
Util::addScript('cookbook', 'cookbook-guest');
return new TemplateResponse($this->appName, 'invalid_guest');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace OCA\Cookbook\Exception;

class UserFolderNotValidException extends \Exception {
class FolderNotValidException extends \Exception {
public function __construct($message = '', $code = 0, $previous = null) {
parent::__construct($message, $code, $previous);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace OCA\Cookbook\Exception;

class UserFolderNotWritableException extends \Exception {
class FolderNotWritableException extends \Exception {
public function __construct($message = '', $code = 0, $previous = null) {
parent::__construct($message, $code, $previous);
}
Expand Down
145 changes: 145 additions & 0 deletions lib/Helper/FolderHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php

namespace OCA\Cookbook\Helper;

use NotImplementedException;
use OCA\Cookbook\Exception\FolderNotValidException;
use OCA\Cookbook\Exception\FolderNotWritableException;
use OCA\Cookbook\Exception\UserNotLoggedInException;
use OCP\Files\FileInfo;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IL10N;

/**
* This class caches the access to a folder throughout the app.
*
*/
class FolderHelper {
/**
* @var UserConfigHelper
*/
protected $config;

/**
* @var IL10N
*/
protected $l;

/**
* @var ?string
*/
protected $userId;

/**
* @var IRootFolder
*/
protected $root;

/**
* The folder or null if this is not yet cached
*
* @var ?Node
*/
protected $cache;

public function __construct(
?string $UserId,
IRootFolder $root,
IL10N $l,
UserConfigHelper $configHelper,
) {
$this->userId = $UserId;
$this->root = $root;
$this->l = $l;
$this->config = $configHelper;

$this->cache = null;
}

/**
* Set the path relative to the user's root folder
*
* @param string The relative path name
* @throws NotImplementedException If the function is not implemented in the child class
*/
#[\Override]
public function setPath(string $path) {
throw new NotImplementedException();
}

/**
* Get the path relative to the user's root folder
*
* @return string The relative path name
* @throws NotImplementedException If the function is not implemented in the child class
*/
#[\Override]
public function getPath(): string {
throw new NotImplementedException();
}

/**
* Get the current folder
*
* @return Folder The folder
* @throws FolderNotValidException If the folder is a file or could not be generated
* @throws UserNotLoggedInException If there is no logged-in user at that time
*/
public function getFolder(): Folder {
// Ensure the cache is built.
if (is_null($this->cache)) {
$path = $this->getPath();

// Correct path to be relative to nc root
$path = '/' . $this->userId . '/files/' . $path;
$path = str_replace('//', '/', $path);


$this->cache = $this->getOrCreateFolder($path);
}

return $this->cache;
}

/**
* Get or Create the folder at path
*
* @param string The folder's relative path name
* @return Folder The folder at path
* @throws FolderNotValidException If the folder is a file or could not be generated
* @throws FolderNotWritableException If the folder cannot be written
*/
protected function getOrCreateFolder(string $path): Folder {
try {
$node = $this->root->get($path);
} catch (NotFoundException $ex) {
try {
$node = $this->root->newFolder($path);
} catch (NotPermittedException $ex1) {
throw new FolderNotValidException(
$this->l->t('The folder cannot be created due to missing permissions.'),
0,
$ex1
);
}
}

if ($node->getType() !== FileInfo::TYPE_FOLDER) {
throw new FolderNotValidException(
$this->l->t('The configured folder is a file.')
);
}

if (!$node->isCreatable()) {
throw new FolderNotWritableException(
$this->l->t('User cannot create folder')
);
}

return $node;
}
}
39 changes: 39 additions & 0 deletions lib/Helper/MyRecipesFolderHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace OCA\Cookbook\Helper;

use OCA\Cookbook\Exception\UserNotLoggedInException;

/**
* This class caches the access to the user's recipe folder throughout the app.
*
* The user's recipes folder is the path, were all recipes are stored.
*/
class MyRecipesFolderHelper extends FolderHelper {
/**
* Set the current path in the settings relative to the user's root folder
*
* @param string $path The name of the path to be used for the recipes
*/
#[\Override]
public function setPath(string $path) {
$this->config->setMyRecipesFolderName($path);
$this->cache = null;
}

/**
* Get the path of the user's recipes relative to the user's root folder
*
* @return string The relative path name
* @throws UserNotLoggedInException If there is currently no logged in user
*/
#[\Override]
public function getPath(): string {
$path = $this->config->getMyRecipesFolderName();

// TODO This was in the original code. Is it still needed?
// $path = str_replace('//', '/', $path);

return $path;
}
}
39 changes: 39 additions & 0 deletions lib/Helper/SharedRecipesFolderHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace OCA\Cookbook\Helper;

use OCA\Cookbook\Exception\UserNotLoggedInException;

/**
* This class caches the access to the shared recipes folder throughout the app.
*
* The shared recipes folder is the path, were all recipes are stored.
*/
class SharedRecipesFolderHelper extends FolderHelper {
/**
* Set the current path in the settings relative to the user's root folder
*
* @param string $path The name of the path to be used for the recipes
*/
#[\Override]
public function setPath(string $path) {
$this->config->setSharedRecipesFolderName($path);
$this->cache = null;
}

/**
* Get the path of the shared recipes relative to the user's root folder
*
* @return string The relative path name
* @throws UserNotLoggedInException If there is currently no logged in user
*/
#[\Override]
public function getPath(): string {
$path = $this->config->getSharedRecipesFolderName();

// TODO This was in the original code. Is it still needed?
// $path = str_replace('//', '/', $path);

return $path;
}
}
Loading