Skip to content

Commit

Permalink
fix: ensure that notes are not stored inside a shared folder
Browse files Browse the repository at this point in the history
  • Loading branch information
DeepDiver1975 committed Sep 16, 2024
1 parent 5b236f3 commit b248957
Showing 1 changed file with 41 additions and 22 deletions.
63 changes: 41 additions & 22 deletions service/notesservice.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace OCA\Notes\Service;

use OCP\IConfig;
use OCP\IL10N;
use OCP\Files\IRootFolder;
use OCP\Files\Folder;
Expand All @@ -23,16 +24,21 @@
* @package OCA\Notes\Service
*/
class NotesService {
/** @var IL10N */
private $l10n;
/** @var IRootFolder */
private $root;
/** @var IConfig */
private $config;

/**
* @param IRootFolder $root
* @param IL10N $l10n
*/
public function __construct(IRootFolder $root, IL10N $l10n) {
public function __construct(IRootFolder $root, IL10N $l10n, IConfig $config) {
$this->root = $root;
$this->l10n = $l10n;
$this->config = $config;
}

/**
Expand All @@ -49,7 +55,7 @@ public function getAll($userId) {
}
}
$tagger = \OC::$server->getTagManager()->load('files');
if ($tagger==null) {
if ($tagger == null) {
$tags = [];
} else {
$tags = $tagger->getTagsForObjects(\array_keys($filesById));
Expand Down Expand Up @@ -80,7 +86,7 @@ public function get($id, $userId) {

private function getTags($id) {
$tagger = \OC::$server->getTagManager()->load('files');
if ($tagger==null) {
if ($tagger == null) {
$tags = [];
} else {
$tags = $tagger->getTagsForObjects([$id]);
Expand Down Expand Up @@ -138,7 +144,9 @@ public function update($id, $content, $userId) {

// generate filename if there were collisions
$currentFilePath = $file->getPath();
$basePath = '/' . $userId . '/files/Notes/';
$notesRoot = $this->config->getUserValue($userId, 'notes', 'notesRoot', 'Notes');

$basePath = '/' . $userId . "/files/$notesRoot/";
$fileExtension = \pathinfo($file->getName(), PATHINFO_EXTENSION);
$newFilePath = $basePath . $this->generateFileName($folder, $title, $fileExtension, $id);

Expand Down Expand Up @@ -209,12 +217,23 @@ private function getFileById($folder, $id) {
* @return Folder
*/
private function getFolderForUser($userId) {
$path = '/' . $userId . '/files/Notes';
if ($this->root->nodeExists($path)) {
$folder = $this->root->get($path);
} else {
$folder = $this->root->newFolder($path);
$notesRoot = $this->config->getUserValue($userId, 'notes', 'notesRoot', 'Notes');
$userFolder = $this->root->getUserFolder($userId);

if (!$userFolder->nodeExists($notesRoot)) {
return $userFolder->newFolder($notesRoot);
}

$folder = $userFolder->get($notesRoot);
$isFolder = $folder->getType() === 'dir';
$isShared = $folder->isShared();
if (!$isFolder || $isShared) {
$userFolder = $this->root->getUserFolder($userId);
$newName = $userFolder->getNonExistingName('Notes');
$folder = $userFolder->newFolder($newName);
$this->config->setUserValue($userId, 'notes', 'notesRoot', $newName);
}

return $folder;
}

Expand All @@ -238,21 +257,21 @@ private function generateFileName(Folder $folder, $title, $extension, $id) {
// need to handle file collisions if it is the filename did not change
if (!$folder->nodeExists($path) || $folder->get($path)->getId() === $id) {
return $path;
}

// increments name (2) to name (3)
$match = \preg_match('/\((?P<id>\d+)\)$/', $title, $matches);
if ($match) {
$newId = ((int) $matches['id']) + 1;
$newTitle = \preg_replace(
'/(.*)\s\((\d+)\)$/',
'$1 (' . $newId . ')',
$title
);
} else {
// increments name (2) to name (3)
$match = \preg_match('/\((?P<id>\d+)\)$/', $title, $matches);
if ($match) {
$newId = ((int) $matches['id']) + 1;
$newTitle = \preg_replace(
'/(.*)\s\((\d+)\)$/',
'$1 (' . $newId . ')',
$title
);
} else {
$newTitle = $title . ' (2)';
}
return $this->generateFileName($folder, $newTitle, $extension, $id);
$newTitle = $title . ' (2)';
}
return $this->generateFileName($folder, $newTitle, $extension, $id);
}

/**
Expand Down

0 comments on commit b248957

Please sign in to comment.