<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>uploaded tempfile | Spiffy Stores Blog</title>
	<atom:link href="https://www.spiffystores.com.au/blog/tag/uploaded-tempfile/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.spiffystores.com.au/blog</link>
	<description>Checkout - The Spiffy Stores Blog</description>
	<lastBuildDate>Tue, 31 Aug 2021 21:00:44 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.3</generator>
<site xmlns="com-wordpress:feed-additions:1">241205771</site>	<item>
		<title>How to Fake an Uploaded File</title>
		<link>https://www.spiffystores.com.au/blog/2008/11/06/how-to-fake-an-uploaded-file/</link>
		
		<dc:creator><![CDATA[Brian]]></dc:creator>
		<pubDate>Wed, 05 Nov 2008 23:40:51 +0000</pubDate>
				<category><![CDATA[Geek stuff]]></category>
		<category><![CDATA[Show All]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[uploaded]]></category>
		<category><![CDATA[uploaded tempfile]]></category>
		<guid isPermaLink="false">http://spiffystores.com.au/blog/?p=74</guid>

					<description><![CDATA[<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 &#8230; <a href="https://www.spiffystores.com.au/blog/2008/11/06/how-to-fake-an-uploaded-file/">Continue reading <span class="meta-nav">&#8594;</span></a></p>
The post <a href="https://www.spiffystores.com.au/blog/2008/11/06/how-to-fake-an-uploaded-file/">How to Fake an Uploaded File</a> first appeared on <a href="https://www.spiffystores.com.au/blog">Spiffy Stores Blog</a>.]]></description>
										<content:encoded><![CDATA[<div style='display:none;' class='shareaholic-canvas' data-app='share_buttons' data-title='How to Fake an Uploaded File' data-link='https://www.spiffystores.com.au/blog/2008/11/06/how-to-fake-an-uploaded-file/' data-app-id-name='category_above_content'></div>
<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 a file, given a specific URL for an image.</p>



<p>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>



<span id="more-74"></span>



<p>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>



<pre class="wp-block-code"><code lang="ruby" class="language-ruby line-numbers">uri = URI.parse(image_url)
image_file_name = File.basename(uri.path) # Extract the file name
image_file = nil
begin
  timeout(10) do
    Net::HTTP.start(uri.host, uri.port) do |http|
      resp = http.get(uri.path)
      image_file = ActionController::UploadedTempfile.new(image_file_name)
      image_file.binmode
      image_file.write(resp.body)
      image_file.original_path = image_file_name
      image_file.rewind
    end
  end
rescue TimeoutError
  raise "Timeout connecting to #{uri.host}"
end
</code></pre>



<p>There are a few things that need to be set up, so that the fake upload file works properly.</p>



<p>The file is set to binary mode with</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>image_file.binmode</p></blockquote>



<p>The original file name is set with</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>image_file.original_path = image_file_name</p></blockquote>



<p>A content type attribute can also be set with</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>image_file.content_type</p></blockquote>



<p>but we don&#8217;t use this attribute, so it&#8217;s not set here.</p>



<p>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>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>image_file.close!</p></blockquote>
<div style='display:none;' class='shareaholic-canvas' data-app='share_buttons' data-title='How to Fake an Uploaded File' data-link='https://www.spiffystores.com.au/blog/2008/11/06/how-to-fake-an-uploaded-file/' data-app-id-name='category_below_content'></div><div style='display:none;' class='shareaholic-canvas' data-app='recommendations' data-title='How to Fake an Uploaded File' data-link='https://www.spiffystores.com.au/blog/2008/11/06/how-to-fake-an-uploaded-file/' data-app-id-name='category_below_content'></div>The post <a href="https://www.spiffystores.com.au/blog/2008/11/06/how-to-fake-an-uploaded-file/">How to Fake an Uploaded File</a> first appeared on <a href="https://www.spiffystores.com.au/blog">Spiffy Stores Blog</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">74</post-id>	</item>
	</channel>
</rss>
