mirror of https://github.com/elunez/eladmin
文件上传优化,加入FileProperties,根据系统选择上传目录
parent
646a79537a
commit
3694add6c1
|
@ -0,0 +1,41 @@
|
||||||
|
package me.zhengjie.config;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Configuration
|
||||||
|
@ConfigurationProperties(prefix = "file")
|
||||||
|
public class FileProperties {
|
||||||
|
|
||||||
|
/** 文件大小限制 */
|
||||||
|
private Long maxSize;
|
||||||
|
|
||||||
|
/** 头像大小限制 */
|
||||||
|
private Long avatarMaxSize;
|
||||||
|
|
||||||
|
private ElPath mac;
|
||||||
|
|
||||||
|
private ElPath linux;
|
||||||
|
|
||||||
|
private ElPath windows;
|
||||||
|
|
||||||
|
public ElPath getPath(){
|
||||||
|
String os = System.getProperty("os.name");
|
||||||
|
if(os.toLowerCase().startsWith("win")) {
|
||||||
|
return windows;
|
||||||
|
} else if(os.toLowerCase().startsWith("mac")){
|
||||||
|
return mac;
|
||||||
|
}
|
||||||
|
return linux;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class ElPath{
|
||||||
|
|
||||||
|
private String path;
|
||||||
|
|
||||||
|
private String avatar;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,5 @@
|
||||||
package me.zhengjie.config;
|
package me.zhengjie.config;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.web.cors.CorsConfiguration;
|
import org.springframework.web.cors.CorsConfiguration;
|
||||||
|
@ -20,11 +19,12 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
@EnableWebMvc
|
@EnableWebMvc
|
||||||
public class ConfigurerAdapter implements WebMvcConfigurer {
|
public class ConfigurerAdapter implements WebMvcConfigurer {
|
||||||
|
|
||||||
@Value("${file.path}")
|
/** 文件配置 */
|
||||||
private String path;
|
private final FileProperties properties;
|
||||||
|
|
||||||
@Value("${file.avatar}")
|
public ConfigurerAdapter(FileProperties properties) {
|
||||||
private String avatar;
|
this.properties = properties;
|
||||||
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public CorsFilter corsFilter() {
|
public CorsFilter corsFilter() {
|
||||||
|
@ -40,8 +40,9 @@ public class ConfigurerAdapter implements WebMvcConfigurer {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||||
String avatarUtl = "file:" + avatar.replace("\\","/");
|
FileProperties.ElPath path = properties.getPath();
|
||||||
String pathUtl = "file:" + path.replace("\\","/");
|
String avatarUtl = "file:" + path.getAvatar().replace("\\","/");
|
||||||
|
String pathUtl = "file:" + path.getPath().replace("\\","/");
|
||||||
registry.addResourceHandler("/avatar/**").addResourceLocations(avatarUtl).setCachePeriod(0);
|
registry.addResourceHandler("/avatar/**").addResourceLocations(avatarUtl).setCachePeriod(0);
|
||||||
registry.addResourceHandler("/file/**").addResourceLocations(pathUtl).setCachePeriod(0);
|
registry.addResourceHandler("/file/**").addResourceLocations(pathUtl).setCachePeriod(0);
|
||||||
registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/").setCachePeriod(0);
|
registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/").setCachePeriod(0);
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package me.zhengjie.modules.system.service.impl;
|
package me.zhengjie.modules.system.service.impl;
|
||||||
|
|
||||||
|
import me.zhengjie.config.FileProperties;
|
||||||
import me.zhengjie.modules.system.domain.User;
|
import me.zhengjie.modules.system.domain.User;
|
||||||
import me.zhengjie.exception.EntityExistException;
|
import me.zhengjie.exception.EntityExistException;
|
||||||
import me.zhengjie.exception.EntityNotFoundException;
|
import me.zhengjie.exception.EntityNotFoundException;
|
||||||
|
@ -12,7 +13,6 @@ import me.zhengjie.modules.system.service.dto.UserDto;
|
||||||
import me.zhengjie.modules.system.service.dto.UserQueryCriteria;
|
import me.zhengjie.modules.system.service.dto.UserQueryCriteria;
|
||||||
import me.zhengjie.modules.system.service.mapper.UserMapper;
|
import me.zhengjie.modules.system.service.mapper.UserMapper;
|
||||||
import me.zhengjie.utils.*;
|
import me.zhengjie.utils.*;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.cache.annotation.CacheConfig;
|
import org.springframework.cache.annotation.CacheConfig;
|
||||||
import org.springframework.cache.annotation.CacheEvict;
|
import org.springframework.cache.annotation.CacheEvict;
|
||||||
import org.springframework.cache.annotation.Cacheable;
|
import org.springframework.cache.annotation.Cacheable;
|
||||||
|
@ -41,15 +41,13 @@ public class UserServiceImpl implements UserService {
|
||||||
private final UserMapper userMapper;
|
private final UserMapper userMapper;
|
||||||
private final RedisUtils redisUtils;
|
private final RedisUtils redisUtils;
|
||||||
private final UserAvatarRepository userAvatarRepository;
|
private final UserAvatarRepository userAvatarRepository;
|
||||||
|
private final FileProperties properties;
|
||||||
@Value("${file.avatar}")
|
public UserServiceImpl(UserRepository userRepository, UserMapper userMapper, RedisUtils redisUtils, UserAvatarRepository userAvatarRepository, FileProperties properties) {
|
||||||
private String avatar;
|
|
||||||
|
|
||||||
public UserServiceImpl(UserRepository userRepository, UserMapper userMapper, RedisUtils redisUtils, UserAvatarRepository userAvatarRepository) {
|
|
||||||
this.userRepository = userRepository;
|
this.userRepository = userRepository;
|
||||||
this.userMapper = userMapper;
|
this.userMapper = userMapper;
|
||||||
this.redisUtils = redisUtils;
|
this.redisUtils = redisUtils;
|
||||||
this.userAvatarRepository = userAvatarRepository;
|
this.userAvatarRepository = userAvatarRepository;
|
||||||
|
this.properties = properties;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -177,7 +175,7 @@ public class UserServiceImpl implements UserService {
|
||||||
if(userAvatar != null){
|
if(userAvatar != null){
|
||||||
oldPath = userAvatar.getPath();
|
oldPath = userAvatar.getPath();
|
||||||
}
|
}
|
||||||
File file = FileUtil.upload(multipartFile, avatar);
|
File file = FileUtil.upload(multipartFile, properties.getPath().getAvatar());
|
||||||
assert file != null;
|
assert file != null;
|
||||||
userAvatar = userAvatarRepository.save(new UserAvatar(userAvatar,file.getName(), file.getPath(), FileUtil.getSize(multipartFile.getSize())));
|
userAvatar = userAvatarRepository.save(new UserAvatar(userAvatar,file.getName(), file.getPath(), FileUtil.getSize(multipartFile.getSize())));
|
||||||
user.setUserAvatar(userAvatar);
|
user.setUserAvatar(userAvatar);
|
||||||
|
|
|
@ -65,8 +65,15 @@ swagger:
|
||||||
|
|
||||||
# 文件存储路径
|
# 文件存储路径
|
||||||
file:
|
file:
|
||||||
path: C:\eladmin\file\
|
mac:
|
||||||
avatar: C:\eladmin\avatar\
|
path: ~/file/
|
||||||
|
avatar: ~/avatar/
|
||||||
|
linux:
|
||||||
|
path: /home/eladmin/file/
|
||||||
|
avatar: /home/eladmin/avatar/
|
||||||
|
windows:
|
||||||
|
path: C:\eladmin\file\
|
||||||
|
avatar: C:\eladmin\avatar\
|
||||||
# 文件大小 /M
|
# 文件大小 /M
|
||||||
maxSize: 100
|
maxSize: 100
|
||||||
avatarMaxSize: 5
|
avatarMaxSize: 5
|
|
@ -74,8 +74,15 @@ swagger:
|
||||||
|
|
||||||
# 文件存储路径
|
# 文件存储路径
|
||||||
file:
|
file:
|
||||||
path: /home/eladmin/file/
|
mac:
|
||||||
avatar: /home/eladmin/avatar/
|
path: ~/file/
|
||||||
|
avatar: ~/avatar/
|
||||||
|
linux:
|
||||||
|
path: /home/eladmin/file/
|
||||||
|
avatar: /home/eladmin/avatar/
|
||||||
|
windows:
|
||||||
|
path: C:\eladmin\file\
|
||||||
|
avatar: C:\eladmin\avatar\
|
||||||
# 文件大小 /M
|
# 文件大小 /M
|
||||||
maxSize: 100
|
maxSize: 100
|
||||||
avatarMaxSize: 5
|
avatarMaxSize: 5
|
|
@ -1,6 +1,7 @@
|
||||||
package me.zhengjie.service.impl;
|
package me.zhengjie.service.impl;
|
||||||
|
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
|
import me.zhengjie.config.FileProperties;
|
||||||
import me.zhengjie.domain.LocalStorage;
|
import me.zhengjie.domain.LocalStorage;
|
||||||
import me.zhengjie.service.dto.LocalStorageDto;
|
import me.zhengjie.service.dto.LocalStorageDto;
|
||||||
import me.zhengjie.service.dto.LocalStorageQueryCriteria;
|
import me.zhengjie.service.dto.LocalStorageQueryCriteria;
|
||||||
|
@ -9,7 +10,6 @@ import me.zhengjie.exception.BadRequestException;
|
||||||
import me.zhengjie.utils.*;
|
import me.zhengjie.utils.*;
|
||||||
import me.zhengjie.repository.LocalStorageRepository;
|
import me.zhengjie.repository.LocalStorageRepository;
|
||||||
import me.zhengjie.service.LocalStorageService;
|
import me.zhengjie.service.LocalStorageService;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.cache.annotation.CacheConfig;
|
import org.springframework.cache.annotation.CacheConfig;
|
||||||
import org.springframework.cache.annotation.CacheEvict;
|
import org.springframework.cache.annotation.CacheEvict;
|
||||||
import org.springframework.cache.annotation.Cacheable;
|
import org.springframework.cache.annotation.Cacheable;
|
||||||
|
@ -42,15 +42,12 @@ public class LocalStorageServiceImpl implements LocalStorageService {
|
||||||
|
|
||||||
private final LocalStorageMapper localStorageMapper;
|
private final LocalStorageMapper localStorageMapper;
|
||||||
|
|
||||||
@Value("${file.path}")
|
private final FileProperties properties;
|
||||||
private String path;
|
|
||||||
|
|
||||||
@Value("${file.maxSize}")
|
public LocalStorageServiceImpl(LocalStorageRepository localStorageRepository, LocalStorageMapper localStorageMapper, FileProperties properties) {
|
||||||
private long maxSize;
|
|
||||||
|
|
||||||
public LocalStorageServiceImpl(LocalStorageRepository localStorageRepository, LocalStorageMapper localStorageMapper) {
|
|
||||||
this.localStorageRepository = localStorageRepository;
|
this.localStorageRepository = localStorageRepository;
|
||||||
this.localStorageMapper = localStorageMapper;
|
this.localStorageMapper = localStorageMapper;
|
||||||
|
this.properties = properties;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -78,10 +75,10 @@ public class LocalStorageServiceImpl implements LocalStorageService {
|
||||||
@CacheEvict(allEntries = true)
|
@CacheEvict(allEntries = true)
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public LocalStorageDto create(String name, MultipartFile multipartFile) {
|
public LocalStorageDto create(String name, MultipartFile multipartFile) {
|
||||||
FileUtil.checkSize(maxSize, multipartFile.getSize());
|
FileUtil.checkSize(properties.getMaxSize(), multipartFile.getSize());
|
||||||
String suffix = FileUtil.getExtensionName(multipartFile.getOriginalFilename());
|
String suffix = FileUtil.getExtensionName(multipartFile.getOriginalFilename());
|
||||||
String type = FileUtil.getFileType(suffix);
|
String type = FileUtil.getFileType(suffix);
|
||||||
File file = FileUtil.upload(multipartFile, path + type + File.separator);
|
File file = FileUtil.upload(multipartFile, properties.getPath().getPath() + type + File.separator);
|
||||||
if(ObjectUtil.isNull(file)){
|
if(ObjectUtil.isNull(file)){
|
||||||
throw new BadRequestException("上传失败");
|
throw new BadRequestException("上传失败");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue