Skip to content

Commit 560ac7a

Browse files
Copilotswissspidy
andcommitted
Fix STDIN file type detection using mime_content_type
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 9ef4106 commit 560ac7a

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

src/Media_Command.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,8 @@ public function import( $args, $assoc_args = array() ) {
330330

331331
// Read from STDIN and save to a temporary file
332332
$stdin_content = file_get_contents( 'php://stdin' );
333-
if ( false === $stdin_content ) {
334-
WP_CLI::warning( 'Unable to import file from STDIN. Reason: Could not read STDIN.' );
333+
if ( false === $stdin_content || empty( $stdin_content ) ) {
334+
WP_CLI::warning( 'Unable to import file from STDIN. Reason: No input provided.' );
335335
++$errors;
336336
continue;
337337
}
@@ -344,10 +344,24 @@ public function import( $args, $assoc_args = array() ) {
344344
continue;
345345
}
346346

347-
// Try to determine file extension from content
348-
$filetype = wp_check_filetype_and_ext( $tempfile, '' );
349-
$ext = ! empty( $filetype['ext'] ) ? '.' . $filetype['ext'] : '';
350-
$name = 'stdin-' . time() . $ext;
347+
// Determine file extension from content
348+
$mimetype = mime_content_type( $tempfile );
349+
350+
// Map MIME type to extension
351+
$ext = '';
352+
if ( $mimetype && function_exists( 'wp_get_mime_types' ) ) {
353+
$mime_types = wp_get_mime_types();
354+
foreach ( $mime_types as $exts => $mime ) {
355+
if ( $mime === $mimetype ) {
356+
$ext_array = explode( '|', $exts );
357+
$ext = '.' . $ext_array[0];
358+
break;
359+
}
360+
}
361+
}
362+
363+
// Generate filename with proper extension
364+
$name = 'stdin-' . time() . $ext;
351365

352366
$orig_filename = 'STDIN';
353367
$file_time = '';

0 commit comments

Comments
 (0)