1010use \OCA \DAV \Connector \Sabre \FilesPlugin ;
1111use \OCA \DAV \Connector \Sabre \TagsPlugin ;
1212use OCA \DAV \Connector \Sabre \File ;
13+ use OCA \Recognize \Db \FaceClusterMapper ;
1314use OCA \Recognize \Db \FaceDetectionMapper ;
1415use OCA \Recognize \Db \FaceDetectionWithTitle ;
16+ use OCP \AppFramework \Db \DoesNotExistException ;
17+ use OCP \AppFramework \Db \MultipleObjectsReturnedException ;
18+ use OCP \DB \Exception ;
1519use OCP \Files \DavUtil ;
1620use OCP \IPreview ;
21+ use Sabre \DAV \Exception \Forbidden ;
1722use Sabre \DAV \INode ;
1823use Sabre \DAV \PropFind ;
1924use Sabre \DAV \Server ;
@@ -31,13 +36,15 @@ final class PropFindPlugin extends ServerPlugin {
3136 public function __construct (
3237 private FaceDetectionMapper $ faceDetectionMapper ,
3338 private IPreview $ previewManager ,
39+ private FaceClusterMapper $ faceClusterMapper ,
3440 ) {
3541 }
3642
3743 public function initialize (Server $ server ) {
3844 $ this ->server = $ server ;
3945
4046 $ this ->server ->on ('propFind ' , [$ this , 'propFind ' ]);
47+ $ this ->server ->on ('beforeMove ' , [$ this , 'beforeMove ' ]);
4148 }
4249
4350
@@ -87,4 +94,23 @@ public function propFind(PropFind $propFind, INode $node) {
8794 });
8895 }
8996 }
97+
98+ public function beforeMove ($ source , $ target ) {
99+ // recognize/{userId}/faces/{name}
100+ if (str_starts_with ($ source , 'recognize ' ) && str_starts_with ($ target , 'recognize ' )) {
101+ $ sourceParts = explode ('/ ' , $ source );
102+ $ targetParts = explode ('/ ' , $ target );
103+ if ($ sourceParts [2 ] === 'faces ' && $ targetParts [2 ] === 'faces ' && count ($ sourceParts ) === 4 && count ($ targetParts ) === 4 ) {
104+ try {
105+ $ this ->faceClusterMapper ->findByUserAndTitle ($ targetParts [1 ], $ targetParts [3 ]);
106+ throw new Forbidden ('The target node already exists and cannot be overwritten ' );
107+ } catch (DoesNotExistException $ e ) {
108+ return true ;
109+ } catch (MultipleObjectsReturnedException |Exception $ e ) {
110+ throw $ e ;
111+ }
112+ }
113+ }
114+ return true ;
115+ }
90116}
0 commit comments