diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index 4f64abbb..28731a43 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -33,6 +33,7 @@ class AttachmentsController < ApplicationController # 2. 上传到云 begin upload_file = params["file"] || params["#{params[:file_param_name]}"]# 这里的file_param_name是为了方便其他插件名称 + dun_check_file = upload_file.dup uid_logger("#########################file_params####{params["#{params[:file_param_name]}"]}") raise "未上传文件" unless upload_file @@ -71,7 +72,15 @@ class AttachmentsController < ApplicationController @attachment.author_id = current_user.id @attachment.disk_directory = month_folder @attachment.cloud_url = remote_path - @attachment.save! + # @attachment.save! + if @attachment.save! + check_result = Attachment.check_image_able(dun_check_file) + if check_result[:status].to_i == -1 + @attachment.destroy + raise "上传失败,#{check_result[:message]}" + # return render json: {status: -1, message: "上传失败,#{check_result[:message]}" } + end + end else logger.info "文件已存在,id = #{@attachment.id}, filename = #{@attachment.filename}" end diff --git a/app/forms/contents/create_form.rb b/app/forms/contents/create_form.rb index 6a029488..af870995 100644 --- a/app/forms/contents/create_form.rb +++ b/app/forms/contents/create_form.rb @@ -1,13 +1,24 @@ class Contents::CreateForm < BaseForm - attr_accessor :filepath, :branch, :new_branch + attr_accessor :filepath, :branch, :new_branch, :content validates :filepath, presence: true validate :check_branch + validate :dun_content_check + def check_branch raise "branch和new_branch必须存在一个 " if branch.blank? && new_branch.blank? # raise "branch和new_branch只能存在一个" if !branch.blank? && !new_branch.blank? end + def dun_content_check + if content.present? + check_result = DunCheck::TextCheck.new({title: "", content: content}).call + if check_result[:status].to_i == -1 + raise "内容含有:#{check_result[:extra_params][:infos]},请修改" + end + end + end + end diff --git a/app/interactors/gitea/create_file_interactor.rb b/app/interactors/gitea/create_file_interactor.rb index d8232379..17bec87f 100644 --- a/app/interactors/gitea/create_file_interactor.rb +++ b/app/interactors/gitea/create_file_interactor.rb @@ -47,7 +47,8 @@ module Gitea { filepath: @params[:filepath], branch: @params[:branch], - new_branch: @params[:new_branch] + new_branch: @params[:new_branch], + content: @params[:content] } end diff --git a/lib/dun_check/image_check.rb b/app/libs/dun_check/image_check.rb similarity index 98% rename from lib/dun_check/image_check.rb rename to app/libs/dun_check/image_check.rb index 77d19eaf..b8c7fd87 100644 --- a/lib/dun_check/image_check.rb +++ b/app/libs/dun_check/image_check.rb @@ -15,7 +15,7 @@ class DunCheck::ImageCheck Rails.logger.info("==========@image_params===========#{@image_params}") dun_params = check_dun_params(@image_params) - api_url = Redmine::Configuration['dun']['image_api'] + api_url = EduSetting.get("dun_image_api") uri = URI.parse(api_url) diff --git a/lib/dun_check/public_params.rb b/app/libs/dun_check/public_params.rb similarity index 78% rename from lib/dun_check/public_params.rb rename to app/libs/dun_check/public_params.rb index 7b44cae4..4bc2a977 100644 --- a/lib/dun_check/public_params.rb +++ b/app/libs/dun_check/public_params.rb @@ -6,8 +6,8 @@ class DunCheck::PublicParams def call public_params = { - secretId: Redmine::Configuration['dun']['secretId'], - businessId: Redmine::Configuration['dun']["#{@type}_businessId"], + secretId: EduSetting.get("dun_secret_id"), + businessId: EduSetting.get("dun_#{@type}_businessId"), timestamp: DateTime.current.strftime('%Q').to_i, nonce: rand(10 ** 11).to_i } @@ -15,7 +15,7 @@ class DunCheck::PublicParams end def generate_sign(params) - secretkey = Redmine::Configuration['dun']['secretKey'] + secretkey = EduSetting.get("dun_secrect_key") sort_params = params.sort.to_h sign_str = "" sort_params.each do |k,v| diff --git a/lib/dun_check/text_check.rb b/app/libs/dun_check/text_check.rb similarity index 97% rename from lib/dun_check/text_check.rb rename to app/libs/dun_check/text_check.rb index c7df9529..c9c0873b 100644 --- a/lib/dun_check/text_check.rb +++ b/app/libs/dun_check/text_check.rb @@ -44,8 +44,8 @@ class DunCheck::TextCheck begin dun_params = check_dun_params(text_params) - api_url = Redmine::Configuration['dun']['text_api'] - + # api_url = Redmine::Configuration['dun']['text_api'] + api_url = EduSetting.get("dun_text_api") uri = URI.parse(api_url) http = Net::HTTP.new(uri.hostname, uri.port) diff --git a/app/models/attachment.rb b/app/models/attachment.rb index 4057e7b9..a52158ec 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -3,7 +3,7 @@ class Attachment < ApplicationRecord include Publicable include Publishable include Lockable - + include DunCheckImageAble belongs_to :container, polymorphic: true, optional: true belongs_to :author, class_name: "User", foreign_key: :author_id # belongs_to :course, foreign_key: :container_id, optional: true diff --git a/app/views/attachments/create.json.jbuilder b/app/views/attachments/create.json.jbuilder index 3c0ef355..1907565c 100644 --- a/app/views/attachments/create.json.jbuilder +++ b/app/views/attachments/create.json.jbuilder @@ -1,2 +1,3 @@ +json.status 0 json.id @attachment.id json.filesize @attachment.filesize