|
|
@@ -3,6 +3,7 @@
|
|
|
namespace App\Service\Storage;
|
|
|
|
|
|
use App\Entity\Core\File;
|
|
|
+use App\Service\Api1\Api1RequestService;
|
|
|
use App\Service\Utils\UrlBuilder;
|
|
|
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
|
|
|
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
|
|
|
@@ -16,9 +17,8 @@ use Symfony\Contracts\HttpClient\HttpClientInterface;
|
|
|
class Api1Storage implements FileStorageInterface
|
|
|
{
|
|
|
public function __construct(
|
|
|
- private HttpClientInterface $api_client
|
|
|
- )
|
|
|
- {}
|
|
|
+ private Api1RequestService $api1RequestService
|
|
|
+ ) {}
|
|
|
|
|
|
/**
|
|
|
* Reads the given file and returns its content as a string
|
|
|
@@ -28,31 +28,6 @@ class Api1Storage implements FileStorageInterface
|
|
|
*/
|
|
|
public function read(File $file): string
|
|
|
{
|
|
|
- $headers = [
|
|
|
- 'x-accessid' => $_REQUEST['AccessId'],
|
|
|
- 'SFSESSID' => $_REQUEST['SFSESSID'],
|
|
|
- 'BEARER' => $_REQUEST['BEARER'],
|
|
|
- 'authorization' => 'BEARER ' . $_REQUEST['BEARER'],
|
|
|
- ];
|
|
|
-
|
|
|
- try {
|
|
|
- $response = $this->api_client->request(
|
|
|
- 'POST',
|
|
|
- 'api/files/' . $file->getId() .'/download',
|
|
|
- ['headers' => $headers]
|
|
|
- );
|
|
|
- $content = $response->getContent();
|
|
|
- } catch (ClientExceptionInterface $e) {
|
|
|
- throw new \RuntimeException("File " . $file->getId() . " not found", 0, $e);
|
|
|
- } catch (ServerExceptionInterface | RedirectionExceptionInterface | TransportExceptionInterface $e) {
|
|
|
- throw new \RuntimeException("File could not be fetched because of a server error", 0, $e);
|
|
|
- }
|
|
|
-
|
|
|
- // When the user isn't well authenticated, the api may redirect to the login page or to the portail
|
|
|
- if (str_contains($content, "<!DOCTYPE html>") && str_contains($content, "This website is powered by TYPO3")) {
|
|
|
- throw new \RuntimeException("Permission error");
|
|
|
- }
|
|
|
-
|
|
|
- return $content;
|
|
|
+ return $this->api1RequestService->getContent('api/files/' . $file->getId() .'/download');
|
|
|
}
|
|
|
}
|