1010use OC \Files \Storage \Wrapper \Wrapper ;
1111use OCA \Files_Trashbin \Events \MoveToTrashEvent ;
1212use OCA \Files_Trashbin \Trash \ITrashManager ;
13+ use OCP \App \IAppManager ;
1314use OCP \Encryption \Exceptions \GenericEncryptionException ;
1415use OCP \EventDispatcher \IEventDispatcher ;
1516use OCP \Files \IRootFolder ;
1617use OCP \Files \Node ;
1718use OCP \Files \Storage \IStorage ;
19+ use OCP \IRequest ;
1820use OCP \IUserManager ;
21+ use OCP \Server ;
1922use Psr \Log \LoggerInterface ;
2023
2124class Storage extends Wrapper {
@@ -33,6 +36,7 @@ public function __construct(
3336 private ?LoggerInterface $ logger = null ,
3437 private ?IEventDispatcher $ eventDispatcher = null ,
3538 private ?IRootFolder $ rootFolder = null ,
39+ private ?IRequest $ request = null ,
3640 ) {
3741 $ this ->mountPoint = $ parameters ['mountPoint ' ];
3842 parent ::__construct ($ parameters );
@@ -118,26 +122,26 @@ protected function createMoveToTrashEvent(Node $node): MoveToTrashEvent {
118122 * @return bool true if the operation succeeded, false otherwise
119123 */
120124 private function doDelete (string $ path , string $ method ): bool {
121- if (
122- !\OC ::$ server ->getAppManager ()->isEnabledForUser ('files_trashbin ' )
123- || (pathinfo ($ path , PATHINFO_EXTENSION ) === 'part ' )
124- || $ this ->shouldMoveToTrash ($ path ) === false
125- ) {
126- return call_user_func ([$ this ->storage , $ method ], $ path );
127- }
125+ $ isTrashbinEnabled = Server::get (IAppManager::class)->isEnabledForUser ('files_trashbin ' );
126+ $ isPartFile = pathinfo ($ path , PATHINFO_EXTENSION ) === 'part ' ;
127+ $ isSkipTrashHeaderSet = $ this ->request !== null && $ this ->request ->getHeader ('X-NC-Skip-Trashbin ' ) === 'true ' ;
128+ // We keep the shouldMoveToTrash call at the end to prevent emitting unnecessary event.
129+ $ shouldMoveToTrash = $ isTrashbinEnabled && !$ isPartFile && !$ isSkipTrashHeaderSet && $ this ->shouldMoveToTrash ($ path );
130+
131+ if ($ shouldMoveToTrash ) {
132+ // check permissions before we continue, this is especially important for
133+ // shared files
134+ if (!$ this ->isDeletable ($ path )) {
135+ return false ;
136+ }
128137
129- // check permissions before we continue, this is especially important for
130- // shared files
131- if (! $ this -> isDeletable ( $ path )) {
132- return false ;
138+ $ isMovedToTrash = $ this -> trashManager -> moveToTrash ( $ this , $ path );
139+ if ( $ isMovedToTrash ) {
140+ return true ;
141+ }
133142 }
134143
135- $ isMovedToTrash = $ this ->trashManager ->moveToTrash ($ this , $ path );
136- if (!$ isMovedToTrash ) {
137- return call_user_func ([$ this ->storage , $ method ], $ path );
138- } else {
139- return true ;
140- }
144+ return call_user_func ([$ this ->storage , $ method ], $ path );
141145 }
142146
143147 /**
@@ -149,16 +153,18 @@ public static function setupStorage(): void {
149153 $ logger = \OC ::$ server ->get (LoggerInterface::class);
150154 $ eventDispatcher = \OC ::$ server ->get (IEventDispatcher::class);
151155 $ rootFolder = \OC ::$ server ->get (IRootFolder::class);
156+ $ request = \OC ::$ server ->get (IRequest::class);
152157 Filesystem::addStorageWrapper (
153158 'oc_trashbin ' ,
154- function (string $ mountPoint , IStorage $ storage ) use ($ trashManager , $ userManager , $ logger , $ eventDispatcher , $ rootFolder ) {
159+ function (string $ mountPoint , IStorage $ storage ) use ($ trashManager , $ userManager , $ logger , $ eventDispatcher , $ rootFolder, $ request ) {
155160 return new Storage (
156161 ['storage ' => $ storage , 'mountPoint ' => $ mountPoint ],
157162 $ trashManager ,
158163 $ userManager ,
159164 $ logger ,
160165 $ eventDispatcher ,
161166 $ rootFolder ,
167+ $ request ,
162168 );
163169 },
164170 1 );
0 commit comments