• Resolved rsrsrs

    (@rsrsrs)


    I have an application that requires using media_sideload_image to sideload images from a URL and then attach the to a wordpress post. The function uses the temp location on the filesystem (by default /tmp or WP_TEMP_DIR) as a temporary download area for the file. This fails when running on appengine (since there is no local storage). Any ideas how i can make this work seamlessly using the appengine plugin. I have tried setting the WP_TEMP_DIR to “gs://<bucket>” but that does not work. I am able to manually upload files just fine so the plugin is setup correctly and bucket permissions are ok.

    https://www.ads-software.com/plugins/google-app-engine/

Viewing 1 replies (of 1 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;
    }
Viewing 1 replies (of 1 total)
  • The topic ‘How to use media_sideload_image (which requires temp storage space)’ is closed to new replies.