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

NotesService::deleteEmptyFolder()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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