fix: upload user image allow base64
This commit is contained in:
parent
f339df699e
commit
499734ebf9
|
@ -709,14 +709,20 @@ class ApplicationController < ActionController::Base
|
||||||
Rails.application.config_for(:configuration)['platform_url'] || request.base_url
|
Rails.application.config_for(:configuration)['platform_url'] || request.base_url
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def image_type?(str)
|
||||||
|
default_type = %w(png jpg gif tif psd svg bmp webp jpeg ico psd)
|
||||||
|
default_type.include?(str&.downcase)
|
||||||
|
end
|
||||||
|
|
||||||
def convert_image!
|
def convert_image!
|
||||||
@image = params[:image]
|
@image = params[:image]
|
||||||
@image = @image.nil? && params[:user].present? ? params[:user][:image] : @image
|
@image = @image.nil? && params[:user].present? ? params[:user][:image] : @image
|
||||||
return unless @image.present?
|
return unless @image.present?
|
||||||
max_size = EduSetting.get('upload_avatar_max_size') || 2 * 1024 * 1024 # 2M
|
max_size = EduSetting.get('upload_avatar_max_size') || 2 * 1024 * 1024 # 2M
|
||||||
if @image.class == ActionDispatch::Http::UploadedFile
|
if @image.class == ActionDispatch::Http::UploadedFile
|
||||||
render_error('请上传文件') if @image.size.zero?
|
return render_error('请上传文件') if @image.size.zero?
|
||||||
render_error('文件大小超过限制') if @image.size > max_size.to_i
|
return render_error('文件大小超过限制') if @image.size > max_size.to_i
|
||||||
|
return render_error('头像格式不正确!') unless image_type?(File.extname(@image.original_filename.to_s)[1..-1])
|
||||||
else
|
else
|
||||||
image = @image.to_s.strip
|
image = @image.to_s.strip
|
||||||
return render_error('请上传正确的图片') if image.blank?
|
return render_error('请上传正确的图片') if image.blank?
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
class UsersController < ApplicationController
|
class UsersController < ApplicationController
|
||||||
include ApplicationHelper
|
include ApplicationHelper
|
||||||
include Ci::DbConnectable
|
include Ci::DbConnectable
|
||||||
include RepositoriesHelper
|
|
||||||
|
|
||||||
before_action :load_user, only: [:show, :homepage_info, :sync_token, :sync_gitea_pwd, :projects, :watch_users, :fan_users, :hovercard]
|
before_action :load_user, only: [:show, :homepage_info, :sync_token, :sync_gitea_pwd, :projects, :watch_users, :fan_users, :hovercard]
|
||||||
before_action :check_user_exist, only: [:show, :homepage_info,:projects, :watch_users, :fan_users, :hovercard]
|
before_action :check_user_exist, only: [:show, :homepage_info,:projects, :watch_users, :fan_users, :hovercard]
|
||||||
|
@ -91,12 +90,11 @@ class UsersController < ApplicationController
|
||||||
return render_not_found unless @user = User.find_by(login: params[:id]) || User.find_by_id(params[:id])
|
return render_not_found unless @user = User.find_by(login: params[:id]) || User.find_by_id(params[:id])
|
||||||
return render_forbidden unless User.current.logged? && (current_user&.admin? || current_user.id == @user.id)
|
return render_forbidden unless User.current.logged? && (current_user&.admin? || current_user.id == @user.id)
|
||||||
|
|
||||||
return render_error(-1, '头像格式不正确!') unless params[:image].present? && image_type?(File.extname(params[:image].original_filename.to_s)[1..-1])
|
Util.write_file(@image, avatar_path(@user))
|
||||||
if Util.write_file(@image, avatar_path(@user)) && params[:image].present?
|
return render_ok({message: '头像修改成功'})
|
||||||
render_ok({message: '头像修改成功'})
|
rescue Exception => e
|
||||||
else
|
uid_logger_error(e.message)
|
||||||
render_error(-1, '头像修改失败!')
|
render_error(-1, '头像修改失败!')
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def me
|
def me
|
||||||
|
|
Loading…
Reference in New Issue