From 3614f3e15b700d16c0b4ae5910d296975d437d41 Mon Sep 17 00:00:00 2001 From: jasder Date: Fri, 9 Jul 2021 16:57:11 +0800 Subject: [PATCH] =?UTF-8?q?FIX=20=E8=A7=A3=E5=86=B3=E7=A7=81=E6=9C=89?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E5=BA=93=E5=BA=93=E4=B8=8D=E8=83=BD=E8=AE=BF?= =?UTF-8?q?=E9=97=AE=E3=80=81=E4=B8=8B=E8=BD=BD=E5=9B=BE=E7=89=87=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/helpers/repositories_helper.rb | 37 ++++++++++++++++++- .../repositories/_simple_entry.json.jbuilder | 11 +++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/app/helpers/repositories_helper.rb b/app/helpers/repositories_helper.rb index e704acb8..95c669b7 100644 --- a/app/helpers/repositories_helper.rb +++ b/app/helpers/repositories_helper.rb @@ -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 diff --git a/app/views/repositories/_simple_entry.json.jbuilder b/app/views/repositories/_simple_entry.json.jbuilder index ec2a045b..7d41bbd2 100644 --- a/app/views/repositories/_simple_entry.json.jbuilder +++ b/app/views/repositories/_simple_entry.json.jbuilder @@ -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'])