みてるぞ 4 days ago
parent
commit
1b08d12bf1
1 changed files with 9 additions and 0 deletions
  1. +9
    -0
      backend/app/controllers/preview_controller.rb

+ 9
- 0
backend/app/controllers/preview_controller.rb View File

@@ -6,6 +6,10 @@ class PreviewController < ApplicationController
url = params[:url]
return head :bad_request unless url.present?

unless url.start_with?(/http(s)?:\/\//)
url = 'http://' + url
end

html = URI.open(url, open_timeout: 5, read_timeout: 5).read
doc = Nokogiri::HTML.parse(html)
title = doc.at('title')&.text&.strip
@@ -17,11 +21,16 @@ class PreviewController < ApplicationController

def thumbnail
# TODO: 既知ドメインであれば指定のアドレスからサムネールを取得するやぅにする.

return head :unauthorized unless current_user

url = params[:url]
return head :bad_request if url.blank?

unless url.start_with?(/http(s)?:\/\//)
url = 'http://' + url
end

path = Rails.root.join('tmp', "thumb_#{ SecureRandom.hex }.png")
system("node #{ Rails.root }/lib/screenshot.js #{ Shellwords.escape(url) } #{ path }")



Loading…
Cancel
Save