diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index a79aae425..8f8ade13d 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, :preview_attachment] + before_action :require_login, :check_auth, except: [:show, :preview_attachment, :get_file] before_action :find_file, only: %i[show destroy] before_action :attachment_candown, only: [:show] skip_before_action :check_sign, only: [:show, :create] @@ -28,6 +28,14 @@ class AttachmentsController < ApplicationController update_downloads(@file) end + + def get_file + normal_status(-1, "参数缺失") if params[:download_url].blank? + response = Faraday.get(URI.parse(params[:download_url].to_s.gsub("http:", "https:"))) + filename = params[:download_url].to_s.split("/").pop() + send_data(response.body.force_encoding("UTF-8"), filename: filename, type: "application/octet-stream", disposition: 'attachment') + end + def create # 1. 本地存储 # 2. 上传到云 diff --git a/app/helpers/repositories_helper.rb b/app/helpers/repositories_helper.rb index 2b6d9d405..c6274393a 100644 --- a/app/helpers/repositories_helper.rb +++ b/app/helpers/repositories_helper.rb @@ -1,11 +1,11 @@ module RepositoriesHelper def render_decode64_content(str) return nil if str.blank? - Base64.decode64(str).force_encoding('UTF-8') + Base64.decode64(str).force_encoding("UTF-8") end def download_type(str) - default_type = %w(xlsx xls ppt pptx pdf zip 7z rar exe pdb obj idb png jpg gif tif psd svg RData rdata) + default_type = %w(xlsx xls ppt pptx pdf zip 7z rar exe pdb obj idb png jpg gif tif psd svg RData rdata doc docx mpp) default_type.include?(str&.downcase) end diff --git a/app/views/repositories/_simple_entry.json.jbuilder b/app/views/repositories/_simple_entry.json.jbuilder index 4890f4047..be603d91b 100644 --- a/app/views/repositories/_simple_entry.json.jbuilder +++ b/app/views/repositories/_simple_entry.json.jbuilder @@ -7,7 +7,7 @@ json.sha entry['sha'] json.path entry['path'] json.type entry['type'] json.size entry['size'] -json.content entry['content'].present? && !direct_download ? render_decode64_content(entry['content']).force_encoding('UTF-8') : "" +json.content entry['content'].present? && !direct_download ? render_decode64_content(entry['content']) : "" json.target entry['target'] json.download_url entry['download_url'] json.direct_download direct_download diff --git a/config/routes.rb b/config/routes.rb index 40d1546ba..c9a277ad4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,8 +8,10 @@ Rails.application.routes.draw do # Serve websocket cable requests in-process mount ActionCable.server => '/cable' + get 'attachments/entries/get_file', to: 'attachments#get_file' get 'attachments/download/:id', to: 'attachments#show' get 'attachments/download/:id/:filename', to: 'attachments#show' + get 'auth/qq/callback', to: 'oauth/qq#create' get 'auth/failure', to: 'oauth/base#auth_failure' get 'auth/cas/callback', to: 'oauth/cas#create'