@@ -130,30 +130,29 @@ public function create(IShare $share) {
130130 $ share ->setSharedWith ($ cloudId ->getId ());
131131
132132 try {
133- $ remoteShare = $ this ->getShareFromExternalShareTable ($ share );
133+ $ remoteShare = $ this ->getShareFromExternalShareTable ($ share-> getShareOwner (), $ share -> getTarget () );
134134 } catch (ShareNotFound $ e ) {
135135 $ remoteShare = null ;
136136 }
137137
138138 if ($ remoteShare ) {
139- try {
140- $ ownerCloudId = $ this ->cloudIdManager ->getCloudId ($ remoteShare ['owner ' ], $ remoteShare ['remote ' ]);
141- $ shareId = $ this ->addShareToDB ($ itemSource , $ itemType , $ shareWith , $ sharedBy , $ ownerCloudId ->getId (), $ permissions , 'tmp_token_ ' . time (), $ shareType , $ expirationDate );
142- $ share ->setId ($ shareId );
143- [$ token , $ remoteId ] = $ this ->askOwnerToReShare ($ shareWith , $ share , $ shareId );
144- // remote share was create successfully if we get a valid token as return
145- $ send = is_string ($ token ) && $ token !== '' ;
146- } catch (\Exception $ e ) {
147- // fall back to old re-share behavior if the remote server
148- // doesn't support flat re-shares (was introduced with Nextcloud 9.1)
149- $ this ->removeShareFromTable ($ share );
150- $ shareId = $ this ->createFederatedShare ($ share );
151- }
152- if ($ send ) {
139+ $ ownerCloudId = $ this ->cloudIdManager ->getCloudId ($ remoteShare ['owner ' ], $ remoteShare ['remote ' ]);
140+ $ shareId = $ this ->addShareToDB ($ itemSource , $ itemType , $ shareWith , $ sharedBy , $ ownerCloudId ->getId (), $ permissions , 'tmp_token_ ' . time (), $ shareType , $ expirationDate );
141+ [$ token , $ remoteId ] = $ this ->notifications ->requestReShare (
142+ $ remoteShare ['share_token ' ],
143+ $ remoteShare ['remote_id ' ],
144+ $ shareId ,
145+ $ remoteShare ['remote ' ],
146+ $ shareWith ,
147+ $ permissions ,
148+ $ share ->getNode ()->getName (),
149+ );
150+ // remote share was create successfully if we get a valid token as return
151+ if (is_string ($ token ) && $ token !== '' ) {
153152 $ this ->updateSuccessfulReshare ($ shareId , $ token );
154153 $ this ->storeRemoteId ($ shareId , $ remoteId );
155154 } else {
156- $ this ->removeShareFromTable ($ share );
155+ $ this ->removeShareFromTable ($ shareId );
157156 $ message_t = $ this ->l ->t ('File is already shared with %s ' , [$ shareWith ]);
158157 throw new \Exception ($ message_t );
159158 }
@@ -220,7 +219,7 @@ protected function createFederatedShare(IShare $share) {
220219 }
221220
222221 if ($ failure ) {
223- $ this ->removeShareFromTableById ($ shareId );
222+ $ this ->removeShareFromTable ($ shareId );
224223 $ message_t = $ this ->l ->t ('Sharing %1$s failed, could not find %2$s, maybe the server is currently unreachable or uses a self-signed certificate. ' ,
225224 [$ share ->getNode ()->getName (), $ share ->getSharedWith ()]);
226225 throw new \Exception ($ message_t );
@@ -229,45 +228,18 @@ protected function createFederatedShare(IShare $share) {
229228 return $ shareId ;
230229 }
231230
232- /**
233- * @param string $shareWith
234- * @param IShare $share
235- * @param string $shareId internal share Id
236- * @return array
237- * @throws \Exception
238- */
239- protected function askOwnerToReShare ($ shareWith , IShare $ share , $ shareId ) {
240- $ remoteShare = $ this ->getShareFromExternalShareTable ($ share );
241- $ token = $ remoteShare ['share_token ' ];
242- $ remoteId = $ remoteShare ['remote_id ' ];
243- $ remote = $ remoteShare ['remote ' ];
244-
245- [$ token , $ remoteId ] = $ this ->notifications ->requestReShare (
246- $ token ,
247- $ remoteId ,
248- $ shareId ,
249- $ remote ,
250- $ shareWith ,
251- $ share ->getPermissions (),
252- $ share ->getNode ()->getName ()
253- );
254-
255- return [$ token , $ remoteId ];
256- }
257-
258231 /**
259232 * get federated share from the share_external table but exclude mounted link shares
260233 *
261- * @param IShare $share
262234 * @return array
263235 * @throws ShareNotFound
264236 */
265- protected function getShareFromExternalShareTable (IShare $ share ) {
237+ protected function getShareFromExternalShareTable (string $ owner , string $ target ) {
266238 $ query = $ this ->dbConnection ->getQueryBuilder ();
267239 $ query ->select ('* ' )->from ($ this ->externalShareTable )
268- ->where ($ query ->expr ()->eq ('user ' , $ query ->createNamedParameter ($ share -> getShareOwner () )))
269- ->andWhere ($ query ->expr ()->eq ('mountpoint ' , $ query ->createNamedParameter ($ share -> getTarget () )));
270- $ qResult = $ query ->execute ();
240+ ->where ($ query ->expr ()->eq ('user ' , $ query ->createNamedParameter ($ owner )))
241+ ->andWhere ($ query ->expr ()->eq ('mountpoint ' , $ query ->createNamedParameter ($ target )));
242+ $ qResult = $ query ->executeQuery ();
271243 $ result = $ qResult ->fetchAll ();
272244 $ qResult ->closeCursor ();
273245
@@ -289,7 +261,7 @@ protected function getShareFromExternalShareTable(IShare $share) {
289261 * @param int $permissions
290262 * @param string $token
291263 * @param int $shareType
292- * @param \DateTime $expirationDate
264+ * @param ? \DateTime $expirationDate
293265 * @return int
294266 */
295267 private function addShareToDB ($ itemSource , $ itemType , $ shareWith , $ sharedBy , $ uidOwner , $ permissions , $ token , $ shareType , $ expirationDate ) {
@@ -475,7 +447,7 @@ public function delete(IShare $share) {
475447
476448 // only remove the share when all messages are send to not lose information
477449 // about the share to early
478- $ this ->removeShareFromTable ($ share );
450+ $ this ->removeShareFromTable (( int ) $ share-> getId () );
479451 }
480452
481453 /**
@@ -506,20 +478,9 @@ protected function revokeShare($share, $isOwner) {
506478 }
507479
508480 /**
509- * remove share from table
510- *
511- * @param IShare $share
512- */
513- public function removeShareFromTable (IShare $ share ) {
514- $ this ->removeShareFromTableById ($ share ->getId ());
515- }
516-
517- /**
518- * remove share from table
519- *
520- * @param string $shareId
481+ * Remove share from table.
521482 */
522- private function removeShareFromTableById ( $ shareId ) {
483+ public function removeShareFromTable ( int $ shareId ): void {
523484 $ qb = $ this ->dbConnection ->getQueryBuilder ();
524485 $ qb ->delete ('share ' )
525486 ->where ($ qb ->expr ()->eq ('id ' , $ qb ->createNamedParameter ($ shareId )))
0 commit comments