From 8c512c63d79e2e048a161f217fd8614fdeb66cbe Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Fri, 13 Dec 2019 17:38:33 +0800 Subject: [PATCH] =?UTF-8?q?=E9=99=84=E4=BB=B6=E4=B8=8A=E4=BC=A0=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/shixuns_controller.rb | 66 +++++++++++++++------------ 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/app/controllers/shixuns_controller.rb b/app/controllers/shixuns_controller.rb index 90aab6981..67c3bfbe2 100644 --- a/app/controllers/shixuns_controller.rb +++ b/app/controllers/shixuns_controller.rb @@ -469,37 +469,43 @@ class ShixunsController < ApplicationController # 实训测试集附件 def upload_data_sets - upload_file = params["file"] - raise "未上传文件" unless upload_file - folder = edu_setting('shixun_folder') - raise "存储目录未定义" unless folder.present? - rep_name = @shixun.data_sets.pluck(:filename).include?(upload_file.original_filename) - raise "文件名已经存在\"#{upload_file.original_filename}\", 请删除后再上传" if rep_name - tpm_folder = params[:identifier] # 这个是实训的identifier - save_path = File.join(folder, tpm_folder) - ext = file_ext(upload_file.original_filename) - local_path, digest = file_save_to_local(save_path, upload_file.tempfile, ext) - content_type = upload_file.content_type.presence || 'application/octet-stream' - disk_filename = local_path[save_path.size + 1, local_path.size] - @attachment = Attachment.where(disk_filename: disk_filename, - author_id: current_user.id).first - if @attachment.blank? - @attachment = Attachment.new - @attachment.filename = upload_file.original_filename - @attachment.disk_filename = local_path[save_path.size + 1, local_path.size] - @attachment.filesize = upload_file.tempfile.size - @attachment.content_type = content_type - @attachment.digest = digest - @attachment.author_id = current_user.id - @attachment.disk_directory = tpm_folder - @attachment.container_id = @shixun.id - @attachment.container_type = @shixun.class.name - @attachment.attachtype = 2 - @attachment.save! - else - logger.info "文件已存在,id = #{@attachment.id}, filename = #{@attachment.filename}" + begin + + upload_file = params["file"] + raise "未上传文件" unless upload_file + folder = edu_setting('shixun_folder') + raise "存储目录未定义" unless folder.present? + rep_name = @shixun.data_sets.pluck(:filename).include?(upload_file.original_filename) + raise "文件名已经存在\"#{upload_file.original_filename}\", 请删除后再上传" if rep_name + tpm_folder = params[:identifier] # 这个是实训的identifier + save_path = File.join(folder, tpm_folder) + ext = file_ext(upload_file.original_filename) + local_path, digest = file_save_to_local(save_path, upload_file.tempfile, ext) + content_type = upload_file.content_type.presence || 'application/octet-stream' + disk_filename = local_path[save_path.size + 1, local_path.size] + @attachment = Attachment.where(disk_filename: disk_filename, + author_id: current_user.id).first + if @attachment.blank? + @attachment = Attachment.new + @attachment.filename = upload_file.original_filename + @attachment.disk_filename = local_path[save_path.size + 1, local_path.size] + @attachment.filesize = upload_file.tempfile.size + @attachment.content_type = content_type + @attachment.digest = digest + @attachment.author_id = current_user.id + @attachment.disk_directory = tpm_folder + @attachment.container_id = @shixun.id + @attachment.container_type = @shixun.class.name + @attachment.attachtype = 2 + @attachment.save! + else + logger.info "文件已存在,id = #{@attachment.id}, filename = #{@attachment.filename}" + end + render_ok + rescue => e + uid_logger_error(e.message) + tip_exception(e.message) end - render_ok end # 多文件删除