From ff10e9ff0a772fee2fa7e529aed5595a2e44a2b1 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Wed, 4 Dec 2019 15:52:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A=E9=99=84=E4=BB=B6=E7=9A=84?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E5=92=8C=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/attachments_controller.rb | 4 ++-- app/models/attachment.rb | 11 ++++++++++- app/views/student_works/show.json.jbuilder | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index 0c9c49621..a4ceeac09 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -18,9 +18,9 @@ class AttachmentsController < ApplicationController pdf_attachment = params[:disposition] || "attachment" if pdf_attachment == "inline" - send_file absolute_path(local_path(@file)),filename: @file.filename, disposition: 'inline',type: 'application/pdf' + send_file absolute_path(local_path(@file)),filename: @file.title, disposition: 'inline',type: 'application/pdf' else - send_file(absolute_path(local_path(@file)), filename: @file.filename,stream:false, type: @file.content_type.presence || 'application/octet-stream') + send_file(absolute_path(local_path(@file)), filename: @file.title,stream:false, type: @file.content_type.presence || 'application/octet-stream') end update_downloads(@file) end diff --git a/app/models/attachment.rb b/app/models/attachment.rb index 37884e40b..7096ca330 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -27,12 +27,21 @@ class Attachment < ApplicationRecord validates_length_of :description, maximum: 100 + DCODES = %W(2 3 4 5 6 7 8 9 a b c f e f g h i j k l m n o p q r s t u v w x y z) + def diskfile File.join(File.join(Rails.root, "files"), disk_directory.to_s, disk_filename.to_s) end def title - filename + title = filename + if container.is_a?(StudentWork) && author_id != User.current.id + course = container&.homework_common&.course + unless User.current.teacher_of_course?(course) + title = "#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}_#{DCODES.sample(8).join}" + File.extname(filename) + end + end + title end def downloads_count diff --git a/app/views/student_works/show.json.jbuilder b/app/views/student_works/show.json.jbuilder index 9bafbe93e..7b3a91b5e 100644 --- a/app/views/student_works/show.json.jbuilder +++ b/app/views/student_works/show.json.jbuilder @@ -11,7 +11,7 @@ json.update_user_name @is_evaluation ? "匿名" : @work.update_user.try(:real_na json.update_atta @homework.late_duration && @is_author json.attachments @attachments do |atta| - json.partial! "attachments/attachment_simple", locals: {attachment: atta, delete: false} + json.partial! "attachments/attachment_simple", locals: {attachment: atta, delete: false, } end unless @is_evaluation