Example #1
0
 public function uploadAction(Request $request)
 {
     $handle = $request->files->get('Filedata');
     if (substr($handle->getClientOriginalName(), strrpos($handle->getClientOriginalName(), ".") + 1) == "php") {
         throw new AccessDeniedException();
     }
     $upload = new Upload();
     $ownerType = $request->request->get('ownerType');
     $ownerId = $request->request->get('ownerId');
     $fileName = $handle->getClientOriginalName();
     $extension = null;
     $folder = $this->container->getParameter('lowbi.form.file.folder');
     $folder .= '/' . date("Y") . '/' . date("m") . '/' . date("d");
     $uploadDir = $this->container->getParameter('lowbi.form.file.upload_dir');
     $uploadDir .= '/' . date("Y") . '/' . date("m") . '/' . date("d");
     //$value = print_r($request,true);
     $logger = $this->get('logger');
     //$logger->info('------------>'. var_export($request->request->get('ownerType'), true));
     if ($this->container->hasParameter('lowbi.form.file.disable_guess_extension')) {
         $name = uniqid() . '.' . substr($handle->getClientOriginalName(), strrpos($handle->getClientOriginalName(), ".") + 1);
         $extension = substr($handle->getClientOriginalName(), strrpos($handle->getClientOriginalName(), ".") + 1);
         $realName = $name;
     } else {
         $name = uniqid() . '.' . $handle->guessExtension();
         $extension = $handle->guessExtension();
         $realName = $name;
     }
     $json = array();
     if (false !== ($handle = $handle->move($uploadDir, $name))) {
         $json = array('result' => '1', 'thumbnail' => array(), 'image' => array(), 'file' => '');
         if (0 === strpos($handle->getMimeType(), 'image')) {
             $handle = new Image($handle->getPathname());
             $thumbnail = $handle;
             if (true === $this->container->hasParameter('lowbi.form.image.thumbnails')) {
                 $thumbnails = $this->container->getParameter('lowbi.form.image.thumbnails');
                 foreach ($thumbnails as $name => $thumbnail) {
                     $handle->createThumbnail($name, $thumbnail[0], $thumbnail[1]);
                 }
                 $selected = key(reset($thumbnails));
                 if (true === $this->container->hasParameter('lowbi.form.image.selected')) {
                     $selected = $this->container->getParameter('lowbi.form.image.selected');
                 }
                 $thumbnail = $handle->getThumbnail($selected);
             }
             $json = array_replace($json, array('thumbnail' => array('file' => $folder . '/' . $thumbnail->getFilename() . '?' . time(), 'width' => $thumbnail->getWidth(), 'height' => $thumbnail->getHeight()), 'image' => array('width' => $handle->getWidth(), 'height' => $handle->getHeight())));
         }
         $json['file'] = $folder . '/' . $handle->getFilename() . '?' . time();
     } else {
         $json['result'] = '0';
     }
     //save metadata in Upload entity
     $em = $this->getDoctrine()->getEntityManager();
     try {
         $upload->setName($fileName);
         $upload->setUniqId($realName);
         $logger->info('------------>' . $ownerType);
         $logger->info('------------>' . strlen($ownerType));
         if (strlen($ownerType) >= 5) {
             //check for null
             $upload->setOwnerType($ownerType);
             $upload->setOwnerId($ownerId);
         }
         $upload->setExtension($extension);
         $upload->setPath('/web/' . $folder);
         $upload->setSize(round(filesize($uploadDir . '/' . $realName) / 1024));
         $em->persist($upload);
         $em->flush();
     } catch (\Exception $ex) {
         $logger->info('------------>' . print_r($ex));
     }
     $json['file'] = $upload->getId();
     return new Response(json_encode($json));
 }