Skip to content

Commit

Permalink
scripts: Handle redirects in image URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
garrett committed Aug 9, 2023
1 parent 2703e46 commit 39ed663
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions _scripts/generate-release-notes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,19 +178,26 @@ def build_frontmatter
}.to_yaml.gsub(/^:/, '')
end

# Download the image from GitHub and save locally
# Download the image from a GitHub and save locally
def download_image(url, basename)
extension = url.split('.').last
filename = "#{basename}.#{extension}"

# Download and save file
image = Net::HTTP.get(URI(url))
File.write("images/#{filename}", image)

@files_images.push("images/#{filename}")
uri = URI(url)
response = Net::HTTP.get_response(uri)

case response
when Net::HTTPRedirection
new_url = response['location']
download_image(new_url, basename)
when Net::HTTPSuccess
extension = File.extname(uri.path)
local_file = "#{basename}#{extension}"
File.write("images/#{local_file}", response.body)
@files_images << local_file
else
warn "Error: Unable to download the image from #{url} - Response: #{response.code}"
end

# Return filename (with extension)
filename
rescue StandardError => e
warn "Error downloading the image: #{e.message}"
end

def process_images(notes)
Expand Down

0 comments on commit 39ed663

Please sign in to comment.