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

show notes inside subdirectories #249

Closed
wants to merge 1 commit into from
Closed
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
84 changes: 31 additions & 53 deletions service/notesservice.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,20 @@
* @author Bernhard Posselt <dev@bernhard-posselt.com>
* @copyright Bernhard Posselt 2012, 2014
*/

namespace OCA\Notes\Service;

use OCP\Files\FileInfo;
use OCP\IL10N;
use OCP\Files\IRootFolder;
use OCP\Files\Folder;

use OCA\Notes\Db\Note;

/**
* Class NotesService
*
* @package OCA\Notes\Service
*/
class NotesService {

private $l10n;
private $root;

/**
* @param IRootFolder $root
* @param IL10N $l10n
Expand All @@ -35,37 +30,28 @@ public function __construct (IRootFolder $root, IL10N $l10n) {
$this->root = $root;
$this->l10n = $l10n;
}


/**
* @param string $userId
* @return array with all notes in the current directory
*/
public function getAll ($userId){
$folder = $this->getFolderForUser($userId);
$files = $folder->getDirectoryListing();
$notes = $this->gatherNoteFiles($this->getFolderForUser($userId));
$filesById = [];
foreach($files as $file) {
if($this->isNote($file)) {
$filesById[$file->getId()] = $file;
}
foreach($notes as $note) {
$filesById[$note->getId()] = $note;
}
$tagger = \OC::$server->getTagManager()->load('files');
if($tagger==null) {
$tags = [];
} else {
$tags = $tagger->getTagsForObjects(array_keys($filesById));
}

$notes = [];
foreach($filesById as $id=>$file) {
$notes[] = Note::fromFile($file, array_key_exists($id, $tags) ? $tags[$id] : []);
}

return $notes;
}


/**
* Used to get a single note by id
* @param int $id the id of the note to get
Expand All @@ -77,7 +63,6 @@ public function get ($id, $userId) {
$folder = $this->getFolderForUser($userId);
return Note::fromFile($this->getFileById($folder, $id), $this->getTags($id));
}

private function getTags ($id) {
$tagger = \OC::$server->getTagManager()->load('files');
if($tagger==null) {
Expand All @@ -87,7 +72,6 @@ private function getTags ($id) {
}
return array_key_exists($id, $tags) ? $tags[$id] : [];
}

/**
* Creates a note and returns the empty note
* @param string $userId
Expand All @@ -97,17 +81,13 @@ private function getTags ($id) {
public function create ($userId) {
$title = $this->l10n->t('New note');
$folder = $this->getFolderForUser($userId);

// check new note exists already and we need to number it
// pass -1 because no file has id -1 and that will ensure
// to only return filenames that dont yet exist
$path = $this->generateFileName($folder, $title, "txt", -1);
$file = $folder->newFile($path);

return Note::fromFile($file);
}


/**
* Updates a note. Be sure to check the returned note since the title is
* dynamically generated and filename conflicts are resolved
Expand All @@ -118,42 +98,35 @@ public function create ($userId) {
* @return \OCA\Notes\Db\Note the updated note
*/
public function update ($id, $content, $userId){
$folder = $this->getFolderForUser($userId);
$file = $this->getFileById($folder, $id);

$notesFolder = $this->getFolderForUser($userId);
$file = $this->getFileById($notesFolder, $id);
$folder = $file->getParent();
// generate content from the first line of the title
$splitContent = explode("\n", $content);
$title = $splitContent[0];

if(!$title) {
$title = $this->l10n->t('New note');
}

// prevent directory traversal
$title = str_replace(array('/', '\\'), '', $title);
// remove hash and space characters from the beginning of the filename
// in case of markdown
$title = ltrim($title, ' #');
// using a maximum of 100 chars should be enough
$title = mb_substr($title, 0, 100, "UTF-8");

// generate filename if there were collisions
$currentFilePath = $file->getPath();
$basePath = '/' . $userId . '/files/Notes/';
$basePath = pathinfo($file->getPath(), PATHINFO_DIRNAME);
\OCP\Util::writeLog('notes', $basePath, \OCP\Util::ERROR);
$fileExtension = pathinfo($file->getName(), PATHINFO_EXTENSION);
$newFilePath = $basePath . $this->generateFileName($folder, $title, $fileExtension, $id);

$newFilePath = $basePath . '/' . $this->generateFileName($folder, $title, $fileExtension, $id);
// if the current path is not the new path, the file has to be renamed
if($currentFilePath !== $newFilePath) {
$file->move($newFilePath);
}

$file->putContent($content);

return Note::fromFile($file, $this->getTags($id));
}


/**
* Set or unset a note as favorite.
* @param int $id the id of the note used to update
Expand All @@ -172,12 +145,9 @@ public function favorite ($id, $favorite, $userId){
$tagger->addToFavorites($id);
else
$tagger->removeFromFavorites($id);

$tags = $tagger->getTagsForObjects([$id]);
return in_array(\OC\Tags::TAG_FAVORITE, $tags[$id]);
}


/**
* Deletes a note
* @param int $id the id of the note which should be deleted
Expand All @@ -190,8 +160,6 @@ public function delete ($id, $userId) {
$file = $this->getFileById($folder, $id);
$file->delete();
}


/**
* @param Folder $folder
* @param int $id
Expand All @@ -200,14 +168,11 @@ public function delete ($id, $userId) {
*/
private function getFileById ($folder, $id) {
$file = $folder->getById($id);

if(count($file) <= 0 || !$this->isNote($file[0])) {
throw new NoteDoesNotExistException();
}
return $file[0];
}


/**
* @param string $userId the user id
* @return Folder
Expand All @@ -221,8 +186,6 @@ private function getFolderForUser ($userId) {
}
return $folder;
}


/**
* get path of file and the title.txt and check if they are the same
* file. If not the title needs to be renamed
Expand All @@ -238,7 +201,6 @@ private function getFolderForUser ($userId) {
*/
private function generateFileName (Folder $folder, $title, $extension, $id) {
$path = $title . '.' . $extension;

// if file does not exist, that name has not been taken. Similar we don't
// need to handle file collisions if it is the filename did not change
if (!$folder->nodeExists($path) || $folder->get($path)->getId() === $id) {
Expand All @@ -256,7 +218,26 @@ private function generateFileName (Folder $folder, $title, $extension, $id) {
return $this->generateFileName($folder, $newTitle, $extension, $id);
}
}

/**
* gather note files in given directory and all subdirectories
* @param Folder $folder
* @return array
*/
private function gatherNoteFiles ($folder) {
$notes = [];
$nodes = $folder->getDirectoryListing();
foreach($nodes as $node) {
\OCP\Util::writeLog('notes', $node->getType(), \OCP\Util::ERROR);
if($node->getType() === FileInfo::TYPE_FOLDER) {
$notes = array_merge($notes, $this->gatherNoteFiles($node));
continue;
}
if($this->isNote($node)) {
$notes[] = $node;
}
}
return $notes;
}
/**
* test if file is a note
*
Expand All @@ -265,14 +246,11 @@ private function generateFileName (Folder $folder, $title, $extension, $id) {
*/
private function isNote($file) {
$allowedExtensions = ['txt', 'org', 'markdown', 'md', 'note'];

if($file->getType() !== 'file') return false;
if(!in_array(
pathinfo($file->getName(), PATHINFO_EXTENSION),
$allowedExtensions
)) return false;

return true;
}

}
}