class-file-size.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Services
  6. */
  7. /**
  8. * Represents the file size service.
  9. */
  10. class WPSEO_File_Size_Service {
  11. /**
  12. * Retrieves an indexable.
  13. *
  14. * @param WP_REST_Request $request The request object.
  15. *
  16. * @return WP_REST_Response The response.
  17. */
  18. public function get( WP_REST_Request $request ) {
  19. try {
  20. $file_url = $this->get_file_url( $request );
  21. return new WP_REST_Response(
  22. [
  23. 'type' => 'success',
  24. 'size_in_bytes' => $this->get_file_size( $file_url ),
  25. ],
  26. 404
  27. );
  28. }
  29. catch ( WPSEO_File_Size_Exception $exception ) {
  30. return new WP_REST_Response(
  31. [
  32. 'type' => 'failure',
  33. 'response' => $exception->getMessage(),
  34. ],
  35. 404
  36. );
  37. }
  38. }
  39. /**
  40. * Retrieves the file url.
  41. *
  42. * @param WP_REST_Request $request The request to retrieve file url from.
  43. *
  44. * @return string The file url.
  45. * @throws WPSEO_File_Size_Exception The file is hosted externally.
  46. */
  47. protected function get_file_url( WP_REST_Request $request ) {
  48. $file_url = rawurldecode( $request->get_param( 'url' ) );
  49. if ( ! $this->is_externally_hosted( $file_url ) ) {
  50. return $file_url;
  51. }
  52. throw WPSEO_File_Size_Exception::externally_hosted( $file_url );
  53. }
  54. /**
  55. * Checks if the file is hosted externally.
  56. *
  57. * @param string $file_url The file url.
  58. *
  59. * @return bool True if it is hosted externally.
  60. */
  61. protected function is_externally_hosted( $file_url ) {
  62. return wp_parse_url( home_url(), PHP_URL_HOST ) !== wp_parse_url( $file_url, PHP_URL_HOST );
  63. }
  64. /**
  65. * Returns the file size.
  66. *
  67. * @param string $file_url The file url to get the size for.
  68. *
  69. * @return int The file size.
  70. * @throws WPSEO_File_Size_Exception Retrieval of file size went wrong for unknown reasons.
  71. */
  72. protected function get_file_size( $file_url ) {
  73. $file_config = wp_upload_dir();
  74. $file_url = str_replace( $file_config['baseurl'], '', $file_url );
  75. $file_size = $this->calculate_file_size( $file_url );
  76. if ( ! $file_size ) {
  77. throw WPSEO_File_Size_Exception::unknown_error( $file_url );
  78. }
  79. return $file_size;
  80. }
  81. /**
  82. * Calculates the file size using the Utils class.
  83. *
  84. * @param string $file_url The file to retrieve the size for.
  85. *
  86. * @return int|bool The file size or False if it could not be retrieved.
  87. */
  88. protected function calculate_file_size( $file_url ) {
  89. return WPSEO_Image_Utils::get_file_size(
  90. [
  91. 'path' => $file_url,
  92. ]
  93. );
  94. }
  95. }