You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
2.8 KiB

2 years ago
  1. <?php namespace WpsMcloud\Actions\Convert\Replacer;
  2. use WpsMcloud\Actions\Convert\ReplacerService;
  3. use WpsMcloud\Models\PostAttachment;
  4. class PostAttachmentReplacer
  5. {
  6. private PostAttachment $post;
  7. private ReplacerService $replacer;
  8. public string $mcloudPostUrl;
  9. public string $mcloudPostBaseUrl;
  10. public string $localPostBaseUrl;
  11. /**
  12. * @var string
  13. * @desc Filename with prepend parts after "wp-content/uploads/"
  14. */
  15. public string $key;
  16. /**
  17. * @var string Only prepends parts without filename
  18. */
  19. public string $baseKey;
  20. public function __construct(PostAttachment $post, ReplacerService $replacer)
  21. {
  22. $this->post = $post;
  23. $this->replacer = $replacer;
  24. $this->mcloudPostUrl = str_replace(
  25. $replacer->getUploadsDirBaseUrl(),
  26. $replacer->getMcloudBaseUrl(),
  27. $post->getUrl()
  28. );
  29. $this->mcloudPostBaseUrl = trailingslashit(
  30. str_replace($this->post->getBasename(), '', $this->mcloudPostUrl)
  31. );
  32. $this->localPostBaseUrl = trailingslashit(
  33. str_replace($this->post->getBasename(), '', $this->post->getUrl())
  34. );
  35. $this->key = ltrim(
  36. str_replace($replacer->getMcloudBaseUrl(), '', $this->mcloudPostUrl),
  37. '/',
  38. );
  39. $this->baseKey = ltrim(
  40. trailingslashit(
  41. str_replace(
  42. $this->post->getBasename(),
  43. '',
  44. $this->key
  45. )
  46. ),
  47. '/'
  48. );
  49. }
  50. public function getGeneralS3Info(): array
  51. {
  52. return $this->getS3Info($this->getMcloudPostUrl(), $this->getKey());
  53. }
  54. public function getSizeS3Info(string $sizeFilename): array
  55. {
  56. return $this->getS3Info(
  57. $this->getMcloudPostBaseUrl() . $sizeFilename,
  58. $this->getBaseKey() . $sizeFilename
  59. );
  60. }
  61. private function getS3Info(string $url, string $key): array
  62. {
  63. return [
  64. 'url' => $url,
  65. 'bucket' => $this->replacer->getBucket(),
  66. 'provider' => 's3',
  67. 'privacy' => ReplacerService::S3_ACL,
  68. 'v' => MEDIA_CLOUD_INFO_VERSION,
  69. 'key' => $key,
  70. 'options' => [],
  71. 'mime-type' => $this->post->getPostMimeType(),
  72. ];
  73. }
  74. public function getMcloudPostUrl(): string
  75. {
  76. return $this->mcloudPostUrl;
  77. }
  78. public function getMcloudPostBaseUrl(): string
  79. {
  80. return $this->mcloudPostBaseUrl;
  81. }
  82. public function getLocalPostBaseUrl(): string
  83. {
  84. return $this->localPostBaseUrl;
  85. }
  86. public function getKey(): string
  87. {
  88. return $this->key;
  89. }
  90. public function getBaseKey(): string
  91. {
  92. return $this->baseKey;
  93. }
  94. }