pagecontent/app/controllers/page_contents_controller.rb

133 lines
4.6 KiB
Ruby
Raw Normal View History

2014-05-06 09:20:15 +00:00
class PageContentsController < OrbitAdminController
2022-10-24 09:52:02 +00:00
require 'uri'
IMG_INFO = {}
2014-08-05 06:45:30 +00:00
def index
params = OrbitHelper.params
2016-08-24 13:28:22 +00:00
if params["wiki"].present?
2022-10-24 10:04:57 +00:00
page = WikiPage.where(:uid => params["wiki"].to_s).first
2021-11-23 03:53:23 +00:00
page.view_count += 1
page.save
2023-05-14 02:59:17 +00:00
name = page.name rescue ""
2016-08-24 13:28:22 +00:00
html = page.content rescue ""
url_to_edit = OrbitHelper.user_can_edit?(page) ? "/admin/wiki_pages/#{page.id}/edit?page_id=#{page.page.id.to_s}" : ""
else
2022-10-24 10:04:57 +00:00
page = Page.where(:page_id => params[:page_id].to_s).first
2016-08-24 13:28:22 +00:00
name = page.name rescue ""
html = page.page_contexts.order(:version=>-1).first.content rescue ""
2016-08-24 13:28:22 +00:00
url_to_edit = OrbitHelper.user_can_edit?(page) ? "/admin/page_contents/new?page_id=#{page.id.to_s}" : ""
end
2017-11-07 07:31:51 +00:00
request = OrbitHelper.request
meta_desc = html.nil? ? "" : ActionView::Base.full_sanitizer.sanitize(html)[0..200]
doc = Nokogiri::HTML( html )
2023-07-13 03:32:05 +00:00
img_srcs = doc.css('img').map{ |i| i['src'] }.compact
2023-05-14 02:59:17 +00:00
metas = [{"property" => "og:description", "content" => meta_desc},{"property" => "og:type", "content" => "Article"}]
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)
2024-06-12 14:31:52 +00:00
img_src_path = URI.decode(img_src_path)
if File.exist?(img_src_path)
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
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)
2024-08-27 15:26:21 +00:00
image_file.close
begin
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.unlink
rescue MiniMagick::Invalid => e
puts ["src = #{img_src}", e.to_s]
end
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
2017-11-07 07:31:51 +00:00
OrbitHelper.render_meta_tags(metas)
2016-08-24 13:28:22 +00:00
2014-08-05 06:45:30 +00:00
{
2016-08-24 13:28:22 +00:00
"html" => html,
"title" => name,
2015-04-13 06:35:21 +00:00
"view_count" => (page.view_count rescue ""),
2015-04-13 06:13:41 +00:00
"view-count-head" =>t('page_content.view_count'),
2014-08-05 06:45:30 +00:00
"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
2014-05-06 09:20:15 +00:00
end