Passed
Pull Request — master (#381)
by korelstar
01:49
created

NotesService::getOrCreateFolder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace OCA\Notes\Service;
4
5
use OCP\IL10N;
6
use OCP\ILogger;
7
use OCP\Encryption\Exceptions\GenericEncryptionException;
8
use OCP\Files\IRootFolder;
9
use OCP\Files\File;
10
use OCP\Files\FileInfo;
11
use OCP\Files\Folder;
12
use OCA\Notes\Db\Note;
13
use OCA\Notes\Service\SettingsService;
14
use OCA\Notes\Util\NoteUtil;
15
use OCP\IConfig;
16
use OCP\IUserSession;
17
18
/**
19
 * Class NotesService
20
 *
21
 * @package OCA\Notes\Service
22
 */
23
class NotesService {
24
25
	private $l10n;
26
	private $root;
27
	private $logger;
28
	private $config;
29
	private $settings;
30
	private $noteUtil;
31
	private $appName;
32
33
	/**
34
	 * @param IRootFolder $root
35
	 * @param IL10N $l10n
36
	 * @param ILogger $logger
37
	 * @param IConfig $config
38
	 * @param SettingsService $settings
39
	 * @param NoteUtil $noteUtil
40
	 * @param String $appName
41
	 */
42
	public function __construct(
43
		IRootFolder $root,
44
		IL10N $l10n,
45
		ILogger $logger,
46
		IConfig $config,
47
		SettingsService $settings,
48
		NoteUtil $noteUtil,
49
		$appName
50
	) {
51
		$this->root = $root;
52
		$this->l10n = $l10n;
53
		$this->logger = $logger;
54
		$this->config = $config;
55
		$this->settings = $settings;
56
		$this->noteUtil = $noteUtil;
57
		$this->appName = $appName;
58
	}
59
60
61
	/**
62
	 * @param string $userId
63
	 * @return array with all notes in the current directory
64
	 */
65
	public function getAll($userId, $onlyMeta = false) {
66
		$notesFolder = $this->getFolderForUser($userId);
67
		$notes = $this->noteUtil->gatherNoteFiles($notesFolder);
68
		$filesById = [];
69
		foreach ($notes as $note) {
70
			$filesById[$note->getId()] = $note;
71
		}
72
		$tagger = \OC::$server->getTagManager()->load('files');
73
		if ($tagger===null) {
74
			$tags = [];
75
		} else {
76
			$tags = $tagger->getTagsForObjects(array_keys($filesById));
77
		}
78
79
		$notes = [];
80
		foreach ($filesById as $id => $file) {
81
			$notes[] = $this->getNote($file, $notesFolder, array_key_exists($id, $tags) ? $tags[$id] : [], $onlyMeta);
82
		}
83
84
		return $notes;
85
	}
86
87
88
	/**
89
	 * Used to get a single note by id
90
	 * @param int $id the id of the note to get
91
	 * @param string $userId
92
	 * @throws NoteDoesNotExistException if note does not exist
93
	 * @return Note
94
	 */
95
	public function get($id, $userId) : Note {
96
		$folder = $this->getFolderForUser($userId);
97
		return $this->getNote($this->getFileById($folder, $id), $folder, $this->getTags($id));
98
	}
99
100
	private function getTags($id) {
101
		$tagger = \OC::$server->getTagManager()->load('files');
102
		if ($tagger===null) {
103
			$tags = [];
104
		} else {
105
			$tags = $tagger->getTagsForObjects([$id]);
106
		}
107
		return array_key_exists($id, $tags) ? $tags[$id] : [];
108
	}
109
110
	private function getNote(File $file, Folder $notesFolder, $tags = [], $onlyMeta = false) : Note {
111
		$id = $file->getId();
112
		try {
113
			$note = Note::fromFile($file, $notesFolder, $tags, $onlyMeta);
114
		} catch (GenericEncryptionException $e) {
115
			$message = $this->l10n->t('Encryption Error').': ('.$file->getName().') '.$e->getMessage();
116
			$note = Note::fromException($message, $file, $notesFolder, array_key_exists($id, $tags) ? $tags[$id] : []);
117
		} catch (\Exception $e) {
118
			$message = $this->l10n->t('Error').': ('.$file->getName().') '.$e->getMessage();
119
			$note = Note::fromException($message, $file, $notesFolder, array_key_exists($id, $tags) ? $tags[$id] : []);
120
		}
121
		return $note;
122
	}
123
124
125
	/**
126
	 * Creates a note and returns the empty note
127
	 * @param string $userId
128
	 * @see update for setting note content
129
	 * @return Note the newly created note
130
	 */
131
	public function create($userId) : Note {
132
		$title = $this->l10n->t('New note');
133
		$folder = $this->getFolderForUser($userId);
134
135
		// check new note exists already and we need to number it
136
		// pass -1 because no file has id -1 and that will ensure
137
		// to only return filenames that dont yet exist
138
		$path = $this->noteUtil->generateFileName($folder, $title, $this->settings->get($userId, 'fileSuffix'), -1);
139
		$file = $folder->newFile($path);
140
141
		// If server-side encryption is activated, the server creates an empty file without signature
142
		// which leads to an GenericEncryptionException('Missing Signature') afterwards.
143
		// Saving a space-char (and removing it later) is a working work-around.
144
		$file->putContent(' ');
145
146
		return $this->getNote($file, $folder);
147
	}
148
149
150
	/**
151
	 * Updates a note. Be sure to check the returned note since the title is
152
	 * dynamically generated and filename conflicts are resolved
153
	 * @param int $id the id of the note used to update
154
	 * @param string|null $content the content which will be written into the note
155
	 * the title is generated from the first line of the content
156
	 * @param string|null $category the category in which the note should be saved
157
	 * @param int $mtime time of the note modification (optional)
158
	 * @throws NoteDoesNotExistException if note does not exist
159
	 * @return \OCA\Notes\Db\Note the updated note
160
	 */
161
	public function update($id, $content, $userId, $category = null, $mtime = 0) : Note {
162
		$notesFolder = $this->getFolderForUser($userId);
163
		$file = $this->getFileById($notesFolder, $id);
164
		$title = $this->noteUtil->getSafeTitleFromContent($content===null ? $file->getContent() : $content);
165
166
		// rename/move file with respect to title/category
167
		// this can fail if access rights are not sufficient or category name is illegal
168
		try {
169
			$this->noteUtil->moveNote($notesFolder, $file, $category, $title);
170
		} catch (\OCP\Files\NotPermittedException $e) {
171
			$err = 'Moving note '.$id.' ('.$title.') to the desired target is not allowed.'
172
				.' Please check the note\'s target category ('.$category.').';
173
			$this->logger->error($err, ['app' => $this->appName]);
174
		} catch (\Exception $e) {
175
			$err = 'Moving note '.$id.' ('.$title.') to the desired target has failed '
176
				.'with a '.get_class($e).': '.$e->getMessage();
177
			$this->logger->error($err, ['app' => $this->appName]);
178
		}
179
180
		if ($content !== null) {
181
			$file->putContent($content);
182
		}
183
184
		if ($mtime) {
185
			$file->touch($mtime);
186
		}
187
188
		return $this->getNote($file, $notesFolder, $this->getTags($id));
189
	}
190
191
	/**
192
	 * Set or unset a note as favorite.
193
	 * @param int $id the id of the note used to update
194
	 * @param boolean $favorite whether the note should be a favorite or not
195
	 * @throws NoteDoesNotExistException if note does not exist
196
	 * @return boolean the new favorite state of the note
197
	 */
198
	public function favorite($id, $favorite, $userId) {
199
		$folder = $this->getFolderForUser($userId);
200
		// check if file is note
201
		$this->getFileById($folder, $id);
202
		$tagger = \OC::$server->getTagManager()->load('files');
203
		if ($favorite) {
204
			$tagger->addToFavorites($id);
205
		} else {
206
			$tagger->removeFromFavorites($id);
207
		}
208
209
		$tags = $tagger->getTagsForObjects([$id]);
210
		return array_key_exists($id, $tags) && in_array(\OC\Tags::TAG_FAVORITE, $tags[$id]);
0 ignored issues
show
Bug introduced by
The type OC\Tags was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
211
	}
212
213
214
	/**
215
	 * Deletes a note
216
	 * @param int $id the id of the note which should be deleted
217
	 * @param string $userId
218
	 * @throws NoteDoesNotExistException if note does not
219
	 * exist
220
	 */
221
	public function delete($id, $userId) {
222
		$notesFolder = $this->getFolderForUser($userId);
223
		$file = $this->getFileById($notesFolder, $id);
224
		$parent = $file->getParent();
225
		$file->delete();
226
		$this->noteUtil->deleteEmptyFolder($notesFolder, $parent);
227
	}
228
229
	/**
230
	 * @param Folder $folder
231
	 * @param int $id
232
	 * @throws NoteDoesNotExistException
233
	 * @return \OCP\Files\File
234
	 */
235
	private function getFileById(Folder $folder, $id) : File {
236
		$file = $folder->getById($id);
237
238
		if (count($file) <= 0 || !($file[0] instanceof File) || !$this->noteUtil->isNote($file[0])) {
239
			throw new NoteDoesNotExistException();
240
		}
241
		return $file[0];
242
	}
243
244
	/**
245
	 * @param string $userId the user id
246
	 * @return boolean true if folder is accessible, or Exception otherwise
247
	 */
248
	public function checkNotesFolder($userId) {
249
		$this->getFolderForUser($userId);
250
		return true;
251
	}
252
253
	/**
254
	 * @param string $userId the user id
255
	 * @return Folder
256
	 */
257
	private function getFolderForUser($userId) : Folder {
258
		// TODO use IRootFolder->getUserFolder()
259
		$path = '/' . $userId . '/files/' . $this->settings->get($userId, 'notesPath');
260
		try {
261
			$folder = $this->noteUtil->getOrCreateFolder($path);
262
		} catch (\Exception $e) {
263
			throw new NotesFolderException($path);
264
		}
265
		return $folder;
266
	}
267
}
268