fix: readme relative delay and replace_content
This commit is contained in:
parent
83e43f0c97
commit
779c86108a
|
@ -225,7 +225,8 @@ class RepositoriesController < ApplicationController
|
||||||
@path = Gitea.gitea_config[:domain]+"/#{@owner.login}/#{@repository.identifier}/raw/branch/#{params[:ref]}/"
|
@path = Gitea.gitea_config[:domain]+"/#{@owner.login}/#{@repository.identifier}/raw/branch/#{params[:ref]}/"
|
||||||
@readme = result[:status] === :success ? result[:body] : nil
|
@readme = result[:status] === :success ? result[:body] : nil
|
||||||
@readme['content'] = decode64_content(@readme, @owner, @repository, params[:ref], @path)
|
@readme['content'] = decode64_content(@readme, @owner, @repository, params[:ref], @path)
|
||||||
render json: @readme.slice("type", "encoding", "size", "name", "path", "content", "sha")
|
@readme['replace_content'] = readme_decode64_content(@readme, @owner, @repository, params[:ref], @path)
|
||||||
|
render json: @readme.slice("type", "encoding", "size", "name", "path", "content", "sha", "replace_content")
|
||||||
rescue
|
rescue
|
||||||
render json: nil
|
render json: nil
|
||||||
end
|
end
|
||||||
|
|
|
@ -88,8 +88,8 @@ module RepositoriesHelper
|
||||||
end
|
end
|
||||||
|
|
||||||
# author hui.he
|
# author hui.he
|
||||||
def new_readme_render_decode64_content(str, owner, repo, ref)
|
def new_readme_render_decode64_content(str, owner, repo, ref, readme_path, readme_name)
|
||||||
path = [owner&.login, repo&.identifier, 'tree', ref].join("/")
|
file_path = readme_path.include?('/') ? readme_path.gsub("/#{readme_name}", '') : readme_path.gsub("#{readme_name}", '')
|
||||||
return nil if str.blank?
|
return nil if str.blank?
|
||||||
content = Base64.decode64(str).force_encoding('UTF-8')
|
content = Base64.decode64(str).force_encoding('UTF-8')
|
||||||
s_regex = /\[.*?\]\((.*?)\)/
|
s_regex = /\[.*?\]\((.*?)\)/
|
||||||
|
@ -103,15 +103,19 @@ module RepositoriesHelper
|
||||||
s_content = s[0]
|
s_content = s[0]
|
||||||
# 链接直接跳过不做替换
|
# 链接直接跳过不做替换
|
||||||
next if s_content.starts_with?('http://') || s_content.starts_with?('https://') || s_content.starts_with?('mailto:')
|
next if s_content.starts_with?('http://') || s_content.starts_with?('https://') || s_content.starts_with?('mailto:')
|
||||||
# 路径替换
|
ext = File.extname(s_content)[1..-1]
|
||||||
if s_content.starts_with?('./') || s_content.starts_with?("../") || s_content.starts_with?("/")
|
|
||||||
|
if image_type?(ext) || download_type(ext)
|
||||||
|
s_content = File.expand_path(s_content, file_path)
|
||||||
|
s_content = s_content.split("#{Rails.root}/")[1]
|
||||||
|
# content = content.gsub(s[0], "/#{s_content}")
|
||||||
|
s_content = [base_url, "/api/#{owner&.login}/#{repo.identifier}/raw?filepath=#{s_content}&ref=#{ref}"].join
|
||||||
|
content = content.gsub(s[0], s_content)
|
||||||
|
else
|
||||||
|
path = [owner&.login, repo&.identifier, 'tree', ref, file_path].join("/")
|
||||||
s_content = File.expand_path(s_content, path)
|
s_content = File.expand_path(s_content, path)
|
||||||
s_content = s_content.split("#{Rails.root}/")[1]
|
s_content = s_content.split("#{Rails.root}/")[1]
|
||||||
content = content.gsub(s[0], "/#{s_content}")
|
content = content.gsub(s[0], "/#{s_content}")
|
||||||
else
|
|
||||||
# 图片资源替换
|
|
||||||
s_content = [base_url, "/api/#{owner&.login}/#{repo.identifier}/raw?filepath=#{s_content}&ref=#{ref}"].join
|
|
||||||
content = content.gsub(s[0], s_content)
|
|
||||||
end
|
end
|
||||||
rescue
|
rescue
|
||||||
next
|
next
|
||||||
|
@ -133,13 +137,21 @@ module RepositoriesHelper
|
||||||
date.to_time.strftime("%Y-%m-%d %H:%M")
|
date.to_time.strftime("%Y-%m-%d %H:%M")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def readme_decode64_content(entry, owner, repo, ref, path=nil)
|
||||||
|
Rails.logger.info("entry===#{entry["type"]} #{entry["name"]}")
|
||||||
|
content = Gitea::Repository::Entries::GetService.call(owner, repo.identifier, URI.escape(entry['path']), ref: ref)['content']
|
||||||
|
Rails.logger.info("content===#{content}")
|
||||||
|
# readme_render_decode64_content(content, owner, repo, ref)
|
||||||
|
new_readme_render_decode64_content(content, owner, repo, ref, entry['path'], entry['name'])
|
||||||
|
end
|
||||||
|
|
||||||
def decode64_content(entry, owner, repo, ref, path=nil)
|
def decode64_content(entry, owner, repo, ref, path=nil)
|
||||||
if is_readme?(entry['type'], entry['name'])
|
if is_readme?(entry['type'], entry['name'])
|
||||||
Rails.logger.info("entry===#{entry["type"]} #{entry["name"]}")
|
Rails.logger.info("entry===#{entry["type"]} #{entry["name"]}")
|
||||||
content = Gitea::Repository::Entries::GetService.call(owner, repo.identifier, URI.escape(entry['path']), ref: ref)['content']
|
content = Gitea::Repository::Entries::GetService.call(owner, repo.identifier, URI.escape(entry['path']), ref: ref)['content']
|
||||||
Rails.logger.info("content===#{content}")
|
Rails.logger.info("content===#{content}")
|
||||||
# readme_render_decode64_content(content, owner, repo, ref)
|
# readme_render_decode64_content(content, owner, repo, ref)
|
||||||
new_readme_render_decode64_content(content, owner, repo, ref)
|
return Base64.decode64(content).force_encoding('UTF-8')
|
||||||
else
|
else
|
||||||
file_type = File.extname(entry['name'].to_s)[1..-1]
|
file_type = File.extname(entry['name'].to_s)[1..-1]
|
||||||
if image_type?(file_type)
|
if image_type?(file_type)
|
||||||
|
|
|
@ -8,7 +8,10 @@ if @project.forge?
|
||||||
json.path entry['path']
|
json.path entry['path']
|
||||||
json.type entry['type']
|
json.type entry['type']
|
||||||
json.size entry['size']
|
json.size entry['size']
|
||||||
|
is_readme = is_readme?(entry['type'], entry['name'])
|
||||||
|
if is_readme
|
||||||
|
json.replace_content readme_decode64_content(entry, @owner, @repository, @ref, @path)
|
||||||
|
end
|
||||||
json.content direct_download ? nil : decode64_content(entry, @owner, @repository, @ref, @path)
|
json.content direct_download ? nil : decode64_content(entry, @owner, @repository, @ref, @path)
|
||||||
json.target entry['target']
|
json.target entry['target']
|
||||||
|
|
||||||
|
@ -24,7 +27,7 @@ if @project.forge?
|
||||||
|
|
||||||
json.direct_download direct_download
|
json.direct_download direct_download
|
||||||
json.image_type image_type
|
json.image_type image_type
|
||||||
json.is_readme_file is_readme?(entry['type'], entry['name'])
|
json.is_readme_file is_readme
|
||||||
json.commit do
|
json.commit do
|
||||||
json.partial! 'last_commit', latest_commit: entry['latest_commit']
|
json.partial! 'last_commit', latest_commit: entry['latest_commit']
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue