Skip to content

Instantly share code, notes, and snippets.

@sunmockyang
Last active September 4, 2017 04:03
Show Gist options
  • Select an option

  • Save sunmockyang/4046eca1cd2c2041e0800760598e0abb to your computer and use it in GitHub Desktop.

Select an option

Save sunmockyang/4046eca1cd2c2041e0800760598e0abb to your computer and use it in GitHub Desktop.
Download to a temp directory before moving file to destination
def download_temp_file(url, file_path)
temp_folder = Dir.tmpdir()
temp_file_path = "#{temp_folder}/#{File.basename(file_path)}"
File.open(temp_file_path, "wb") do |file|
file.write open(url).read
end
return temp_file_path
end
def safe_download(url, file_path)
temp_file_path = download_temp_file(stream_url, file_path)
FileUtils.mv(temp_file_path, file_path)
end
## OR put those together
def safe_download(url, file_path)
temp_folder = Dir.tmpdir()
temp_file_path = "#{temp_folder}/#{File.basename(file_path)}"
File.open(temp_file_path, "wb") do |file|
file.write open(url).read
end
FileUtils.mv(temp_file_path, file_path)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment