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(); } /** * @param SystemBox $element * @return SystemBox|null */ public function quickUpdate(SystemBox $element): ?SystemBox { 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){ $systemBox = $this->entityManager->getRepository(SystemBox::class)->findOneBy(['name'=>$name]); if($systemBox === null){ $systemBox = $this->create($name); } return $systemBox; } private function create(string $name){ if (!isset($name)) return null; $systemBox = new SystemBox(); $systemBox->setDefault($name); if (!$this->quickUpdate($systemBox)) return null; return $systemBox; } /* * directories is children */ public function addDirectories(SystemBox $systemBox, Directory $directory){ if (!$systemBox instanceof SystemBox || !$directory instanceof Directory) return null; $systemBox->addDirectory($directory); return $this->update($systemBox); } public function addFile(SystemBox $systemBox, FileItem $fileItem){ if (!$systemBox instanceof SystemBox || !$fileItem instanceof FileItem) return null; $systemBox->addFile($fileItem); return $this->update($systemBox); } public function addFolderOwner(SystemBox $systemBox, FolderOwner $folderOwner){ if (!$systemBox instanceof SystemBox || !$folderOwner instanceof FolderOwner) return null; $systemBox->addFolderOwner($folderOwner); return $this->update($systemBox); } public function addFolderGroupOwner(SystemBox $systemBox, FolderGroupPermission $folderGroupPermission){ if (!$systemBox instanceof SystemBox || !$folderGroupPermission instanceof FolderGroupPermission) return null; $systemBox->addFolderGroupPermission($folderGroupPermission); return $this->update($systemBox); } public function update(SystemBox $folder){ //Dispatch some events return $this->quickUpdate($folder); } public function remove(SystemBox $systemBox){ try { $this->entityManager->remove($systemBox); $this->entityManager->flush($systemBox); } catch (ORMException $e) { return false; } return true; } public function upload_file(UploadedFile $file, array $response){ $system_box = $this->getByName("SystemBox"); $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'; $directory = $this->container->get('net15.directory.manager')->getByName($directoryName); // 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); dump($file_name); // Create fileItem if ($file_item = $this->container->get('net15.file.item.manager')->create($this->photo_warehouse.DIRECTORY_SEPARATOR.$file_name, $file->getClientOriginalName())){ dump($file_item); $directory->addFile($file_item); $response['message'] = $this->translator->trans('file_manager.file.file_uploaded', [], null, $this->local); } dump($directory); // generate thumbnail try { $this->container->get('net15.files.thumbnail.main')->generate($file_item); dump($file_item); } catch (\Exception $e) { } $system_box->addDirectory($directory); dump($system_box); if (!$this->update($system_box)) return null; $response['system_box'] = $system_box; $response['success'] = true; return $response; } }