From 7848e092e45dfa298bce2120ad0382b4a3dfa225 Mon Sep 17 00:00:00 2001 From: xxq250 Date: Mon, 13 Jul 2026 15:37:18 +0800 Subject: [PATCH] =?UTF-8?q?fixed=20=E4=B8=8B=E8=BD=BD=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/attachments_controller.rb | 29 ++++++++++++----------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index 6db403265..17425e291 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -35,30 +35,31 @@ class AttachmentsController < ApplicationController normal_status(-1, "参数缺失") if params[:download_url].blank? tip_exception('不可访问') if params[:download_url].to_s.include?("chinese_dictionary.txt") url = base_url.starts_with?("https:") ? params[:download_url].to_s.gsub("http:", "https:") : params[:download_url].to_s - md5_file = Digest::MD5.hexdigest(params[:download_url]) - FileUtils.mkdir_p("#{Rails.root}#{EduSetting.get("attachment_folder")}gitea/") unless Dir.exists?("#{Rails.root}#{EduSetting.get("attachment_folder")}gitea/") - tmp_path = "#{Rails.root}#{EduSetting.get("attachment_folder")}gitea/#{Time.now.strftime('%Y%m%d')}-#{md5_file}" if url.starts_with?(base_url) && !url.starts_with?("#{base_url}/repo") domain = GiteaService.gitea_config[:domain] api_url = GiteaService.gitea_config[:base_url] url = ("/repos"+url.split(base_url + "/api")[1]) - ref = url.split("?")[1] - filepath = url.split("?")[0].split("/")[-1] - url.gsub!(filepath+"?"+ref, '') + filepath, ref = url.split("/")[-1].split("?") + url.gsub!(url.split("/")[-1], '') # Rails.logger.info("url===#{url}") Rails.logger.info(filepath) - ref = ref.blank? ? "" : URI.escape(ref.split('ref=')[1]) - request_url = [domain, api_url, URI.encode(url), URI.escape(filepath), "?ref=#{ref}&access_token=#{User.where(admin: true).take&.gitea_token}"].join + request_url = [domain, api_url, URI.encode(url), URI.escape(filepath), "?ref=#{URI.escape(ref.split('ref=')[1])}&access_token=#{User.where(admin: true).take&.gitea_token}"].join Rails.logger.info("request_url===#{request_url}") - File.delete(tmp_path) if File.exist?(tmp_path) # 删除之前的文件 - Util.download_file(request_url, tmp_path) - filename = filepath + suffix = request_url.split("?").first.split(".").last + suffix_arr = ["zip", "rar", "xls", "xlsx", "pdf", "doc", "docx", "exe"] # 非文本文件直接重定向下载 + Rails.logger.info("suffix===#{suffix},#{suffix_arr.include?(suffix.to_s.downcase)}") + if suffix_arr.include?(suffix.to_s.downcase) + redirect_to request_url + return + else + response = Faraday.get(request_url) + filename = filepath + end else - File.delete(tmp_path) if File.exist?(tmp_path) # 删除之前的文件 - Util.download_file(URI.encode(url), tmp_path) + response = Faraday.get(URI.encode(url)) filename = params[:download_url].to_s.split("/").pop() end - send_file(tmp_path, filename: filename, type: "application/octet-stream", disposition: 'attachment') + send_data(response.body.force_encoding("UTF-8"), filename: filename, type: "application/octet-stream", disposition: 'attachment') end def create