mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-09-24 20:57:21 +08:00
新增:新增上传头像 API,采用本地存储方式存储头像
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.charles7c.cnadmin.common.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.io.file.FileNameUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
|
||||
/**
|
||||
* 文件工具类
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2023/1/2 21:34
|
||||
*/
|
||||
@Slf4j
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class FileUtils {
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
*
|
||||
* @param multipartFile
|
||||
* 源文件对象
|
||||
* @param filePath
|
||||
* 文件路径
|
||||
* @param isKeepOriginalFilename
|
||||
* 是否保留原文件名
|
||||
* @return 目标文件对象
|
||||
*/
|
||||
public static File upload(MultipartFile multipartFile, String filePath, boolean isKeepOriginalFilename) {
|
||||
String originalFilename = multipartFile.getOriginalFilename();
|
||||
String extensionName = FileNameUtil.extName(originalFilename);
|
||||
|
||||
String filename;
|
||||
if (isKeepOriginalFilename) {
|
||||
filename = String.format("%s-%s.%s", FileNameUtil.getPrefix(originalFilename),
|
||||
DateUtil.format(LocalDateTime.now(), "yyyyMMddHHmmssS"), extensionName);
|
||||
} else {
|
||||
filename = String.format("%s.%s", IdUtil.fastSimpleUUID(), extensionName);
|
||||
}
|
||||
|
||||
try {
|
||||
String pathname = filePath + filename;
|
||||
File dest = new File(pathname).getCanonicalFile();
|
||||
// 如果父路径不存在,自动创建
|
||||
if (!dest.getParentFile().exists()) {
|
||||
if (!dest.getParentFile().mkdirs()) {
|
||||
log.error("Create upload file parent path failed.");
|
||||
}
|
||||
}
|
||||
// 文件写入
|
||||
multipartFile.transferTo(dest);
|
||||
return dest;
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@@ -65,6 +65,17 @@ public class LoginHelper {
|
||||
return loginUser;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新登录用户信息
|
||||
*
|
||||
* @param loginUser
|
||||
* 登录用户信息
|
||||
*/
|
||||
public static void updateLoginUser(LoginUser loginUser) {
|
||||
SaHolder.getStorage().set(CacheConstants.LOGIN_USER_CACHE_KEY, loginUser);
|
||||
StpUtil.getTokenSession().set(CacheConstants.LOGIN_USER_CACHE_KEY, loginUser);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取登录用户 ID
|
||||
*
|
||||
|
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.charles7c.cnadmin.common.util.validate;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import top.charles7c.cnadmin.common.exception.ServiceException;
|
||||
|
||||
/**
|
||||
* 业务检查工具类(抛出 500 ServiceException)
|
||||
*
|
||||
* @author Charles7c
|
||||
* @see ServiceException
|
||||
* @since 2023/1/2 22:12
|
||||
*/
|
||||
@Slf4j
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class CheckUtils extends Validator {
|
||||
|
||||
private static final Class<ServiceException> EXCEPTION_TYPE = ServiceException.class;
|
||||
|
||||
/**
|
||||
* 如果为空,抛出异常
|
||||
*
|
||||
* @param obj
|
||||
* 被检测的对象
|
||||
* @param message
|
||||
* 错误信息
|
||||
*/
|
||||
public static void exIfNull(Object obj, String message) {
|
||||
exIfNull(obj, message, EXCEPTION_TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果为空,抛出异常
|
||||
*
|
||||
* @param str
|
||||
* 被检测的字符串
|
||||
* @param message
|
||||
* 错误信息
|
||||
*/
|
||||
public static void exIfBlank(CharSequence str, String message) {
|
||||
exIfBlank(str, message, EXCEPTION_TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果相同,抛出异常
|
||||
*
|
||||
* @param obj1
|
||||
* 要比较的对象1
|
||||
* @param obj2
|
||||
* 要比较的对象2
|
||||
* @param message
|
||||
* 错误信息
|
||||
*/
|
||||
public static void exIfEqual(Object obj1, Object obj2, String message) {
|
||||
exIfEqual(obj1, obj2, message, EXCEPTION_TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果不相同,抛出异常
|
||||
*
|
||||
* @param obj1
|
||||
* 要比较的对象1
|
||||
* @param obj2
|
||||
* 要比较的对象2
|
||||
* @param message
|
||||
* 错误信息
|
||||
*/
|
||||
public static void exIfNotEqual(Object obj1, Object obj2, String message) {
|
||||
exIfNotEqual(obj1, obj2, message, EXCEPTION_TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果条件成立,抛出异常
|
||||
*
|
||||
* @param conditionSupplier
|
||||
* 条件
|
||||
* @param message
|
||||
* 错误信息
|
||||
*/
|
||||
public static void exIfCondition(java.util.function.BooleanSupplier conditionSupplier, String message) {
|
||||
exIfCondition(conditionSupplier, message, EXCEPTION_TYPE);
|
||||
}
|
||||
}
|
@@ -14,26 +14,26 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.charles7c.cnadmin.common.util;
|
||||
package top.charles7c.cnadmin.common.util.validate;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import top.charles7c.cnadmin.common.exception.BadRequestException;
|
||||
|
||||
/**
|
||||
* 检查工具类
|
||||
* 校验工具类(抛出 400 BadRequestException)
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2022/12/21 20:56
|
||||
* @see BadRequestException
|
||||
*/
|
||||
@Slf4j
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class CheckUtils {
|
||||
public class ValidationUtils extends Validator {
|
||||
|
||||
private static final Class<BadRequestException> EXCEPTION_TYPE = BadRequestException.class;
|
||||
|
||||
/**
|
||||
* 如果为空,抛出异常
|
||||
@@ -44,10 +44,7 @@ public class CheckUtils {
|
||||
* 错误信息
|
||||
*/
|
||||
public static void exIfNull(Object obj, String message) {
|
||||
if (obj == null) {
|
||||
log.error(message);
|
||||
throw new BadRequestException(message);
|
||||
}
|
||||
exIfNull(obj, message, EXCEPTION_TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -59,10 +56,7 @@ public class CheckUtils {
|
||||
* 错误信息
|
||||
*/
|
||||
public static void exIfBlank(CharSequence str, String message) {
|
||||
if (StrUtil.isBlank(str)) {
|
||||
log.error(message);
|
||||
throw new BadRequestException(message);
|
||||
}
|
||||
exIfBlank(str, message, EXCEPTION_TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -76,10 +70,7 @@ public class CheckUtils {
|
||||
* 错误信息
|
||||
*/
|
||||
public static void exIfEqual(Object obj1, Object obj2, String message) {
|
||||
if (ObjectUtil.equals(obj1, obj2)) {
|
||||
log.error(message);
|
||||
throw new BadRequestException(message);
|
||||
}
|
||||
exIfEqual(obj1, obj2, message, EXCEPTION_TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -93,10 +84,7 @@ public class CheckUtils {
|
||||
* 错误信息
|
||||
*/
|
||||
public static void exIfNotEqual(Object obj1, Object obj2, String message) {
|
||||
if (ObjectUtil.notEqual(obj1, obj2)) {
|
||||
log.error(message);
|
||||
throw new BadRequestException(message);
|
||||
}
|
||||
exIfNotEqual(obj1, obj2, message, EXCEPTION_TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -108,9 +96,6 @@ public class CheckUtils {
|
||||
* 错误信息
|
||||
*/
|
||||
public static void exIfCondition(java.util.function.BooleanSupplier conditionSupplier, String message) {
|
||||
if (conditionSupplier != null && conditionSupplier.getAsBoolean()) {
|
||||
log.error(message);
|
||||
throw new BadRequestException(message);
|
||||
}
|
||||
exIfCondition(conditionSupplier, message, EXCEPTION_TYPE);
|
||||
}
|
||||
}
|
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.charles7c.cnadmin.common.util.validate;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
/**
|
||||
* @author Charles7c
|
||||
* @since 2023/1/2 22:12
|
||||
*/
|
||||
@Slf4j
|
||||
@NoArgsConstructor(access = AccessLevel.PROTECTED)
|
||||
public class Validator {
|
||||
|
||||
/**
|
||||
* 如果为空,抛出异常
|
||||
*
|
||||
* @param obj
|
||||
* 被检测的对象
|
||||
* @param message
|
||||
* 错误信息
|
||||
* @param exceptionType
|
||||
* 异常类型
|
||||
*/
|
||||
protected static void exIfNull(Object obj, String message, Class<? extends RuntimeException> exceptionType) {
|
||||
if (obj == null) {
|
||||
log.error(message);
|
||||
throw ReflectUtil.newInstance(exceptionType, message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果为空,抛出异常
|
||||
*
|
||||
* @param str
|
||||
* 被检测的字符串
|
||||
* @param message
|
||||
* 错误信息
|
||||
* @param exceptionType
|
||||
* 异常类型
|
||||
*/
|
||||
public static void exIfBlank(CharSequence str, String message, Class<? extends RuntimeException> exceptionType) {
|
||||
if (StrUtil.isBlank(str)) {
|
||||
log.error(message);
|
||||
throw ReflectUtil.newInstance(exceptionType, message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果相同,抛出异常
|
||||
*
|
||||
* @param obj1
|
||||
* 要比较的对象1
|
||||
* @param obj2
|
||||
* 要比较的对象2
|
||||
* @param message
|
||||
* 错误信息
|
||||
* @param exceptionType
|
||||
* 异常类型
|
||||
*/
|
||||
public static void exIfEqual(Object obj1, Object obj2, String message,
|
||||
Class<? extends RuntimeException> exceptionType) {
|
||||
if (ObjectUtil.equals(obj1, obj2)) {
|
||||
log.error(message);
|
||||
throw ReflectUtil.newInstance(exceptionType, message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果不相同,抛出异常
|
||||
*
|
||||
* @param obj1
|
||||
* 要比较的对象1
|
||||
* @param obj2
|
||||
* 要比较的对象2
|
||||
* @param message
|
||||
* 错误信息
|
||||
* @param exceptionType
|
||||
* 异常类型
|
||||
*/
|
||||
public static void exIfNotEqual(Object obj1, Object obj2, String message,
|
||||
Class<? extends RuntimeException> exceptionType) {
|
||||
if (ObjectUtil.notEqual(obj1, obj2)) {
|
||||
log.error(message);
|
||||
throw ReflectUtil.newInstance(exceptionType, message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果条件成立,抛出异常
|
||||
*
|
||||
* @param conditionSupplier
|
||||
* 条件
|
||||
* @param message
|
||||
* 错误信息
|
||||
* @param exceptionType
|
||||
* 异常类型
|
||||
*/
|
||||
public static void exIfCondition(java.util.function.BooleanSupplier conditionSupplier, String message,
|
||||
Class<? extends RuntimeException> exceptionType) {
|
||||
if (conditionSupplier != null && conditionSupplier.getAsBoolean()) {
|
||||
log.error(message);
|
||||
throw ReflectUtil.newInstance(exceptionType, message);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user