From 2a9e451eb259e40d3c6fa6e7205a2c60af2ade12 Mon Sep 17 00:00:00 2001 From: Hugo Solar Date: Mon, 15 Apr 2024 16:25:30 -0400 Subject: [PATCH] fix content type issue defaulting to octet-stream --- includes/class-windows-azure-helper.php | 11 ++++++----- includes/class-windows-azure-rest-api-client.php | 8 ++++++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/includes/class-windows-azure-helper.php b/includes/class-windows-azure-helper.php index d95643b0..5f908b69 100644 --- a/includes/class-windows-azure-helper.php +++ b/includes/class-windows-azure-helper.php @@ -410,14 +410,15 @@ static public function put_uploaded_file_to_blob_storage( $container_name, $blob $remote_path = self::get_unique_blob_name( $container_name, $blob_name ); - $result = $rest_api_client->put_blob( $container_name, $local_path, $remote_path, true ); - if ( ! $result || is_wp_error( $result ) ) { - return $result; - } $finfo = finfo_open( FILEINFO_MIME_TYPE ); $mime_type = finfo_file( $finfo, $local_path ); finfo_close( $finfo ); + $result = $rest_api_client->put_blob( $container_name, $local_path, $remote_path, true, $mime_type ); + if ( ! $result || is_wp_error( $result ) ) { + return $result; + } + $rest_api_client->put_blob_properties( $container_name, $remote_path, array( Windows_Azure_Rest_Api_Client::API_HEADER_MS_BLOB_CONTENT_TYPE => $mime_type, Windows_Azure_Rest_Api_Client::API_HEADER_CACHE_CONTROL => sprintf( "max-age=%d, must-revalidate", Windows_Azure_Helper::get_cache_control() ), @@ -479,7 +480,7 @@ static public function put_media_to_blob_storage( $container_name, $blob_name, $ list( $account_name, $account_key ) = self::get_api_credentials( $account_name, $account_key ); $rest_api_client = new Windows_Azure_Rest_Api_Client( $account_name, $account_key ); - $result = $rest_api_client->put_blob( $container_name, $local_path, $blob_name ); + $result = $rest_api_client->put_blob( $container_name, $local_path, $blob_name, false, $mime_type ); if ( ! $result || is_wp_error( $result ) ) { return $result; } diff --git a/includes/class-windows-azure-rest-api-client.php b/includes/class-windows-azure-rest-api-client.php index 7f5a77a5..29f19ef6 100644 --- a/includes/class-windows-azure-rest-api-client.php +++ b/includes/class-windows-azure-rest-api-client.php @@ -48,6 +48,7 @@ use MicrosoftAzure\Storage\Blob\Models\ListContainersOptions; use MicrosoftAzure\Storage\Blob\Models\SetBlobPropertiesOptions; use MicrosoftAzure\Storage\Blob\Models\SetBlobTierOptions; +use MicrosoftAzure\Storage\Blob\Models\CreateBlockBlobOptions; class Windows_Azure_Rest_Api_Client { @@ -985,10 +986,11 @@ public function put_blobs( $container, array $files = array() ) { * @param string $local_path Local path. * @param string $remote_path Remote path. * @param bool $force_direct_file_access Whether to force direct file access. + * @param string $content_type File content type * * @return bool|string|WP_Error Newly put blob URI or WP_Error|false on failure. */ - public function put_blob( $container, $local_path, $remote_path, $force_direct_file_access = false ) { + public function put_blob( $container, $local_path, $remote_path, $force_direct_file_access = false, $content_type = 'application/octet-stream' ) { $blobClient = BlobRestProxy::createBlobService( $this->_connection_string ); $contents_provider = new Windows_Azure_File_Contents_Provider( $local_path, null ); $is_valid = $contents_provider->is_valid(); @@ -998,10 +1000,12 @@ public function put_blob( $container, $local_path, $remote_path, $force_direct_f } $blob_content = fopen( $contents_provider->get_file_path(), 'r' ); + $blob_options = new CreateBlockBlobOptions(); + $blob_options->setContentType( $content_type ); //Upload blob. try { - $blobClient->createBlockBlob( $container, $remote_path, $blob_content ); + $blobClient->createBlockBlob( $container, $remote_path, $blob_content, $blob_options ); } catch ( Exception $exception ) { return new \WP_Error( $exception->getMessage() ); }