fix: download file need authorize

This commit is contained in:
yystopf 2021-09-10 10:52:21 +08:00
parent d5de6aa66b
commit 90ace8b753
4 changed files with 18 additions and 1 deletions

View File

@ -206,6 +206,17 @@ class RepositoriesController < ApplicationController
redirect_to file_path
end
def raw
domain = Gitea.gitea_config[:domain]
api_url = Gitea.gitea_config[:base_url]
url = "/repos/#{@owner.login}/#{@repository.identifier}/raw/#{params[:filepath]}?ref=#{params[:ref]}"
file_path = [domain, api_url, url].join
file_path = [file_path, "access_token=#{current_user&.gitea_token}"].join("&") if @repository.hidden?
redirect_to file_path
end
private
def find_project

View File

@ -21,6 +21,10 @@ module ProjectsHelper
[base_url, archive_repositories_path(owner&.login, repository, "#{archive}.tar.gz")].join
end
def render_download_file_url(owner, repository, filepath, ref)
[base_url, "/api/#{owner&.login}/#{repository.identifier}/raw?filepath=#{filepath}&ref=#{ref}"].join
end
def render_http_url(project)
[gitea_domain, project.owner.login, "#{project.identifier}.git"].join('/')
end

View File

@ -17,7 +17,8 @@ if @project.forge?
dir_path = [@owner.login, @repository.identifier, "raw/branch", @ref].join('/')
render_download_image_url(dir_path, entry['path'], decode64_content(entry, @owner, @repository, @ref))
else
entry['download_url']
# entry['download_url']
render_download_file_url(@owner, @repository, entry['path'].to_s, @ref)
end
json.download_url download_url

View File

@ -434,6 +434,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"
end
end