ぼざクリ タグ広場 https://hub.nizika.monster
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

47 lines
1.4 KiB

  1. class PreviewController < ApplicationController
  2. def title
  3. # TODO: # 既知サイトなら決まったフォーマットで title 取得するやぅに.
  4. return head :unauthorized unless current_user
  5. url = params[:url]
  6. return head :bad_request unless url.present?
  7. unless url.start_with?(/http(s)?:\/\//)
  8. url = 'http://' + url
  9. end
  10. html = URI.open(url, open_timeout: 5, read_timeout: 5).read
  11. doc = Nokogiri::HTML.parse(html)
  12. title = doc.at('title')&.text&.strip
  13. render json: { title: title }
  14. rescue => e
  15. render json: { error: e.message }, status: :bad_request
  16. end
  17. def thumbnail
  18. # TODO: 既知ドメインであれば指定のアドレスからサムネールを取得するやぅにする.
  19. return head :unauthorized unless current_user
  20. url = params[:url]
  21. return head :bad_request if url.blank?
  22. unless url.start_with?(/http(s)?:\/\//)
  23. url = 'http://' + url
  24. end
  25. path = Rails.root.join('tmp', "thumb_#{ SecureRandom.hex }.png")
  26. system("node #{ Rails.root }/lib/screenshot.js #{ Shellwords.escape(url) } #{ path }")
  27. if File.exist?(path)
  28. image = MiniMagick::Image.open(path)
  29. image.resize '180x180'
  30. File.delete(path) rescue nil
  31. send_file image.path, type: 'image/png', disposition: 'inline'
  32. else
  33. render json: { error: 'Failed to generate thumbnail' }, status: :internal_server_error
  34. end
  35. end
  36. end