#37 check if url is valid

This commit is contained in:
2015-11-23 22:49:18 +08:00
parent 0192f32fe3
commit 00323b413d
+13 -3
View File
@@ -1,6 +1,6 @@
class Api::V1::PostsController < Api::V1::ApplicationController
before_action :permit_params, :map_post_type, :get_post, only: [:create]
before_action :permit_params, :check_url, :map_post_type, :get_post, only: [:create]
before_action :load_post, only: [:add_viewed_times, :show]
skip_before_action :authenticate_user_from_token, only: [:posts, :show, :liked_posts, :bookmarked_posts]
@@ -57,7 +57,7 @@ class Api::V1::PostsController < Api::V1::ApplicationController
def search
params.permit(:keywords)
render json: Post.where('title LIKE ?',"%#{params[:keywords]}%").page(params[:page]), each_serializer: EachPostSerializer
render json: Post.where('title LIKE ?', "%#{params[:keywords]}%").page(params[:page]), each_serializer: EachPostSerializer
end
private
@@ -86,8 +86,18 @@ class Api::V1::PostsController < Api::V1::ApplicationController
end
end
def check_url
begin
uri = URI.parse params[:url]
res = uri.kind_of?(URI::HTTP)
rescue URI::InvalidURIError
res = false
end
return render json: {msg: 'URL不合法, 请重新输入'}, status: 400 unless res
end
def process_post
if !@post.processed
if !@post.processed
ProcessPostWorker.perform_async @post.url
end
end