diff --git a/app/helpers/repositories_helper.rb b/app/helpers/repositories_helper.rb index 10ad1377e..07646c07a 100644 --- a/app/helpers/repositories_helper.rb +++ b/app/helpers/repositories_helper.rb @@ -11,7 +11,7 @@ module RepositoriesHelper end def download_type(str) - default_type = %w(xlsx xls ppt pptx pdf zip 7z rar exe pdb obj idb png jpg gif tif psd svg RData rdata doc docx mpp vsdx) + default_type = %w(xlsx xls ppt pptx pdf zip 7z rar exe pdb obj idb png jpg gif tif psd svg RData rdata doc docx mpp vsdx dot) default_type.include?(str&.downcase) end @@ -20,8 +20,8 @@ module RepositoriesHelper default_type.include?(str&.downcase) end - def is_readme_type?(str) - return false if str.blank? + def is_readme?(type, str) + return false if type != 'file' || str.blank? readme_types = ["readme.md", "readme", "readme_en.md", "readme_zh.md", "readme_en", "readme_zh"] readme_types.include?(str.to_s.downcase) end @@ -72,4 +72,15 @@ module RepositoriesHelper def render_format_time_with_date(date) date.to_time.strftime("%Y-%m-%d %H:%M") end + + def decode64_content(entry, owner, repo, ref, path=nil) + if is_readme?(entry['type'], entry['name']) + content = Gitea::Repository::Entries::GetService.call(owner, repo.identifier, entry['path'], ref: ref)['content'] + readme_render_decode64_content(content, path) + else + file_type = entry['name'].to_s.split(".").last + return entry['content'] if download_type(file_type) + render_decode64_content(entry['content']) + end + end end diff --git a/app/views/repositories/_simple_entry.json.jbuilder b/app/views/repositories/_simple_entry.json.jbuilder index ab9910dd1..1e8e001af 100644 --- a/app/views/repositories/_simple_entry.json.jbuilder +++ b/app/views/repositories/_simple_entry.json.jbuilder @@ -8,12 +8,13 @@ if @project.forge? json.path entry['path'] json.type entry['type'] json.size entry['size'] - json.content entry['content'].present? && !direct_download ? render_decode64_content(entry['content']) : "" + + json.content decode64_content(entry, @owner, @repository, @ref) json.target entry['target'] json.download_url entry['download_url'] json.direct_download direct_download json.image_type image_type - json.is_readme_file is_readme_type?(file_name) + json.is_readme_file is_readme?(entry['type'], entry['name']) if entry['latest_commit'] if entry['type'] != 'file' json.partial! 'last_commit', entry: entry diff --git a/app/views/repositories/entries.json.jbuilder b/app/views/repositories/entries.json.jbuilder index 20fecec32..ca041fd53 100644 --- a/app/views/repositories/entries.json.jbuilder +++ b/app/views/repositories/entries.json.jbuilder @@ -51,17 +51,8 @@ if @project.forge? json.sha entry['sha'] json.type entry['type'] json.size entry['size'] - content = - if is_readme_type?(entry['name']) - is_readme_file = true - content = Gitea::Repository::Entries::GetService.call(@owner, @project.identifier, entry['name'], ref: @ref)['content'] - readme_render_decode64_content(content, @path) - else - is_readme_file = false - entry['content'] - end - json.is_readme_file is_readme_file - json.content content + json.is_readme_file is_readme?(entry['type'], entry['name']) + json.content decode64_content(entry, @owner, @repository, @ref, @path) json.target entry['target'] if entry['latest_commit'] json.partial! 'last_commit', entry: entry