Merge branch 'develop' into dev_trustie_server

This commit is contained in:
jasder 2021-06-22 09:51:46 +08:00
commit 23f7439a8c
7 changed files with 67 additions and 13 deletions

View File

@ -5,7 +5,7 @@ class ProjectsController < ApplicationController
include Acceleratorable
before_action :require_login, except: %i[index branches group_type_list simple show fork_users praise_users watch_users recommend about menu_list]
before_action :load_project, except: %i[index group_type_list migrate create recommend]
before_action :load_repository, except: %i[index group_type_list migrate create recommend]
before_action :authorizate_user_can_edit_project!, only: %i[update]
before_action :project_public?, only: %i[fork_users praise_users watch_users]

View File

@ -5,9 +5,9 @@ class RepositoriesController < ApplicationController
before_action :require_login, only: %i[edit update create_file update_file delete_file sync_mirror]
before_action :load_repository
before_action :authorizate!, except: [:sync_mirror, :tags, :commit]
before_action :authorizate!, except: [:sync_mirror, :tags, :commit, :archive]
before_action :authorizate_user_can_edit_repo!, only: %i[sync_mirror]
before_action :get_ref, only: %i[entries sub_entries top_counts file]
before_action :get_ref, only: %i[entries sub_entries top_counts file archive]
before_action :get_latest_commit, only: %i[entries sub_entries top_counts]
before_action :get_statistics, only: %i[top_counts]
@ -192,6 +192,19 @@ class RepositoriesController < ApplicationController
render json: languages_precentagable
end
def archive
domain = Gitea.gitea_config[:domain]
api_url = Gitea.gitea_config[:base_url]
archive_url = "/repos/#{@owner.login}/#{@repository.identifier}/archive/#{params[:archive]}"
file_path = [domain, api_url, archive_url].join
file_path = [file_path, "access_token=#{current_user&.gitea_token}"].join("?") if @repository.hidden?
return render_not_found if !request.format.zip? && !request.format.gzip?
redirect_to file_path
end
private
def find_project
@ -266,7 +279,7 @@ class RepositoriesController < ApplicationController
# uploadPushInfo
end
def create_new_pr(params)
if params[:new_branch].present? && params[:new_branch] != params[:branch]
local_params = {

View File

@ -13,12 +13,12 @@ module ProjectsHelper
end
end
def render_zip_url(project, archive_name)
[gitea_domain, project.owner.login, project.identifier, "archive", "#{archive_name}.zip"].join('/')
def render_zip_url(owner, repository, archive)
[base_url, archive_repositories_path(owner&.login, repository, "#{archive}.zip")].join
end
def render_tar_url(project, archive_name)
[gitea_domain, project.owner.login, project.identifier, "archive", "#{archive_name}.tar.gz"].join('/')
def render_tar_url(owner, repository, archive)
[base_url, archive_repositories_path(owner&.login, repository, "#{archive}.tar.gz")].join
end
def render_http_url(project)

View File

@ -0,0 +1,40 @@
class Gitea::Repository::ArchiveService < Gitea::ClientService
attr_reader :owner, :repo, :archive, :token
def initialize(owner, repo, archive, token=nil)
@owner = owner
@repo = repo
@archive = archive
@token = token
end
def call
response = get(url, params)
response_payload(response)
end
private
def params
Hash.new.merge(token: token)
end
def url
"/repos/#{owner}/#{repo}/archive/#{archive}".freeze
end
def response_payload(response)
status = response.status
body = response&.body
log_error(status, body)
status_payload(status, body)
end
def status_payload(status, body)
case status
when 200 then success
when 404 then error("你操作的链接不存在!")
else error("系统错误!")
end
end
end

View File

@ -4,8 +4,8 @@ json.array! @branches do |branch|
json.user_can_merge branch['user_can_merge']
json.protected branch['protected']
json.http_url render_http_url(@project)
json.zip_url render_zip_url(@project, branch['name'])
json.tar_url render_tar_url(@project, branch['name'])
json.zip_url render_zip_url(@owner, @repository, branch['name'])
json.tar_url render_tar_url(@owner, @repository, branch['name'])
json.last_commit do
json.sha branch['commit']['id']
json.message branch['commit']['message']

View File

@ -42,8 +42,9 @@ if @project.forge?
#json.tags_count @tags_count
#json.branches_count @branches_count
json.commits_count @commits_count
json.zip_url render_zip_url(@project, @ref)
json.tar_url render_tar_url(@project, @ref)
# json.zip_url archive_repositories_path(@owner&.login, @repository, @ref)
json.zip_url render_zip_url(@owner, @repository, @ref)
json.tar_url render_tar_url(@owner, @repository, @ref)
json.entries do
json.array! @entries do |entry|
json.name entry['name']

View File

@ -417,7 +417,6 @@ Rails.application.routes.draw do
member do
get :files
get :detail
get :archive
get :entries
match :sub_entries, :via => [:get, :put]
get :commits
@ -432,6 +431,7 @@ Rails.application.routes.draw do
get 'commits/:sha', to: 'repositories#commit', as: 'commit'
get 'readme'
get 'languages'
get 'archive/:archive', to: 'repositories#archive', as: "archive", constraints: { archive: /.+/, format: /(zip|gzip)/ }
end
end