gitea文件下载缓存
This commit is contained in:
parent
7675370239
commit
9db6713a34
|
@ -32,23 +32,38 @@ class AttachmentsController < ApplicationController
|
||||||
def get_file
|
def get_file
|
||||||
normal_status(-1, "参数缺失") if params[:download_url].blank?
|
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
|
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")
|
md5_file = Base64.strict_encode64(params[:download_url])
|
||||||
domain = GiteaService.gitea_config[:domain]
|
tmp_path = "#{Rails.root}/public/files/#{Time.now.strftime('%Y%m%d%')}-#{md5_file}"
|
||||||
api_url = GiteaService.gitea_config[:base_url]
|
cache_key ="get_file:#{Time.now.strftime('%Y%m%d%')}:#{md5_file}"
|
||||||
url = ("/repos"+url.split(base_url + "/api")[1])
|
value = Rails.cache.read(cache_key)
|
||||||
filepath, ref = url.split("/")[-1].split("?")
|
if value.to_i >= 5 && File.exist?(tmp_path)
|
||||||
url.gsub!(url.split("/")[-1], '')
|
send_data(tmp_path, filename: filename, type: "application/octet-stream", disposition: 'attachment')
|
||||||
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)
|
|
||||||
filename = filepath
|
|
||||||
else
|
else
|
||||||
response = Faraday.get(URI.encode(url))
|
if url.starts_with?(base_url) && !url.starts_with?("#{base_url}/repo")
|
||||||
filename = params[:download_url].to_s.split("/").pop()
|
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)
|
||||||
|
filename = filepath
|
||||||
|
else
|
||||||
|
response = Faraday.get(URI.encode(url))
|
||||||
|
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')
|
||||||
end
|
end
|
||||||
send_data(response.body.force_encoding("UTF-8"), filename: filename, type: "application/octet-stream", disposition: 'attachment')
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
|
|
Loading…
Reference in New Issue