self::MAX_BYTES ) { return new WP_Error( 'forwp_drive_media_size', __( 'Image is too large to preview.', '4wp-drive' ) ); } $mime = self::detect_mime( $name, $bytes ); if ( is_wp_error( $mime ) ) { return $mime; } if ( '' === $name ) { $name = 'image'; } return array( 'bytes' => $bytes, 'mime' => $mime, 'name' => $name, ); } /** * @return string|WP_Error */ public static function detect_mime( string $name, string $bytes ) { if ( '' === $bytes ) { return new WP_Error( 'forwp_drive_media_empty', __( 'Empty file.', '4wp-drive' ) ); } $info = @getimagesizefromstring( $bytes ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged $mime = is_array( $info ) && ! empty( $info['mime'] ) ? strtolower( (string) $info['mime'] ) : ''; if ( self::is_allowed_mime( $mime ) ) { return $mime; } return new WP_Error( 'forwp_drive_media_type', __( 'This file is not a previewable image.', '4wp-drive' ) ); } /** * @return string|WP_Error */ private static function download( string $source_slug, string $file_id ) { if ( GitHub_Source::SLUG === $source_slug || 0 === strpos( $file_id, 'gh:' ) ) { $path = GitHub_Source::path_from_file_id( $file_id ); if ( '' === $path ) { return new WP_Error( 'forwp_drive_github_path', __( 'Could not resolve GitHub file path.', '4wp-drive' ) ); } return ( new GitHub_Client() )->get_file_contents( $path ); } if ( Google_Drive_Source::SLUG !== $source_slug && '' !== $source_slug ) { return new WP_Error( 'forwp_drive_media_source', __( 'This source cannot preview files.', '4wp-drive' ) ); } return ( new Google_Drive_Client( Google_OAuth::instance() ) )->download_file( $file_id ); } private static function is_allowed_mime( string $mime ): bool { return in_array( $mime, array( 'image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/avif', ), true ); } }