Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scripts: Handle redirects in image URLs #656

Merged
merged 1 commit into from
Aug 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions _scripts/generate-release-notes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,19 +178,29 @@ def build_frontmatter
}.to_yaml.gsub(/^:/, '')
end

# Download the image from 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}")
# Download the image from a GitHub and save locally
def download_image(url, basename, limit = 3)
raise ArgumentError, 'too many HTTP redirects' if limit == 0

uri = URI(url)
response = Net::HTTP.get_response(uri)

case response
when Net::HTTPRedirection
new_url = response['location']
download_image(new_url, basename, limit - 1)
when Net::HTTPSuccess
extension = File.extname(uri.path)
local_file = "#{basename}#{extension}"
File.write("images/#{local_file}", response.body)
@files_images << "images/#{local_file}"

# Return the local file on success
local_file
end

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

def process_images(notes)
Expand Down