Change xmlrpc upload logic. Props Joseph Scott.

git-svn-id: http://svn.automattic.com/wordpress/trunk@5008 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2007-03-09 20:14:52 +00:00
parent 2a11c99459
commit 2051c80cac
2 changed files with 19 additions and 51 deletions

View File

@ -1094,7 +1094,7 @@ function wp_upload_dir() {
return apply_filters('upload_dir', $uploads); return apply_filters('upload_dir', $uploads);
} }
function wp_upload_bits($name, $type, $bits, $overwrite = false) { function wp_upload_bits($name, $type, $bits) {
if ( empty($name) ) if ( empty($name) )
return array('error' => __("Empty filename")); return array('error' => __("Empty filename"));
@ -1122,21 +1122,10 @@ function wp_upload_bits($name, $type, $bits, $overwrite = false) {
$filename = str_replace("$number$ext", ++$number . $ext, $filename); $filename = str_replace("$number$ext", ++$number . $ext, $filename);
} }
// If we are asked to over write the file then make sure $new_file = $upload['path'] . "/$filename";
// the $name has the complete path and is writable. if ( ! wp_mkdir_p( dirname($new_file) ) ) {
if($overwrite) { $message = sprintf(__('Unable to create directory %s. Is its parent directory writable by the server?'), dirname($new_file));
if(!is_writable($name)) { return array('error' => $message);
return(array("error" => __("Can not over write file.")));
}
$new_file = $name;
$filename = basename($name);
}
else {
$new_file = $upload['path'] . "/$filename";
if ( ! wp_mkdir_p( dirname($new_file) ) ) {
$message = sprintf(__('Unable to create directory %s. Is its parent directory writable by the server?'), dirname($new_file));
return array('error' => $message);
}
} }
$ifp = @ fopen($new_file, 'wb'); $ifp = @ fopen($new_file, 'wb');
@ -1151,11 +1140,8 @@ function wp_upload_bits($name, $type, $bits, $overwrite = false) {
$perms = $perms & 0000666; $perms = $perms & 0000666;
@ chmod($new_file, $perms); @ chmod($new_file, $perms);
// Compute the URL if this is a new file. // Compute the URL
$url = $upload['url'] . "/$filename"; $url = $upload['url'] . "/$filename";
if($overwrite) {
$url = $name;
}
return array('file' => $new_file, 'url' => $url, 'error' => false); return array('file' => $new_file, 'url' => $url, 'error' => false);
} }

View File

@ -1383,29 +1383,22 @@ class wp_xmlrpc_server extends IXR_Server {
$type = $data['type']; $type = $data['type'];
$bits = $data['bits']; $bits = $data['bits'];
// Default to new file, not over write.
$overwrite = false;
if(!empty($data["overwrite"]) && ($data["overwrite"] == true)) { if(!empty($data["overwrite"]) && ($data["overwrite"] == true)) {
$overwrite = true;
// If the file isn't writable then error out now.
if(!is_writable($data["name"])) {
return(new IXR_Error(500, "File is not writable, over write failed."));
}
// We now know the file is good so don't use the sanitized name.
$name = $data["name"];
// Get postmeta info on the object. // Get postmeta info on the object.
$old_meta = $wpdb->get_row(" $old_file = $wpdb->get_row("
SELECT * SELECT ID
FROM {$wpdb->postmeta} FROM {$wpdb->posts}
WHERE meta_key = '_wp_attached_file' WHERE post_title = '{$name}'
AND meta_value = '{$name}' AND post_type = 'attachment'
"); ");
// Get post info on the object. // Delete previous file.
$old_post = get_post($old_meta->post_id); wp_delete_attachment($old_file->ID);
// Make sure the new name is different by pre-pending the
// previous post id.
$filename = preg_replace("/^wpid\d+-/", "", $name);
$name = "wpid{$old_file->ID}-{$filename}";
} }
logIO('O', '(MW) Received '.strlen($bits).' bytes'); logIO('O', '(MW) Received '.strlen($bits).' bytes');
@ -1440,22 +1433,11 @@ class wp_xmlrpc_server extends IXR_Server {
'guid' => $upload[ 'url' ] 'guid' => $upload[ 'url' ]
); );
// If we are over writing then set the correct post_id and URL
// instead of getting new ones.
if($overwrite) {
$post_id = $old_meta->post_id;
$attachment["post_parent"] = $old_meta->post_id;
$attachment["ID"] = $old_meta->post_id;
$upload["url"] = $old_post->guid;
$attachment["guid"] = $old_post->guid;
}
// Save the data // Save the data
$id = wp_insert_attachment( $attachment, $upload[ 'file' ], $post_id ); $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $post_id );
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) ); wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
return apply_filters( 'wp_handle_upload', array( 'file' => $upload[ 'file' ], 'url' => $upload[ 'url' ], 'type' => $type ) ); return apply_filters( 'wp_handle_upload', array( 'file' => $name, 'url' => $upload[ 'url' ], 'type' => $type ) );
} }