@@ -35,40 +35,40 @@ discard block |
||
| 35 | 35 | $this->allowedMimetypes = $allowedMimetypes; |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * $current_uri String actual uri of the file |
|
| 40 | - * $dest_folder String future uri of the file starting from web/upload folder |
|
| 41 | - * $lifetime DateTime lifetime of the file. If time goes over this limit, the file will be deleted. |
|
| 42 | - **/ |
|
| 43 | - public function upload(UploadedFile $file, $dest_folder = '', \DateTime $lifetime = null) |
|
| 44 | - { |
|
| 45 | - //preparing dir name |
|
| 38 | + /** |
|
| 39 | + * $current_uri String actual uri of the file |
|
| 40 | + * $dest_folder String future uri of the file starting from web/upload folder |
|
| 41 | + * $lifetime DateTime lifetime of the file. If time goes over this limit, the file will be deleted. |
|
| 42 | + **/ |
|
| 43 | + public function upload(UploadedFile $file, $dest_folder = '', \DateTime $lifetime = null) |
|
| 44 | + { |
|
| 45 | + //preparing dir name |
|
| 46 | 46 | $dest_folder = date('Ymd').'/'.date('G').'/'.$dest_folder; |
| 47 | 47 | |
| 48 | 48 | //checking mimetypes |
| 49 | 49 | $mimeTypePassed = false; |
| 50 | - foreach ($this->allowedMimetypes as $mimeType) { |
|
| 51 | - if (preg_match('@'.$mimeType.'@', $file->getMimeType())) { |
|
| 52 | - $mimeTypePassed = true; |
|
| 53 | - } |
|
| 54 | - } |
|
| 50 | + foreach ($this->allowedMimetypes as $mimeType) { |
|
| 51 | + if (preg_match('@'.$mimeType.'@', $file->getMimeType())) { |
|
| 52 | + $mimeTypePassed = true; |
|
| 53 | + } |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - if (!$mimeTypePassed) { |
|
| 57 | - throw new InvalidMimeTypeException('Only following filetypes are allowed : '.implode(', ', $this->allowedMimetypes)); |
|
| 58 | - } |
|
| 56 | + if (!$mimeTypePassed) { |
|
| 57 | + throw new InvalidMimeTypeException('Only following filetypes are allowed : '.implode(', ', $this->allowedMimetypes)); |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - $fs = new Filesystem(); |
|
| 61 | - if (!$fs->exists($this->uploadDir.$dest_folder)) { |
|
| 62 | - $fs->mkdir($this->uploadDir.$dest_folder); |
|
| 63 | - } |
|
| 60 | + $fs = new Filesystem(); |
|
| 61 | + if (!$fs->exists($this->uploadDir.$dest_folder)) { |
|
| 62 | + $fs->mkdir($this->uploadDir.$dest_folder); |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - $em = $this->entityManager; |
|
| 66 | - $media = new Media(); |
|
| 67 | - $media->setMime($file->getMimeType()); |
|
| 65 | + $em = $this->entityManager; |
|
| 66 | + $media = new Media(); |
|
| 67 | + $media->setMime($file->getMimeType()); |
|
| 68 | 68 | |
| 69 | 69 | // Sanitizing the filename |
| 70 | 70 | $slugify = new Slugify(); |
| 71 | - $filename = $slugify->slugify($file->getClientOriginalName()); |
|
| 71 | + $filename = $slugify->slugify($file->getClientOriginalName()); |
|
| 72 | 72 | |
| 73 | 73 | // A media can have a lifetime and will be deleted with the cleanup function |
| 74 | 74 | if (!empty($lifetime)) { |
@@ -77,43 +77,43 @@ discard block |
||
| 77 | 77 | |
| 78 | 78 | // Checking for a media with the same name |
| 79 | 79 | $mediaExists = $this->entityManager->getRepository('AlpixelMediaBundle:Media')->findOneByUri($dest_folder.$filename); |
| 80 | - if (count($mediaExists) === 0) { |
|
| 81 | - $mediaExists = $fs->exists($this->uploadDir.$dest_folder.$filename); |
|
| 82 | - } |
|
| 80 | + if (count($mediaExists) === 0) { |
|
| 81 | + $mediaExists = $fs->exists($this->uploadDir.$dest_folder.$filename); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | 84 | // If there's one, we try to generate a new name |
| 85 | 85 | $extension = $file->getExtension(); |
| 86 | - if (empty($extension)) { |
|
| 87 | - $extension = $file->guessExtension(); |
|
| 88 | - } |
|
| 86 | + if (empty($extension)) { |
|
| 87 | + $extension = $file->guessExtension(); |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - if (count($mediaExists) > 0) { |
|
| 91 | - $filename = basename($filename, '.'.$extension); |
|
| 90 | + if (count($mediaExists) > 0) { |
|
| 91 | + $filename = basename($filename, '.'.$extension); |
|
| 92 | 92 | |
| 93 | - $i = 1; |
|
| 94 | - do { |
|
| 95 | - $media->setName($filename.'-'.$i++.'.'.$extension); |
|
| 96 | - $media->setUri($dest_folder.$media->getName()); |
|
| 97 | - $mediaExists = $this->entityManager->getRepository('AlpixelMediaBundle:Media')->findOneByUri($media->getUri()); |
|
| 98 | - } while (count($mediaExists) > 0); |
|
| 99 | - } else { |
|
| 100 | - $media->setName($filename.'.'.$extension); |
|
| 101 | - $media->setUri($dest_folder.$media->getName()); |
|
| 102 | - } |
|
| 93 | + $i = 1; |
|
| 94 | + do { |
|
| 95 | + $media->setName($filename.'-'.$i++.'.'.$extension); |
|
| 96 | + $media->setUri($dest_folder.$media->getName()); |
|
| 97 | + $mediaExists = $this->entityManager->getRepository('AlpixelMediaBundle:Media')->findOneByUri($media->getUri()); |
|
| 98 | + } while (count($mediaExists) > 0); |
|
| 99 | + } else { |
|
| 100 | + $media->setName($filename.'.'.$extension); |
|
| 101 | + $media->setUri($dest_folder.$media->getName()); |
|
| 102 | + } |
|
| 103 | 103 | |
| 104 | - $file->move($this->uploadDir.$dest_folder, $media->getName()); |
|
| 104 | + $file->move($this->uploadDir.$dest_folder, $media->getName()); |
|
| 105 | 105 | |
| 106 | - chmod($this->uploadDir.$dest_folder.$media->getName(), 0664); |
|
| 106 | + chmod($this->uploadDir.$dest_folder.$media->getName(), 0664); |
|
| 107 | 107 | |
| 108 | 108 | // Getting the salt defined in parameters.yml |
| 109 | 109 | $secret = $this->container->getParameter('secret'); |
| 110 | - $media->setSecretKey(hash('sha256', $secret.$media->getName().$media->getUri())); |
|
| 110 | + $media->setSecretKey(hash('sha256', $secret.$media->getName().$media->getUri())); |
|
| 111 | 111 | |
| 112 | - $em->persist($media); |
|
| 113 | - $em->flush(); |
|
| 112 | + $em->persist($media); |
|
| 113 | + $em->flush(); |
|
| 114 | 114 | |
| 115 | - return $media; |
|
| 116 | - } |
|
| 115 | + return $media; |
|
| 116 | + } |
|
| 117 | 117 | |
| 118 | 118 | public function cleanup() |
| 119 | 119 | { |
@@ -38,10 +38,10 @@ discard block |
||
| 38 | 38 | $media = $this->get('alpixel_media.manager')->upload($file, $this->get('request')->get('folder'), $lifetime); |
| 39 | 39 | $path = $mediaPreview->generatePathFromSecretKey($media->getSecretKey()); |
| 40 | 40 | $returnData[] = [ |
| 41 | - 'id' => $media->getSecretKey(), |
|
| 42 | - 'path' => $path, |
|
| 43 | - 'name' => $media->getName(), |
|
| 44 | - ]; |
|
| 41 | + 'id' => $media->getSecretKey(), |
|
| 42 | + 'path' => $path, |
|
| 43 | + 'name' => $media->getName(), |
|
| 44 | + ]; |
|
| 45 | 45 | } |
| 46 | 46 | } |
| 47 | 47 | |
@@ -97,7 +97,7 @@ discard block |
||
| 97 | 97 | |
| 98 | 98 | $path = 'upload/'.$media->getUri(); |
| 99 | 99 | |
| 100 | - // find the image and determine its type |
|
| 100 | + // find the image and determine its type |
|
| 101 | 101 | $image = $dataManager->find($filter, $path); |
| 102 | 102 | |
| 103 | 103 | // run the filter |
@@ -15,7 +15,7 @@ |
||
| 15 | 15 | public function configure() |
| 16 | 16 | { |
| 17 | 17 | $this->setName('alpixel:media:cleanup') |
| 18 | - ->setDescription('Cleaning unused media'); |
|
| 18 | + ->setDescription('Cleaning unused media'); |
|
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | public function execute(InputInterface $input, OutputInterface $output) |
@@ -16,7 +16,7 @@ |
||
| 16 | 16 | { |
| 17 | 17 | parent::configure(); |
| 18 | 18 | $this->setName('alpixel:media:liip_cleanup') |
| 19 | - ->setDescription('Cleaning liip cache folders'); |
|
| 19 | + ->setDescription('Cleaning liip cache folders'); |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | public function execute(InputInterface $input, OutputInterface $output) |