From 7e66c1349708b4f973125d3b53ea725014ad7696 Mon Sep 17 00:00:00 2001 From: xxq250 Date: Fri, 30 Jun 2023 11:45:03 +0800 Subject: [PATCH] =?UTF-8?q?readme=20GBK=E7=BC=96=E7=A0=81=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/helpers/repositories_helper.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/app/helpers/repositories_helper.rb b/app/helpers/repositories_helper.rb index 4ccaa7d36..4ddae72d5 100644 --- a/app/helpers/repositories_helper.rb +++ b/app/helpers/repositories_helper.rb @@ -115,7 +115,7 @@ module RepositoriesHelper def new_readme_render_decode64_content(str, owner, repo, ref, readme_path, readme_name) file_path = readme_path.include?('/') ? readme_path.gsub("/#{readme_name}", '') : readme_path.gsub("#{readme_name}", '') return nil if str.blank? - content = Base64.decode64(str).force_encoding('UTF-8') + content = Base64.decode64(str).encoding == Encoding::ASCII_8BIT ? Base64.decode64(str).force_encoding("GBK").encode("UTF-8"): Base64.decode64(str).force_encoding('UTF-8') # s_regex = /\s\!\[.*?\]\((.*?)\)\s/ s_regex_c = /`{1,2}[^`](.*?)`{1,2}/ s_regex = /```([\s\S]*?)```[\s]?/ @@ -180,7 +180,9 @@ module RepositoriesHelper end return content - rescue + rescue Exception => e + Rails.logger.error("===================#{readme_path}:#{readme_name}:error:#{e}") + # e.backtrace.each { |msg| Rails.logger.error(msg) } return str end @@ -196,7 +198,7 @@ module RepositoriesHelper 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'] + content = entry['content'].present? ? entry['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']) @@ -205,9 +207,9 @@ module RepositoriesHelper def decode64_content(entry, owner, repo, ref, path=nil) if is_readme?(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 = entry['content'].present? ? entry['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) + return Base64.decode64(content).force_encoding("GBK").encode("UTF-8") if Base64.decode64(content).encoding == Encoding::ASCII_8BIT return Base64.decode64(content).force_encoding('UTF-8') else file_type = File.extname(entry['name'].to_s)[1..-1]