1111
1212namespace OCA \Notes \Service ;
1313
14+ use OCP \IConfig ;
1415use OCP \IL10N ;
1516use OCP \Files \IRootFolder ;
1617use OCP \Files \Folder ;
2324 * @package OCA\Notes\Service
2425 */
2526class NotesService {
27+ /** @var IL10N */
2628 private $ l10n ;
29+ /** @var IRootFolder */
2730 private $ root ;
31+ /** @var IConfig */
32+ private $ config ;
2833
2934 /**
3035 * @param IRootFolder $root
3136 * @param IL10N $l10n
3237 */
33- public function __construct (IRootFolder $ root , IL10N $ l10n ) {
38+ public function __construct (IRootFolder $ root , IL10N $ l10n, IConfig $ config ) {
3439 $ this ->root = $ root ;
3540 $ this ->l10n = $ l10n ;
41+ $ this ->config = $ config ;
3642 }
3743
3844 /**
@@ -49,7 +55,7 @@ public function getAll($userId) {
4955 }
5056 }
5157 $ tagger = \OC ::$ server ->getTagManager ()->load ('files ' );
52- if ($ tagger== null ) {
58+ if ($ tagger == null ) {
5359 $ tags = [];
5460 } else {
5561 $ tags = $ tagger ->getTagsForObjects (\array_keys ($ filesById ));
@@ -80,7 +86,7 @@ public function get($id, $userId) {
8086
8187 private function getTags ($ id ) {
8288 $ tagger = \OC ::$ server ->getTagManager ()->load ('files ' );
83- if ($ tagger== null ) {
89+ if ($ tagger == null ) {
8490 $ tags = [];
8591 } else {
8692 $ tags = $ tagger ->getTagsForObjects ([$ id ]);
@@ -138,7 +144,9 @@ public function update($id, $content, $userId) {
138144
139145 // generate filename if there were collisions
140146 $ currentFilePath = $ file ->getPath ();
141- $ basePath = '/ ' . $ userId . '/files/Notes/ ' ;
147+ $ notesRoot = $ this ->config ->getUserValue ($ userId , 'notes ' , 'notesRoot ' , 'Notes ' );
148+
149+ $ basePath = '/ ' . $ userId . "/files/ $ notesRoot/ " ;
142150 $ fileExtension = \pathinfo ($ file ->getName (), PATHINFO_EXTENSION );
143151 $ newFilePath = $ basePath . $ this ->generateFileName ($ folder , $ title , $ fileExtension , $ id );
144152
@@ -209,12 +217,23 @@ private function getFileById($folder, $id) {
209217 * @return Folder
210218 */
211219 private function getFolderForUser ($ userId ) {
212- $ path = '/ ' . $ userId . '/files/Notes ' ;
213- if ($ this ->root ->nodeExists ($ path )) {
214- $ folder = $ this ->root ->get ($ path );
215- } else {
216- $ folder = $ this ->root ->newFolder ($ path );
220+ $ notesRoot = $ this ->config ->getUserValue ($ userId , 'notes ' , 'notesRoot ' , 'Notes ' );
221+ $ userFolder = $ this ->root ->getUserFolder ($ userId );
222+
223+ if (!$ userFolder ->nodeExists ($ notesRoot )) {
224+ return $ userFolder ->newFolder ($ notesRoot );
225+ }
226+
227+ $ folder = $ userFolder ->get ($ notesRoot );
228+ $ isFolder = $ folder ->getType () === 'dir ' ;
229+ $ isShared = $ folder ->isShared ();
230+ if (!$ isFolder || $ isShared ) {
231+ $ userFolder = $ this ->root ->getUserFolder ($ userId );
232+ $ newName = $ userFolder ->getNonExistingName ('Notes ' );
233+ $ folder = $ userFolder ->newFolder ($ newName );
234+ $ this ->config ->setUserValue ($ userId , 'notes ' , 'notesRoot ' , $ newName );
217235 }
236+
218237 return $ folder ;
219238 }
220239
@@ -238,21 +257,21 @@ private function generateFileName(Folder $folder, $title, $extension, $id) {
238257 // need to handle file collisions if it is the filename did not change
239258 if (!$ folder ->nodeExists ($ path ) || $ folder ->get ($ path )->getId () === $ id ) {
240259 return $ path ;
260+ }
261+
262+ // increments name (2) to name (3)
263+ $ match = \preg_match ('/\((?P<id>\d+)\)$/ ' , $ title , $ matches );
264+ if ($ match ) {
265+ $ newId = ((int ) $ matches ['id ' ]) + 1 ;
266+ $ newTitle = \preg_replace (
267+ '/(.*)\s\((\d+)\)$/ ' ,
268+ '$1 ( ' . $ newId . ') ' ,
269+ $ title
270+ );
241271 } else {
242- // increments name (2) to name (3)
243- $ match = \preg_match ('/\((?P<id>\d+)\)$/ ' , $ title , $ matches );
244- if ($ match ) {
245- $ newId = ((int ) $ matches ['id ' ]) + 1 ;
246- $ newTitle = \preg_replace (
247- '/(.*)\s\((\d+)\)$/ ' ,
248- '$1 ( ' . $ newId . ') ' ,
249- $ title
250- );
251- } else {
252- $ newTitle = $ title . ' (2) ' ;
253- }
254- return $ this ->generateFileName ($ folder , $ newTitle , $ extension , $ id );
272+ $ newTitle = $ title . ' (2) ' ;
255273 }
274+ return $ this ->generateFileName ($ folder , $ newTitle , $ extension , $ id );
256275 }
257276
258277 /**
0 commit comments