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.

130 lines
3.3 KiB

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