Skip to content
Open
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
2 changes: 1 addition & 1 deletion appinfo/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

use OCA\RenameWithMetadata\AppInfo\Application;

$app = \OC::$server->query(Application::class);
$app = \OC::$server->get(Application::class);
$app->register();
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
<category>files</category>
<bugs>https://github.com/RCOSDP/nextcloud-rename_with_metadata/issues</bugs>
<dependencies>
<nextcloud min-version="15" max-version="19" />
<nextcloud min-version="25" max-version="26" />
</dependencies>
</info>
21 changes: 9 additions & 12 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,19 @@
class Application extends App {
public const APP_ID = 'rename_with_metadata';

public function __construct(array $urlParams = []) {
public function __construct(array $urlParams=array()) {
parent::__construct(self::APP_ID, $urlParams);
$container = $this->getContainer();

/**
* Controllers
*/
$container->registerService('UserHooks', function() {
$container = $this->getContainer();
$server = $container->getServer();
**/
$container->registerService('UserHooks', function ($c) {
return new UserHooks(
$server->getLogger(),
$server->getRootFolder(),
$server->getDatabaseConnection(),
$server->getUserSession(),
$server->getMountManager()
$c->query('ServerContainer')->getRootFolder(),
$c->query('ServerContainer')->getDatabaseConnection(),
$c->query('ServerContainer')->getUserSession(),
$c->query('ServerContainer')->getMountManager(),
);
});
}
Expand All @@ -36,6 +33,6 @@ public function register() {

public function registerHooks() {
$container = $this->getContainer();
$container->query('UserHooks')->register();
$container->get('UserHooks')->register();
}
}
}
16 changes: 16 additions & 0 deletions lib/Db/Property.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace OCA\RenameWithMetadata\Db;

use OCP\AppFramework\Db\Entity;

class Property extends Entity {
protected $propertypath;
protected $propertyname;
protected $userid;
protected $propertyvalue;
protected $valuetype;
public function __construct() {}
}
41 changes: 41 additions & 0 deletions lib/Db/PropertyMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace OCA\RenameWithMetadata\Db;

use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;

class PropertyMapper extends QBMapper {

public function __construct(IDBConnection $db)
{
parent::__construct($db, 'properties', Property::class);
}

public function findProperties($property_path) {
$qb = $this->db->getQueryBuilder();

$qb->select('*')->from($this->tableName)->where(
$qb->expr()->like(
'propertypath',
$qb->createNamedParameter(
'%' . $this->db->escapeLikeParameter($property_path) . '%',
IQueryBuilder::PARAM_STR
),
IQueryBuilder::PARAM_STR
)
);

try {
$result = $this->findEntities($qb);
} catch (DoesNotExistException $e) {
return null;
}

return $result;
}
}
35 changes: 25 additions & 10 deletions lib/Hooks/UserHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,43 @@

namespace OCA\RenameWithMetadata\Hooks;

use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\Mount\IMountManager;
use OCP\IDBConnection;
use OCP\ILogger;
use OCP\IUserSession;
use OC\Files\Filesystem;
use OC\Files\Node\Node;

use OCA\RenameWithMetadata\Db\PropertyMapper;

use OCA\DAV\DAV\CustomPropertiesBackend;

class UserHooks {

private $logger;
private $rootFolder;
private $databaseConnection;
private $userSession;
private $mountManager;
private $customPropertiesBackend;
private $mapper;

public function __construct(ILogger $logger,
IRootFolder $rootFolder,
public function __construct(IRootFolder $rootFolder,
IDBConnection $databaseConnection,
IUserSession $userSession,
IMountManager $mountManager) {
$this->logger = $logger;
$this->rootFolder = $rootFolder;
$this->databaseConnection = $databaseConnection;
$this->userSession = $userSession;
$this->mountManager = $mountManager;
$this->mapper = new PropertyMapper($databaseConnection);

}

public function register() {
$callback = function(Node $source, Node $target) {
$objectTree = new \OCA\DAV\Connector\Sabre\ObjectTree();
$userFolder = \OC::$server->getUserFolder();
$view = \OC\Files\Filesystem::getView();
$view = Filesystem::getView();

if ($userFolder instanceof Folder && $userFolder->getPath() === $view->getRoot()) {
$rootInfo = $userFolder;
Expand Down Expand Up @@ -72,9 +73,23 @@ public function register() {
$targetPath = $target->getPath();
$targetPath = substr($targetPath, $userPrefixLength);
$targetPath = substr($targetPath, $filesPrefixLength);

$this->customPropertiesBackend->delete($targetPath);
$this->customPropertiesBackend->move($sourcePath, $targetPath);
$dump_sourcePath = 'files' . $userPrefix . '/' . $sourcePath;
$dump_sourcePathLength = strlen($dump_sourcePath);
$dump_targetPath = 'files' . $userPrefix . '/' . $targetPath;
$entities = $this->mapper->findProperties($dump_sourcePath);

if (!empty($entities)) {
foreach ($entities as $entity) {
$property_path = $entity->getPropertypath();
if (str_contains($property_path, $dump_sourcePath)) {
$value = substr($property_path, $dump_sourcePathLength);
$new_sourcePath = $dump_sourcePath . $value;
$new_targetPath = $dump_targetPath . $value;
$this->customPropertiesBackend->delete($targetPath);
$this->customPropertiesBackend->move($new_sourcePath, $new_targetPath);
}
}
}
};
$this->rootFolder->listen('\OC\Files', 'postRename', $callback);
}
Expand Down