diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index 4f64abbb6..b592bb3f9 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -2,7 +2,7 @@ # # 文件上传 class AttachmentsController < ApplicationController - before_action :require_login, :check_auth, except: [:show] + before_action :require_login, :check_auth, except: [:show, :preview_attachment] before_action :find_file, only: %i[show destroy] before_action :attachment_candown, only: [:show] skip_before_action :check_sign, only: [:show, :create] @@ -98,6 +98,26 @@ class AttachmentsController < ApplicationController end end + # 附件为视频时,点击播放 + def preview_attachment + attachment = Attachment.find_by(id: params[:id]) + dir_path = "#{Rails.root}/public/preview" + Dir.mkdir(dir_path) unless Dir.exist?(dir_path) + if params[:status] == "preview" + if system("cp -r #{absolute_path(local_path(attachment))} #{dir_path}/") + render json: {status: 1, url: "#{dir_path}/#{attachment.disk_filename}"} + else + normal_status(-1, "出现错误,请稍后重试") + end + else + if system("rm -rf #{dir_path}/#{attachment.disk_filename}") + normal_status(1, "操作成功") + else + normal_status(-1, "出现错误,请稍后重试") + end + end + end + private def find_file @file = diff --git a/app/views/attachments/_attachment_simple.json.jbuilder b/app/views/attachments/_attachment_simple.json.jbuilder index 0681d6022..6737838d1 100644 --- a/app/views/attachments/_attachment_simple.json.jbuilder +++ b/app/views/attachments/_attachment_simple.json.jbuilder @@ -3,7 +3,6 @@ json.title attachment.title json.filesize number_to_human_size attachment.filesize json.description attachment.description json.is_pdf attachment.is_pdf? -json.absolute_path "https://forgeplus.trustie.net/#{absolute_path(local_path(attachment))}" json.url attachment.is_pdf? ? download_url(attachment,disposition:"inline") : download_url(attachment) # json.url download_url(attachment) json.set! :delete, delete.nil? ? true : delete if defined? delete \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index f8b4eec71..0af147919 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -33,6 +33,7 @@ Rails.application.routes.draw do resources :attachments do collection do delete :destroy_files + post :preview_attachment end end get 'home/index'