Browse Source

更改:下载文件以及查看文件逻辑

pull/343/head
yystopf 3 years ago
parent
commit
0830850518
5 changed files with 19 additions and 15 deletions
  1. +7
    -4
      app/controllers/attachments_controller.rb
  2. +1
    -1
      app/controllers/repositories_controller.rb
  3. +1
    -1
      app/helpers/projects_helper.rb
  4. +9
    -8
      app/views/repositories/_simple_entry.json.jbuilder
  5. +1
    -1
      config/routes.rb

+ 7
- 4
app/controllers/attachments_controller.rb View File

@@ -31,14 +31,17 @@ class AttachmentsController < ApplicationController

def get_file
normal_status(-1, "参数缺失") if params[:download_url].blank?
url = base_url.starts_with?("https:") ? URI.encode(params[:download_url].to_s.gsub("http:", "https:")) : URI.encode(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")
domain = GiteaService.gitea_config[:domain]
api_url = GiteaService.gitea_config[:base_url]
url = ("/repos"+url.split(base_url + "/api")[1]).gsub('?filepath=', '/').gsub('&', '?')
request_url = [domain, api_url, url, "?ref=#{params[:ref]}&access_token=#{User.where(admin: true).take&.gitea_token}"].join
url = ("/repos"+url.split(base_url + "/api")[1])
filepath, ref = url.split("/")[-1].split("?")
url.gsub!(url.split("/")[-1], '')
puts filepath
request_url = [domain, api_url, url, CGI.escape(filepath), "?ref=#{CGI.escape(ref.split('ref=')[1])}&access_token=#{User.where(admin: true).take&.gitea_token}"].join
response = Faraday.get(request_url)
filename = url.to_s.split("/").pop()
filename = filepath
else
response = Faraday.get(url)
filename = params[:download_url].to_s.split("/").pop()


+ 1
- 1
app/controllers/repositories_controller.rb View File

@@ -273,7 +273,7 @@ class RepositoriesController < ApplicationController
domain = GiteaService.gitea_config[:domain]
api_url = GiteaService.gitea_config[:base_url]
url = "/repos/#{@owner.login}/#{@repository.identifier}/raw/#{Addressable::URI.escape(params[:filepath])}?ref=#{Addressable::URI.escape(params[:ref])}"
url = "/repos/#{@owner.login}/#{@repository.identifier}/raw/#{CGI.escape(params[:filepath])}?ref=#{CGI.escape(params[:ref])}"
file_path = [domain, api_url, url].join
file_path = [file_path, "access_token=#{@owner&.gitea_token}"].join("&")


+ 1
- 1
app/helpers/projects_helper.rb View File

@@ -22,7 +22,7 @@ module ProjectsHelper
end

def render_download_file_url(owner, repository, filepath, ref)
[base_url, "/api/#{owner&.login}/#{repository.identifier}/raw?filepath=#{filepath}&ref=#{ref}"].join
[base_url, "/api/#{owner&.login}/#{repository.identifier}/raw/#{CGI.escape(filepath)}?ref=#{CGI.escape(ref)}"].join
end

def render_http_url(project)


+ 9
- 8
app/views/repositories/_simple_entry.json.jbuilder View File

@@ -17,14 +17,15 @@ if @project.forge?
json.content (direct_download || image_type || is_dir) ? nil : decode64_content(entry, @owner, @repository, @ref, @path)
json.target entry['target']
download_url =
if image_type
dir_path = [@owner.login, @repository.identifier, "raw/branch", @ref].join('/')
is_dir ? "" : render_download_image_url(dir_path, entry['path'], decode64_content(entry, @owner, @repository, @ref))
else
# entry['download_url']
is_dir ? "" : render_download_file_url(@owner, @repository, entry['path'].to_s, @ref)
end
download_url = is_dir ? "" : render_download_file_url(@owner, @repository, entry['path'].to_s, @ref)
# if image_type
# # dir_path = [@owner.login, @repository.identifier, "raw/branch", @ref].join('/')
# # is_dir ? "" : render_download_image_url(dir_path, entry['path'], decode64_content(entry, @owner, @repository, @ref))
# is_dir ? "" : render_gitea_raw_url(entry['download_url'])
# else
# # entry['download_url']
# is_dir ? "" : render_download_file_url(@owner, @repository, entry['path'].to_s, @ref)
# end
json.download_url download_url

json.direct_download direct_download


+ 1
- 1
config/routes.rb View File

@@ -507,7 +507,7 @@ Rails.application.routes.draw do
get 'readme'
get 'languages'
get 'archive/:archive', to: 'repositories#archive', as: "archive", constraints: { archive: /.+/, format: /(zip|gzip)/ }
get 'raw', to: 'repositories#raw', as: "raw"
get 'raw/*filepath', to: 'repositories#raw', as: "raw", constraints: { filepath: /.+/}
end
end



Loading…
Cancel
Save