ぼざクリ タグ広場 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.
 
 
 
 
 
 

38 lines
1.3 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. html = URI.open(url, open_timeout: 5, read_timeout: 5).read
  8. doc = Nokogiri::HTML.parse(html)
  9. title = doc.at('title')&.text&.strip
  10. render json: { title: title }
  11. rescue => e
  12. render json: { error: e.message }, status: :bad_request
  13. end
  14. def thumbnail
  15. # TODO: 既知ドメインであれば指定のアドレスからサムネールを取得するやぅにする.
  16. return head :unauthorized unless current_user
  17. url = params[:url]
  18. return head :bad_request unless url.present?
  19. path = Rails.root.join('tmp', "thumb_#{ SecureRandom.hex }.png")
  20. system("node #{ Rails.root }/lib/screenshot.js #{ Shellwords.escape(url) } #{ path }")
  21. if File.exist?(path)
  22. image = MiniMagick::Image.open(path)
  23. image.resize '180x180'
  24. File.delete(path) rescue nil
  25. send_file image.path, type: 'image/png', disposition: 'inline'
  26. else
  27. render json: { error: 'Failed to generate thumbnail' }, status: :internal_server_error
  28. end
  29. end
  30. end