diff --git a/eladmin-common/src/main/java/me/zhengjie/config/FileProperties.java b/eladmin-common/src/main/java/me/zhengjie/config/FileProperties.java new file mode 100644 index 00000000..8097be77 --- /dev/null +++ b/eladmin-common/src/main/java/me/zhengjie/config/FileProperties.java @@ -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; + } +} diff --git a/eladmin-system/src/main/java/me/zhengjie/config/ConfigurerAdapter.java b/eladmin-system/src/main/java/me/zhengjie/config/ConfigurerAdapter.java index 81efa31c..00827251 100644 --- a/eladmin-system/src/main/java/me/zhengjie/config/ConfigurerAdapter.java +++ b/eladmin-system/src/main/java/me/zhengjie/config/ConfigurerAdapter.java @@ -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; @@ -20,11 +19,12 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @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() { @@ -40,8 +40,9 @@ public class ConfigurerAdapter implements WebMvcConfigurer { @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); diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/UserServiceImpl.java b/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/UserServiceImpl.java index feffd0fe..ee5919ec 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/UserServiceImpl.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/UserServiceImpl.java @@ -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; @@ -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.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; @@ -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 @@ -177,7 +175,7 @@ public class UserServiceImpl implements UserService { 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); diff --git a/eladmin-system/src/main/resources/config/application-dev.yml b/eladmin-system/src/main/resources/config/application-dev.yml index 353ed531..df73af38 100644 --- a/eladmin-system/src/main/resources/config/application-dev.yml +++ b/eladmin-system/src/main/resources/config/application-dev.yml @@ -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 \ No newline at end of file diff --git a/eladmin-system/src/main/resources/config/application-prod.yml b/eladmin-system/src/main/resources/config/application-prod.yml index f9017058..ec98ac13 100644 --- a/eladmin-system/src/main/resources/config/application-prod.yml +++ b/eladmin-system/src/main/resources/config/application-prod.yml @@ -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 \ No newline at end of file diff --git a/eladmin-tools/src/main/java/me/zhengjie/service/impl/LocalStorageServiceImpl.java b/eladmin-tools/src/main/java/me/zhengjie/service/impl/LocalStorageServiceImpl.java index 50bfd561..861e31e6 100644 --- a/eladmin-tools/src/main/java/me/zhengjie/service/impl/LocalStorageServiceImpl.java +++ b/eladmin-tools/src/main/java/me/zhengjie/service/impl/LocalStorageServiceImpl.java @@ -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; @@ -9,7 +10,6 @@ import me.zhengjie.exception.BadRequestException; 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; @@ -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 @@ -78,10 +75,10 @@ public class LocalStorageServiceImpl implements LocalStorageService { @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("上传失败"); }