Last active
September 4, 2017 04:03
-
-
Save sunmockyang/4046eca1cd2c2041e0800760598e0abb to your computer and use it in GitHub Desktop.
Download to a temp directory before moving file to destination
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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