Ejemplo n.º 1
0
 /**
  * Test that file is attached to the contribution
  */
 public function testAttachFileTo()
 {
     $fileAttacher = new FileAttacher($this->pathGenerator);
     $this->assertFalse($fileAttacher->attachFileTo($this->contribution));
     $this->contribution->setFileName('not_an_empty_name.jpg');
     $fileAttacher->attachFileTo($this->contribution);
     $this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\File\\File', $this->contribution->getFile());
 }
Ejemplo n.º 2
0
 /**
  * @param Contribution $contribution
  * @param File $file
  *
  * @return string
  */
 public function generateRelativePath(Contribution $contribution, File $file)
 {
     $path = $this->directory . DIRECTORY_SEPARATOR;
     $path .= $contribution->getAuthProvider() . DIRECTORY_SEPARATOR;
     $path .= $contribution->getIdentifier();
     $path .= '.' . ($file->guessExtension() ?: $file->getExtension());
     return $path;
 }
Ejemplo n.º 3
0
 /**
  * Generate a HaPHPy File and attach it to the contribution
  *
  * @param Contribution $contribution
  *
  * @return false|void
  */
 public function attachTo(Contribution $contribution)
 {
     if (!$contribution->getFileName()) {
         return false;
     }
     $absPath = $this->pathGenerator->getFileAbsolutePath($contribution);
     $file = new File($absPath);
     $contribution->setFile($file);
 }
 /**
  * @param Contribution $contribution
  */
 public function remove(Contribution $contribution)
 {
     if (!$contribution->getFileName()) {
         return;
     }
     unlink($this->pathGenerator->getFileAbsolutePath($contribution));
     $this->entityManager->remove($contribution);
     $this->entityManager->flush();
 }
Ejemplo n.º 5
0
 /**
  * Test if generated relative path for new contribution is ok
  */
 public function testGenerateRelativePath()
 {
     $pathGenerator = new PathGenerator($this->directory);
     $path = $this->contribution->getAuthProvider() . DIRECTORY_SEPARATOR;
     $path .= $this->contribution->getIdentifier();
     $path .= '.' . $this->extension;
     $relativePath = $pathGenerator->generateRelativePath($this->contribution, $this->file);
     $this->assertEquals($relativePath, $path);
 }
Ejemplo n.º 6
0
 /**
  * @param Request      $request
  * @param Contribution $contribution
  *
  * @Route(
  *     "/media/{auth}/{identifier}",
  *     name="media_serve",
  *     requirements={
  *         "auth": "github|twitter|facebook"
  *     }
  * )
  * @ParamConverter(
  *     "contribution",
  *     class="AppBundle:Contribution",
  *     options={
  *         "mapping": {
  *             "auth"       = "authProvider",
  *             "identifier" = "identifier"
  *         }
  *     }
  * )
  *
  * @return array for template
  */
 public function serveMediaAction(Request $request, Contribution $contribution)
 {
     $user = $this->getUser();
     // For both following tests, we don't want a redirection to login
     // So we return a raw Response (not throwing AccessDeniedException)
     if ($user === null) {
         return new Response('Unauthorized', 401);
     }
     $authProvider = $contribution->getAuthProvider() === $user->getAuthProvider();
     $identifier = $contribution->getIdentifier() === $user->getUsername();
     if (!($authProvider && $identifier)) {
         return new Response('Access denied', 403);
     }
     $this->get('haphpy.file_attacher')->attachTo($contribution);
     $headers = ['Cache-Control' => 'private', 'Content-type' => $contribution->getFile()->getMimeTypeForHtmlPlayer(), 'Content-Length' => $contribution->getFile()->getSize(), 'Content-Transfer-Encoding' => 'binary'];
     return new Response(file_get_contents($contribution->getFile()->getRealPath()), 200, $headers);
 }
 /**
  * Generate file path relative to contributed media directory
  *
  * @param Contribution $contribution
  * @param SplFileInfo  $file
  *
  * @return string
  */
 public function generateAbsolutePath(Contribution $contribution, File $file)
 {
     return $this->directory . DIRECTORY_SEPARATOR . $contribution->getAuthProvider() . DIRECTORY_SEPARATOR . $contribution->getIdentifier() . '.' . ($file->guessExtension() ?: $file->getExtension());
 }
 /**
  * get a contribution depending on user
  * If none yet, create one
  *
  * @param UserInterface $user Current user
  *
  * @return Contribution
  */
 private function getOrGenerateContribution(UserInterface $user = null)
 {
     if ($user) {
         $contribution = $this->getDoctrine()->getEntityManager()->getRepository('AFUP\\HaphpyBirthdayBundle\\Entity\\Contribution')->findOneBy(['authProvider' => $user->getAuthProvider(), 'identifier' => $user->getUsername()]);
         if ($contribution) {
             return $contribution;
         }
         $contribution = new Contribution();
         $contribution->setAuthProvider($user->getAuthProvider());
         $contribution->setIdentifier($user->getUsername());
         $contribution->setVisibleName($user->getVisibleName());
         return $contribution;
     }
     return new Contribution();
 }
Ejemplo n.º 9
0
 /**
  * Generate file path relative to contributed media directory
  *
  * @param Contribution $contribution
  *
  * @return string
  */
 public function getFileAbsolutePath(Contribution $contribution)
 {
     return $this->directory . DIRECTORY_SEPARATOR . $contribution->getFileName();
 }
Ejemplo n.º 10
0
 /**
  * @param Request      $request
  * @param Contribution $contribution
  *
  * @Route("/toggle-acceptance/{authProvider}/{identifier}", name="haphpy_admin_toggle")
  * @Template()
  * @ParamConverter(converter="contribution")
  *
  * @return \Symfony\Component\HttpFoundation\RedirectResponse
  */
 public function toggleVisibilityAction(Request $request, Contribution $contribution)
 {
     $contribution->setAccepted(!$contribution->isAccepted());
     $this->get('haphpy.contribution_persister')->updatePersistence($contribution);
     return $this->redirectToRoute('haphpy_admin_index');
 }
 /**
  * Update the EntityContribution depending on FormContribution
  *
  * @param EntityContribution $entityContribution
  * @param FormContribution   $formContribution
  */
 public static function updateEntityFromFormContribution(EntityContribution $entityContribution, FormContribution $formContribution)
 {
     $entityContribution->setCreditWanted($formContribution->creditWanted);
 }