forked from Gitlink/forgeplus
fixed 下载文件
This commit is contained in:
parent
94d5a2d531
commit
7848e092e4
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue