In case it’s useful to anyone else. I got tips via this post which has a snippet that allows you to see what files are open.
ObjectSpace.each_object(File) do |f|
unless f.closed?
puts "This file is still open: %s: %d\n" % [f.path, f.fileno]
end
end
It turned out I was leaving my temporary file open. The upload code is from here.
post '/image' do
cross_origin
name = params[:name]
my_file = params[:my_file]
unless params[:my_file] && (tmpfile = params[:my_file][:tempfile])
@error = "No file selected"
return haml(:upload)
end
STDERR.puts "Uploading file, original name #{name}"
directory = "public/data"
path = File.join(directory, name+".jpg")
File.open(path, 'wb') do |f|
while chunk = tmpfile.read(65536)
f.write(chunk)
end
tmpfile.close
end
"Upload complete\n"
end
or more concisely (thanks @andrewn)
post '/image' do
cross_origin
name = params[:name]
my_file = params[:my_file]
unless params[:my_file] && (tmpfile = params[:my_file][:tempfile])
@error = "No file selected"
return haml(:upload)
end
STDERR.puts "Uploading file, original name #{name}"
directory = "public/data"
path = File.join(directory, name+".jpg")
FileUtils.mv(tmpfile, path)
"Upload complete\n"
end
and on the other side, the curl command is:
curl http://example.com/image -F my_file=@"/run/shm/image.jpg" -F "name=new_name"