附件上传报错问题:

chromesetting
daiao 5 years ago
parent 178f992d37
commit 9691e2db52

@ -28,52 +28,57 @@ class AttachmentsController < ApplicationController
def create def create
# 1. 本地存储 # 1. 本地存储
# 2. 上传到云 # 2. 上传到云
upload_file = params["file"] || params["#{params[:file_param_name]}"] # 这里的file_param_name是为了方便其他插件名称 begin
uid_logger("#########################file_params####{params["#{params[:file_param_name]}"]}") upload_file = params["file"] || params["#{params[:file_param_name]}"] # 这里的file_param_name是为了方便其他插件名称
raise "未上传文件" unless upload_file uid_logger("#########################file_params####{params["#{params[:file_param_name]}"]}")
raise "未上传文件" unless upload_file
folder = edu_setting('attachment_folder')
raise "存储目录未定义" unless folder.present? folder = edu_setting('attachment_folder')
raise "存储目录未定义" unless folder.present?
month_folder = current_month_folder
save_path = File.join(folder, month_folder) month_folder = current_month_folder
save_path = File.join(folder, month_folder)
ext = file_ext(upload_file.original_filename)
ext = file_ext(upload_file.original_filename)
local_path, digest = file_save_to_local(save_path, upload_file.tempfile, ext)
local_path, digest = file_save_to_local(save_path, upload_file.tempfile, ext)
content_type = upload_file.content_type.presence || 'application/octet-stream'
content_type = upload_file.content_type.presence || 'application/octet-stream'
# remote_path = file_save_to_ucloud(local_path[folder.size, local_path.size], local_path, content_type)
remote_path = nil # TODO 暂时本地上传,待域名配置后方可上传至云端 # remote_path = file_save_to_ucloud(local_path[folder.size, local_path.size], local_path, content_type)
remote_path = nil # TODO 暂时本地上传,待域名配置后方可上传至云端
logger.info "local_path: #{local_path}"
logger.info "remote_path: #{remote_path}" logger.info "local_path: #{local_path}"
logger.info "remote_path: #{remote_path}"
disk_filename = local_path[save_path.size + 1, local_path.size]
#存数据库 disk_filename = local_path[save_path.size + 1, local_path.size]
# #存数据库
@attachment = Attachment.where(disk_filename: disk_filename, #
author_id: current_user.id, @attachment = Attachment.where(disk_filename: disk_filename,
cloud_url: remote_path).first author_id: current_user.id,
if @attachment.blank? cloud_url: remote_path).first
@attachment = Attachment.new if @attachment.blank?
@attachment.filename = upload_file.original_filename @attachment = Attachment.new
@attachment.disk_filename = local_path[save_path.size + 1, local_path.size] @attachment.filename = upload_file.original_filename
@attachment.filesize = upload_file.tempfile.size @attachment.disk_filename = local_path[save_path.size + 1, local_path.size]
@attachment.content_type = content_type @attachment.filesize = upload_file.tempfile.size
@attachment.digest = digest @attachment.content_type = content_type
@attachment.author_id = current_user.id @attachment.digest = digest
@attachment.disk_directory = month_folder @attachment.author_id = current_user.id
@attachment.cloud_url = remote_path @attachment.disk_directory = month_folder
@attachment.disk_directory = save_path @attachment.cloud_url = remote_path
@attachment.save! @attachment.disk_directory = save_path
else @attachment.save!
logger.info "文件已存在id = #{@attachment.id}, filename = #{@attachment.filename}" else
end logger.info "文件已存在id = #{@attachment.id}, filename = #{@attachment.filename}"
end
render_json render_json
rescue => e
uid_logger_error(e.message)
tip_exception(e.message)
end
end end
def destroy def destroy

Loading…
Cancel
Save