添加多运行环境支持 #14
@@ -3,6 +3,7 @@ package cn.celess.blog.configuration;
|
|||||||
import cn.celess.blog.enmu.ConfigKeyEnum;
|
import cn.celess.blog.enmu.ConfigKeyEnum;
|
||||||
import cn.celess.blog.entity.Config;
|
import cn.celess.blog.entity.Config;
|
||||||
import cn.celess.blog.mapper.ConfigMapper;
|
import cn.celess.blog.mapper.ConfigMapper;
|
||||||
|
import cn.celess.blog.service.fileserviceimpl.LocalFileServiceImpl;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.ApplicationArguments;
|
import org.springframework.boot.ApplicationArguments;
|
||||||
@@ -10,6 +11,7 @@ import org.springframework.boot.ApplicationRunner;
|
|||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@@ -34,6 +36,7 @@ public class ApplicationListener implements ApplicationRunner {
|
|||||||
setProperty(ConfigKeyEnum.FILE_QINIU_SECRET_KEY);
|
setProperty(ConfigKeyEnum.FILE_QINIU_SECRET_KEY);
|
||||||
setProperty(ConfigKeyEnum.FILE_QINIU_ACCESS_KEY);
|
setProperty(ConfigKeyEnum.FILE_QINIU_ACCESS_KEY);
|
||||||
setProperty(ConfigKeyEnum.FILE_QINIU_BUCKET);
|
setProperty(ConfigKeyEnum.FILE_QINIU_BUCKET);
|
||||||
|
setProperty(ConfigKeyEnum.BLOG_FILE_PATH);
|
||||||
|
|
||||||
List<Config> configurations = configMapper.getConfigurations()
|
List<Config> configurations = configMapper.getConfigurations()
|
||||||
.stream()
|
.stream()
|
||||||
@@ -41,6 +44,10 @@ public class ApplicationListener implements ApplicationRunner {
|
|||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
configurations.forEach(config -> System.setProperty(config.getName(), config.getValue()));
|
configurations.forEach(config -> System.setProperty(config.getName(), config.getValue()));
|
||||||
log.debug("注入配置成功 {}", configurations.stream().map(Config::getName).collect(Collectors.toList()));
|
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) {
|
private void setProperty(ConfigKeyEnum e) {
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ public enum ConfigKeyEnum {
|
|||||||
FILE_QINIU_SECRET_KEY("file.qiniu.secretKey"),
|
FILE_QINIU_SECRET_KEY("file.qiniu.secretKey"),
|
||||||
FILE_QINIU_BUCKET("file.qiniu.bucket"),
|
FILE_QINIU_BUCKET("file.qiniu.bucket"),
|
||||||
FILE_LOCAL_DIRECTORY_PATH("file.local.directoryPath"),
|
FILE_LOCAL_DIRECTORY_PATH("file.local.directoryPath"),
|
||||||
|
BLOG_FILE_PATH("blog.file.path"),
|
||||||
DB_TYPE("db.type");
|
DB_TYPE("db.type");
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -25,17 +25,22 @@ import java.util.List;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
@Service("localFileServiceImpl")
|
@Service("localFileServiceImpl")
|
||||||
public class LocalFileServiceImpl implements FileManager {
|
public class LocalFileServiceImpl implements FileManager {
|
||||||
|
private static String path = null;
|
||||||
|
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
@Override
|
@Override
|
||||||
public FileResponse uploadFile(InputStream is, String fileName) {
|
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()) {
|
if (!directory.exists() && directory.mkdirs()) {
|
||||||
log.info("不存在文件夹,创建文件夹=>{}", directory.getAbsolutePath());
|
log.info("不存在文件夹,创建文件夹=>{}", directory.getAbsolutePath());
|
||||||
}
|
}
|
||||||
log.info("上传文件 {}", fileName);
|
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);
|
FileOutputStream fos = new FileOutputStream(file1);
|
||||||
byte[] cache = new byte[1024 * 100];
|
byte[] cache = new byte[1024 * 100];
|
||||||
int len = 0;
|
int len = 0;
|
||||||
@@ -55,7 +60,10 @@ public class LocalFileServiceImpl implements FileManager {
|
|||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
@Override
|
@Override
|
||||||
public List<FileInfo> getFileList() {
|
public List<FileInfo> 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();
|
File[] files = file.listFiles();
|
||||||
List<FileInfo> fileInfoList = new ArrayList<>();
|
List<FileInfo> fileInfoList = new ArrayList<>();
|
||||||
if (files == null) {
|
if (files == null) {
|
||||||
@@ -74,17 +82,23 @@ public class LocalFileServiceImpl implements FileManager {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean deleteFile(String fileName) {
|
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();
|
return file.delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getPath() {
|
public static String getPath(String path) {
|
||||||
String path = System.getProperty(ConfigKeyEnum.FILE_LOCAL_DIRECTORY_PATH.getKey()).replaceAll("//", File.separator);
|
if (path == null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
String pathCop = path.replaceAll("//", File.separator);
|
||||||
if (path.startsWith("~")) {
|
if (path.startsWith("~")) {
|
||||||
// 家目录
|
// 家目录
|
||||||
path = path.replaceFirst("~", "");
|
pathCop = path.replaceFirst("~", "");
|
||||||
path = System.getProperty("user.home") + path;
|
pathCop = System.getProperty("user.home") + pathCop;
|
||||||
}
|
}
|
||||||
return path;
|
return pathCop;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -219,5 +219,6 @@ VALUES (1, 'file.type', 'local'),
|
|||||||
(2, 'file.qiniu.accessKey', null),
|
(2, 'file.qiniu.accessKey', null),
|
||||||
(3, 'file.qiniu.secretKey', null),
|
(3, 'file.qiniu.secretKey', null),
|
||||||
(4, 'file.qiniu.bucket', 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')
|
(7, 'db.type', 'h2')
|
||||||
|
|||||||
Reference in New Issue
Block a user