public function onUpload(PostPersistEvent $event) { $response = $event->getResponse(); /** @var File $file */ $file = $event->getFile(); $uploadType = $event->getType(); $filePath = $file->getPathName(); $fileName = $file->getFileName(); $fileSize = $file->getSize(); $fileMimeType = $file->getMimeType(); // move to folder or create a nested folder structure $fileHelper = new FileHelper(); $uploadRootPath = $this->rootDir . '/../web/uploads/' . $uploadType . '/'; /** * @var string $uploaNestedDirs generated nested folder structure under rootpath. c33b/f671/1712/ */ $nestedDirs = $fileHelper->generatePath($fileName, true, $uploadRootPath); rename($filePath, $uploadRootPath . $nestedDirs . $fileName); $fileDir = $uploadRootPath . $nestedDirs; $uploadUrl = str_replace($uploadRootPath, $uploadType, $fileDir); if ($uploadType === 'avatarfiles') { $helper = new ImageResizeHelper(array('image_name' => $fileName, 'upload_dir' => $fileDir, 'upload_url' => $uploadUrl)); $helper->resize(); } $response['files'] = array('name' => $file->getFileName(), 'path' => $fileHelper->generatePath($file->getFileName(), false), 'size' => $fileSize, 'url' => '', 'mimeType' => $fileMimeType, 'fullpath' => DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . $uploadType . DIRECTORY_SEPARATOR . $fileHelper->generatePath($file->getFileName(), false)); return $response; }
public function downloadFileAction(Request $request, $id) { /** @var File $file */ $fileObject = $this->getDoctrine()->getManager()->find('OjsJournalBundle:File', $id); if (!$fileObject) { throw new NotFoundHttpException(); } $dm = $this->get('doctrine_mongodb')->getManager(); $objectDownload = new ObjectDownloads(); $objectDownload->setEntity('file'); $objectDownload->setFilePath($fileObject->getPath()); $objectDownload->setIpAddress($request->getClientIp()); $objectDownload->setLogDate(new \DateTime("now")); $objectDownload->setObjectId($id); $objectDownload->setTransferSize($fileObject->getSize()); $dm->persist($objectDownload); $dm->flush(); $objectDownload = new ObjectDownloads(); /** @var Article $article */ $article = $fileObject->getArticleFiles()->first(); $objectDownload->setEntity('article'); $objectDownload->setFilePath($fileObject->getPath()); $objectDownload->setIpAddress($request->getClientIp()); $objectDownload->setLogDate(new \DateTime("now")); $objectDownload->setObjectId($article->getId()); $objectDownload->setTransferSize($fileObject->getSize()); $dm->persist($objectDownload); $dm->flush(); $fileHelper = new FileHelper(); $file = $fileHelper->generatePath($fileObject->getName(), false) . $fileObject->getName(); $uploaddir = $this->get('kernel')->getRootDir() . '/../web/uploads/'; $yamlParser = new Parser(); $vars = $yamlParser->parse(file_get_contents($this->container->getParameter('kernel.root_dir') . '/config/media.yml')); $mappings = $vars['oneup_uploader']['mappings']; $url = false; $fullPath = null; foreach ($mappings as $key => $value) { if (is_file($uploaddir . $key . '/' . $file)) { $url = '/uploads/' . $key . '/' . $file; $fullPath = $uploaddir . $key . '/' . $file; break; } } if (!$url) { throw new NotFoundHttpException("File not found on drive"); } $response = new Response(); $content = \file_get_contents($fullPath); $response->headers->set('Content-Type', $fileObject->getMimeType()); $response->headers->set('Content-Length', filesize($fullPath)); $response->setContent($content); return $response; }
/** * @param $file * @return string */ public function generateFilePath($file) { $fileHelper = new FileHelper(); return $fileHelper->generatePath($file, false) . $file; }
/** * Edits an existing ArticleFile entity. * * @param Request $request * @param $id * @return RedirectResponse|Response */ public function updateAction(Request $request, $id) { $journal = $this->get('ojs.journal_service')->getSelectedJournal(); $em = $this->getDoctrine()->getManager(); /** @var ArticleFile $entity */ $entity = $em->getRepository('OjsJournalBundle:ArticleFile')->find($id); $file_entity = $entity->getFile(); if (!$entity) { throw $this->createNotFoundException('notFound'); } if (!$this->isGranted('EDIT', $journal, 'articles') && !$this->isGranted('EDIT', $entity->getArticle())) { throw new AccessDeniedException("You not authorized for this page!"); } $editForm = $this->createEditForm($entity, $journal->getId()); $editForm->handleRequest($request); $fileHelper = new FileHelper(); if ($editForm->isValid()) { $file = $request->request->get('file'); $file_entity->setName($file); $file_entity->setName($file); $imagePath = $this->get('kernel')->getRootDir() . '/../web/uploads/articlefiles/' . $fileHelper->generatePath($file, false); $file_entity->setSize(filesize($imagePath . $file)); $file_entity->setMimeType(mime_content_type($imagePath . $file)); $file_entity->setPath('/uploads/articlefiles/' . $fileHelper->generatePath($file, false)); $em->persist($file_entity); $em->flush(); $this->successFlashBag('Successfully updated.'); return $this->redirect($this->generateUrl('ojs_journal_article_file_edit', array('id' => $id, 'journalId' => $journal->getId(), 'articleId' => $entity->getArticleId()))); } return $this->render('OjsJournalBundle:ArticleFile:edit.html.twig', array('entity' => $entity, 'edit_form' => $editForm->createView())); }
/** * Edits an existing Article entity. * * @param Request $request * @param int $id * @return RedirectResponse|Response */ public function updateAction(Request $request, $id) { $journal = $this->get('ojs.journal_service')->getSelectedJournal(); /** @var Article $entity */ $em = $this->getDoctrine()->getManager(); $entity = $em->getRepository('OjsJournalBundle:Article')->findOneBy(array('journal' => $journal, 'id' => $id)); $this->throw404IfNotFound($entity); if (!$this->isGranted('EDIT', $entity->getJournal(), 'articles')) { throw new AccessDeniedException("You not authorized for this page!"); } $editForm = $this->createEditForm($entity, $journal); $editForm->handleRequest($request); if ($editForm->isValid()) { $header = $request->request->get('header'); if ($header) { $entity->setHeaderOptions(json_encode($header)); } $files = $request->request->get('articlefiles', []); $fileHelper = new FileHelper(); foreach ($files as $file) { $file_entity = new File(); $file_entity->setName($file); $imagepath = $this->get('kernel')->getRootDir() . '/../web/uploads/articlefiles/' . $fileHelper->generatePath($file, false); $file_entity->setSize(filesize($imagepath . $file)); $file_entity->setMimeType(mime_content_type($imagepath . $file)); $file_entity->setPath('/uploads/articlefiles/' . $fileHelper->generatePath($file, false)); $em->persist($file_entity); $articleFile = new ArticleFile(); $articleFile->setArticle($entity); $articleFile->setFile($file_entity); $articleFile->setType(0); // TODO: See ArticleFileParams::$FILE_TYPES $articleFile->setVersion(1); $articleFile->setLangCode('tr'); // TODO: Don't use hardcoded locale $em->persist($articleFile); $entity->addArticleFile($articleFile); } $em->flush(); $this->successFlashBag('successful.update'); return $this->redirect($this->generateUrl('ojs_journal_article_edit', array('id' => $id, 'journalId' => $journal->getId()))); } return $this->render('OjsJournalBundle:Article:edit.html.twig', ['entity' => $entity, 'edit_form' => $editForm->createView()]); }