add: user upload image

This commit is contained in:
yystopf 2022-01-03 22:32:09 +08:00
parent cfd94cb3b7
commit f339df699e
4 changed files with 18 additions and 3 deletions

View File

@ -1,12 +1,13 @@
class UsersController < ApplicationController
include ApplicationHelper
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 :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 :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]
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?
@user.attributes = user_params.except(:image)
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

View File

@ -15,7 +15,7 @@ module RepositoriesHelper
end
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)
end

View File

@ -29,6 +29,7 @@ module Util
file.write(io)
end
end
true
end
def download_file(url, save_path)

View File

@ -213,6 +213,7 @@ Rails.application.routes.draw do
get :watch_users
get :fan_users
get :hovercard
put :update_image
end
collection do
post :following