Fix og:image:width and og:image:height missing.

This commit is contained in:
邱博亞 2024-06-10 15:32:40 +08:00
parent 939619c090
commit 9056303b5d
1 changed files with 93 additions and 3 deletions

View File

@ -1,5 +1,8 @@
class PageContentsController < OrbitAdminController
require 'uri'
IMG_INFO = {}
def index
params = OrbitHelper.params
if params["wiki"].present?
@ -20,9 +23,49 @@ class PageContentsController < OrbitAdminController
doc = Nokogiri::HTML( html )
img_srcs = doc.css('img').map{ |i| i['src'] }.compact
metas = [{"property" => "og:description", "content" => meta_desc},{"property" => "og:type", "content" => "Article"}]
# if img_srcs.count > 0
# metas << {"property" => "og:image", "content" => URI.join(request.base_url, URI.encode(img_srcs[0])).to_s}
# end
if img_srcs.count > 0
img_src = img_srcs[0]
metas << {"property" => "og:image", "content" => URI.join(request.base_url, URI.encode(img_src)).to_s}
if img_src.start_with?('/')
img_src_path = File.join('public', img_src)
image = MiniMagick::Image.open(img_src_path)
if image[:width]
metas << {"property" => "og:image:width", "content" => image[:width].to_s}
end
if image[:height]
metas << {"property" => "og:image:height", "content" => image[:height].to_s}
end
else
res = get_response(URI.parse(img_src))
image = IMG_INFO[img_src]
if image.nil?
if res.code == "200"
image_file = Tempfile.new
image_file.binmode
image_file.write(res.body)
image = MiniMagick::Image.open(image_file.path)
IMG_INFO[img_src] = {width: image[:width], height: image[:height]}
if image[:width]
metas << {"property" => "og:image:width", "content" => image[:width].to_s}
end
if image[:height]
metas << {"property" => "og:image:height", "content" => image[:height].to_s}
end
image_file.close
image_file.unlink
else
IMG_INFO[img_src] = {}
end
else
if image[:width]
metas << {"property" => "og:image:width", "content" => image[:width].to_s}
end
if image[:height]
metas << {"property" => "og:image:height", "content" => image[:height].to_s}
end
end
end
end
OrbitHelper.render_meta_tags(metas)
{
@ -33,4 +76,51 @@ class PageContentsController < OrbitAdminController
"url_to_edit" => url_to_edit
}
end
private
def net_http_get_response(uri,headers={})
port = uri.port
host = uri.host
http = Net::HTTP.new(host, port)
scheme = uri.scheme
if scheme == "https" || port.to_i == 443
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.use_ssl = true
end
http.open_timeout = 5
relative_path = (uri.path.blank? ? "/" : uri.path)
if !relative_path.start_with?("http") && !relative_path.start_with?("/")
relative_path = "/" + relative_path
end
relative_path = relative_path.sub(/^\/\.\.\/(\.\.\/)*/,'/')
while relative_path.include?("/../") do
relative_path = relative_path.gsub(/\/[^\/]+\/\.\.\//,'/')
end
relative_path = relative_path.gsub('//','/')
relative_path = URI.decode(relative_path)
relative_path += (uri.query.blank? ? '' : "?#{URI.decode(uri.query)}")
res = http.get(relative_path.gsub(" ","%20"))
if res.code == "400"
res = http.get(URI.encode(relative_path.gsub(" ","%20")))
end
res.uri = uri
res
end
def get_response(uri)
res = net_http_get_response(uri)
if res.code == "301" || res.code == "302"
location = res['Location']
cookie = res['Set-Cookie']
headers = {
'Cookie' => cookie.to_s
}
if location[0] == "/"
uri = URI.parse("#{uri.scheme}://#{uri.host}#{location}")
else
uri = URI.parse(location)
end
res = net_http_get_response(uri,headers)
end
return res
end
end