container = $container; $this->entityManager = $container->get('doctrine.orm.entity_manager'); $this->kernel = $this->container->get('kernel'); $this->translator = $this->container->get('translator'); $this->requestStack = $this->container->get('request_stack'); $this->photo_warehouse = $photo_warehouse; $this->local = $this->requestStack->getCurrentRequest()->getLocale(); $this->repository = $this->entityManager->getRepository(Box::class); } /** * @param Box $element * @return Box|null */ public function quickUpdate(Box $element): ?Box { if (!$this->entityManager->contains($element)) { try { $this->entityManager->persist($element); } catch (ORMException $e) { return null; } } try { $this->entityManager->flush(); } catch (OptimisticLockException $e) { return null; } catch (ORMException $e) { return null; } return $element; } public function getByName(string $name, string $type){ // $box = $this->entityManager->getRepository(Box::class)->findOneBy(['name'=>$name]); $box= $this->repository->findOneBy(['name'=>$name, 'type'=>$type]); if($box === null){ $box = $this->create($name, $type); } return $box; } private function create(string $name, string $type){ if (!isset($name)) return null; $box = new Box(); $box->setDefault($name, $type); switch ($type){ case 'system': $box->setSystemBox(true); break; case 'userBox': $box->setUserBox(true); break; case 'trashBox': $box->setTrashBox(true); break; default: break; } if (!$this->quickUpdate($box)) return null; return $box; } /* * directories is children */ public function addDirectories(Box $box, Directory $directory){ if (!$box instanceof Box || !$directory instanceof Directory) return null; $box->addDirectory($directory); return $this->update($box); } public function addFile(Box $box, FileItem $fileItem){ if (!$box instanceof Box || !$fileItem instanceof FileItem) return null; $box->addFile($fileItem); return $this->update($box); } public function addFolderOwner(Box $box, FolderOwner $folderOwner){ if (!$box instanceof Box || !$folderOwner instanceof FolderOwner) return null; $box->addFolderOwner($folderOwner); return $this->update($box); } public function addFolderGroupOwner(Box $box, FolderGroupPermission $folderGroupPermission){ if (!$box instanceof Box || !$folderGroupPermission instanceof FolderGroupPermission) return null; $box->addFolderGroupPermission($folderGroupPermission); return $this->update($box); } public function update(Box $folder){ //Dispatch some events return $this->quickUpdate($folder); } public function remove(Box $box){ try { $this->entityManager->remove($box); $this->entityManager->flush($box); } catch (ORMException $e) { return false; } return true; } public function upload_file(UploadedFile $file, array $response){ // Create SystemBox if not exist $system_box = $this->getByName("systemBox", 'system'); // Recuperate mime type for check and create directory name $mime_type = $file->getMimeType(); $directoryName = 'default'; if (preg_match('/image/i', $mime_type)) $directoryName = 'images'; if (preg_match('/video/i', $mime_type)) $directoryName = 'videos'; if (preg_match('/application/i', $mime_type)) $directoryName = 'applications'; // Create directory $directory = $this->container->get('net15.directory.manager')->getByName($directoryName, $system_box); // if directory for upload is not exit we create it if (!is_dir($this->photo_warehouse)){ mkdir($this->photo_warehouse); } //upload tmp dir photo $file_name = $this->container->get('net15.file.upload.manager')->upload($file, $this->photo_warehouse); // Create fileItem if ($file_item = $this->container->get('net15.file.item.manager')->create($this->photo_warehouse.DIRECTORY_SEPARATOR.$file_name,$file->getClientOriginalName())){ $directory->addFile($file_item); $response['message'] = $this->translator->trans('file_manager.file.file_uploaded', [], null, $this->local); } // generate thumbnail try { $result = $this->container->get('net15.files.thumbnail.main')->generate($file_item); } catch (\Exception $e) { } // Add directory to systemBox $system_box->addDirectoriesChild($directory); if (!$this->update($system_box)) return null; $response['system_box'] = $system_box; $response['directory'] = $directory; $response['success'] = true; return $response; } }