| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @author Robin Appelman <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @copyright Copyright (c) 2016, ownCloud, Inc. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @license AGPL-3.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * This code is free software: you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * it under the terms of the GNU Affero General Public License, version 3, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * as published by the Free Software Foundation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * This program is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * GNU Affero General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * You should have received a copy of the GNU Affero General Public License, version 3, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * along with this program.  If not, see <http://www.gnu.org/licenses/> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | namespace OCA\Files_External\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use OC\Core\Command\Base; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use OCA\Files_external\NotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use OCA\Files_external\Service\GlobalStoragesService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use OCP\Files\Storage\INotifyStorage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use OCP\Files\Storage\IStorage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use Symfony\Component\Console\Command\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | use Symfony\Component\Console\Helper\TableHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | use Symfony\Component\Console\Input\InputArgument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | class Watch extends Base { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	 * @var GlobalStoragesService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	protected $globalService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	function __construct(GlobalStoragesService $globalService) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 		parent::__construct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		$this->globalService = $globalService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  | 	protected function configure() { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  | 		$this | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  | 			->setName('files_external:watch') | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  | 			->setDescription('Watch external storages for changes') | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  | 			->addArgument( | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  | 				'mount_id', | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  | 				InputArgument::REQUIRED, | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  | 				'The id of the mount to check' | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  | 			); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  | 		parent::configure(); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	protected function execute(InputInterface $input, OutputInterface $output) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		$mountId = $input->getArgument('mount_id'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 			$mount = $this->globalService->getStorage($mountId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		} catch (NotFoundException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 			$output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"</error>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 			return 404; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		$class = $mount->getBackend()->getStorageClass(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		$storage = new $class($mount->getBackendOptions()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 		if ($storage instanceof INotifyStorage && $storage instanceof IStorage) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 			$updater = $storage->getUpdater(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 			$storage->notify('', function ($code, $path, $path2) use ($updater, $storage) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 				switch ($code) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 					case INotifyStorage::NOTIFY_ADDED: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 					case INotifyStorage::NOTIFY_MODIFIED: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 						var_dump('update', $path); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 						$updater->update($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 						break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 					case INotifyStorage::NOTIFY_REMOVED: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 						$updater->remove($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 						break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 					case INotifyStorage::NOTIFY_RENAME: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 						$updater->renameFromStorage($storage, $path, $path2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 						break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 			}); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 			$output->writeln('<error>storage backend does not support external change watching</error>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 93 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |  |