Skip to content

Commit

Permalink
Merge pull request #224 from hugosolar/fix/mime-type-not-being-sent-o…
Browse files Browse the repository at this point in the history
…n-put-blob
  • Loading branch information
jeffpaul authored Apr 23, 2024
2 parents b5fcb51 + 2a9e451 commit 01fc36e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
11 changes: 6 additions & 5 deletions includes/class-windows-azure-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() ),
Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 6 additions & 2 deletions includes/class-windows-azure-rest-api-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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();
Expand All @@ -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() );
}
Expand Down

0 comments on commit 01fc36e

Please sign in to comment.