diff --git a/src/main/java/cn/celess/blog/configuration/ApplicationListener.java b/src/main/java/cn/celess/blog/configuration/ApplicationListener.java index 98e0ea4..0305e8b 100644 --- a/src/main/java/cn/celess/blog/configuration/ApplicationListener.java +++ b/src/main/java/cn/celess/blog/configuration/ApplicationListener.java @@ -3,6 +3,7 @@ package cn.celess.blog.configuration; import cn.celess.blog.enmu.ConfigKeyEnum; import cn.celess.blog.entity.Config; import cn.celess.blog.mapper.ConfigMapper; +import cn.celess.blog.service.fileserviceimpl.LocalFileServiceImpl; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.ApplicationArguments; @@ -10,6 +11,7 @@ import org.springframework.boot.ApplicationRunner; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; +import java.io.File; import java.util.List; import java.util.stream.Collectors; @@ -34,6 +36,7 @@ public class ApplicationListener implements ApplicationRunner { setProperty(ConfigKeyEnum.FILE_QINIU_SECRET_KEY); setProperty(ConfigKeyEnum.FILE_QINIU_ACCESS_KEY); setProperty(ConfigKeyEnum.FILE_QINIU_BUCKET); + setProperty(ConfigKeyEnum.BLOG_FILE_PATH); List configurations = configMapper.getConfigurations() .stream() @@ -41,6 +44,10 @@ public class ApplicationListener implements ApplicationRunner { .collect(Collectors.toList()); configurations.forEach(config -> System.setProperty(config.getName(), config.getValue())); log.debug("注入配置成功 {}", configurations.stream().map(Config::getName).collect(Collectors.toList())); + File path = new File(LocalFileServiceImpl.getPath(System.getProperty(ConfigKeyEnum.BLOG_FILE_PATH.getKey()))); + if (!path.exists() && !path.mkdirs()) { + throw new IllegalAccessException("创建数据目录失败==>" + path.getAbsolutePath()); + } } private void setProperty(ConfigKeyEnum e) { diff --git a/src/main/java/cn/celess/blog/enmu/ConfigKeyEnum.java b/src/main/java/cn/celess/blog/enmu/ConfigKeyEnum.java index 1cbe6f0..2f4f551 100644 --- a/src/main/java/cn/celess/blog/enmu/ConfigKeyEnum.java +++ b/src/main/java/cn/celess/blog/enmu/ConfigKeyEnum.java @@ -15,6 +15,7 @@ public enum ConfigKeyEnum { FILE_QINIU_SECRET_KEY("file.qiniu.secretKey"), FILE_QINIU_BUCKET("file.qiniu.bucket"), FILE_LOCAL_DIRECTORY_PATH("file.local.directoryPath"), + BLOG_FILE_PATH("blog.file.path"), DB_TYPE("db.type"); diff --git a/src/main/java/cn/celess/blog/service/fileserviceimpl/LocalFileServiceImpl.java b/src/main/java/cn/celess/blog/service/fileserviceimpl/LocalFileServiceImpl.java index 66aa66a..b1d6eef 100644 --- a/src/main/java/cn/celess/blog/service/fileserviceimpl/LocalFileServiceImpl.java +++ b/src/main/java/cn/celess/blog/service/fileserviceimpl/LocalFileServiceImpl.java @@ -25,17 +25,22 @@ import java.util.List; @Slf4j @Service("localFileServiceImpl") public class LocalFileServiceImpl implements FileManager { + private static String path = null; + @SneakyThrows @Override public FileResponse uploadFile(InputStream is, String fileName) { + if (path == null) { + path = System.getProperty(ConfigKeyEnum.FILE_LOCAL_DIRECTORY_PATH.getKey()); + } // 判断文件夹是否存在 - File directory = new File(getPath()); + File directory = new File(getPath(path)); if (!directory.exists() && directory.mkdirs()) { log.info("不存在文件夹,创建文件夹=>{}", directory.getAbsolutePath()); } log.info("上传文件 {}", fileName); // 存储文件 - File file1 = new File(getPath() + File.separator + fileName); + File file1 = new File(getPath(path) + File.separator + fileName); FileOutputStream fos = new FileOutputStream(file1); byte[] cache = new byte[1024 * 100]; int len = 0; @@ -55,7 +60,10 @@ public class LocalFileServiceImpl implements FileManager { @SneakyThrows @Override public List getFileList() { - File file = new File(getPath()); + if (path == null) { + path = System.getProperty(ConfigKeyEnum.FILE_LOCAL_DIRECTORY_PATH.getKey()); + } + File file = new File(getPath(path)); File[] files = file.listFiles(); List fileInfoList = new ArrayList<>(); if (files == null) { @@ -74,17 +82,23 @@ public class LocalFileServiceImpl implements FileManager { @Override public boolean deleteFile(String fileName) { - File file = new File(getPath() + File.separator + fileName); + if (path == null) { + path = System.getProperty(ConfigKeyEnum.FILE_LOCAL_DIRECTORY_PATH.getKey()); + } + File file = new File(getPath(path) + File.separator + fileName); return file.delete(); } - private String getPath() { - String path = System.getProperty(ConfigKeyEnum.FILE_LOCAL_DIRECTORY_PATH.getKey()).replaceAll("//", File.separator); + public static String getPath(String path) { + if (path == null) { + return ""; + } + String pathCop = path.replaceAll("//", File.separator); if (path.startsWith("~")) { // 家目录 - path = path.replaceFirst("~", ""); - path = System.getProperty("user.home") + path; + pathCop = path.replaceFirst("~", ""); + pathCop = System.getProperty("user.home") + pathCop; } - return path; + return pathCop; } } diff --git a/src/main/resources/sql/data.sql b/src/main/resources/sql/data.sql index 09127c2..0c80ed2 100644 --- a/src/main/resources/sql/data.sql +++ b/src/main/resources/sql/data.sql @@ -219,5 +219,6 @@ VALUES (1, 'file.type', 'local'), (2, 'file.qiniu.accessKey', null), (3, 'file.qiniu.secretKey', null), (4, 'file.qiniu.bucket', null), - (6, 'file.local.directoryPath', '~/blog/files'), + (5, 'blog.file.path', '~/blog/'), + (6, 'file.local.directoryPath', '~/blog/files/'), (7, 'db.type', 'h2')