Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter rsrsrs

    (@rsrsrs)

    So after perusing through at all the hooks (actions and filters) available i realized this is not possible using a hook because none is available for media_sideload_image or wp_handle_sideload or media_handle_sideload or download_url or wp_tempfnam. So i ended up writing a simple procedure in place of media_sideload_image() and download_url(). media_sideload_image_in_GAE() and download_url_in_GAE(). Here is the code for others benefit:

    /**
     * Download an image from the specified URL and attach it to a post.
     *
     * Replaced media_sideload_image with media_sideload_image_in_GAE to take into account Google Appengine where wp_tempnam fails since there is no temp filesystem location
     *
     */
    private function media_sideload_image_in_GAE($file, $post_id, $desc = null) {
            if ( ! empty($file) ) {
                    // Download file to temp location
                    $tmp = $this->store_tmpfile_in_GAE( $file );
    
                    // Set variables for storage
                    // fix file filename for query strings
                    preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches );
                    $file_array['name'] = basename($matches[0]);
                    $file_array['tmp_name'] = $tmp;
    
                    // If error storing temporarily, unlink
                    if ( is_wp_error( $tmp ) ) {
                            @unlink($file_array['tmp_name']);
                            $file_array['tmp_name'] = '';
                    }
    
                    // do the validation and storage stuff
                    $id = media_handle_sideload( $file_array, $post_id, $desc );
                    // If error storing permanently, unlink
                    if ( is_wp_error($id) ) {
                            @unlink($file_array['tmp_name']);
                            return $id;
                    }
    
                    $src = wp_get_attachment_url( $id );
            }
    
            // Finally check to make sure the file has been saved, then return the html
            if ( ! empty($src) ) {
                    $alt = isset($desc) ? esc_attr($desc) : '';
                    $html = "<img src='$src' alt='$alt' />";
                    return $html;
            }
    }
    
    private function store_tmpfile_in_GAE($filename) {
            $filesourceurl=$filename;
            $filename = basename($filename);
            $filename = preg_replace('|\..*$|', '.tmp', $filename);
            //$filename = $dir . wp_unique_filename($dir, $filename);
    
    	$bucket_name = get_option('appengine_uploads_bucket', '');
    	$filepath = "gs://" . $bucket_name . "/" . $filename;
            $filecontent = file_get_contents($filesourceurl);
            $fp = fopen($filepath, "w");
            //fwrite($fp, "");
            fwrite($fp, $filecontent);
            fclose($fp);
            return $filepath;
    }

    Experienced the exact same problem with 4.0. Downgraded to 3.9.1 and the problem went away. The change in cookie handling causing the problem definitely makes sense.

Viewing 2 replies - 1 through 2 (of 2 total)