class PreviewController < ApplicationController def title # TODO: # 既知サイトなら決まったフォーマットで title 取得するやぅに. return head :unauthorized unless current_user url = params[:url] return head :bad_request unless url.present? html = URI.open(url, open_timeout: 5, read_timeout: 5).read doc = Nokogiri::HTML.parse(html) title = doc.at('title')&.text&.strip render json: { title: title } rescue => e render json: { error: e.message }, status: :bad_request end def thumbnail # TODO: 既知ドメインであれば指定のアドレスからサムネールを取得するやぅにする. return head :unauthorized unless current_user url = params[:url] return head :bad_request unless url.present? path = Rails.root.join('tmp', "thumb_#{ SecureRandom.hex }.png") system("node #{ Rails.root }/lib/screenshot.js #{ Shellwords.escape(url) } #{ path }") if File.exist?(path) image = MiniMagick::Image.open(path) image.resize '180x180' File.delete(path) rescue nil send_file image.path, type: 'image/png', disposition: 'inline' else render json: { error: 'Failed to generate thumbnail' }, status: :internal_server_error end end end