diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 8e3f496b..9ef44090 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -205,6 +205,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 diff --git a/app/helpers/projects_helper.rb b/app/helpers/projects_helper.rb index 8af54219..cc4a3fe0 100644 --- a/app/helpers/projects_helper.rb +++ b/app/helpers/projects_helper.rb @@ -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 diff --git a/app/views/repositories/_simple_entry.json.jbuilder b/app/views/repositories/_simple_entry.json.jbuilder index 6c208875..cc9bdc3a 100644 --- a/app/views/repositories/_simple_entry.json.jbuilder +++ b/app/views/repositories/_simple_entry.json.jbuilder @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 346468fb..2b92e939 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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