rake copy attachemnt

This commit is contained in:
xxq250 2023-12-01 14:59:44 +08:00
parent 33c56e3e79
commit d0623e4d57
2 changed files with 48 additions and 6 deletions

View File

@ -245,13 +245,12 @@ GEM
powerpack (0.1.2)
prettier (0.18.2)
public_suffix (4.0.3)
puma (3.12.2)
puma (5.6.5)
nio4r (~> 2.0)
raabro (1.4.0)
rack (2.0.9)
rack-cors (1.1.1)
rack (>= 2.0.0)
rack-mini-profiler (2.0.1)
rack (>= 1.2.0)
rack-protection (2.0.8.1)
rack
rack-test (1.1.0)
@ -514,9 +513,8 @@ DEPENDENCIES
parallel (~> 1.19, >= 1.19.1)
pdfkit
prettier
puma (~> 3.11)
puma (~> 5.6.5)
rack-cors
rack-mini-profiler
rails (~> 5.2.0)
rails-i18n (~> 5.1)
ransack
@ -538,7 +536,7 @@ DEPENDENCIES
sidekiq-cron (= 1.2.0)
sidekiq-failures
simple_form
simple_xlsx_reader
simple_xlsx_reader (~> 1.0.4)
sinatra
solargraph (~> 0.38.0)
spreadsheet

View File

@ -0,0 +1,44 @@
namespace :copy_attachment_files do
desc "copy_attachment_files to out path"
task done: :environment do
cp_path = ENV['path'] || "/home/pdl/cp_files"
container_type = ENV['_type']
next if container_type.blank?
Dir.mkdir(cp_path) unless Dir.exist?(cp_path)
attachments = Attachment.where(container_type: container_type)
attachments.each do |file|
file_path = File.join(file.disk_directory.to_s, file.disk_filename.to_s)
next unless File.exist?("#{Rails.root.to_s}/files/#{file_path}")
dir_path_year = "#{cp_path}/#{file.disk_directory.to_s.split("/")[0]}"
Dir.mkdir(dir_path_year) unless Dir.exist?(dir_path_year)
dir_path = "#{cp_path}/#{file.disk_directory}"
Dir.mkdir(dir_path) unless Dir.exist?(dir_path)
puts "cp #{Rails.root.to_s + File.join("/files/", file_path)} #{dir_path}"
system("cp #{Rails.root.to_s + File.join("/files/", file_path)} #{dir_path}")
end
end
task memo: :environment do
cp_path = ENV['path'] || "/home/pdl/cp_files"
memos = Memo.where("content like '%/attachments/send_file/%'")
att_ids = []
memos.each do |memo|
# 附件的格式为(/api/attachments/ + 附件id的形式提取出id进行附件属性关联做附件访问权限控制
att_ids += memo.content.to_s.scan(/\(\/attachments\/send_file\/.+\)/).map{|s|s.match(/\d+/)[0]}
att_ids += memo.content.to_s.scan(/\/attachments\/send_file\/.+\"/).map{|s|s.match(/\d+/)[0]}
att_ids += memo.content.to_s.scan(/\/attachments\/send_file\/\d+/).map{|s|s.match(/\d+/)[0]}
end
if att_ids.present?
attachments = Attachment.where(id: att_ids)
attachments.each do |file|
file_path = File.join(file.disk_directory, file.disk_filename)
dir_path_year = "#{cp_path}/#{file.disk_directory.to_s.split("/")[0]}"
Dir.mkdir(dir_path_year) unless Dir.exist?(dir_path_year)
dir_path = "#{cp_path}/#{file.disk_directory}"
Dir.mkdir(dir_path) unless Dir.exist?(dir_path)
puts "cp #{Rails.root.to_s + File.join("/files/", file_path)} #{dir_path}"
system("cp #{Rails.root.to_s + File.join("/files/", file_path)} #{dir_path}")
end
end
end
end