Check external paths (#30)

pull/37/head
Varun Patil 2022-09-13 10:39:38 -07:00
parent 4d5431c6b4
commit be961b3fb4
5 changed files with 64 additions and 20 deletions

View File

@ -210,11 +210,12 @@ class Index extends Command {
\OC_Util::tearDownFS();
\OC_Util::setupFS($user->getUID());
$userFolder = $this->rootFolder->getUserFolder($user->getUID());
$this->parseFolder($userFolder, $refresh);
$uid = $user->getUID();
$userFolder = $this->rootFolder->getUserFolder($uid);
$this->parseFolder($userFolder, $refresh, $uid);
}
private function parseFolder(Folder &$folder, bool &$refresh): void {
private function parseFolder(Folder &$folder, bool &$refresh, string $uid): void {
try {
$folderPath = $folder->getPath();
@ -230,9 +231,9 @@ class Index extends Command {
foreach ($nodes as &$node) {
if ($node instanceof Folder) {
$this->parseFolder($node, $refresh);
$this->parseFolder($node, $refresh, $uid);
} elseif ($node instanceof File) {
$this->parseFile($node, $refresh);
$this->parseFile($node, $refresh, $uid);
}
}
} catch (StorageNotAvailableException $e) {
@ -243,8 +244,8 @@ class Index extends Command {
}
}
private function parseFile(File &$file, bool &$refresh): void {
$res = $this->timelineWrite->processFile($file, $refresh);
private function parseFile(File &$file, bool &$refresh, string $uid): void {
$res = $this->timelineWrite->processFile($file, $refresh, $uid);
if ($res === 2) {
$this->nProcessed++;
} else if ($res === 1) {

View File

@ -70,10 +70,14 @@ trait TimelineQueryDay {
array $queryTransforms = []
): array {
// Filter by path starting with timeline path
$path = "files" . Exif::getPhotosPath($config, $user) . "%";
$configPath = Exif::getPhotosPath($config, $user);
$likeHome = Exif::removeExtraSlash("files/" . $configPath . "%");
$likeExt = Exif::removeLeadingSlash(Exif::removeExtraSlash($configPath . "%"));
$query = $this->connection->getQueryBuilder();
$this->makeQueryDay($query, $dayId, $user, $query->expr()->like(
'f.path', $query->createNamedParameter($path)
$this->makeQueryDay($query, $dayId, $user, $query->expr()->orX(
$query->expr()->like('f.path', $query->createNamedParameter($likeHome)),
$query->expr()->like('f.path', $query->createNamedParameter($likeExt)),
));
// Filter by UID

View File

@ -55,10 +55,14 @@ trait TimelineQueryDays {
): array {
// Filter by path starting with timeline path
$path = "files" . Exif::getPhotosPath($config, $user) . "%";
$configPath = Exif::getPhotosPath($config, $user);
$likeHome = Exif::removeExtraSlash("files/" . $configPath . "%");
$likeExt = Exif::removeLeadingSlash(Exif::removeExtraSlash($configPath . "%"));
$query = $this->connection->getQueryBuilder();
$this->makeQueryDays($query, $query->expr()->like(
'f.path', $query->createNamedParameter($path)
$this->makeQueryDays($query, $query->expr()->orX(
$query->expr()->like('f.path', $query->createNamedParameter($likeHome)),
$query->expr()->like('f.path', $query->createNamedParameter($likeExt)),
));
// Filter by user

View File

@ -36,7 +36,11 @@ class TimelineWrite {
* @param File $file
* @return int 2 if processed, 1 if skipped, 0 if not valid
*/
public function processFile(File &$file, bool $force=false): int {
public function processFile(
File &$file,
bool $force=false,
string $fallbackUid=null
): int {
// There is no easy way to UPSERT in a standard SQL way, so just
// do multiple calls. The worst that can happen is more updates,
// but that's not a big deal.
@ -51,9 +55,17 @@ class TimelineWrite {
// Get parameters
$mtime = $file->getMtime();
$user = $file->getOwner()->getUID();
$fileId = $file->getId();
$uid = $file->getOwner()->getUID();
if (empty($uid)) {
// Most likely this is an extrnal mount (that may be shared between users)
$uid = $fallbackUid;
}
if (empty($uid)) {
return 0;
}
// Check if need to update
$query = $this->connection->getQueryBuilder();
$query->select('fileid', 'mtime')
@ -61,7 +73,7 @@ class TimelineWrite {
->where(
$query->expr()->andX(
$query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)),
$query->expr()->eq('uid', $query->createNamedParameter($user, IQueryBuilder::PARAM_STR)),
$query->expr()->eq('uid', $query->createNamedParameter($uid, IQueryBuilder::PARAM_STR)),
));
$prevRow = $query->executeQuery()->fetch();
if ($prevRow && !$force && intval($prevRow['mtime']) === $mtime) {
@ -89,7 +101,7 @@ class TimelineWrite {
->where(
$query->expr()->andX(
$query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)),
$query->expr()->eq('uid', $query->createNamedParameter($user, IQueryBuilder::PARAM_STR)),
$query->expr()->eq('uid', $query->createNamedParameter($uid, IQueryBuilder::PARAM_STR)),
));
$query->executeStatement();
} else {
@ -98,7 +110,7 @@ class TimelineWrite {
$query->insert('memories')
->values([
'fileid' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT),
'uid' => $query->createNamedParameter($user, IQueryBuilder::PARAM_STR),
'uid' => $query->createNamedParameter($uid, IQueryBuilder::PARAM_STR),
'dayid' => $query->createNamedParameter($dayId, IQueryBuilder::PARAM_INT),
'datetaken' => $query->createNamedParameter($dateTaken, IQueryBuilder::PARAM_STR),
'mtime' => $query->createNamedParameter($mtime, IQueryBuilder::PARAM_INT),

View File

@ -64,9 +64,32 @@ class Exif {
public static function getPhotosPath(IConfig &$config, string &$userId) {
$p = $config->getUserValue($userId, Application::APPNAME, 'timelinePath', '');
if (empty($p)) {
return '/Photos/';
return 'Photos/';
}
return $p;
return self::sanitizePath($p);
}
/**
* Sanitize a path to keep only ASCII characters and special characters.
* @param string $path
*/
public static function sanitizePath(string &$path) {
$path = preg_replace('/[^a-zA-Z0-9\/\.\-\_]/', '', $path);
return $path;
}
/**
* Keep only one slash if multiple repeating
*/
public static function removeExtraSlash(string $path) {
return preg_replace('~/+~', '/', $path);
}
/**
* Remove any leading slash present on the path
*/
public static function removeLeadingSlash(string $path) {
return preg_replace('~^/+~', '', $path);
}
/**