diff --git a/app/controllers/initialization_data_controller.rb b/app/controllers/initialization_data_controller.rb index e94d8c6f4..f8bef787a 100644 --- a/app/controllers/initialization_data_controller.rb +++ b/app/controllers/initialization_data_controller.rb @@ -366,5 +366,30 @@ class InitializationDataController < ApplicationController end end + # 获取关卡附件 + def get_attachment_data + task_pass = Challenge.where(id: @challenges).where("task_pass like '%/api/attachments/%'").pluck(:task_pass) + @att_ids = [] + # 附件的格式为(/api/attachments/ + 附件id)的形式,因此下面的正文匹配是为了取出附件的id,方便迁移attachments + task_pass.each do |tp| + @att_ids += tp.scan(/\(\/api\/attachments\/.+\)/).map{|s|s.match(/\d+/)[0]} + end + end + + # 创建附件数据 + def copy_attachment_data + return if @att_ids.blank? + attachment_attrs = %i[id container_id container_type filename disk_filename filesize content_type digest author_id created_on + downloads description disk_directory attachtype is_public copy_from quotes is_publish publish_time resource_bank_id unified_setting + cloud_url course_second_category_id] + Attachment.bulk_insert(*attachment_attrs) do |worker| + Attachment.where(id: @att_ids).find_each do |att| + base_attr = att.attributes.except("author_id", "disk_directory", "copy_from") + # 本地版将过关任务的图片迁移到files/localshixuns 目录下, copy_from记录的是他原来的目录结构 + worker.add(base_attr.merge({author_id: 1, disk_directory: "localshixuns", copy_from: att.disk_directory})) + end + end + end + end