readme.md支持图片相对路径
This commit is contained in:
parent
c0e97409cb
commit
2346b0f848
|
@ -1,148 +1,163 @@
|
||||||
module RepositoriesHelper
|
module RepositoriesHelper
|
||||||
def render_permission(user, project)
|
def render_permission(user, project)
|
||||||
return "Admin" if user&.admin?
|
return "Admin" if user&.admin?
|
||||||
project.get_premission(user)
|
project.get_premission(user)
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_decode64_content(str)
|
def render_decode64_content(str)
|
||||||
return nil if str.blank?
|
return nil if str.blank?
|
||||||
Base64.decode64(str).force_encoding("UTF-8").encode("UTF-8", invalid: :replace)
|
Base64.decode64(str).force_encoding("UTF-8").encode("UTF-8", invalid: :replace)
|
||||||
end
|
end
|
||||||
|
|
||||||
def download_type(str)
|
def download_type(str)
|
||||||
default_type = %w(xlsx xls ppt pptx pdf zip 7z rar exe pdb obj idb RData rdata doc docx mpp vsdx dot otf eot ttf woff woff2 mp4 mov wmv flv mpeg avi avchd webm mkv)
|
default_type = %w(xlsx xls ppt pptx pdf zip 7z rar exe pdb obj idb RData rdata doc docx mpp vsdx dot otf eot ttf woff woff2 mp4 mov wmv flv mpeg avi avchd webm mkv)
|
||||||
default_type.include?(str&.downcase) || str.blank?
|
default_type.include?(str&.downcase) || str.blank?
|
||||||
end
|
end
|
||||||
|
|
||||||
def image_type?(str)
|
def image_type?(str)
|
||||||
default_type = %w(png jpg gif tif psd svg bmp webp jpeg ico psd)
|
default_type = %w(png jpg gif tif psd svg bmp webp jpeg ico psd)
|
||||||
default_type.include?(str&.downcase)
|
default_type.include?(str&.downcase)
|
||||||
end
|
end
|
||||||
|
|
||||||
def is_readme?(type, str)
|
def is_readme?(type, str)
|
||||||
return false if type != 'file' || str.blank?
|
return false if type != 'file' || str.blank?
|
||||||
readme_types = ["readme.md", "readme", "readme_en.md", "readme_zh.md", "readme_en", "readme_zh"]
|
readme_types = ["readme.md", "readme", "readme_en.md", "readme_zh.md", "readme_en", "readme_zh"]
|
||||||
readme_types.include?(str.to_s.downcase)
|
readme_types.include?(str.to_s.downcase)
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_commit_author(author_json)
|
def render_commit_author(author_json)
|
||||||
return nil if author_json.blank? || (author_json["id"].blank? && author_json['name'].blank?)
|
return nil if author_json.blank? || (author_json["id"].blank? && author_json['name'].blank?)
|
||||||
if author_json["id"].present?
|
if author_json["id"].present?
|
||||||
return find_user_by_gitea_uid author_json['id']
|
return find_user_by_gitea_uid author_json['id']
|
||||||
end
|
end
|
||||||
if author_json["id"].nil? && (author_json["name"].present? && author_json["email"].present?)
|
if author_json["id"].nil? && (author_json["name"].present? && author_json["email"].present?)
|
||||||
return find_user_by_login_and_mail(author_json['name'], author_json["email"])
|
return find_user_by_login_and_mail(author_json['name'], author_json["email"])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_cache_commit_author(author_json)
|
def render_cache_commit_author(author_json)
|
||||||
Rails.logger.info author_json['Email']
|
Rails.logger.info author_json['Email']
|
||||||
if author_json["name"].present? && author_json["email"].present?
|
if author_json["name"].present? && author_json["email"].present?
|
||||||
return find_user_in_redis_cache(author_json['name'], author_json['email'])
|
return find_user_in_redis_cache(author_json['name'], author_json['email'])
|
||||||
end
|
end
|
||||||
if author_json["Name"].present? && author_json["Email"].present?
|
if author_json["Name"].present? && author_json["Email"].present?
|
||||||
return find_user_in_redis_cache(author_json['Name'], author_json['Email'])
|
return find_user_in_redis_cache(author_json['Name'], author_json['Email'])
|
||||||
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=\"(.*?)\"/
|
||||||
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.to_s.gsub("'","\"").scan(src_regex)
|
||||||
total_images = ss + ss_src
|
total_images = ss + ss_src
|
||||||
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
|
end
|
||||||
# if r_content.include?("?")
|
path_last = r_content
|
||||||
# new_r_content = r_content + "&raw=true"
|
path_current = ""
|
||||||
# else
|
# 相对路径处理
|
||||||
# new_r_content = r_content + "?raw=true"
|
if r_content.start_with?("../")
|
||||||
# end
|
relative_path_length = r_content.split("../").size - 1
|
||||||
new_r_content = r_content
|
path_pre = path.split("/").size - 1 - relative_path_length
|
||||||
|
path_pre = 0 if path_pre < 0
|
||||||
unless r_content.include?("http://") || r_content.include?("https://") || r_content.include?("mailto:")
|
path_current = path.split("/")[0..path_pre].join("/")
|
||||||
# new_r_content = "#{path}" + new_r_content
|
path_last = r_content.split("../").last
|
||||||
new_r_content = [base_url, "/api/#{owner&.login}/#{repo.identifier}/raw?filepath=#{r_content}&ref=#{ref}"].join
|
elsif r_content.start_with?("/") # 根路径处理
|
||||||
end
|
path_last = r_content[1..r_content.size]
|
||||||
content = content.gsub(/#{r_content}/, new_r_content)
|
else
|
||||||
end
|
path_current = path
|
||||||
end
|
end
|
||||||
|
# if r_content.include?("?")
|
||||||
return content
|
# new_r_content = r_content + "&raw=true"
|
||||||
rescue
|
# else
|
||||||
return str
|
# new_r_content = r_content + "?raw=true"
|
||||||
end
|
# end
|
||||||
end
|
new_r_content = r_content
|
||||||
|
|
||||||
# unix_time values for example: 1604382982
|
unless r_content.include?("http://") || r_content.include?("https://") || r_content.include?("mailto:")
|
||||||
def render_format_time_with_unix(unix_time)
|
# new_r_content = "#{path}" + new_r_content
|
||||||
Time.at(unix_time).strftime("%Y-%m-%d %H:%M")
|
new_r_content = ["base_url", "/api/#{owner&.login}/#{repo.identifier}/raw?filepath=#{path_current}/#{path_last}&ref=#{ref}"].join
|
||||||
end
|
end
|
||||||
|
content = content.gsub(/src=\"#{r_content}\"/, "src=\"#{new_r_content}\"")
|
||||||
# date for example: 2020-11-01T19:57:27+08:00
|
end
|
||||||
def render_format_time_with_date(date)
|
end
|
||||||
date.to_time.strftime("%Y-%m-%d %H:%M")
|
|
||||||
end
|
return content
|
||||||
|
rescue
|
||||||
def decode64_content(entry, owner, repo, ref, path=nil)
|
return str
|
||||||
if is_readme?(entry['type'], entry['name'])
|
end
|
||||||
content = Gitea::Repository::Entries::GetService.call(owner, repo.identifier, URI.escape(entry['path']), ref: ref)['content']
|
end
|
||||||
readme_render_decode64_content(content, owner, repo, ref)
|
|
||||||
else
|
# unix_time values for example: 1604382982
|
||||||
file_type = File.extname(entry['name'].to_s)[1..-1]
|
def render_format_time_with_unix(unix_time)
|
||||||
if image_type?(file_type)
|
Time.at(unix_time).strftime("%Y-%m-%d %H:%M")
|
||||||
return entry['content'].nil? ? Gitea::Repository::Entries::GetService.call(owner, repo.identifier, URI.escape(entry['path']), ref: ref)['content'] : entry['content']
|
end
|
||||||
end
|
|
||||||
if download_type(file_type)
|
# date for example: 2020-11-01T19:57:27+08:00
|
||||||
return entry['content']
|
def render_format_time_with_date(date)
|
||||||
end
|
date.to_time.strftime("%Y-%m-%d %H:%M")
|
||||||
render_decode64_content(entry['content'])
|
end
|
||||||
end
|
|
||||||
end
|
def decode64_content(entry, owner, repo, ref, path=nil)
|
||||||
|
if is_readme?(entry['type'], entry['name'])
|
||||||
def base64_to_image(path, content)
|
content = Gitea::Repository::Entries::GetService.call(owner, repo.identifier, URI.escape(entry['path']), ref: ref)['content']
|
||||||
# generate to https://git.trusite.net/pawm36ozq/-/raw/branch/master/entrn.png"
|
path = URI.escape(entry['path']).to_s.downcase.gsub("/readme.md","")
|
||||||
content = Base64.decode64(content)
|
readme_render_decode64_content(content, owner, repo, ref, path)
|
||||||
File.open(path, 'wb') { |f| f.write(content) }
|
else
|
||||||
end
|
file_type = File.extname(entry['name'].to_s)[1..-1]
|
||||||
|
if image_type?(file_type)
|
||||||
def render_download_image_url(dir_path, file_path, content)
|
return entry['content'].nil? ? Gitea::Repository::Entries::GetService.call(owner, repo.identifier, URI.escape(entry['path']), ref: ref)['content'] : entry['content']
|
||||||
full_path = file_path.starts_with?("/") ? [dir_path, file_path].join("") : [dir_path, file_path].join("/")
|
end
|
||||||
file_name = full_path.split("/")[-1]
|
if download_type(file_type)
|
||||||
# 用户名/项目标识/文件路径
|
return entry['content']
|
||||||
dir_path = generate_dir_path(full_path.split("/"+file_name)[0])
|
end
|
||||||
|
render_decode64_content(entry['content'])
|
||||||
file_path = [dir_path, file_name].join('/')
|
end
|
||||||
|
end
|
||||||
puts "##### render_download_image_url file_path: #{file_path}"
|
|
||||||
base64_to_image(file_path, content)
|
def base64_to_image(path, content)
|
||||||
file_path = file_path[6..-1]
|
# generate to https://git.trusite.net/pawm36ozq/-/raw/branch/master/entrn.png"
|
||||||
File.join(base_url, file_path)
|
content = Base64.decode64(content)
|
||||||
end
|
File.open(path, 'wb') { |f| f.write(content) }
|
||||||
|
end
|
||||||
def generate_dir_path(dir_path)
|
|
||||||
# tmp_dir_path
|
def render_download_image_url(dir_path, file_path, content)
|
||||||
# eg: jasder/forgeplus/raw/branch/ref
|
full_path = file_path.starts_with?("/") ? [dir_path, file_path].join("") : [dir_path, file_path].join("/")
|
||||||
dir_path = ["public", tmp_dir, dir_path].join('/')
|
file_name = full_path.split("/")[-1]
|
||||||
puts "#### dir_path: #{dir_path}"
|
# 用户名/项目标识/文件路径
|
||||||
unless Dir.exists?(dir_path)
|
dir_path = generate_dir_path(full_path.split("/"+file_name)[0])
|
||||||
FileUtils.mkdir_p(dir_path) ##不成功这里会抛异常
|
|
||||||
end
|
file_path = [dir_path, file_name].join('/')
|
||||||
dir_path
|
|
||||||
end
|
puts "##### render_download_image_url file_path: #{file_path}"
|
||||||
|
base64_to_image(file_path, content)
|
||||||
def tmp_dir
|
file_path = file_path[6..-1]
|
||||||
"repo"
|
File.join(base_url, file_path)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
def generate_dir_path(dir_path)
|
||||||
|
# tmp_dir_path
|
||||||
|
# eg: jasder/forgeplus/raw/branch/ref
|
||||||
|
dir_path = ["public", tmp_dir, dir_path].join('/')
|
||||||
|
puts "#### dir_path: #{dir_path}"
|
||||||
|
unless Dir.exists?(dir_path)
|
||||||
|
FileUtils.mkdir_p(dir_path) ##不成功这里会抛异常
|
||||||
|
end
|
||||||
|
dir_path
|
||||||
|
end
|
||||||
|
|
||||||
|
def tmp_dir
|
||||||
|
"repo"
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
Loading…
Reference in New Issue