forked from Gitlink/forgeplus
add: user upload image
This commit is contained in:
parent
cfd94cb3b7
commit
f339df699e
|
@ -1,12 +1,13 @@
|
||||||
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]
|
||||||
before_action :require_login, only: %i[me list sync_user_info]
|
before_action :require_login, only: %i[me list sync_user_info]
|
||||||
before_action :connect_to_ci_db, only: [:get_user_info]
|
before_action :connect_to_ci_db, only: [:get_user_info]
|
||||||
before_action :convert_image!, only: [:update]
|
before_action :convert_image!, only: [:update, :update_image]
|
||||||
skip_before_action :check_sign, only: [:attachment_show]
|
skip_before_action :check_sign, only: [:attachment_show]
|
||||||
|
|
||||||
def connect_to_ci_db(options={})
|
def connect_to_ci_db(options={})
|
||||||
|
@ -82,7 +83,19 @@ class UsersController < ApplicationController
|
||||||
Util.write_file(@image, avatar_path(@user)) if user_params[:image].present?
|
Util.write_file(@image, avatar_path(@user)) if user_params[:image].present?
|
||||||
@user.attributes = user_params.except(:image)
|
@user.attributes = user_params.except(:image)
|
||||||
unless @user.save
|
unless @user.save
|
||||||
render_error(@user.errors.full_messages.join(", "))
|
render_error(-1, @user.errors.full_messages.join(", "))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def update_image
|
||||||
|
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_error(-1, '头像格式不正确!') unless params[:image].present? && image_type?(File.extname(params[:image].original_filename.to_s)[1..-1])
|
||||||
|
if Util.write_file(@image, avatar_path(@user)) && params[:image].present?
|
||||||
|
render_ok({message: '头像修改成功'})
|
||||||
|
else
|
||||||
|
render_error(-1, '头像修改失败!')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ module RepositoriesHelper
|
||||||
end
|
end
|
||||||
|
|
||||||
def image_type?(str)
|
def image_type?(str)
|
||||||
default_type = %w(png jpg gif tif psd svg bmp webp jpeg)
|
default_type = %w(png jpg gif tif psd svg bmp webp jpeg ico psd)
|
||||||
default_type.include?(str&.downcase)
|
default_type.include?(str&.downcase)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -29,6 +29,7 @@ module Util
|
||||||
file.write(io)
|
file.write(io)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
def download_file(url, save_path)
|
def download_file(url, save_path)
|
||||||
|
|
|
@ -213,6 +213,7 @@ Rails.application.routes.draw do
|
||||||
get :watch_users
|
get :watch_users
|
||||||
get :fan_users
|
get :fan_users
|
||||||
get :hovercard
|
get :hovercard
|
||||||
|
put :update_image
|
||||||
end
|
end
|
||||||
collection do
|
collection do
|
||||||
post :following
|
post :following
|
||||||
|
|
Loading…
Reference in New Issue