diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index 3e41e2c91..37383712f 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -92,7 +92,13 @@ class MessagesController < ApplicationController @message.board = @board @message.safe_attributes = params[:message] if request.post? - @message.save_attachments(params[:attachments]) + if @project + is_public = @project.is_public + elsif @course + is_public = @course.is_public + end + # 公开项目/课程上传的资源是公开的,私有项目上传的是私有的 + @message.save_attachments_containers(params[:attachments], User.current, is_public) if @message.save # 更新kindeditor上传的图片资源所有者 if params[:asset_id] diff --git a/lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb b/lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb index f5b48544d..20bc0a7ad 100644 --- a/lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb +++ b/lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb @@ -172,6 +172,76 @@ module Redmine {:files => saved_attachments, :unsaved => unsaved_attachments} end + # 扩展方法,因为类型太多,为了不影响其它的 + # 最终需要形成一个方法 + def save_attachments_containers(attachments, author, is_public) + # 清除临时文件 + if attachments + tempAttach = attachments[:dummy] + if tempAttach && tempAttach[:file] + attachments.delete(:dummy) + end + end + + if attachments.is_a?(Hash) + attachments = attachments.stringify_keys + attachments = attachments.to_a.sort {|a, b| + if a.first.to_i > 0 && b.first.to_i > 0 + a.first.to_i <=> b.first.to_i + elsif a.first.to_i > 0 + 1 + elsif b.first.to_i > 0 + -1 + else + a.first <=> b.first + end + } + attachments = attachments.map(&:last) + end + if attachments.is_a?(Array) + attachments.each do |attachment| + if attachment.is_a?(Hash) + a = nil + file = attachment['file'] + token = attachment['token'] + t = file && file.size > 0 + if file && file.size > 0 + a = Attachment.create(:file => file, :author => author) + elsif token + # 通过token值找到对应的attachment + a = Attachment.find_by_token_only(token) + if a + a.filename = attachment['filename'] unless attachment['filename'].blank? + a.content_type = attachment['content_type'] + end + end + end + + if a && !attachment['is_public_checkbox'] + # 考虑到更新操作,所以全部设置为公开,私有项目、课程是不能访问的 + if is_public + a.is_public = true + else + a.is_public = false + end + elsif a && attachment['is_public_checkbox'] + a.is_public = true + end + set_attachment_public(a) if a + next unless a + a.description = attachment['description'].to_s.strip + a.attachtype = @curattachment_type + if a.new_record? + unsaved_attachments << a + else + saved_attachments << a + end + end + end + {:files => saved_attachments, :unsaved => unsaved_attachments} + end + + def attach_saved_attachments saved_attachments.each do |attachment| self.attachments << attachment