Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat custom propagator #2440

Closed
Closed
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
127 changes: 127 additions & 0 deletions lib/Mount/GroupFolderPropagator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023, Grégory Brousse <[email protected]>
*
* @author Grégory Brousse <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\GroupFolders\Mount;

use OC\Files\Cache\Propagator;
use OCP\IDBConnection;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\ILogger;

class GroupFolderPropagator extends Propagator {
private $logger;
/**
* @var IDBConnection
*/
private $connection;

/**
*
* @var int
*/
private $folderId;


/**
* @param \OCA\GroupFolders\Mount\GroupFolderStorage $storage
* @param \OCP\IDBConnection $connection
* @param \OCP\ILogger $logger
*/
public function __construct(\OC\Files\Storage\Storage $storage, IDBConnection $connection, ILogger $logger) {
parent::__construct($storage, $connection);
$this->connection = $connection;
$this->folderId = $storage->getFolderId();
$this->logger = $logger;
}

/**
* get paths of parent directories
*
* @param string $pathOrigin
* @return array
*/
protected function getParents($pathOrigin) {
$groupFolderPath = $this->getGroupFolderMountPoint($this->folderId);
if(!strstr($pathOrigin,$groupFolderPath)){
$path=str_replace('//','/',$groupFolderPath.'/'.$pathOrigin);
}else{
$path = $pathOrigin;
}
$parents = parent::getParents($path);
$parentsGroupFolders = $this->getGroupFolderParents();
$fullParents = array_merge($parents,$parentsGroupFolders);
$this->logger->debug('GroupFolders::Propagator',[
'pathOrigin'=>$pathOrigin,
'path'=>$path,
'groupFolderPath'=>$groupFolderPath,
'parents'=>$parents,
'parentsGroupFolders'=>$parentsGroupFolders,
'fullParents'=>$fullParents,
]);
return $fullParents;
}

/**
* get all parent groupfolders of the current groupfolder
*
* @return array
*/
protected function getGroupFolderParents(){
// Get folder mountpoint
$query = $this->connection->getQueryBuilder();
$query->select('mount_point')
->from('group_folders')
->where($query->expr()->eq('folder_id', $query->createNamedParameter($this->folderId)));
$mountPoint = $query->execute()->fetchOne();
$parentsMountPoints = [];
while($mountPoint != '.'){
$parentMountPoint = dirname($mountPoint);
if($parentMountPoint != '.'){
$parentsMountPoints[]=$parentMountPoint;
}
$mountPoint = $parentMountPoint;
}
$query->select('folder_id')
->from('group_folders')
->where($query->expr()->in('mount_point',$query->createNamedParameter($parentsMountPoints, IQueryBuilder::PARAM_STR_ARRAY)));

$parentsIds = $query->execute()->fetchAll();
return array_map(function($folderId){
return $this->getGroupFolderMountPoint($folderId['folder_id']);
}
,$parentsIds);
}

/**
* get the groupfolder mount point
*
* @param int $groupFolderId
* @return string
*/
protected function getGroupFolderMountPoint($groupFolderId){
return '__groupfolders/'.$groupFolderId;
}

}
17 changes: 17 additions & 0 deletions lib/Mount/GroupFolderStorage.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
/**
* @copyright Copyright (c) 2018 Robin Appelman <[email protected]>
* @copyright Copyright (c) 2023, Grégory Brousse <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
Expand Down Expand Up @@ -70,6 +71,22 @@ public function getCache($path = '', $storage = null) {
return $this->cache;
}

/**
* get a propagator instance for the cache
*
* @param \OC\Files\Storage\Storage (optional) the storage to pass to the watcher
* @return \OC\Files\Cache\Propagator
*/
public function getPropagator($storage = null) {
if (!$storage) {
$storage = $this;
}
if (!isset($this->propagator)) {
$this->propagator = new GroupFolderPropagator($storage, \OC::$server->getDatabaseConnection(), \OC::$server->getLogger());
}
return $this->propagator;
}

public function getScanner($path = '', $storage = null) {
/** @var \OC\Files\Storage\Storage $storage */
if (!$storage) {
Expand Down