FIX 解决私有版本库库不能访问、下载图片的问题

This commit is contained in:
jasder 2021-07-09 16:57:11 +08:00
parent e76d475fef
commit 3614f3e15b
2 changed files with 46 additions and 2 deletions

View File

@ -15,7 +15,7 @@ module RepositoriesHelper
end
def image_type?(str)
default_type = %w(png jpg gif tif psd svg)
default_type = %w(png jpg gif tif psd svg gif bmp webp jpeg)
default_type.include?(str&.downcase)
end
@ -86,4 +86,39 @@ module RepositoriesHelper
render_decode64_content(entry['content'])
end
end
def base64_to_image(path, content)
# generate to https://git.trusite.net/pawm36ozq/-/raw/branch/master/entrn.png"
content = Base64.decode64(content)
File.open(path, 'wb') { |f| f.write(content) }
end
def render_download_image_url(dir_path, file_path, content)
# 用户名/项目标识/文件路径
dir_path = generate_dir_path(dir_path)
file_path = [dir_path, file_path].join('/')
puts "##### render_download_image_url file_path: #{file_path}"
base64_to_image(file_path, content)
file_path = file_path.split('public')[1]
File.join(base_url, file_path)
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

View File

@ -11,7 +11,16 @@ if @project.forge?
json.content decode64_content(entry, @owner, @repository, @ref)
json.target entry['target']
json.download_url entry['download_url']
download_url =
if image_type
dir_path = [@owner.login, @repository.identifier, "raw/branch", @ref].join('/')
render_download_image_url(dir_path, entry['path'], decode64_content(entry, @owner, @repository, @ref))
else
entry['download_url']
end
json.download_url download_url
json.direct_download direct_download
json.image_type image_type
json.is_readme_file is_readme?(entry['type'], entry['name'])