Skip to content

Commit

Permalink
文件上传优化,加入FileProperties,根据系统选择上传目录
Browse files Browse the repository at this point in the history
  • Loading branch information
elunez committed Mar 28, 2020
1 parent 646a795 commit 3694add
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package me.zhengjie.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
Expand All @@ -20,11 +19,12 @@
@EnableWebMvc
public class ConfigurerAdapter implements WebMvcConfigurer {

@Value("${file.path}")
private String path;
/** 文件配置 */
private final FileProperties properties;

@Value("${file.avatar}")
private String avatar;
public ConfigurerAdapter(FileProperties properties) {
this.properties = properties;
}

@Bean
public CorsFilter corsFilter() {
Expand All @@ -40,8 +40,9 @@ public CorsFilter corsFilter() {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
String avatarUtl = "file:" + avatar.replace("\\","/");
String pathUtl = "file:" + path.replace("\\","/");
FileProperties.ElPath path = properties.getPath();
String avatarUtl = "file:" + path.getAvatar().replace("\\","/");
String pathUtl = "file:" + path.getPath().replace("\\","/");
registry.addResourceHandler("/avatar/**").addResourceLocations(avatarUtl).setCachePeriod(0);
registry.addResourceHandler("/file/**").addResourceLocations(pathUtl).setCachePeriod(0);
registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/").setCachePeriod(0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.config.FileProperties;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.exception.EntityExistException;
import me.zhengjie.exception.EntityNotFoundException;
Expand All @@ -12,7 +13,6 @@
import me.zhengjie.modules.system.service.dto.UserQueryCriteria;
import me.zhengjie.modules.system.service.mapper.UserMapper;
import me.zhengjie.utils.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
Expand Down Expand Up @@ -41,15 +41,13 @@ public class UserServiceImpl implements UserService {
private final UserMapper userMapper;
private final RedisUtils redisUtils;
private final UserAvatarRepository userAvatarRepository;

@Value("${file.avatar}")
private String avatar;

public UserServiceImpl(UserRepository userRepository, UserMapper userMapper, RedisUtils redisUtils, UserAvatarRepository userAvatarRepository) {
private final FileProperties properties;
public UserServiceImpl(UserRepository userRepository, UserMapper userMapper, RedisUtils redisUtils, UserAvatarRepository userAvatarRepository, FileProperties properties) {
this.userRepository = userRepository;
this.userMapper = userMapper;
this.redisUtils = redisUtils;
this.userAvatarRepository = userAvatarRepository;
this.properties = properties;
}

@Override
Expand Down Expand Up @@ -177,7 +175,7 @@ public void updateAvatar(MultipartFile multipartFile) {
if(userAvatar != null){
oldPath = userAvatar.getPath();
}
File file = FileUtil.upload(multipartFile, avatar);
File file = FileUtil.upload(multipartFile, properties.getPath().getAvatar());
assert file != null;
userAvatar = userAvatarRepository.save(new UserAvatar(userAvatar,file.getName(), file.getPath(), FileUtil.getSize(multipartFile.getSize())));
user.setUserAvatar(userAvatar);
Expand Down
11 changes: 9 additions & 2 deletions eladmin-system/src/main/resources/config/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,15 @@ swagger:

# 文件存储路径
file:
path: C:\eladmin\file\
avatar: C:\eladmin\avatar\
mac:
path: ~/file/
avatar: ~/avatar/
linux:
path: /home/eladmin/file/
avatar: /home/eladmin/avatar/
windows:
path: C:\eladmin\file\
avatar: C:\eladmin\avatar\
# 文件大小 /M
maxSize: 100
avatarMaxSize: 5
11 changes: 9 additions & 2 deletions eladmin-system/src/main/resources/config/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,15 @@ swagger:

# 文件存储路径
file:
path: /home/eladmin/file/
avatar: /home/eladmin/avatar/
mac:
path: ~/file/
avatar: ~/avatar/
linux:
path: /home/eladmin/file/
avatar: /home/eladmin/avatar/
windows:
path: C:\eladmin\file\
avatar: C:\eladmin\avatar\
# 文件大小 /M
maxSize: 100
avatarMaxSize: 5
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.zhengjie.service.impl;

import cn.hutool.core.util.ObjectUtil;
import me.zhengjie.config.FileProperties;
import me.zhengjie.domain.LocalStorage;
import me.zhengjie.service.dto.LocalStorageDto;
import me.zhengjie.service.dto.LocalStorageQueryCriteria;
Expand All @@ -9,7 +10,6 @@
import me.zhengjie.utils.*;
import me.zhengjie.repository.LocalStorageRepository;
import me.zhengjie.service.LocalStorageService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
Expand Down Expand Up @@ -42,15 +42,12 @@ public class LocalStorageServiceImpl implements LocalStorageService {

private final LocalStorageMapper localStorageMapper;

@Value("${file.path}")
private String path;
private final FileProperties properties;

@Value("${file.maxSize}")
private long maxSize;

public LocalStorageServiceImpl(LocalStorageRepository localStorageRepository, LocalStorageMapper localStorageMapper) {
public LocalStorageServiceImpl(LocalStorageRepository localStorageRepository, LocalStorageMapper localStorageMapper, FileProperties properties) {
this.localStorageRepository = localStorageRepository;
this.localStorageMapper = localStorageMapper;
this.properties = properties;
}

@Override
Expand Down Expand Up @@ -78,10 +75,10 @@ public LocalStorageDto findById(Long id){
@CacheEvict(allEntries = true)
@Transactional(rollbackFor = Exception.class)
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 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)){
throw new BadRequestException("上传失败");
}
Expand Down

0 comments on commit 3694add

Please sign in to comment.