From 76bad50a9788a4592f43d0f6d9b56794bc55681a Mon Sep 17 00:00:00 2001 From: Brandon Dunne Date: Wed, 11 Oct 2023 15:55:49 -0400 Subject: [PATCH] Add some logic for following redirects --- site/_plugins/download_filter.rb | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/site/_plugins/download_filter.rb b/site/_plugins/download_filter.rb index 588f55331..37b49d666 100644 --- a/site/_plugins/download_filter.rb +++ b/site/_plugins/download_filter.rb @@ -48,16 +48,20 @@ def file_info_from_url(url) uri = URI(url) Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http| response = http.request_head(uri.path) - if response.code != '200' + case response.code + when '200' + file_size = response['content-length'].to_f + { + "size" => ActiveSupport::NumberHelper.number_to_human_size(file_size), + "md5" => response['etag'].delete('"') + } + when '302' + Jekyll.logger.info("Following redirect for #{url}...") + file_info_from_url(response["location"]) + else Jekyll.logger.error("Jekyll::DownloadFilter.file_info_from_url(#{url})", "code=#{response.code}, message=#{response.message}") return no_file_info end - - file_size = response['content-length'].to_f - return { - "size" => ActiveSupport::NumberHelper.number_to_human_size(file_size), - "md5" => response['etag'].delete('"') - } end rescue => error Jekyll.logger.error("Jekyll::DownloadFilter.file_info_from_url(#{url})", "error=#{error.message}")