refactor: move utils to Util from Exif

Signed-off-by: Varun Patil <varunpatil@ucla.edu>
pull/579/head
Varun Patil 2023-04-16 16:03:59 -07:00
parent 14011dc5fd
commit 265c0f795c
6 changed files with 32 additions and 42 deletions

View File

@ -24,7 +24,6 @@ declare(strict_types=1);
namespace OCA\Memories\Controller; namespace OCA\Memories\Controller;
use OCA\Memories\Exceptions; use OCA\Memories\Exceptions;
use OCA\Memories\Exif;
use OCA\Memories\Util; use OCA\Memories\Util;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\Http\JSONResponse;
@ -57,7 +56,7 @@ class ArchiveController extends GenericApiController
} }
// Create archive folder in the root of the user's configured timeline // Create archive folder in the root of the user's configured timeline
$configPaths = Exif::getTimelinePaths(Util::getUID()); $configPaths = Util::getTimelinePaths(Util::getUID());
$timelineFolders = []; $timelineFolders = [];
$timelinePaths = []; $timelinePaths = [];
@ -134,7 +133,7 @@ class ArchiveController extends GenericApiController
} else { } else {
// file not in archive, put it in there // file not in archive, put it in there
$af = \OCA\Memories\Util::$ARCHIVE_FOLDER; $af = \OCA\Memories\Util::$ARCHIVE_FOLDER;
$destinationPath = Exif::sanitizePath($af.$relativeFilePath); $destinationPath = Util::sanitizePath($af.$relativeFilePath);
} }
// Remove the filename // Remove the filename

View File

