WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 22 months ago

Last modified 9 months ago

#17604 closed defect (bug) (fixed)

wp.uploadFile overwrite doesn't work

Reported by: M66B Owned by: wonderboymusic
Milestone: 4.4 Priority: normal
Severity: normal Version:
Component: XML-RPC Keywords: has-patch 3.6-early
Focuses: Cc:

Description

Removing the following lines from the function mw_newMediaObject file class-wp-xmlrpc-server.php solves the problem:

$filename = preg_replace('/^wpid\d+-/', '', $name);
$name = "wpid{$old_file->ID}-{$filename}";

Why are these lines there anyway? overwrite is overwrite.

This, presumably, bug exists in WP 3.1 too.

Attachments (1)

17604.diff (1.1 KB) - added by markoheijnen 5 years ago.
removes overwrite from mw_newMediaObject()

Download all attachments as: .zip

Change History (15)

#1 @M66B
6 years ago

  • Cc marcel@… added

#3 follow-up: @markoheijnen
5 years ago

Can we remove this functionality. I don't get why it is in their the first place because it isn't something WordPress does by itself

#4 in reply to: ↑ 3 @nacin
5 years ago

Replying to markoheijnen:

Can we remove this functionality. I don't get why it is in their the first place because it isn't something WordPress does by itself

Works for me.

#5 @nacin
5 years ago

  • Milestone changed from Awaiting Review to 3.5

@markoheijnen
5 years ago

removes overwrite from mw_newMediaObject()

#6 @markoheijnen
5 years ago

The patch removes overwrite functionality. Looked in to it more and it was never overwrite. It's more delete and upload a new one.

#7 @markoheijnen
5 years ago

  • Keywords has-patch added

#8 @nacin
5 years ago

  • Keywords 3.6-early added
  • Milestone changed from 3.5 to Future Release

#9 follow-up: @marco_hoyer
2 years ago

Hi there, what's the matter with this bug? I would like to have unique media names in my wordpress and thus overwrite existing files but if I set overwrite=True via xml-rpc api, strange things happen. Wordpress creates files with "wpid-" prefix, which seems to be caused by the two lines mentioned above. They also don't get overwritten but created several times with the same strange name.

My Wordpress: Version 4.1.1

My request:

POST /xmlrpc.php HTTP/1.1
Host: test.shop.de
Accept-Encoding: gzip
User-Agent: xmlrpclib.py/1.0.1 (by www.pythonware.com)
Content-Type: text/xml
Content-Length: 23651

<?xml version='1.0'?>
<methodCall>
<methodName>wp.uploadFile</methodName>
<params>
<param>
<value><int>0</int></value>
</param>
<param>
<value><string>my-user</string></value>
</param>
<param>
<value><string>my-secret</string></value>
</param>
<param>
<value><struct>
<member>
<name>bits</name>
<value><base64>
...
</base64></value>
</member>
<member>
<name>type</name>
<value><string>image/jpeg</string></value>
</member>
<member>
<name>name</name>
<value><string>test.jpg</string></value>
</member>
<member>
<name>overwrite</name>
<value><boolean>1</boolean></value>
</member>
</struct></value>
</param>
</params>
</methodCall>

Created Files:

{'type': 'image/jpeg', 'id': '571', 'file': 'wpid-test.jpg'}
{'type': 'image/jpeg', 'id': '572', 'file': 'wpid-test.jpg'}
{'type': 'image/jpeg', 'id': '573', 'file': 'wpid-test.jpg'}
{'type': 'image/jpeg', 'id': '574', 'file': 'wpid-test.jpg'}
{'type': 'image/jpeg', 'id': '575', 'file': 'wpid-test.jpg'}

#10 in reply to: ↑ 9 @tohokuaiki
2 years ago

  • Version 3.2 deleted

Replying to marco_hoyer:
Hi, I also encounter this curious behavior about overwrite parameter of XMLRPC uploadFile.

WordPress version is 4.2.2.

I expect that when overwrite parameter is true, an attachment file which has same filename and inherits same post id should be removed, and new attachment file via XMLRPC should be registered.

However, actually each time I post the attached file each time, I found that files continue to increase.
{'type': 'image/jpeg', 'id': '571', 'file': 'wpid-test.jpg'}
{'type': 'image/jpeg', 'id': '572', 'file': 'wpid-test1.jpg'}
{'type': 'image/jpeg', 'id': '573', 'file': 'wpid-test2.jpg'}
{'type': 'image/jpeg', 'id': '574', 'file': 'wpid-test3.jpg'}
{'type': 'image/jpeg', 'id': '575', 'file': 'wpid-test4.jpg'}
....

And, in case of "overwrite" parameter is false, every time I post I can get
{'type': 'image/jpeg', 'id': '571', 'file': 'test.jpg'}
{'type': 'image/jpeg', 'id': '572', 'file': 'test1.jpg'}
{'type': 'image/jpeg', 'id': '573', 'file': 'test2.jpg'}
{'type': 'image/jpeg', 'id': '574', 'file': 'test3.jpg'}
{'type': 'image/jpeg', 'id': '575', 'file': 'test4.jpg'}

What exactly is going on? :-?

#11 @tohokuaiki
2 years ago

I create wp.uploadFile wrapper XML-RPC method.

<?php
add_filter('xmlrpc_methods', 'add_junoe_xmlrpc_methods');

function add_junoe_xmlrpc_methods($methods)
{
    return array_merge($methods, array(
        'wp.JuploadFile'        => 'junoe_wp_uploadFile',
        ));
}

function junoe_wp_uploadFile($args)
{
    global $wp_xmlrpc_server;
    
    if (isset($args[3])) {
        $data = $args[3];
        
        if ((isset($data['overwrite']) && $data['overwrite']) &&
            (isset($data['post_id'])   && $data['post_id'])){
            $attachments = get_posts(array(
                'post_type' => array(
                    'attachment'
                    ),
                'post_parent' => $data['post_id'],
                'posts_per_page' => -1,
                'post_status' => 'inherit',
                ));
            foreach ($attachments as $attachment){
                if ($attachment->post_title == $data['name'] ||
                    $attachment->post_title == "wpid-".$data['name']){
                    wp_delete_attachment($attachment->ID, true);
                }
            }
        }
    }
    
    return $wp_xmlrpc_server->mw_newMediaObject($args);
}

This may remove if upload file name has prefix "wpid-". But, after XMLRPC fileupload with overwrite parameter off had done, these prefixed files has been created. :-(

Last edited 2 years ago by tohokuaiki (previous) (diff)

#12 @wonderboymusic
22 months ago

  • Milestone changed from Future Release to 4.4
  • Owner set to wonderboymusic
  • Status changed from new to assigned

#13 @wonderboymusic
22 months ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 34578:

XML-RPC: move the malfunctioning 'overwrite' code from wp_xmlrpc_server::mw_newMediaObject(). This was suggested 3 years ago.

Props markoheijnen.
Fixes #17604.

This ticket was mentioned in Slack in #core-images by markoheijnen. View the logs.


9 months ago

Note: See TracTickets for help on using tickets.