From 768b4f1f578712e06b0625e500adf9cbacae91e8 Mon Sep 17 00:00:00 2001 From: xxq250 Date: Thu, 17 Oct 2024 11:47:58 +0800 Subject: [PATCH] =?UTF-8?q?fixed=20gitea=E6=96=87=E4=BB=B6=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E7=BC=93=E5=AD=98,=E6=9B=B4=E6=94=B9=E7=9B=AE?= =?UTF-8?q?=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/attachments_controller.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index eb73ea3f7..398b7fcc4 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -32,22 +32,26 @@ class AttachmentsController < ApplicationController def get_file normal_status(-1, "参数缺失") if params[:download_url].blank? url = base_url.starts_with?("https:") ? params[:download_url].to_s.gsub("http:", "https:") : params[:download_url].to_s + if url.starts_with?(base_url) && !url.starts_with?("#{base_url}/repo") + url = ("/repos"+url.to_s.split(base_url + "/api")[1]) + filepath, ref = url.to_s.split("/")[-1].to_s.split("?") + url.gsub!(url.to_s.split("/")[-1], '') + else + filepath, ref = url.to_s.split("/")[-1].to_s.split("?") + end + Rails.logger.info("url===#{url}") + Rails.logger.info(filepath) md5_file = Base64.strict_encode64(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}" cache_key ="get_file:#{Time.now.strftime('%Y%m%d')}:#{md5_file}" value = Rails.cache.read(cache_key) if value.to_i >= 5 && File.exist?(tmp_path) - send_data(tmp_path, filename: filename, type: "application/octet-stream", disposition: 'attachment') + send_data(tmp_path, filename: filepath, type: "application/octet-stream", disposition: 'attachment') else 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]) - filepath, ref = url.split("/")[-1].split("?") - url.gsub!(url.split("/")[-1], '') - Rails.logger.info("url===#{url}") - Rails.logger.info(filepath) 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}") response = Faraday.get(request_url)