/** * {@inheritdoc} */ public function buildView(FormView $view, FormInterface $form) { $request = $this->request; $configs = $form->getAttribute('configs'); $data = $form->getClientData(); $fileMeta = new Upload(); $uploadDir = "/web/temp"; $uploadDir .= '/' . date("Y") . '/' . date("m") . '/' . date("d"); $relativeFilePath = $uploadDir; $absolutePath = str_replace('/', '//', $request->getRootDir()); $absolutePath = str_replace('\\', '\\\\', $absolutePath); if (!empty($data)) { $absoluteFilePath = $request->getRootDir() . '/../' . $data->getPath() . '/' . $data->getUniqId(); $relativeFilePath = $data->getPath() . '/' . $data->getUniqId(); $relativePath = $data->getPath(); $fileMeta = $this->em->getRepository('LowbiSystemBundle:Upload')->find($data->getId()); if ($fileMeta == null) { $fileMeta = new Upload(); } if ($fileMeta->getTodelete()) { try { $this->em->remove($fileMeta); $this->em->flush(); } catch (\EXception $ex) { } $view->set('filePath', $relativeFilePath)->set('absolutePath', $absolutePath)->set('value', null)->set('fileMeta', $fileMeta); return; } if (!$data instanceof Image) { if ($data instanceof File) { $data = new \Lowbi\SystemBundle\Gd\File\Image($absoluteFilePath); } else { $data = new \Lowbi\SystemBundle\Gd\File\Image($absoluteFilePath); } } //if ($data->hasThumbnail($this->selected)) { $thumbnail = $data->getThumbnail($this->selected); $view->set('thumbnail', array('file' => $relativePath . '/' . $thumbnail->getFilename(), 'width' => $thumbnail->getWidth(), 'height' => $thumbnail->getHeight())); //} if ($configs['custom_storage_folder'] && false === ($value = $form->getClientData()) instanceof File) { // This if will be executed only when we load entity with existing file pointed to the folder different // from $configs['folder'] } else { $value = $configs['folder'] . '/' . $data->getFilename(); } $view->set('value', $value)->set('file', $value)->set('width', $data->getWidth())->set('height', $data->getHeight())->set('filePath', $relativeFilePath)->set('absolutePath', $absolutePath)->set('fileMeta', $fileMeta); } else { $view->set('filePath', $relativeFilePath)->set('absolutePath', $absolutePath)->set('fileMeta', $fileMeta); } $view->set('filters', $this->filters); }
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)); }
/** * {@inheritdoc} */ public function buildView(FormView $view, FormInterface $form) { $parentForm = $form->getParent(); if ($parentForm->getClientData() != null && $parentForm->getClientData() != "") { $ownerType = get_class($parentForm->getClientData()); $ownerId = $parentForm->getClientData()->getId(); } else { $ownerType = null; $ownerId = null; } $configs = $form->getAttribute('configs'); //$datas = $form->getClientData(); $datas = null; if ($datas == null) { $datas = $form->getParent()->getClientData(); } $fileMeta = new Upload(); if ($datas != null) { if ($datas->getId() != null) { $fileMeta = $this->em->getRepository('LowbiSystemBundle:Upload')->findOneBy(array('ownerType' => $ownerType, 'ownerId' => $datas->getId())); if ($fileMeta == null) { $fileMeta = new Upload(); } } } if (false === empty($datas) && $fileMeta->getId() != null) { $value = $fileMeta->getPath() . '/' . $fileMeta->getUniqId(); $view->set('value', $value); } else { $view->set('value', null); } $max_upload = (int) ini_get('upload_max_filesize'); $user_upload = $form->getAttribute('sizeLimit'); $uploadLimit = 0; if ($user_upload > $max_upload) { $uploadLimit = $max_upload * 1000000; } else { $uploadLimit = $user_upload * 1000000; } $view->set('type', 'hidden')->set('session', $this->session)->set('fileDesc', $form->getAttribute('fileDesc'))->set('fileExt', $form->getAttribute('fileExt'))->set('sizeLimit', $uploadLimit)->set('configs', $form->getAttribute('configs'))->set('fileMeta', $fileMeta)->set('ownerType', $ownerType)->set('ownerId', $ownerId); }