From cd6e6b18c35a32a44e21af16ce7e05bcdaab8738 Mon Sep 17 00:00:00 2001 From: yystopf Date: Mon, 18 Apr 2022 15:53:22 +0800 Subject: [PATCH 1/2] fix: images avatar url --- app/helpers/api_index_helper.rb | 4 +- app/helpers/application_helper.rb | 42 +++++++++++-------- app/services/memos_service.rb | 10 ++--- app/services/users_service.rb | 6 +-- .../shared/_list.html.erb | 2 +- .../shared/_list.html.erb | 2 +- .../shared/_list.html.erb | 2 +- .../library_applies/shared/_list.html.erb | 2 +- .../shared/_list.html.erb | 2 +- .../shared/_list.html.erb | 2 +- .../shared/_list.html.erb | 2 +- app/views/admins/users/edit.html.erb | 2 +- .../video_applies/shared/_list.html.erb | 2 +- 13 files changed, 44 insertions(+), 36 deletions(-) diff --git a/app/helpers/api_index_helper.rb b/app/helpers/api_index_helper.rb index f368f155..9257439b 100644 --- a/app/helpers/api_index_helper.rb +++ b/app/helpers/api_index_helper.rb @@ -11,7 +11,7 @@ module ApiIndexHelper {username: current_user.show_real_name, login: current_user.login, user_id: current_user.id, - image_url: "/images/#{url_to_avatar(current_user)}?#{Time.now.to_i}", + image_url: "#{url_to_avatar(current_user, true)}?#{Time.now.to_i}", admin: current_user.admin?, user_url: "/users/#{current_user.try(:login)}", is_banned: user_is_banned?(current_user), @@ -26,7 +26,7 @@ module ApiIndexHelper {username: user.show_real_name, user_id: user.id, login: user.login, - image_url: "/images/#{url_to_avatar(user)}?#{Time.now.to_i}", + image_url: "#{url_to_avatar(user, true)}?#{Time.now.to_i}", admin: user.admin?, user_url: "/users/#{user.try(:login)}" } diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 218026e2..3787964a 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -140,27 +140,35 @@ module ApplicationHelper end # 用户图像url,如果不存在的话,source为匿名用户,即默认使用匿名用户图像 - def url_to_avatar(source) + def url_to_avatar(source, with_image=false) platform_url = Rails.application.config_for(:configuration)['forge_platform_url'] if platform_url && source.class.to_s == 'User' return platform_url + "/api/users/#{source&.login}/get_image" else - return "" if source&.id.blank? - if File.exist?(disk_filename(source&.class, source&.id)) - ctime = File.ctime(disk_filename(source.class, source.id)).to_i - if source.class.to_s == 'User' - File.join(relative_path, ["#{source.class}", "#{source.id}"]) + "?t=#{ctime}" - else - File.join("images/avatars", ["#{source.class}", "#{source.id}"]) + "?t=#{ctime}" - end - elsif source.class.to_s == 'User' - str = source.user_extension.try(:gender).to_i == 0 ? "b" : "g" - File.join(relative_path, "#{source.class}", str) - elsif source.class.to_s == 'Subject' - File.join("images","educoder", "index", "subject", "subject#{rand(17)}.jpg") - elsif source.class.to_s == 'Shixun' - File.join("images","educoder", "index", "shixun", "shixun#{rand(23)}.jpg") - end + if with_image + return "/images/" + get_disk_avatar_url(source) + else + return get_disk_avatar_url(source) + end + end + end + + def get_disk_avatar_url(source) + return "" if source&.id.blank? + if File.exist?(disk_filename(source&.class, source&.id)) + ctime = File.ctime(disk_filename(source.class, source.id)).to_i + if source.class.to_s == 'User' + File.join(relative_path, ["#{source.class}", "#{source.id}"]) + "?t=#{ctime}" + else + File.join("images/avatars", ["#{source.class}", "#{source.id}"]) + "?t=#{ctime}" + end + elsif source.class.to_s == 'User' + str = source.user_extension.try(:gender).to_i == 0 ? "b" : "g" + File.join(relative_path, "#{source.class}", str) + elsif source.class.to_s == 'Subject' + File.join("images","educoder", "index", "subject", "subject#{rand(17)}.jpg") + elsif source.class.to_s == 'Shixun' + File.join("images","educoder", "index", "shixun", "shixun#{rand(23)}.jpg") end end diff --git a/app/services/memos_service.rb b/app/services/memos_service.rb index 1d9ac5f7..b276ecb5 100644 --- a/app/services/memos_service.rb +++ b/app/services/memos_service.rb @@ -150,7 +150,7 @@ class MemosService watched: watched, is_blocked: current_user&.blocked_for(memo_author.id), is_blocked_by: memo_author&.blocked_for(current_user.id), - image_url: "/images/#{url_to_avatar(memo_author)}?#{Time.now.to_i}", + image_url: "#{url_to_avatar(memo_author, true)}", identity: memo_author.identity, login: memo_author.login, user_id: memo_author.id, @@ -159,7 +159,7 @@ class MemosService replies_count: memo_author_memos.total_replies.size, watchers_count: memo_author.fan_count, current_login: current_user.try(:login), - current_image_url: current_user.try(:login).present? ? "/images/#{url_to_avatar(current_user)}?#{Time.now.to_i}" : "/images/avatars/User/b", + current_image_url: current_user.try(:login).present? ? "#{url_to_avatar(current_user, true)}" : "/images/avatars/User/b", is_current_user: memo.author_id == current_user.try(:id) } recent_memos = memo_author_memos.posts.where("id != ?", params[:id].to_i).order_index("published_at").select([:id,:subject]).limit(3).as_json @@ -413,7 +413,7 @@ class MemosService memo.children << reply create_user_tidings(reply, "forum_comment") replies = {:id => reply.id, :content => reply.content, :time => time_from_now(reply.created_at), :user_id => reply.author_id, - :image_url => "/images/#{url_to_avatar(reply.author)}?#{Time.now.to_i}", :username => reply.author.show_real_name, :reward => memo.reward, :hidden => reply.hidden, + :image_url => "#{url_to_avatar(reply.author, true)}", :username => reply.author.show_real_name, :reward => memo.reward, :hidden => reply.hidden, :praise_count => reply.praises_count,:user_login => reply.author.try(:login), replies_count: reply.can_see_reply_count(current_user)} { status: 0, @@ -764,7 +764,7 @@ class MemosService permission = current_user ? current_user.manager_of_memo?(memo) : false # 实训(TPM)的管理员可以看到隐藏的评论 replies = {:id => memo.id, :content => memo.content, :time => time_from_now(memo.created_at), :user_id => memo.author_id, - :image_url => "/images/#{url_to_avatar(memo.author)}?#{Time.now.to_i}", :username => memo.author.show_real_name, :reward => memo.reward, :hidden => memo.hidden, + :image_url => "#{url_to_avatar(memo.author, true)}", :username => memo.author.show_real_name, :reward => memo.reward, :hidden => memo.hidden, :permission => permission, :praise_count => memo.praises_count, :user_praise => user_praise, :user_login => memo.author.try(:login), :admin => current_user&.admin, is_banned: is_banned, replies_count: memo.can_see_reply_count(current_user)} childrens = Memo.where(:parent_id => memo.id).includes(:author).reorder("created_at desc").limit(5) @@ -776,7 +776,7 @@ class MemosService children_praise = child.praise_tread.exists?(user_id: current_user.try(:id).to_i) children_is_banned = user_is_banned?(child.author) #帖子的用户是否被禁言 children_list << {:id => child.id, :content => child.content, :time => time_from_now(child.created_at),:praise_count => memo.praises_count, :user_praise => children_praise, - :image_url => "/images/#{url_to_avatar(child.author)}?#{Time.now.to_i}", :username => child.author.show_real_name, :hidden => child.hidden, replies_count: child.can_see_reply_count(current_user), + :image_url => "#{url_to_avatar(child.author,true)}?#{Time.now.to_i}", :username => child.author.show_real_name, :hidden => child.hidden, replies_count: child.can_see_reply_count(current_user), :permission => permission, :user_login => child.author.try(:login), :user_id => child.author.try(:id), :parent_id => child.parent_id, is_banned: children_is_banned} end list << replies.merge({children: children_list}) diff --git a/app/services/users_service.rb b/app/services/users_service.rb index d7322902..16d50df9 100644 --- a/app/services/users_service.rb +++ b/app/services/users_service.rb @@ -76,7 +76,7 @@ class UsersService block_lists.push({ username: user.show_name, login: user.login, - image_url: "/images/#{url_to_avatar(user)}?#{Time.now.to_i}", + image_url: "#{url_to_avatar(user, true)}", brief: user&.user_extension.try(:brief_introduction) }) end @@ -275,7 +275,7 @@ class UsersService #id用户id def show_user(params) @user = User.find(params[:id]) - img_url = "/images/"+url_to_avatar(@user) + img_url = url_to_avatar(@user, true) gender = @user.user_extension.gender.nil? ? 0 : @user.user_extension.gender work_unit = get_user_work_unit @user location = get_user_location @user @@ -419,7 +419,7 @@ class UsersService membership.sort! {|older, newer| newer.created_on <=> older.created_on } course_list = [] membership.each do |mp| - course_list << {:course => mp.course,:img_url => "/images/"+url_to_avatar(mp.course),:current_user_is_member => current_user.member_of_course?(mp.course),:current_user_is_teacher => is_course_teacher(current_user,mp.course)} + course_list << {:course => mp.course,:img_url => +url_to_avatar(mp.course,true),:current_user_is_member => current_user.member_of_course?(mp.course),:current_user_is_teacher => is_course_teacher(current_user,mp.course)} end course_list end diff --git a/app/views/admins/examination_authentications/shared/_list.html.erb b/app/views/admins/examination_authentications/shared/_list.html.erb index a751a7fa..8db7b150 100644 --- a/app/views/admins/examination_authentications/shared/_list.html.erb +++ b/app/views/admins/examination_authentications/shared/_list.html.erb @@ -24,7 +24,7 @@ <%= list_index_no((params[:page] || 1).to_i, index) %> <%= link_to "/users/#{user.login}", class: 'examination-authentication-avatar', target: '_blank', data: { toggle: 'tooltip', title: '个人主页' } do %> - + <% end %> <%= user.real_name %> diff --git a/app/views/admins/identity_authentications/shared/_list.html.erb b/app/views/admins/identity_authentications/shared/_list.html.erb index 6e12d1c5..26a4f880 100644 --- a/app/views/admins/identity_authentications/shared/_list.html.erb +++ b/app/views/admins/identity_authentications/shared/_list.html.erb @@ -43,7 +43,7 @@ <% end %> <%= link_to "/users/#{user.login}", class: 'identity-authentication-avatar', target: '_blank', data: { toggle: 'tooltip', title: '个人主页' } do %> - + <% end %> <% if user.from_sub_site? %> 合作 diff --git a/app/views/admins/item_authentications/shared/_list.html.erb b/app/views/admins/item_authentications/shared/_list.html.erb index 0c6b4cfa..a455eed7 100644 --- a/app/views/admins/item_authentications/shared/_list.html.erb +++ b/app/views/admins/item_authentications/shared/_list.html.erb @@ -30,7 +30,7 @@ <%= list_index_no((params[:page] || 1).to_i, index) %> <%= link_to "/users/#{user.login}", class: 'item-authentication-avatar', target: '_blank', data: { toggle: 'tooltip', title: '个人主页' } do %> - + <% end %> <%= user.real_name %> diff --git a/app/views/admins/library_applies/shared/_list.html.erb b/app/views/admins/library_applies/shared/_list.html.erb index fde3d1d1..1ef33ab3 100644 --- a/app/views/admins/library_applies/shared/_list.html.erb +++ b/app/views/admins/library_applies/shared/_list.html.erb @@ -26,7 +26,7 @@ <%= list_index_no((params[:page] || 1).to_i, index) %> <%= link_to "/users/#{user.login}", class: 'professional-authentication-avatar', target: '_blank', data: { toggle: 'tooltip', title: '个人主页' } do %> - + <% end %> <%= link_to user&.real_name,"/users/#{user&.login}", target: "_blank" %> diff --git a/app/views/admins/professional_authentications/shared/_list.html.erb b/app/views/admins/professional_authentications/shared/_list.html.erb index 7ffa5837..094d0f22 100644 --- a/app/views/admins/professional_authentications/shared/_list.html.erb +++ b/app/views/admins/professional_authentications/shared/_list.html.erb @@ -41,7 +41,7 @@ <% end %> <%= link_to "/users/#{user.login}", class: 'professional-authentication-avatar', target: '_blank', data: { toggle: 'tooltip', title: '个人主页' } do %> - + <% end %> <% if user.from_sub_site? %> 合作 diff --git a/app/views/admins/project_package_applies/shared/_list.html.erb b/app/views/admins/project_package_applies/shared/_list.html.erb index bec0912c..f523cacc 100644 --- a/app/views/admins/project_package_applies/shared/_list.html.erb +++ b/app/views/admins/project_package_applies/shared/_list.html.erb @@ -26,7 +26,7 @@ <%= list_index_no((params[:page] || 1).to_i, index) %> <%= link_to "/users/#{user.login}", class: 'professional-authentication-avatar', target: '_blank', data: { toggle: 'tooltip', title: '个人主页' } do %> - + <% end %> <%= link_to user&.real_name,"/users/#{user&.login}", target: "_blank" %> diff --git a/app/views/admins/subject_authorizations/shared/_list.html.erb b/app/views/admins/subject_authorizations/shared/_list.html.erb index 94503dad..fb37da3b 100644 --- a/app/views/admins/subject_authorizations/shared/_list.html.erb +++ b/app/views/admins/subject_authorizations/shared/_list.html.erb @@ -28,7 +28,7 @@ <%= list_index_no((params[:page] || 1).to_i, index) %> <%= link_to "/users/#{user.login}", class: 'subject-authorization-avatar', target: '_blank', data: { toggle: 'tooltip', title: '个人主页' } do %> - + <% end %> <%= link_to user&.real_name,"/users/#{user&.login}", target: "_blank" %> diff --git a/app/views/admins/users/edit.html.erb b/app/views/admins/users/edit.html.erb index 5abc8271..24db7bfe 100644 --- a/app/views/admins/users/edit.html.erb +++ b/app/views/admins/users/edit.html.erb @@ -8,7 +8,7 @@