feat: 本地文件存储
This commit is contained in:
30
src/main/java/cn/celess/blog/enmu/ConfigKeyEnum.java
Normal file
30
src/main/java/cn/celess/blog/enmu/ConfigKeyEnum.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package cn.celess.blog.enmu;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2020/10/16 16:41
|
||||
* @desc :
|
||||
*/
|
||||
public enum ConfigKeyEnum {
|
||||
|
||||
/**
|
||||
* 枚举
|
||||
*/
|
||||
FILE_TYPE("file.type"),
|
||||
FILE_QINIU_ACCESS_KEY("file.qiniu.accessKey"),
|
||||
FILE_QINIU_SECRET_KEY("file.qiniu.secretKey"),
|
||||
FILE_QINIU_BUCKET("file.qiniu.bucket"),
|
||||
FILE_LOCAL_DIRECTORY_PATH("file.local.directoryPath"),
|
||||
DB_TYPE("db.type");
|
||||
|
||||
|
||||
private final String key;
|
||||
|
||||
ConfigKeyEnum(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
package cn.celess.blog.service.fileserviceimpl;
|
||||
|
||||
import cn.celess.blog.enmu.ConfigKeyEnum;
|
||||
import cn.celess.blog.service.FileManager;
|
||||
import cn.celess.blog.service.FileService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
@@ -11,6 +13,7 @@ import javax.annotation.Resource;
|
||||
* @date : 2020/10/15 18:52
|
||||
* @desc : 提供文件管理器的服务
|
||||
*/
|
||||
@Slf4j
|
||||
@Service("fileServiceImpl")
|
||||
public class FileServiceImpl implements FileService {
|
||||
@Resource(name = "qiniuFileServiceImpl")
|
||||
@@ -21,6 +24,13 @@ public class FileServiceImpl implements FileService {
|
||||
|
||||
@Override
|
||||
public FileManager getFileManager() {
|
||||
String property = System.getProperty(ConfigKeyEnum.FILE_TYPE.getKey());
|
||||
log.info("获取到{}:{}", ConfigKeyEnum.FILE_TYPE.getKey(), property);
|
||||
if ("qiniu".equals(property)){
|
||||
return qiniuFileManager;
|
||||
}else if ("local".equals(property)){
|
||||
return localFileServiceImpl;
|
||||
}
|
||||
return localFileServiceImpl;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,20 @@
|
||||
package cn.celess.blog.service.fileserviceimpl;
|
||||
|
||||
import cn.celess.blog.enmu.ConfigKeyEnum;
|
||||
import cn.celess.blog.entity.model.FileInfo;
|
||||
import cn.celess.blog.entity.model.FileResponse;
|
||||
import cn.celess.blog.service.FileManager;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -13,20 +22,69 @@ import java.util.List;
|
||||
* @date : 2020/10/16 14:39
|
||||
* @desc :
|
||||
*/
|
||||
@Slf4j
|
||||
@Service("localFileServiceImpl")
|
||||
public class LocalFileServiceImpl implements FileManager {
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public FileResponse uploadFile(InputStream is, String fileName) {
|
||||
return null;
|
||||
// 判断文件夹是否存在
|
||||
File directory = new File(getPath());
|
||||
if (!directory.exists() && directory.mkdirs()) {
|
||||
log.info("不存在文件夹,创建文件夹=>{}", directory.getAbsolutePath());
|
||||
}
|
||||
log.info("上传文件 {}", fileName);
|
||||
// 存储文件
|
||||
File file1 = new File(getPath() + File.separator + fileName);
|
||||
FileOutputStream fos = new FileOutputStream(file1);
|
||||
byte[] cache = new byte[1024 * 100];
|
||||
int len = 0;
|
||||
FileResponse fileResponse = new FileResponse();
|
||||
while ((len = is.read(cache)) != -1) {
|
||||
fileResponse.size += len;
|
||||
fos.write(cache, 0, len);
|
||||
}
|
||||
fos.flush();
|
||||
fos.close();
|
||||
is.close();
|
||||
fileResponse.key = URLEncoder.encode(fileName, "UTF-8");
|
||||
fileResponse.bucket = "local";
|
||||
return fileResponse;
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public List<FileInfo> getFileList() {
|
||||
File file = new File(getPath());
|
||||
File[] files = file.listFiles();
|
||||
List<FileInfo> fileInfoList = new ArrayList<>();
|
||||
if (files == null) {
|
||||
return null;
|
||||
}
|
||||
for (File file1 : files) {
|
||||
FileInfo fileInfo = new FileInfo();
|
||||
fileInfo.key = URLEncoder.encode(file1.getName(), "UTF-8");
|
||||
fileInfo.size = file1.length();
|
||||
BasicFileAttributes basicFileAttributes = Files.readAttributes(file1.toPath(), BasicFileAttributes.class);
|
||||
fileInfo.uploadTime = basicFileAttributes.creationTime().toMillis();
|
||||
fileInfoList.add(fileInfo);
|
||||
}
|
||||
return fileInfoList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteFile(String fileName) {
|
||||
return false;
|
||||
File file = new File(getPath() + File.separator + fileName);
|
||||
return file.delete();
|
||||
}
|
||||
|
||||
private String getPath() {
|
||||
String path = System.getProperty(ConfigKeyEnum.FILE_LOCAL_DIRECTORY_PATH.getKey()).replaceAll("//", File.separator);
|
||||
if (path.startsWith("~")) {
|
||||
// 家目录
|
||||
path = path.replaceFirst("~", "");
|
||||
path = System.getProperty("user.home") + path;
|
||||
}
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -219,5 +219,5 @@ VALUES (1, 'file.type', 'local'),
|
||||
(2, 'file.qiniu.accessKey', ''),
|
||||
(3, 'file.qiniu.secretKey', ''),
|
||||
(4, 'file.qiniu.bucket', ''),
|
||||
(6, 'file.local.dictoryPath', ''),
|
||||
(6, 'file.local.directoryPath', '~/blog/files'),
|
||||
(7, 'db.type', 'h2')
|
||||
|
||||
Reference in New Issue
Block a user