|
|
|
@ -25,51 +25,49 @@ class AttachmentsController < ApplicationController
|
|
|
|
|
# 2. 上传到云
|
|
|
|
|
upload_file = params["file"] || params["#{params[:file_param_name]}"] # 这里的file_param_name是为了方便其他插件名称
|
|
|
|
|
uid_logger("#########################file_params####{params["#{params[:file_param_name]}"]}")
|
|
|
|
|
if upload_file
|
|
|
|
|
folder = edu_setting('attachment_folder')
|
|
|
|
|
raise "存储目录未定义" unless folder.present?
|
|
|
|
|
raise "未上传文件" unless upload_file
|
|
|
|
|
|
|
|
|
|
month_folder = current_month_folder
|
|
|
|
|
save_path = File.join(folder, month_folder)
|
|
|
|
|
folder = edu_setting('attachment_folder')
|
|
|
|
|
raise "存储目录未定义" unless folder.present?
|
|
|
|
|
|
|
|
|
|
ext = file_ext(upload_file.original_filename)
|
|
|
|
|
month_folder = current_month_folder
|
|
|
|
|
save_path = File.join(folder, month_folder)
|
|
|
|
|
|
|
|
|
|
local_path, digest = file_save_to_local(save_path, upload_file.tempfile, ext)
|
|
|
|
|
ext = file_ext(upload_file.original_filename)
|
|
|
|
|
|
|
|
|
|
content_type = upload_file.content_type.presence || 'application/octet-stream'
|
|
|
|
|
local_path, digest = file_save_to_local(save_path, upload_file.tempfile, ext)
|
|
|
|
|
|
|
|
|
|
remote_path = file_save_to_ucloud(local_path[folder.size, local_path.size], local_path, content_type)
|
|
|
|
|
content_type = upload_file.content_type.presence || 'application/octet-stream'
|
|
|
|
|
|
|
|
|
|
logger.info "local_path: #{local_path}"
|
|
|
|
|
logger.info "remote_path: #{remote_path}"
|
|
|
|
|
remote_path = file_save_to_ucloud(local_path[folder.size, local_path.size], local_path, content_type)
|
|
|
|
|
|
|
|
|
|
logger.info "local_path: #{local_path}"
|
|
|
|
|
logger.info "remote_path: #{remote_path}"
|
|
|
|
|
|
|
|
|
|
disk_filename = local_path[save_path.size + 1, local_path.size]
|
|
|
|
|
#存数据库
|
|
|
|
|
#
|
|
|
|
|
@attachment = Attachment.where(disk_filename: disk_filename,
|
|
|
|
|
author_id: current_user.id,
|
|
|
|
|
cloud_url: remote_path).first
|
|
|
|
|
|
|
|
|
|
unless @attachment.present?
|
|
|
|
|
@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 = month_folder
|
|
|
|
|
@attachment.cloud_url = remote_path
|
|
|
|
|
@attachment.save!
|
|
|
|
|
else
|
|
|
|
|
logger.info "文件已存在,id = #{@attachment.id}, filename = #{@attachment.filename}"
|
|
|
|
|
end
|
|
|
|
|
disk_filename = local_path[save_path.size + 1, local_path.size]
|
|
|
|
|
#存数据库
|
|
|
|
|
#
|
|
|
|
|
@attachment = Attachment.where(disk_filename: disk_filename,
|
|
|
|
|
author_id: current_user.id,
|
|
|
|
|
cloud_url: remote_path).first
|
|
|
|
|
|
|
|
|
|
render_json
|
|
|
|
|
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 = month_folder
|
|
|
|
|
@attachment.cloud_url = remote_path
|
|
|
|
|
@attachment.save!
|
|
|
|
|
else
|
|
|
|
|
raise "未上传文件"
|
|
|
|
|
logger.info "文件已存在,id = #{@attachment.id}, filename = #{@attachment.filename}"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
render_json
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
|