/**
  * @ApiDoc(
  *  resource=true,
  *  description="Increment object download count"
  * )
  * @Put("/stats/download/{entity}/{id}/")
  *
  * @param  Request         $request
  * @param $id
  * @param $entity
  * @return ObjectDownloads
  */
 public function putObjectDownloadAction(Request $request, $id, $entity)
 {
     $filePath = $request->get('file_path');
     $fileSize = $request->get('file_size') ? $request->get('file_size') : 0;
     $dm = $this->get('doctrine_mongodb')->getManager();
     $objectDownload = new ObjectDownloads();
     $objectDownload->setEntity($entity);
     $objectDownload->setFilePath($filePath);
     $objectDownload->setIpAddress($request->getClientIp());
     $objectDownload->setLogDate(new \DateTime("now"));
     $objectDownload->setObjectId($id);
     $objectDownload->setTransferSize($fileSize);
     $dm->persist($objectDownload);
     $dm->flush();
     return $objectDownload;
 }
Beispiel #2
0
 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;
 }