entityManager = $entityManager; $this->fileItemManager = $fileItemManager; $this->form_forge_warehouse = $form_forge_warehouse; $this->container = $container; $this->avatar_warehouse = $avatar_warehouse; $this->mogrifyExe = $mogrifyExe; $this->kernel_project_dir = $kernel_project_dir; $this->translator = $translator; $this->requestStack = $requestStack; // $this->trashBoxManager = $this->container->get('net15.trash.box.manager'); } public function upload_file(array $files, array $files_uploaded){ if(! isset($files_uploaded['files'])) $files_uploaded['files'] = array(); $local = $this->requestStack->getCurrentRequest()->getLocale(); $maxSize = 1000 * (1000 * 10); if(!empty($files) && is_array($files)){ /** * @var $file UploadedFile */ foreach ($files['files'] as $file){ // We check if file extension has otorezed $allowed = array('jpg', 'png','jpeg', 'pdf'); if (!in_array($file->getClientOriginalExtension(), $allowed)){ $files_uploaded['message'] = $this->translator->trans('file_manager.file.extension_not_found', [], null, $local); }else{ // We check if file size is less then 2Mo, we uploade it // Else send message if ($file->getSize() < $maxSize){ $file_uniq_name = md5(uniqid()); $upload_file = $this->form_forge_warehouse; // Move file to new directory if ($file->move($upload_file,$file_uniq_name)){ $files_uploaded['message'] = $this->translator->trans('file_manager.file.file_uploaded', [], null, $local); $files_uploaded['files'][$file_uniq_name] = $file->getClientOriginalName(); } }else{ $files_uploaded['message'] = $this->translator->trans('file_manager.file.file_max_size', ['%size%'=>($maxSize/1000)/1000], null, $local); } } } } return $files_uploaded; } /** * @param array $files * @param array $response * @param User $user * @return array|null * @throws \Doctrine\ORM\ORMException */ public function upload_avatar(array $files, array $response, User $user){ if(!isset($response)) $response = array(); $local = $this->requestStack->getCurrentRequest()->getLocale(); $maxSize = 1000 * (1000 * 10); if(!empty($files) && is_array($files)){ /** * @var $file UploadedFile */ foreach ($files['files'] as $file){ // We check if file extension has otorezed $allowed = array('jpg', 'png', 'jpeg'); if (!in_array($file->getClientOriginalExtension(), $allowed)){ $response['message'] = $this->translator->trans('file_manager.file.extension_not_found', [], null, $local); }else{ // We check if file size is less then 2Mo, we uploade it // Else send message if ($file->getSize() < $maxSize){ $file_uniq_name = md5(uniqid()); $targetPath = $this->form_forge_warehouse; //Folder to upload // Move file to new directory if ($file->move($targetPath,$file_uniq_name)){ $response['message'] = $this->translator->trans('file_manager.file.file_uploaded', [], null, $local); $response['image'] = $file->getClientOriginalName(); // Create fileItem $fileItem = $this->container->get('net15.file.item.manager')->create($this->form_forge_warehouse.DIRECTORY_SEPARATOR.$file_uniq_name, $file->getClientOriginalName()); if (!$fileItem instanceof FileItem) return null; // Create different format for avatar $pathSourceImage = $fileItem->getPath().$fileItem->getGuid(); // Small size 200x200 $desired_dimensions['small']['width'] = 200; $desired_dimensions['small']['height'] = 200; // Medium size 400x400 $desired_dimensions['medium']['width'] = 400; $desired_dimensions['medium']['height'] = 400; // Large size 800x800 $desired_dimensions['large']['width'] = 800; $desired_dimensions['large']['height'] = 800; $paths_destination = []; $paths_destination['small'] = $this->avatar_warehouse.DIRECTORY_SEPARATOR.'small'; $paths_destination['medium'] = $this->avatar_warehouse.DIRECTORY_SEPARATOR.'medium'; $paths_destination['large'] = $this->avatar_warehouse.DIRECTORY_SEPARATOR.'large'; $quality = 82; $avatar_path_img =[]; $symlink_file_name = []; $avatarDir = []; $fileSystem = new Filesystem(); foreach ($paths_destination as $key=>$value){ if (!is_dir($value)){ mkdir($value); } $commandLine = $this->mogrifyExe." -path $value -filter Triangle -define filter:support=2 -thumbnail ".$desired_dimensions[$key]['width']."x".$desired_dimensions[$key]['height']." -unsharp 0.25x0.25+8+0.065 -dither None -posterize 136 -quality $quality -define jpeg:fancy-upsampling=off -define png:compression-filter=5 -define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude-chunk=all -interlace none -colorspace sRGB -strip $pathSourceImage"; $process = new Process($commandLine); $process->run(); if (!$process) { throw new ProcessFailedException($process); } // Add extension to file $avatar_path_img[$key] = $value.DIRECTORY_SEPARATOR.$fileItem->getGuid().'.jpg'; rename($value.DIRECTORY_SEPARATOR.$fileItem->getGuid(), $avatar_path_img[$key]); // Create a symlink $avatarDir[$key] = DIRECTORY_SEPARATOR.'avatar'.DIRECTORY_SEPARATOR.$key; $symlinkRootDirAvatar = $this->kernel_project_dir.DIRECTORY_SEPARATOR.'public'.DIRECTORY_SEPARATOR.'assets'.$avatarDir[$key]; // Check if directory for symlink exists otherwise create it if(!$fileSystem->exists($symlinkRootDirAvatar)){ $fileSystem->mkdir($symlinkRootDirAvatar); } $symlink_file_name[$key] = $symlinkRootDirAvatar.DIRECTORY_SEPARATOR.substr($fileItem->getGuid(), 0, 4).'.jpg'; $fileSystem->symlink($avatar_path_img[$key], $symlink_file_name[$key]); } // We check if User has an avatar, we update it, if not we create a new avatar if ($user->getAvatar() !== null){ // Delete first avatar $oldFileItem = $user->getAvatar()->getFori(); $fileSystem->remove($oldFileItem->getPath()); // Delete olldAvat file and symlink // Delete small $fileSystem->remove([$this->kernel_project_dir.DIRECTORY_SEPARATOR.'public'.DIRECTORY_SEPARATOR.'assets'.$user->getAvatar()->getFsmall(), $this->avatar_warehouse.DIRECTORY_SEPARATOR.'small'.DIRECTORY_SEPARATOR.$oldFileItem->getGuid().'.jpg' ]); // Delete medium $fileSystem->remove([$this->kernel_project_dir.DIRECTORY_SEPARATOR.'public'.DIRECTORY_SEPARATOR.'assets'.$user->getAvatar()->getFmedium(), $this->avatar_warehouse.DIRECTORY_SEPARATOR.'medium'.DIRECTORY_SEPARATOR.$oldFileItem->getGuid().'.jpg' ]); //Delete large $fileSystem->remove([$this->kernel_project_dir.DIRECTORY_SEPARATOR.'public'.DIRECTORY_SEPARATOR.'assets'.$user->getAvatar()->getFlarge(), $this->avatar_warehouse.DIRECTORY_SEPARATOR.'large'.DIRECTORY_SEPARATOR.$oldFileItem->getGuid().'.jpg' ]); // Update Avatar $user->getAvatar()->setFori($fileItem); $user->getAvatar()->setFsmall($avatarDir['small'].DIRECTORY_SEPARATOR.substr($fileItem->getGuid(), 0, 4).'.jpg'); $user->getAvatar()->setFmedium($avatarDir['medium'].DIRECTORY_SEPARATOR.substr($fileItem->getGuid(), 0, 4).'.jpg'); $user->getAvatar()->setFlarge($avatarDir['large'].DIRECTORY_SEPARATOR.substr($fileItem->getGuid(), 0, 4).'.jpg'); $this->container->get('net15.file.item.manager')->deleteFileItem($oldFileItem); }else{ // Create Avatar $avatar = $this->container->get('net15.avatar.manager')->createAvatarFromFileItem($fileItem, $user); $avatar->setFsmall($avatarDir['small'].DIRECTORY_SEPARATOR.substr($fileItem->getGuid(), 0, 4).'.jpg'); $avatar->setFmedium($avatarDir['medium'].DIRECTORY_SEPARATOR.substr($fileItem->getGuid(), 0, 4).'.jpg'); $avatar->setFlarge($avatarDir['large'].DIRECTORY_SEPARATOR.substr($fileItem->getGuid(), 0, 4).'.jpg'); // Create FileOwner $fileOwner = new FileOwner($user,$fileItem); $this->entityManager->persist($fileOwner); $user->setAvatar($avatar); $user->addFileOwner($fileOwner); } $this->container->get('net15.user.manager')->quickUpdate($user); } }else{ $response['message'] = $this->translator->trans('file_manager.file.file_max_size', ['size'=>2], null, $local); } } } } return $response; } public function upload(UploadedFile $file, string $targetDirectory){ $originalFilename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME); $safeFilename = transliterator_transliterate('Any-Latin; LAtin-ASCII; [^A-Za-z0-9_] remove; Lower()', $originalFilename); $fileName = $safeFilename.'-'.uniqid().'.'.$file->guessExtension(); try { $file->move($targetDirectory, $fileName); } catch (FileException $exception){ // ... handle exception if something happens during file upload return $exception; } return $fileName; } }