Skip to content

Commit

Permalink
cleanup backend stuff
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Veyssier <eneiluj@posteo.net>
  • Loading branch information
Julien Veyssier committed Aug 30, 2022
1 parent 31e5184 commit 5b846f0
Show file tree
Hide file tree
Showing 15 changed files with 221 additions and 109 deletions.
5 changes: 0 additions & 5 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ class Application extends App implements IBootstrap {
'share' => 1,
];

/**
* Constructor
*
* @param array $urlParams
*/
public function __construct(array $urlParams = []) {
parent::__construct(self::APP_ID, $urlParams);

Expand Down
7 changes: 5 additions & 2 deletions lib/Command/CleanupSpacedeck.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

class CleanupSpacedeck extends Command {

protected $output;
/**
* @var SessionService
*/
Expand All @@ -32,14 +31,18 @@ class CleanupSpacedeck extends Command {
* @var SpacedeckAPIService
*/
private $apiService;
/**
* @var IConfig
*/
private $config;

public function __construct(SpacedeckAPIService $apiService,
SessionService $sessionService,
IConfig $config) {
parent::__construct();
$this->config = $config;
$this->sessionService = $sessionService;
$this->apiService = $apiService;
$this->config = $config;
}

protected function configure() {
Expand Down
7 changes: 0 additions & 7 deletions lib/Controller/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,15 @@
use Psr\Log\LoggerInterface;

use OCP\IRequest;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\AppFramework\Controller;

use OCA\Spacedeck\Service\SpacedeckAPIService;
use OCA\Spacedeck\AppInfo\Application;

class ConfigController extends Controller {


private $userId;
private $config;
private $dbtype;

public function __construct($AppName,
IRequest $request,
Expand All @@ -39,7 +33,6 @@ public function __construct($AppName,
?string $userId) {
parent::__construct($AppName, $request);
$this->appName = $AppName;
$this->userId = $userId;
$this->config = $config;
$this->logger = $logger;
$this->spacedeckAPIService = $spacedeckAPIService;
Expand Down
4 changes: 0 additions & 4 deletions lib/Controller/SessionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,9 @@

use OCP\IRequest;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\AppFramework\Controller;

use OCA\Spacedeck\AppInfo\Application;

class SessionController extends Controller {

/**
Expand Down
3 changes: 1 addition & 2 deletions lib/Controller/SpacedeckAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
use OCA\Spacedeck\Service\SpacedeckAPIService;
use OCA\Spacedeck\AppInfo\Application;

if (!function_exists('getallheaders'))
{
if (!function_exists('getallheaders')) {
// polyfill, e.g. on PHP 7.2 setups with nginx.
// Can be removed when 7.2 becomes unsupported
function getallheaders() {
Expand Down
34 changes: 23 additions & 11 deletions lib/Migration/CopySpacedeckAppData.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,35 @@

namespace OCA\Spacedeck\Migration;

use Exception;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
use OCP\ILogger;
use OCP\IConfig;

use OCA\Spacedeck\AppInfo\Application;
use OCA\Spacedeck\Service\SpacedeckBundleService;
use Psr\Log\LoggerInterface;
use Throwable;

class CopySpacedeckAppData implements IRepairStep {
protected $logger;
private $customMimetypeMapping;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var SpacedeckBundleService
*/
private $bundleService;
/**
* @var IConfig
*/
private $config;

public function __construct(ILogger $logger,
SpacedeckBundleService $service,
public function __construct(LoggerInterface $logger,
SpacedeckBundleService $bundleService,
IConfig $config) {
$this->logger = $logger;
$this->service = $service;
$this->bundleService = $bundleService;
$this->config = $config;
}

Expand All @@ -48,14 +60,14 @@ public function getName() {
public function run(IOutput $output) {
$this->logger->info('Copying Spacedeck data...');

$this->service->killSpacedeck();
$this->bundleService->killSpacedeck();
try {
$this->service->copySpacedeckData();
$this->bundleService->copySpacedeckData();
$this->config->setAppValue(Application::APP_ID, 'spacedeck_data_copied', '1');
$this->logger->info('Spacedeck data successfully copied!');
$this->logger->info('Spacedeck data successfully copied!', ['app' => Application::APP_ID]);
} catch (Exception | Throwable $e) {
$this->config->setAppValue(Application::APP_ID, 'spacedeck_data_copied', '0');
$this->logger->warning('Impossible to copy Spacedeck data');
$this->logger->warning('Impossible to copy Spacedeck data', ['app' => Application::APP_ID]);
}
}
}
}
18 changes: 11 additions & 7 deletions lib/Migration/RegisterMimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@

namespace OCA\Spacedeck\Migration;

use OCA\Spacedeck\AppInfo\Application;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
use OCP\ILogger;
use Psr\Log\LoggerInterface;

class RegisterMimeType implements IRepairStep {
protected $logger;
private $customMimetypeMapping;

public function __construct(ILogger $logger) {
/**
* @var LoggerInterface
*/
private $logger;

public function __construct(LoggerInterface $logger) {
$this->logger = $logger;
}

Expand All @@ -38,7 +42,7 @@ public function getName() {
}

public function run(IOutput $output) {
$this->logger->info('Registering the Spacedeck mimetype...');
$this->logger->info('Registering the Spacedeck mimetype...', ['app' => Application::APP_ID]);

$mimetypeMapping = [
'whiteboard' => ['application/spacedeck']
Expand All @@ -53,6 +57,6 @@ public function run(IOutput $output) {

file_put_contents($mimetypeMappingFile, json_encode($mimetypeMapping, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));

$this->logger->info('The Spacedeck mimetype was successfully registered.');
$this->logger->info('The Spacedeck mimetype was successfully registered.', ['app' => Application::APP_ID]);
}
}
}
27 changes: 18 additions & 9 deletions lib/Migration/StopSpacedeck.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,28 @@

namespace OCA\Spacedeck\Migration;

use OCA\Spacedeck\AppInfo\Application;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
use OCP\ILogger;

use OCA\Spacedeck\Service\SpacedeckBundleService;
use Psr\Log\LoggerInterface;

class StopSpacedeck implements IRepairStep {
protected $logger;
private $customMimetypeMapping;

public function __construct(ILogger $logger, SpacedeckBundleService $service) {
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var SpacedeckBundleService
*/
private $bundleService;

public function __construct(LoggerInterface $logger,
SpacedeckBundleService $bundleService) {
$this->logger = $logger;
$this->service = $service;
$this->bundleService = $bundleService;
}

public function getName() {
Expand All @@ -43,11 +52,11 @@ public function getName() {
public function run(IOutput $output) {
$this->logger->info('Stopping Spacedeck...');

$stopped = $this->service->killSpacedeck();
$stopped = $this->bundleService->killSpacedeck();
if ($stopped) {
$this->logger->info('Spacedeck stopped!');
$this->logger->info('Spacedeck stopped!', ['app' => Application::APP_ID]);
} else {
$this->logger->warning('Failed to stop Spacedeck');
$this->logger->warning('Failed to stop Spacedeck', ['app' => Application::APP_ID]);
}
}
}
}
39 changes: 25 additions & 14 deletions lib/Service/FileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,44 @@

namespace OCA\Spacedeck\Service;

use OC\User\NoUserException;
use OCA\Spacedeck\AppInfo\Application;
use OCP\Files\InvalidPathException;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\Share\Exceptions\ShareNotFound;
use Psr\Log\LoggerInterface;
use OCP\Constants;
use OCP\Files\IRootFolder;
use OCP\Files\FileInfo;
use OCP\Files\Node;
use OCP\Share\IManager as IShareManager;
use OCP\IUserManager;

class FileService {

/**
* @var IShareManager
*/
private $shareManager;
/**
* @var IRootFolder
*/
private $root;

public function __construct (string $appName,
IShareManager $shareManager,
IRootFolder $root,
IUserManager $userManager,
LoggerInterface $logger) {
$this->appName = $appName;
$this->logger = $logger;
$this->root = $root;
$this->userManager = $userManager;
public function __construct (string $appName,
IShareManager $shareManager,
IRootFolder $root) {
$this->shareManager = $shareManager;
$this->root = $root;
}

/**
* Get a user file from a fileId
*
* @param ?string $userId
* @param int $fileID
* @param int $fileId
* @return ?Node the file or null if it does not exist (or is not accessible by this user)
* @throws NoUserException
* @throws NotPermittedException
*/
public function getFileFromId(?string $userId, int $fileId): ?Node {
if (is_null($userId)) {
Expand All @@ -66,8 +69,12 @@ public function getFileFromId(?string $userId, int $fileId): ?Node {
* Check if user has write access on a file
*
* @param ?string $userId
* @param int $fileID
* @param int $fileId
* @return bool true if the user can write the file
* @throws NoUserException
* @throws NotPermittedException
* @throws InvalidPathException
* @throws NotFoundException
*/
public function userHasWriteAccess(string $userId, int $fileId): bool {
$userFolder = $this->root->getUserFolder($userId);
Expand Down Expand Up @@ -113,8 +120,10 @@ public function getUserPermissionsOnFile(string $userId, int $fileId): int {
* Check if a share token can access a file
*
* @param string $shareToken
* @param int $fileId the file ID or 0 if the token target is a file (public file share page context)
* @param int $fileId
* @return ?Node the file or null if this token does not exist or can't access this file
* @throws InvalidPathException
* @throws NotFoundException
*/
public function getFileFromShareToken(string $shareToken, int $fileId): ?Node {
try {
Expand All @@ -141,6 +150,8 @@ public function getFileFromShareToken(string $shareToken, int $fileId): ?Node {
* @param string $shareToken
* @param int $fileId
* @return bool true if has write access
* @throws InvalidPathException
* @throws NotFoundException
*/
public function isFileWriteableWithToken(string $shareToken, int $fileId): bool {
$file = $this->getFileFromShareToken($shareToken, $fileId);
Expand Down
11 changes: 11 additions & 0 deletions lib/Service/SessionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

namespace OCA\Spacedeck\Service;

use OC\User\NoUserException;
use OCP\DB\Exception;
use OCP\Files\InvalidPathException;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IConfig;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -101,6 +106,8 @@ private function saveSessionSpaceToFile(array $session) {
* @param string $userId
* @param int $fileId
* @return array|null
* @throws NoUserException
* @throws NotPermittedException
*/
public function createUserSession(string $userId, int $fileId): ?array {
$file = $this->fileService->getFileFromId($userId, $fileId);
Expand All @@ -116,6 +123,8 @@ public function createUserSession(string $userId, int $fileId): ?array {
* @param string $shareToken
* @param int $fileId
* @return array|null
* @throws InvalidPathException
* @throws NotFoundException
*/
public function createShareSession(string $shareToken, int $fileId): ?array {
$file = $this->fileService->getFileFromShareToken($shareToken, $fileId);
Expand All @@ -131,6 +140,7 @@ public function createShareSession(string $shareToken, int $fileId): ?array {
* @param string $userId
* @param string $sessionToken
* @return bool
* @throws Exception
*/
public function deleteUserSession(string $userId, string $sessionToken): bool {
$session = $this->sessionStoreService->getSession(
Expand All @@ -149,6 +159,7 @@ public function deleteUserSession(string $userId, string $sessionToken): bool {
* @param string $shareToken
* @param string $sessionToken
* @return bool
* @throws Exception
*/
public function deleteShareSession(string $shareToken, string $sessionToken): bool {
$session = $this->sessionStoreService->getSession(
Expand Down
Loading

0 comments on commit 5b846f0

Please sign in to comment.