router = $router; $this->em = $em; $this->fileManagerMainService = $fileManagerMainService; $this->tokenStorage = $tokenStorage; $this->rootDir = realpath($rootDir . '/../src/cli-worker/'); } /** * @param string $msg * @param null $desc * @return array */ protected function reportError($msg = "An error occured", $desc = null){ $ret = array( 'error' => true, 'msg' => $msg ); if(!is_null($desc)) $ret['description'] = $desc; return $ret; } /** * @param string $msg * @param array $append * @return array */ protected function reportSuccess($msg = "Operation successful", $append = array()){ $ret = array( 'error' => false, 'msg' => $msg ); return array_merge($ret,$append); } /** * @param $guid * @return FileProcessItem|null|object */ public function getProcessItemFromGuid($guid){ $search = array('guid' => $guid); return $this->em->getRepository('FileManagerBundle:FileProcessItem')->findOneBy($search); } /** * return process with inherent permission * * @param $guid * @return array|FileProcessItem| */ public function getProcessFromGuid($guid){ $p = $this->getProcessItemFromGuid($guid); if(is_null($p) || !$p instanceof FileProcessItem) return $this->reportError('Process not found'); else{ if(is_null($p->getUserFileOwnerInterface()) || $p->getUserFileOwnerInterface() == $this->tokenStorage->getToken()->getUser()){ return $p; } else return $this->reportError('Operation Not Allowed'); } } /** * @param FileProcessItem $file * @return bool */ public function quickUpdate(FileProcessItem $file){ if(empty($file)) return false; try { if(!$this->em->contains($file)) $this->em->persist($file); $this->em->flush(); } catch (OptimisticLockException $e) { //die('Exception : '.$e->getMessage()); return false; } catch (ORMException $e) { return false; } return true; } /** * @param FileProcessItem $file * @return bool */ public function update(FileProcessItem $file){ if(empty($file)) return false; /* do some shit in here */ return $this->quickUpdate($file); } /** * return path ending with "/" * * @return string */ public function getPath(): string{ $p = $this->fileManagerMainService->getPath().'process/'; if(!is_dir($p)) @mkdir($p,0777,true); return $p; } /** * create a new process * @param string $type * @param array $params * @return FileProcessItem|array */ public function createNewProcess(string $type,array $params){ $process = new FileProcessItem($this->getPath()); $url = $this->router->generate('api_worker_filemanager_process',array(),UrlGeneratorInterface::ABSOLUTE_URL); $process ->setType($type) ->setReturnUrl($url) ->setParams($params); $u = $this->tokenStorage->getToken()->getUser(); if(!is_bool($u)) $process->setUserFileOwnerInterface($u); if($this->quickUpdate($process)) return $process; else return $this->reportError('Failed to create a new process'); } /** * launche process * * @param FileProcessItem $fileProcessItem */ public function launchProcess(FileProcessItem $fileProcessItem){ exec('/usr/bin/nohup php7.1 '.$this->getRootDir().$fileProcessItem->getType().'.php '.$fileProcessItem->getPath().' >> '.$this->getRootDir().$fileProcessItem->getType().'.log 2>&1 &'); } /** * @return string */ public function getRootDir(): string { return $this->rootDir.'/'; } }