{"id":74,"date":"2008-11-06T10:40:51","date_gmt":"2008-11-05T23:40:51","guid":{"rendered":"http:\/\/spiffystores.com.au\/blog\/?p=74"},"modified":"2021-09-01T07:00:44","modified_gmt":"2021-08-31T21:00:44","slug":"how-to-fake-an-uploaded-file","status":"publish","type":"post","link":"https:\/\/www.spiffystores.com.au\/blog\/2008\/11\/06\/how-to-fake-an-uploaded-file\/","title":{"rendered":"How to Fake an Uploaded File"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Our store software contains an extensive set of routines for processing uploaded images and resizing them into various image sizes. We&#8217;ve recently been adding some code to support a bulk import function and it&#8217;s become necessary to somehow fake uploading a file, given a specific URL for an image.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The basic plan is to use Net::HTTP to connect to the remote server and grab the image and save it in a temporary file. It turns out that Rails contains a UploadedTempfile class which is a subclass of Tempfile, and this is used by the CGI routines to handle any uploaded files.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p class=\"wp-block-paragraph\">So all we need to do is create our own instance of UploadedTempfile, set some attributes and then pass it along to our existing routines as if it were a real uploaded file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"ruby\" class=\"language-ruby line-numbers\">uri = URI.parse(image_url)\nimage_file_name = File.basename(uri.path) # Extract the file name\nimage_file = nil\nbegin\n  timeout(10) do\n    Net::HTTP.start(uri.host, uri.port) do |http|\n      resp = http.get(uri.path)\n      image_file = ActionController::UploadedTempfile.new(image_file_name)\n      image_file.binmode\n      image_file.write(resp.body)\n      image_file.original_path = image_file_name\n      image_file.rewind\n    end\n  end\nrescue TimeoutError\n  raise \"Timeout connecting to #{uri.host}\"\nend\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">There are a few things that need to be set up, so that the fake upload file works properly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The file is set to binary mode with<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>image_file.binmode<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">The original file name is set with<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>image_file.original_path = image_file_name<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">A content type attribute can also be set with<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>image_file.content_type<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">but we don&#8217;t use this attribute, so it&#8217;s not set here.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Finally we rewind the file so that we can read it later on in the image processing routines. When we&#8217;re finished with the temporary file, we can delete it with a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>image_file.close!<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Our store software contains an extensive set of routines for processing uploaded images and resizing them into various image sizes. We&#8217;ve recently been adding some code to support a bulk import function and it&#8217;s become necessary to somehow fake uploading &hellip; <a href=\"https:\/\/www.spiffystores.com.au\/blog\/2008\/11\/06\/how-to-fake-an-uploaded-file\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ai_generated_summary":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[5,1],"tags":[14,12,11,13,10],"class_list":["post-74","post","type-post","status-publish","format-standard","hentry","category-geek-stuff","category-show-all","tag-file","tag-rails","tag-ruby","tag-uploaded","tag-uploaded-tempfile"],"jetpack_shortlink":"https:\/\/wp.me\/pgk4DF-1c","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/posts\/74","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/comments?post=74"}],"version-history":[{"count":1,"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/posts\/74\/revisions"}],"predecessor-version":[{"id":3226,"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/posts\/74\/revisions\/3226"}],"wp:attachment":[{"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/media?parent=74"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/categories?post=74"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/tags?post=74"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}