/** * 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); }
/** * @param Contribution $contribution * @param File $file * * @return string */ public function generateRelativePath(Contribution $contribution, File $file) { $path = $contribution->getAuthProvider() . DIRECTORY_SEPARATOR; $path .= $contribution->getIdentifier(); $path .= '.' . ($file->guessExtension() ?: $file->getExtension()); return $path; }
/** * @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()); }