#14 作成は完了
This commit is contained in:
@@ -36,37 +36,16 @@ class PostsController < ApplicationController
|
||||
|
||||
# POST /posts
|
||||
def create
|
||||
logger.info ">>> thumbnail: #{params[:thumbnail]&.content_type}"
|
||||
logger.info ">>> filename: #{params[:thumbnail]&.original_filename}"
|
||||
|
||||
return head :unauthorized unless current_user
|
||||
return head :forbidden unless ['admin', 'member'].include?(current_user.role)
|
||||
|
||||
# TODO: URL が正規のものがチェック,不正ならエラー
|
||||
title = params[:title]
|
||||
unless title.present?
|
||||
# TODO: # 既知サイトなら決まったフォーマットで title 取得するやぅに.
|
||||
begin
|
||||
html = URI.open(params[:url], open_timeout: 5, read_timeout: 5).read
|
||||
doc = Nokogiri::HTML.parse(html)
|
||||
title = doc.at('title')&.text&.strip || ''
|
||||
rescue
|
||||
title = ''
|
||||
end
|
||||
end
|
||||
post = Post.new(title: title, url: params[:url], thumbnail_base: '', uploaded_user: current_user)
|
||||
if params[:thumbnail].present?
|
||||
post.thumbnail.attach(params[:thumbnail])
|
||||
else
|
||||
# TODO: 既知ドメインであれば指定のアドレスからサムネールを取得するやぅにする.
|
||||
path = Rails.root.join('tmp', "thumb_#{ SecureRandom.hex }.png")
|
||||
system("node #{ Rails.root }/lib/screenshot.js #{ Shellwords.escape(params[:url]) } #{ path }")
|
||||
if File.exist?(path)
|
||||
image = MiniMagick::Image.open(path)
|
||||
image.resize '180x180'
|
||||
post.thumbnail.attach(io: File.open(image.path),
|
||||
filename: 'thumbnail.png',
|
||||
content_type: 'image/png')
|
||||
File.delete(path) rescue nil
|
||||
end
|
||||
end
|
||||
post.thumbnail.attach(params[:thumbnail])
|
||||
if post.save
|
||||
post.resized_thumbnail!
|
||||
if params[:tags].present?
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
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
|
||||
Reference in New Issue
Block a user