2525use OCP \IUser ;
2626use OCP \SystemTag \ISystemTagManager ;
2727use OCP \SystemTag \ISystemTagObjectMapper ;
28+ use OCP \SystemTag \TagAlreadyExistsException ;
29+ use OCP \SystemTag \TagCreationForbiddenException ;
2830use OCP \SystemTag \TagNotFoundException ;
2931use OCP \UserMigration \IExportDestination ;
3032use OCP \UserMigration \IImportSource ;
@@ -300,8 +302,14 @@ public function import(
300302 $ taggedFiles = json_decode ($ importSource ->getFileContents (static ::PATH_TAGS ), true , 512 , JSON_THROW_ON_ERROR );
301303 $ tagger = $ this ->tagManager ->load (Application::APP_ID , [], false , $ uid );
302304 foreach ($ taggedFiles as $ path => $ tags ) {
305+ try {
306+ $ node = $ userFolder ->get ((string )$ path );
307+ } catch (NotFoundException ) {
308+ /* File/Folder not found, skip */
309+ continue ;
310+ }
303311 foreach ($ tags as $ tag ) {
304- if ($ tagger ->tagAs ($ userFolder -> get ( $ path ) ->getId (), $ tag ) === false ) {
312+ if ($ tagger ->tagAs ($ node ->getId (), $ tag ) === false ) {
305313 throw new UserMigrationException ("Failed to import tag $ tag for path $ path " );
306314 }
307315 }
@@ -311,16 +319,35 @@ public function import(
311319
312320 $ systemTaggedFiles = json_decode ($ importSource ->getFileContents (static ::PATH_SYSTEMTAGS ), true , 512 , JSON_THROW_ON_ERROR );
313321 foreach ($ systemTaggedFiles as $ path => $ systemTags ) {
322+ try {
323+ $ node = $ userFolder ->get ((string )$ path );
324+ if (!$ node ->isUpdatable ()) {
325+ /* Node is read-only, cannot tag */
326+ continue ;
327+ }
328+ } catch (NotFoundException ) {
329+ /* File/Folder not found, skip */
330+ continue ;
331+ }
314332 $ systemTagIds = [];
315333 foreach ($ systemTags as $ systemTag ) {
316334 try {
317335 $ systemTagObject = $ this ->systemTagManager ->getTag ($ systemTag , true , true );
318- } catch (TagNotFoundException $ e ) {
319- $ systemTagObject = $ this ->systemTagManager ->createTag ($ systemTag , true , true , $ user );
336+ if (!$ this ->systemTagManager ->canUserAssignTag ($ systemTagObject , $ user )) {
337+ /* Should not happen because we requested an assignable and visible tag, but let’s make sure */
338+ continue ;
339+ }
340+ } catch (TagNotFoundException ) {
341+ try {
342+ $ systemTagObject = $ this ->systemTagManager ->createTag ($ systemTag , true , true , $ user );
343+ } catch (TagCreationForbiddenException |TagAlreadyExistsException ) {
344+ /* Not allowed to create tag or a restricted tag with the same name exists, skip */
345+ continue ;
346+ }
320347 }
321348 $ systemTagIds [] = $ systemTagObject ->getId ();
322349 }
323- if ($ this ->systemTagMapper ->assignTags ((string )$ userFolder -> get ( $ path ) ->getId (), 'files ' , $ systemTagIds ) === false ) {
350+ if ($ this ->systemTagMapper ->assignTags ((string )$ node ->getId (), 'files ' , $ systemTagIds ) === false ) {
324351 throw new UserMigrationException ("Failed to import system tags for path $ path " );
325352 }
326353 }
@@ -329,6 +356,12 @@ public function import(
329356
330357 $ comments = json_decode ($ importSource ->getFileContents (static ::PATH_COMMENTS ), true , 512 , JSON_THROW_ON_ERROR );
331358 foreach ($ comments as $ path => $ fileComments ) {
359+ try {
360+ $ node = $ userFolder ->get ((string )$ path );
361+ } catch (NotFoundException ) {
362+ /* File/Folder not found, skip */
363+ continue ;
364+ }
332365 foreach ($ fileComments as $ fileComment ) {
333366 if (($ fileComment ['actorType ' ] === 'users ' ) || ($ fileComment ['actorType ' ] === ICommentsManager::DELETED_USER )) {
334367 $ actorId = $ fileComment ['actorId ' ];
@@ -340,7 +373,7 @@ public function import(
340373 $ actorId = ICommentsManager::DELETED_USER ;
341374 $ actorType = ICommentsManager::DELETED_USER ;
342375 }
343- $ commentObject = $ this ->commentsManager ->create ($ actorType , $ actorId , 'files ' , (string )$ userFolder -> get ( $ path ) ->getId ());
376+ $ commentObject = $ this ->commentsManager ->create ($ actorType , $ actorId , 'files ' , (string )$ node ->getId ());
344377 $ commentObject ->setMessage ($ fileComment ['message ' ]);
345378 $ commentObject ->setVerb ($ fileComment ['verb ' ]);
346379 $ commentObject ->setCreationDateTime (new \DateTime ($ fileComment ['creationDateTime ' ]));
0 commit comments