@ -5,6 +5,7 @@ namespace OCA\Memories\Controller;
use OCA\Memories\AppInfo\Application; use OCA\Memories\AppInfo\Application;
use OCA\Memories\Db\FsManager; use OCA\Memories\Db\FsManager;
use OCA\Memories\Db\TimelineQuery; use OCA\Memories\Db\TimelineQuery;
use OCA\Memories\Util;
use OCP\AppFramework\AuthPublicShareController; use OCP\AppFramework\AuthPublicShareController;
use OCP\AppFramework\Http\Template\PublicTemplateResponse; use OCP\AppFramework\Http\Template\PublicTemplateResponse;
use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Http\TemplateResponse;
@ -19,7 +20,6 @@ use OCP\IURLGenerator;
use OCP\IUserSession; use OCP\IUserSession;
use OCP\Share\IManager as IShareManager; use OCP\Share\IManager as IShareManager;
use OCP\Share\IShare; use OCP\Share\IShare;
use OCP\Util;
class PublicController extends AuthPublicShareController class PublicController extends AuthPublicShareController
{ {
@ -110,7 +110,7 @@ class PublicController extends AuthPublicShareController
\OC_User::setIncognitoMode(true); \OC_User::setIncognitoMode(true);
// Scripts // Scripts
Util::addScript($this->appName, 'memories-main'); \OCP\Util::addScript($this->appName, 'memories-main');
PageController::provideCommonInitialState($this->initialState); PageController::provideCommonInitialState($this->initialState);
// Share info // Share info
@ -125,7 +125,7 @@ class PublicController extends AuthPublicShareController
// Add OG metadata // Add OG metadata
$params = ['token' => $this->getToken()]; $params = ['token' => $this->getToken()];
$url = $this->urlGenerator->linkToRouteAbsolute('memories.Public.showShare', $params); $url = $this->urlGenerator->linkToRouteAbsolute('memories.Public.showShare', $params);
\OCA\Memories\Util::addOgMetadata($node, $node->getName(), $url, $params); Util::addOgMetadata($node, $node->getName(), $url, $params);
// Render the template // Render the template
$response = new PublicTemplateResponse($this->appName, 'main'); $response = new PublicTemplateResponse($this->appName, 'main');
@ -195,7 +195,7 @@ class PublicController extends AuthPublicShareController
// Get the user's folders path // Get the user's folders path
$foldersPath = $this->config->getUserValue($user->getUID(), Application::APPNAME, 'foldersPath', ''); $foldersPath = $this->config->getUserValue($user->getUID(), Application::APPNAME, 'foldersPath', '');
$foldersPath = $foldersPath ?: '/'; $foldersPath = $foldersPath ?: '/';
$foldersPath = \OCA\Memories\Exif::sanitizePath($foldersPath); $foldersPath = Util::sanitizePath($foldersPath);
// Check if relPath starts with foldersPath // Check if relPath starts with foldersPath
if (0 !== strpos($relPath, $foldersPath)) { if (0 !== strpos($relPath, $foldersPath)) {

View File

@ -26,7 +26,6 @@ namespace OCA\Memories\Db;
use OC\Files\Search\SearchComparison; use OC\Files\Search\SearchComparison;
use OC\Files\Search\SearchQuery; use OC\Files\Search\SearchQuery;
use OCA\Memories\Exceptions; use OCA\Memories\Exceptions;
use OCA\Memories\Exif;
use OCA\Memories\Util; use OCA\Memories\Util;
use OCP\Files\File; use OCP\Files\File;
use OCP\Files\Folder; use OCP\Files\Folder;
@ -103,13 +102,13 @@ class FsManager
try { try {
if (null !== $folderPath) { if (null !== $folderPath) {
$folder = $userFolder->get(Exif::sanitizePath($folderPath)); $folder = $userFolder->get(Util::sanitizePath($folderPath));
$root->addFolder($folder); $root->addFolder($folder);
} else { } else {
// Get timeline paths // Get timeline paths
$paths = Exif::getTimelinePaths($uid); $paths = Util::getTimelinePaths($uid);
if ($path = $this->request->getParam('timelinePath', null)) { if ($path = $this->request->getParam('timelinePath', null)) {
$paths = [Exif::sanitizePath($path)]; $paths = [Util::sanitizePath($path)];
} }
// Combined etag, for cache invalidation. // Combined etag, for cache invalidation.

View File

@ -7,7 +7,6 @@ namespace OCA\Memories;
use OCA\Memories\AppInfo\Application; use OCA\Memories\AppInfo\Application;
use OCA\Memories\Service\BinExt; use OCA\Memories\Service\BinExt;
use OCP\Files\File; use OCP\Files\File;
use OCP\IConfig;
class Exif class Exif
{ {
@ -65,35 +64,6 @@ class Exif
} }
} }
/**
* Get list of timeline paths as array.
*/
public static function getTimelinePaths(string $uid): array
{
$config = \OC::$server->get(IConfig::class);
$paths = $config->getUserValue($uid, Application::APPNAME, 'timelinePath', null) ?? 'Photos/';
return array_map(fn ($p) => self::sanitizePath(trim($p)), explode(';', $paths));
}
/**
* Sanitize a path to keep only ASCII characters and special characters.
*/
public static function sanitizePath(string $path)
{
$path = mb_ereg_replace('([^\\w\\s\\d\\-_~,;:!@#$&*{}\[\]\'\\[\\]\\(\\).\\\/])', '', $path);
$path = mb_ereg_replace('\/\/+', '/', $path); // remove extra slashes
return $path;
}
/**
* Remove any leading slash present on the path.
*/
public static function removeLeadingSlash(string $path)
{
return mb_ereg_replace('~^/+~', '', $path);
}
/** /**
* Get exif data as a JSON object from a Nextcloud file. * Get exif data as a JSON object from a Nextcloud file.
*/ */

View File

@ -83,7 +83,7 @@ class Index
} elseif ('1' === $mode || '0' === $mode) { // everything (or nothing) } elseif ('1' === $mode || '0' === $mode) { // everything (or nothing)
$paths = ['/']; $paths = ['/'];
} elseif ('2' === $mode) { // timeline } elseif ('2' === $mode) { // timeline
$paths = \OCA\Memories\Exif::getTimelinePaths($uid); $paths = Util::getTimelinePaths($uid);
} elseif ('3' === $mode) { // custom } elseif ('3' === $mode) { // custom
$paths = [Util::getSystemConfig('memories.index.path')]; $paths = [Util::getSystemConfig('memories.index.path')];
} else { } else {

View File

@ -7,6 +7,7 @@ namespace OCA\Memories;
use OC\Files\Search\SearchBinaryOperator; use OC\Files\Search\SearchBinaryOperator;
use OC\Files\Search\SearchComparison; use OC\Files\Search\SearchComparison;
use OC\Files\Search\SearchQuery; use OC\Files\Search\SearchQuery;
use OCA\Memories\AppInfo\Application;
use OCP\App\IAppManager; use OCP\App\IAppManager;
use OCP\Files\Node; use OCP\Files\Node;
use OCP\Files\Search\ISearchBinaryOperator; use OCP\Files\Search\ISearchBinaryOperator;
@ -282,6 +283,27 @@ class Util
return self::getSystemConfig('memories.gis_type'); return self::getSystemConfig('memories.gis_type');
} }
/**
* Get list of timeline paths as array.
*/
public static function getTimelinePaths(string $uid): array
{
$config = \OC::$server->get(IConfig::class);
$paths = $config->getUserValue($uid, Application::APPNAME, 'timelinePath', null) ?? 'Photos/';
return array_map(fn ($p) => self::sanitizePath(trim($p)), explode(';', $paths));
}
/**
* Sanitize a path to keep only ASCII characters and special characters.
*/
public static function sanitizePath(string $path)
{
$path = mb_ereg_replace('([^\\w\\s\\d\\-_~,;:!@#$&*{}\[\]\'\\[\\]\\(\\).\\\/])', '', $path);
return mb_ereg_replace('\/\/+', '/', $path); // remove extra slashes
}
/** /**
* Get a system config key with the correct default. * Get a system config key with the correct default.
* *