From c37a0768689daf41e8fa0c1ba7bdd0b306955cd1 Mon Sep 17 00:00:00 2001 From: xxq250 Date: Thu, 17 Oct 2024 14:22:12 +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,md5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/attachments_controller.rb | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index 8a599cb04..11208171c 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -32,7 +32,7 @@ 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 - md5_file = Base64.strict_encode64(params[:download_url]) + 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}" cache_key ="get_file:#{Time.now.strftime('%Y%m%d')}:#{md5_file}" @@ -51,20 +51,16 @@ class AttachmentsController < ApplicationController 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) + file = Util.download_file(request_url, tmp_path) filename = filepath else - response = Faraday.get(URI.encode(url)) + file = Util.download_file(URI.encode(url), tmp_path) filename = params[:download_url].to_s.split("/").pop() end - if value.to_i == 0 - File.open(tmp_path, 'wb') do |file| - file.write(response.body) - end - end value = value.to_i + 1 Rails.cache.write(cache_key, value, expires_in: 1.day) - send_data(response.body.force_encoding("UTF-8"), filename: filename, type: "application/octet-stream", disposition: 'attachment') + # send_data(response.body.force_encoding("UTF-8"), filename: filename, type: "application/octet-stream", disposition: 'attachment') + send_data(file, filename: filename, type: "application/octet-stream", disposition: 'attachment') end end