ApiLegacyStorage.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Service\File\Storage;
  4. use App\Entity\Access\Access;
  5. use App\Entity\Core\File;
  6. use App\Enum\Core\FileHostEnum;
  7. use App\Service\ApiLegacy\ApiLegacyRequestService;
  8. use App\Service\Utils\UrlBuilder;
  9. use Liip\ImagineBundle\Imagine\Data\DataManager;
  10. /**
  11. * Read and write files into the Opentalent API v1 storage
  12. */
  13. class ApiLegacyStorage implements FileStorageInterface
  14. {
  15. public function __construct(
  16. private readonly ApiLegacyRequestService $apiLegacyRequestService,
  17. protected readonly DataManager $dataManager,
  18. protected readonly UrlBuilder $urlBuilder,
  19. protected readonly string $legacyBaseUrl
  20. )
  21. {}
  22. public function exists(File $file): bool {
  23. throw new \RuntimeException('not implemented error');
  24. }
  25. /**
  26. * Reads the given file and returns its content as a string
  27. *
  28. * @param File $file
  29. * @return string
  30. */
  31. public function read(File $file): string
  32. {
  33. $url = sprintf('_internal/secure/files/%s', $file->getId());
  34. return $this->apiLegacyRequestService->getContent($url);
  35. }
  36. /**
  37. * Retoune l'URL de l'image, à la bonne taille, contenu dans la File
  38. * @param File $file
  39. * @param string $size
  40. * @param bool $relativePath
  41. * @return string
  42. */
  43. public function getImageUrl(File $file, string $size, bool $relativePath): string{
  44. $url = sprintf('api/files/%s/download/%s?relativePath=1', $file->getId(), $size);
  45. return UrlBuilder::concat($this->legacyBaseUrl, [$this->apiLegacyRequestService->getContent($url)], []);
  46. }
  47. /**
  48. * @param File $file
  49. * @return bool
  50. */
  51. public function support(File $file): bool
  52. {
  53. return $file->getHost() === FileHostEnum::API1;
  54. }
  55. }