新增:新增上传头像 API,采用本地存储方式存储头像

This commit is contained in:
2023-01-05 22:32:23 +08:00
parent e77c77419b
commit 5252c54c48
54 changed files with 931 additions and 937 deletions

View File

@@ -35,6 +35,7 @@ import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import top.charles7c.cnadmin.common.config.properties.CorsProperties;
import top.charles7c.cnadmin.common.config.properties.LocalStorageProperties;
/**
* Web MVC 配置
@@ -48,6 +49,7 @@ import top.charles7c.cnadmin.common.config.properties.CorsProperties;
public class WebMvcConfiguration implements WebMvcConfigurer {
private final CorsProperties corsProperties;
private final LocalStorageProperties localStorageProperties;
private final MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter;
/**
@@ -55,6 +57,13 @@ public class WebMvcConfiguration implements WebMvcConfigurer {
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
LocalStorageProperties.LocalStoragePath path = localStorageProperties.getPath();
String avatarUtl = "file:" + path.getAvatar().replace("\\", "/");
String fileUrl = "file:" + path.getFile().replace("\\", "/");
registry.addResourceHandler(localStorageProperties.getFilePattern()).addResourceLocations(fileUrl)
.setCachePeriod(0);
registry.addResourceHandler(localStorageProperties.getAvatarPattern()).addResourceLocations(avatarUtl)
.setCachePeriod(0);
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/")
.setCacheControl(CacheControl.maxAge(5, TimeUnit.HOURS).cachePublic());

View File

@@ -0,0 +1,87 @@
/*
* 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.config.properties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import cn.hutool.system.OsInfo;
import cn.hutool.system.SystemUtil;
/**
* 本地存储配置属性
*
* @author Charles7c
* @since 2023/1/2 19:43
*/
@Data
@Component
@ConfigurationProperties(prefix = "local-storage")
public class LocalStorageProperties {
/** 文件模式 */
private String filePattern;
/** 头像模式 */
private String avatarPattern;
/** 文件大小限制 */
private Long maxSizeInMb;
/** 头像大小限制 */
private Long avatarMaxSizeInMb;
/** Windows 系统本地存储路径 */
private LocalStoragePath windows;
/** Linux 系统本地存储路径 */
private LocalStoragePath linux;
/** MAC 系统本地存储路径 */
private LocalStoragePath mac;
/**
* 获取存储路径
*
* @return /
*/
public LocalStoragePath getPath() {
OsInfo osInfo = SystemUtil.getOsInfo();
if (osInfo.isWindows()) {
return windows;
}
if (osInfo.isMac()) {
return mac;
}
return linux;
}
/**
* 本地存储路径
*/
@Data
public static class LocalStoragePath {
/** 文件存储路径 */
private String file;
/** 头像存储路径 */
private String avatar;
}
}

View File

@@ -0,0 +1,36 @@
/*
* 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.consts;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
/**
* 文件常量
*
* @author Charles7c
* @since 2023/1/2 21:19
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class FileConstants {
/**
* 头像支持的图片类型
*/
public static final String[] AVATAR_SUPPORTED_IMG_TYPES = {"jpg", "png", "gif", "jpeg"};
}

View File

@@ -33,8 +33,10 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import cn.dev33.satoken.exception.NotLoginException;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import top.charles7c.cnadmin.common.exception.BadRequestException;
@@ -164,6 +166,18 @@ public class GlobalExceptionHandler {
return R.fail(HttpStatus.UNAUTHORIZED.value(), "认证失败,无法访问系统资源");
}
/**
* 拦截文件上传异常-超过上传大小限制
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MaxUploadSizeExceededException.class)
public R handleMaxUploadSizeExceededException(MaxUploadSizeExceededException e, HttpServletRequest request) {
String sizeLimit = StrUtil.subBetween(e.getMessage(), "The maximum size ", " for");
log.error("请求地址'{}',上传文件失败,文件大小超过限制", request.getRequestURI(), e);
return R.fail(HttpStatus.BAD_REQUEST.value(),
String.format("请上传小于 %s MB 的文件", NumberUtil.parseLong(sizeLimit) / 1024 / 1024));
}
/**
* 操作日志保存异常信息
*

View File

@@ -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;
}
}

View File

@@ -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
*

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}
}