-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement several helper API methods related to dealing with binary f…
…iles, blobs, and data URLs.
- Loading branch information
1 parent
aed69c1
commit a05d685
Showing
5 changed files
with
243 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
/** | ||
* Class Felix_Arntz\AI_Services\Services\API\Types\Blob | ||
* | ||
* @since n.e.x.t | ||
* @package ai-services | ||
*/ | ||
|
||
namespace Felix_Arntz\AI_Services\Services\API\Types; | ||
|
||
use InvalidArgumentException; | ||
|
||
/** | ||
* Simple value class representing a binary data blob, e.g. from a file. | ||
* | ||
* @since n.e.x.t | ||
*/ | ||
final class Blob { | ||
|
||
/** | ||
* The binary data of the blob. | ||
* | ||
* @since n.e.x.t | ||
* @var string | ||
*/ | ||
private $binary_data; | ||
|
||
/** | ||
* The MIME type of the blob. | ||
* | ||
* @since n.e.x.t | ||
* @var string | ||
*/ | ||
private $mime_type; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @param string $binary_data The binary data of the blob. | ||
* @param string $mime_type The MIME type of the blob. | ||
*/ | ||
public function __construct( string $binary_data, string $mime_type ) { | ||
$this->binary_data = $binary_data; | ||
$this->mime_type = $mime_type; | ||
} | ||
|
||
/** | ||
* Retrieves the binary data of the blob. | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @return string The binary data. | ||
*/ | ||
public function get_binary_data(): string { | ||
return $this->binary_data; | ||
} | ||
|
||
/** | ||
* Retrieves the MIME type of the blob. | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @return string The MIME type. | ||
*/ | ||
public function get_mime_type(): string { | ||
return $this->mime_type; | ||
} | ||
|
||
/** | ||
* Creates a new blob instance from a file. | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @param string $file The file path or URL. | ||
* @param string $mime_type Optional. MIME type, to override the automatic detection. Default empty string. | ||
* @return Blob The blob instance. | ||
* | ||
* @throws InvalidArgumentException Thrown if the file could not be read or if the MIME type cannot be determined. | ||
*/ | ||
public static function from_file( string $file, string $mime_type = '' ): self { | ||
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents | ||
$blob = file_get_contents( $file ); | ||
if ( ! $blob ) { | ||
throw new InvalidArgumentException( | ||
sprintf( 'Could not read file %s.', esc_html( $file ) ) | ||
); | ||
} | ||
|
||
if ( ! $mime_type ) { | ||
$file_type = wp_check_filetype( $file ); | ||
if ( ! $file_type['type'] ) { | ||
throw new InvalidArgumentException( | ||
sprintf( 'Could not determine MIME type of file %s.', esc_html( $file ) ) | ||
); | ||
} | ||
$mime_type = $file_type['type']; | ||
} | ||
|
||
return new self( $blob, $mime_type ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters