diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 6875b8155..8536147ca 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -773,7 +773,26 @@ class ApplicationController < ActionController::Base def base_url request.base_url end + + def convert_image! + @image = params[:image] || user_params[:image] + return unless @image.present? + max_size = EduSetting.get('upload_avatar_max_size') || 2 * 1024 * 1024 # 2M + if @image.class == ActionDispatch::Http::UploadedFile + render_error('请上传文件') if @image.size.zero? + render_error('文件大小超过限制') if @image.size > max_size.to_i + else + image = @image.to_s.strip + return render_error('请上传正确的图片') if image.blank? + @image = Util.convert_base64_image(image, max_size: max_size.to_i) + end + rescue Base64ImageConverter::Error => ex + render_error(ex.message) + end + def avatar_path(object) + ApplicationController.helpers.disk_filename(object.class, object.id) + end private def object_not_found diff --git a/app/controllers/organizations/organizations_controller.rb b/app/controllers/organizations/organizations_controller.rb index 29cb15a6a..7be9390d7 100644 --- a/app/controllers/organizations/organizations_controller.rb +++ b/app/controllers/organizations/organizations_controller.rb @@ -70,25 +70,6 @@ class Organizations::OrganizationsController < Organizations::BaseController end private - def convert_image! - return unless params[:image].present? - max_size = EduSetting.get('upload_avatar_max_size') || 2 * 1024 * 1024 # 2M - if params[:image].class == ActionDispatch::Http::UploadedFile - @image = params[:image] - render_error('请上传文件') if @image.size.zero? - render_error('文件大小超过限制') if @image.size > max_size.to_i - else - image = params[:image].to_s.strip - return render_error('请上传正确的图片') if image.blank? - @image = Util.convert_base64_image(image, max_size: max_size.to_i) - end - rescue Base64ImageConverter::Error => ex - render_error(ex.message) - end - - def avatar_path(organization) - ApplicationController.helpers.disk_filename(organization.class, organization.id) - end def organization_params params.permit(:name, :description, :website, :location, diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 4cbb2cbdf..6c28271db 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -6,6 +6,7 @@ class UsersController < ApplicationController 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] skip_before_action :check_sign, only: [:attachment_show] def connect_to_ci_db(options={}) @@ -73,7 +74,8 @@ class UsersController < ApplicationController def update return render_not_found unless @user = User.find_by_id(params[:id]) || User.find_by(login: params[:id]) - @user.attributes = user_params + Util.write_file(@image, avatar_path(@user)) if user_params[:image].present? + @user.attributes = user_params.except(:image) if @user.save render_ok else @@ -278,7 +280,7 @@ class UsersController < ApplicationController end def user_params - params.require(:user).permit(:nickname, + params.require(:user).permit(:nickname, :image, user_extension_attributes: [ :gender, :location, :location_city, :occupation, :technical_title, diff --git a/app/docs/slate/source/includes/_users.md b/app/docs/slate/source/includes/_users.md index 573e171eb..b72ee0c12 100644 --- a/app/docs/slate/source/includes/_users.md +++ b/app/docs/slate/source/includes/_users.md @@ -1,7 +1,7 @@ # Users @@ -67,6 +67,7 @@ await octokit.request('PATCH/PUT /api/users/:login.json') 参数 | 类型 | 字段说明 --------- | ----------- | ----------- |user.nickname |string |用户昵称 | +|user.image |string/file |用户头像 | |user.user_extension_attributes.gender |int |性别, 0男 1女 | |user.user_extension_attributes.province |string |省份 | |user.user_extension_attributes.city |string |城市 | diff --git a/app/queries/projects/list_my_query.rb b/app/queries/projects/list_my_query.rb index 5bda56120..5c508c961 100644 --- a/app/queries/projects/list_my_query.rb +++ b/app/queries/projects/list_my_query.rb @@ -53,7 +53,7 @@ class Projects::ListMyQuery < ApplicationQuery q = projects.ransack(name_or_identifier_cont: params[:search]) - scope = q.result.includes(:project_category, :project_language,:owner, :repository) + scope = q.result.includes(:project_category, :project_language,:owner, :repository, :has_pinned_users) sort = params[:sort_by] || "updated_on" sort_direction = params[:sort_direction] || "desc" diff --git a/public/docs/api.html b/public/docs/api.html index e713ea1d0..71753c32b 100644 --- a/public/docs/api.html +++ b/public/docs/api.html @@ -614,7 +614,7 @@ Success — a happy kitten is an authenticated kitten!

Users

获取当前登陆用户信息

@@ -700,6 +700,11 @@ Success — a happy kitten is an authenticated kitten! 用户昵称 +user.image +string/file +用户头像 + + user.user_extension_attributes.gender int 性别, 0男 1女