Skip to content

Add STDIN support to wp media import#221

Draft
Copilot wants to merge 5 commits intomainfrom
copilot/add-stdin-support-to-media-import
Draft

Add STDIN support to wp media import#221
Copilot wants to merge 5 commits intomainfrom
copilot/add-stdin-support-to-media-import

Conversation

Copy link

Copilot AI commented Feb 14, 2026

Plan: Add STDIN support to wp media import

  • Understand current implementation
  • Implement STDIN support
  • Add tests
  • Validate implementation
    • Run linter
    • Run code style checks
    • Run PHPStan
    • Run code review
    • Run security check
  • Address test failures
    • Fix file type detection using mime_content_type() instead of wp_check_filetype_and_ext()
    • Add check for empty STDIN content
    • Map MIME types to proper file extensions using wp_get_mime_types()

Changes for Test Fixes

The tests were failing because:

  1. WordPress's wp_check_filetype_and_ext() was rejecting files without proper filename extensions
  2. Empty STDIN was being processed by WordPress instead of being caught early

Fixed by:

  • Using mime_content_type() to detect actual file MIME type from content
  • Mapping MIME type to proper extension using WordPress's wp_get_mime_types()
  • Adding check for empty STDIN content with proper error message
  • Generating filename with correct extension (e.g., stdin-{timestamp}.png)
Original prompt

This section details on the original issue you should resolve

<issue_title>wp media import to be able to import from STDIN</issue_title>
<issue_description>For example:

$ curl http://example.com/large-file.png | wp media import -

Related:
wp-cli/entity-command#96</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits February 14, 2026 21:38
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI changed the title [WIP] Add support for importing media from STDIN Add STDIN support to wp media import Feb 14, 2026
Copilot AI requested a review from swissspidy February 14, 2026 21:42
@swissspidy
Copy link
Member

@copilot failing tests:

001 Scenario: Import media from STDIN                                                                    # features/media-import.feature:290
      When I run `cat {CACHE_DIR}/codeispoetry.png | wp media import - --title="From STDIN" --porcelain` # features/media-import.feature:295
        $ cat /tmp/wp-cli-test-cache/codeispoetry.png | wp media import - --title="From STDIN" --porcelain
        
        Warning: Unable to import file 'STDIN'. Reason: Sorry, this file type is not permitted for security reasons.
        cwd: /tmp/wp-cli-test-run--6990f6b8c9f091.32634377/
        run time: 0.24478697776794
        exit status: 1 (RuntimeException)

002 Scenario: Import media from STDIN with file_name                                                         # features/media-import.feature:304
      When I run `cat {CACHE_DIR}/codeispoetry.png | wp media import - --file_name=my-image.png --porcelain` # features/media-import.feature:309
        $ cat /tmp/wp-cli-test-cache/codeispoetry.png | wp media import - --file_name=my-image.png --porcelain
        
        Warning: Unable to import file 'STDIN'. Reason: Sorry, this file type is not permitted for security reasons.
        cwd: /tmp/wp-cli-test-run--6990f6ba122d80.59454036/
        run time: 0.2444121837616
        exit status: 1 (RuntimeException)

003 Scenario: Fail to import from STDIN when no input provided # features/media-import.feature:319
      Then STDERR should contain:                              # features/media-import.feature:321
        $ wp media import - </dev/null
        
        Warning: Unable to import file 'STDIN'. Reason: File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini or by post_max_size being defined as smaller than upload_max_filesize in php.ini.
        Error: No items imported.
        cwd: /tmp/wp-cli-test-run--6990f6ba7c4d19.58308543/
        run time: 0.24285101890564
        exit status: 1
        
        --- Expected
        +++ Actual
        @@ @@
        -Warning: Unable to import file from STDIN. Reason: No input provided.
        +Warning: Unable to import file 'STDIN'. Reason: File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini or by post_max_size being defined as smaller than upload_max_filesize in php.ini.
        +Error: No items imported.

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copy link
Author

Copilot AI commented Feb 15, 2026

@copilot failing tests:

...

Fixed in commit 560ac7a. The issue was that WordPress's file validation was rejecting files without proper extensions. Now using mime_content_type() to detect the actual MIME type from content and mapping it to the correct extension via wp_get_mime_types(). Also added check for empty STDIN content to provide the expected error message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wp media import to be able to import from STDIN

2 participants