Make WordPress Core

Changeset 5008


Ignore:
Timestamp:
03/09/2007 08:14:52 PM (18 years ago)
Author:
ryan
Message:

Change xmlrpc upload logic. Props Joseph Scott.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/functions.php

    r4990 r5008  
    10951095}
    10961096
    1097 function wp_upload_bits($name, $type, $bits, $overwrite = false) {
     1097function wp_upload_bits($name, $type, $bits) {
    10981098    if ( empty($name) )
    10991099        return array('error' => __("Empty filename"));
     
    11231123    }
    11241124
    1125     // If we are asked to over write the file then make sure
    1126     // the $name has the complete path and is writable.
    1127     if($overwrite) {
    1128         if(!is_writable($name)) {
    1129             return(array("error" => __("Can not over write file.")));
    1130         }
    1131         $new_file = $name;
    1132         $filename = basename($name);
    1133     }
    1134     else {
    1135         $new_file = $upload['path'] . "/$filename";
    1136         if ( ! wp_mkdir_p( dirname($new_file) ) ) {
    1137             $message = sprintf(__('Unable to create directory %s. Is its parent directory writable by the server?'), dirname($new_file));
    1138             return array('error' => $message);
    1139         }
     1125    $new_file = $upload['path'] . "/$filename";
     1126    if ( ! wp_mkdir_p( dirname($new_file) ) ) {
     1127        $message = sprintf(__('Unable to create directory %s. Is its parent directory writable by the server?'), dirname($new_file));
     1128        return array('error' => $message);
    11401129    }
    11411130
     
    11521141    @ chmod($new_file, $perms);
    11531142
    1154     // Compute the URL if this is a new file.
     1143    // Compute the URL
    11551144    $url = $upload['url'] . "/$filename";
    1156     if($overwrite) {
    1157         $url = $name;
    1158     }
    11591145
    11601146    return array('file' => $new_file, 'url' => $url, 'error' => false);
  • trunk/xmlrpc.php

    r4961 r5008  
    13841384        $bits = $data['bits'];
    13851385
    1386         // Default to new file, not over write.
    1387         $overwrite = false;
    13881386        if(!empty($data["overwrite"]) && ($data["overwrite"] == true)) {
    1389             $overwrite = true;
    1390 
    1391             // If the file isn't writable then error out now.
    1392             if(!is_writable($data["name"])) {
    1393                 return(new IXR_Error(500, "File is not writable, over write failed."));
    1394             }
    1395 
    1396             // We now know the file is good so don't use the sanitized name.
    1397             $name = $data["name"];
    1398 
    13991387            // Get postmeta info on the object.
    1400             $old_meta = $wpdb->get_row("
    1401                 SELECT *
    1402                 FROM {$wpdb->postmeta}
    1403                 WHERE meta_key = '_wp_attached_file'
    1404                     AND meta_value = '{$name}'
     1388            $old_file = $wpdb->get_row("
     1389                SELECT ID
     1390                FROM {$wpdb->posts}
     1391                WHERE post_title = '{$name}'
     1392                    AND post_type = 'attachment'
    14051393            ");
    14061394
    1407             // Get post info on the object.
    1408             $old_post = get_post($old_meta->post_id);
     1395            // Delete previous file.
     1396            wp_delete_attachment($old_file->ID);
     1397
     1398            // Make sure the new name is different by pre-pending the
     1399            // previous post id.
     1400            $filename = preg_replace("/^wpid\d+-/", "", $name);
     1401            $name = "wpid{$old_file->ID}-{$filename}";
    14091402        }
    14101403
     
    14411434        );
    14421435
    1443         // If we are over writing then set the correct post_id and URL
    1444         // instead of getting new ones.
    1445         if($overwrite) {
    1446             $post_id                    = $old_meta->post_id;
    1447             $attachment["post_parent"]  = $old_meta->post_id;
    1448             $attachment["ID"]           = $old_meta->post_id;
    1449 
    1450             $upload["url"]              = $old_post->guid;
    1451             $attachment["guid"]         = $old_post->guid;
    1452         }
    1453 
    14541436        // Save the data
    14551437        $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $post_id );
    14561438        wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
    14571439
    1458         return apply_filters( 'wp_handle_upload', array( 'file' => $upload[ 'file' ], 'url' => $upload[ 'url' ], 'type' => $type ) );
     1440        return apply_filters( 'wp_handle_upload', array( 'file' => $name, 'url' => $upload[ 'url' ], 'type' => $type ) );
    14591441    }
    14601442
Note: See TracChangeset for help on using the changeset viewer.