ApiLegacyStorage.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Service\File\Storage;
  4. use App\Entity\Core\File;
  5. use App\Enum\Core\FileHostEnum;
  6. use App\Service\ApiLegacy\ApiLegacyRequestService;
  7. use App\Service\Utils\UrlBuilder;
  8. use Liip\ImagineBundle\Imagine\Data\DataManager;
  9. /**
  10. * Read and write files into the Opentalent API v1 storage.
  11. */
  12. class ApiLegacyStorage implements FileStorageInterface
  13. {
  14. public function __construct(
  15. private readonly ApiLegacyRequestService $apiLegacyRequestService,
  16. protected readonly DataManager $dataManager,
  17. protected readonly UrlBuilder $urlBuilder,
  18. protected readonly string $legacyBaseUrl,
  19. protected readonly string $publicLegacyBaseUrl,
  20. ) {
  21. }
  22. public function exists(File $file): bool
  23. {
  24. throw new \RuntimeException('not implemented error');
  25. }
  26. /**
  27. * Reads the given file and returns its content as a string.
  28. */
  29. public function read(File $file): string
  30. {
  31. $url = sprintf('_internal/secure/files/%s', $file->getId());
  32. return $this->apiLegacyRequestService->getContent($url);
  33. }
  34. /**
  35. * Retoune l'URL de l'image, à la bonne taille, contenu dans la File.
  36. */
  37. public function getImageUrl(File $file, string $size, bool $relativePath): string
  38. {
  39. $url = sprintf('api/files/%s/download/%s?relativePath=1', $file->getId(), $size);
  40. // L'url interne est l'équivalent d'un chemin relatif dans ce cas
  41. $baseUrl = $relativePath ? $this->legacyBaseUrl : $this->publicLegacyBaseUrl;
  42. return UrlBuilder::concat($baseUrl, [$this->apiLegacyRequestService->getContent($url)], []);
  43. }
  44. public function support(File $file): bool
  45. {
  46. return $file->getHost() === FileHostEnum::API1;
  47. }
  48. /**
  49. * Permanently delete the entire file storage of the given Organization.
  50. */
  51. public function deleteOrganizationFiles(int $organizationId): void
  52. {
  53. $url = sprintf('/_internal/request/organization-files/delete/%s', $organizationId);
  54. $this->apiLegacyRequestService->get($url);
  55. }
  56. /**
  57. * Permanently delete the entire file storage of the given Person.
  58. */
  59. public function deletePersonFiles(int $personId): void
  60. {
  61. $url = sprintf('/_internal/request/person-files/delete/%s', $personId);
  62. $this->apiLegacyRequestService->get($url);
  63. }
  64. }