Merge branch 'standalone_develop' of https://gitlink.org.cn/Trustie/forgeplus into standalone_develop

This commit is contained in:
yystopf 2022-03-22 10:45:55 +08:00
commit 4dc3546124
3 changed files with 560 additions and 541 deletions

View File

@ -45,23 +45,39 @@ module RepositoriesHelper
end end
end end
def readme_render_decode64_content(str, owner, repo, ref) def readme_render_decode64_content(str, owner, repo, ref, path)
return nil if str.blank? return nil if str.blank?
begin begin
content = Base64.decode64(str).force_encoding('UTF-8') content = Base64.decode64(str).force_encoding('UTF-8')
c_regex = /\!\[.*?\]\((.*?)\)/ c_regex = /\!\[.*?\]\((.*?)\)/
src_regex = /src=\"(.*?)\"/ src_regex = /src=\"(.*?)\"/
src2_regex = /src='(.*?)'/
ss = content.to_s.scan(c_regex) ss = content.to_s.scan(c_regex)
ss_src = content.to_s.scan(src_regex) ss_src = content.scan(src_regex)
total_images = ss + ss_src ss_src2 = content.scan(src2_regex)
total_images = ss + ss_src + ss_src2
if total_images.length > 0 if total_images.length > 0
total_images.each do |s| total_images.each do |s|
image_title = /\"(.*?)\"/ image_title = /\"(.*?)\"/
r_content = s[0] r_content = s[0]
remove_title = r_content.to_s.scan(image_title) remove_title = r_content.to_s.scan(image_title)
if remove_title.length > 0 # if remove_title.length > 0
r_content = r_content.gsub(/#{remove_title[0]}/, "").strip # r_content = r_content.gsub(/#{remove_title[0]}/, "").strip
# end
path_last = r_content
path_current = ""
# 相对路径处理
if r_content.start_with?("../")
relative_path_length = r_content.split("../").size - 1
path_pre = path.split("/").size - 1 - relative_path_length
path_pre = 0 if path_pre < 0
path_current = path_pre == 0 ? "" : path.split("/")[0..path_pre].join("/")
path_last = r_content.split("../").last
elsif r_content.start_with?("/") # 根路径处理
path_last = r_content[1..r_content.size]
else
path_current = path
end end
# if r_content.include?("?") # if r_content.include?("?")
# new_r_content = r_content + "&raw=true" # new_r_content = r_content + "&raw=true"
@ -72,9 +88,9 @@ module RepositoriesHelper
unless r_content.include?("http://") || r_content.include?("https://") || r_content.include?("mailto:") unless r_content.include?("http://") || r_content.include?("https://") || r_content.include?("mailto:")
# new_r_content = "#{path}" + new_r_content # new_r_content = "#{path}" + new_r_content
new_r_content = [base_url, "/api/#{owner&.login}/#{repo.identifier}/raw?filepath=#{r_content}&ref=#{ref}"].join new_r_content = [base_url, "/api/#{owner&.login}/#{repo.identifier}/raw?filepath=#{path_current}/#{path_last}&ref=#{ref}"].join
end end
content = content.gsub(/#{r_content}/, new_r_content) content = content.gsub(/src=\"#{r_content}\"/, "src=\"#{new_r_content}\"").gsub(/src='#{r_content}'/, "src=\"#{new_r_content}\"")
end end
end end
@ -96,8 +112,10 @@ module RepositoriesHelper
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'])
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']
readme_render_decode64_content(content, owner, repo, ref) content = entry['content']
path = URI.escape(entry['path']).to_s.downcase.gsub("/readme.md","")
readme_render_decode64_content(content, owner, repo, ref, path)
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)

View File

@ -1,4 +1,5 @@
if @project.forge? if @project.forge?
is_dir = @sub_entries.is_a?(Array)
file_name = entry['name'] file_name = entry['name']
file_type = File.extname(file_name.to_s)[1..-1] file_type = File.extname(file_name.to_s)[1..-1]
direct_download = download_type(file_type) direct_download = download_type(file_type)
@ -9,16 +10,16 @@ if @project.forge?
json.type entry['type'] json.type entry['type']
json.size entry['size'] json.size entry['size']
json.content direct_download ? nil : decode64_content(entry, @owner, @repository, @ref, @path) json.content (direct_download || image_type || is_dir) ? nil : decode64_content(entry, @owner, @repository, @ref, @path)
json.target entry['target'] json.target entry['target']
download_url = download_url =
if image_type if image_type
dir_path = [@owner.login, @repository.identifier, "raw/branch", @ref].join('/') dir_path = [@owner.login, @repository.identifier, "raw/branch", @ref].join('/')
render_download_image_url(dir_path, entry['path'], decode64_content(entry, @owner, @repository, @ref)) is_dir ? "" : render_download_image_url(dir_path, entry['path'], decode64_content(entry, @owner, @repository, @ref))
else else
# entry['download_url'] # entry['download_url']
render_download_file_url(@owner, @repository, entry['path'].to_s, @ref) is_dir ? "" : render_download_file_url(@owner, @repository, entry['path'].to_s, @ref)
end end
json.download_url download_url json.download_url download_url