From 2ea41d0100c7d5c435e435d96e12bafb505fd4ad Mon Sep 17 00:00:00 2001 From: xxq250 Date: Wed, 22 Nov 2023 11:17:22 +0800 Subject: [PATCH] =?UTF-8?q?fixed=20=E8=A7=A3=E5=86=B3=E5=AE=89=E5=85=A8?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E8=AE=BF=E9=97=AE=E9=99=84=E4=BB=B6=EF=BC=8C?= =?UTF-8?q?id=E6=94=B9=E4=B8=BAuuid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/attachments_controller.rb | 4 +++- app/helpers/application_helper.rb | 2 +- app/models/issue.rb | 10 ++++++++++ app/models/journal.rb | 10 ++++++++++ app/views/attachments/create.json.jbuilder | 2 +- 5 files changed, 25 insertions(+), 3 deletions(-) diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index 915723b64..bf8c870a0 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -94,6 +94,7 @@ class AttachmentsController < ApplicationController @attachment.author_id = current_user.id @attachment.disk_directory = month_folder @attachment.cloud_url = remote_path + @attachment.uuid = SecureRandom.uuid @attachment.save! else logger.info "文件已存在,id = #{@attachment.id}, filename = #{@attachment.filename}" @@ -147,8 +148,9 @@ class AttachmentsController < ApplicationController if params[:type] == 'history' AttachmentHistory.find params[:id] else - Attachment.find params[:id] || Attachment.find_by(uuid: params[:id]) + Attachment.find_by(id: params[:id]) || Attachment.find_by(uuid: params[:id]) end + tip_exception(404, "您访问的页面不存在或已被删除") if @file.blank? end def delete_file(file_path) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 936452470..5d5582428 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -299,7 +299,7 @@ module ApplicationHelper end def download_url attachment,options={} - attachment_path(attachment,options) + attachment&.uuid.present? ? attachment_path(attachment.uuid,options) : attachment_path(attachment,options) end # 耗时:天、小时、分、秒 diff --git a/app/models/issue.rb b/app/models/issue.rb index 85094e748..0d55163b4 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -248,6 +248,7 @@ class Issue < ApplicationRecord # 关附件到功能 def associate_attachment_container + return if self.project_id == 0 att_ids = [] # 附件的格式为(/api/attachments/ + 附件id)的形式,提取出id进行附件属性关联,做附件访问权限控制 att_ids += self.description.to_s.scan(/\(\/api\/attachments\/.+\)/).map{|s|s.match(/\d+/)[0]} @@ -256,6 +257,15 @@ class Issue < ApplicationRecord if att_ids.present? Attachment.where(id: att_ids).where("container_type IS NULL OR container_type = 'Issue'").update_all(container_id: self.project_id, container_type: 'Project') end + + att_ids2 = [] + # uuid_regex= /[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/ + # 附件的格式为(/api/attachments/ + uuid)的形式,提取出id进行附件属性关联,做附件访问权限控制 + att_ids2 += self.description.to_s.scan(/\(\/api\/attachments\/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\)/).map{|s|s.match(/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/)[0]} + att_ids2 += self.description.to_s.scan(/\/api\/attachments\/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/).map{|s|s.match(/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/)[0]} + if att_ids2.present? + Attachment.where(uuid: att_ids2).where("container_type IS NULL OR container_type = 'Issue'").update_all(container_id: self.project_id, container_type: 'Project') + end end def to_builder diff --git a/app/models/journal.rb b/app/models/journal.rb index e0553ad40..2798c107a 100644 --- a/app/models/journal.rb +++ b/app/models/journal.rb @@ -61,6 +61,7 @@ class Journal < ApplicationRecord # 关附件到功能 def associate_attachment_container + return if self.issue&.project_id.to_i == 0 att_ids = [] # 附件的格式为(/api/attachments/ + 附件id)的形式,提取出id进行附件属性关联,做附件访问权限控制 att_ids += self.notes.to_s.scan(/\(\/api\/attachments\/.+\)/).map{|s|s.match(/\d+/)[0]} @@ -69,6 +70,15 @@ class Journal < ApplicationRecord if att_ids.present? Attachment.where(id: att_ids).where("container_type IS NULL OR container_type = 'Journal'").update_all(container_id: self.issue.project_id, container_type: "Project") end + + att_ids2 = [] + # uuid_regex= /[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/ + # 附件的格式为(/api/attachments/ + uuid)的形式,提取出id进行附件属性关联,做附件访问权限控制 + att_ids2 += self.description.to_s.scan(/\(\/api\/attachments\/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\)/).map{|s|s.match(/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/)[0]} + att_ids2 += self.description.to_s.scan(/\/api\/attachments\/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/).map{|s|s.match(/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/)[0]} + if att_ids2.present? + Attachment.where(uuid: att_ids).where("container_type IS NULL OR container_type = 'Journal'").update_all(container_id: self.issue.project_id, container_type: "Project") + end end def operate_content diff --git a/app/views/attachments/create.json.jbuilder b/app/views/attachments/create.json.jbuilder index 6ddc5ced2..3b12193c9 100644 --- a/app/views/attachments/create.json.jbuilder +++ b/app/views/attachments/create.json.jbuilder @@ -1,4 +1,4 @@ -json.id @attachment.id +json.id @attachment.uuid json.title @attachment.title json.filesize number_to_human_size(@attachment.filesize) json.is_pdf @attachment.is_pdf?