优化文件存储
This commit is contained in:
parent
147b4cedc8
commit
6a63833611
|
@ -1,67 +0,0 @@
|
||||||
package net.maku.api.module.storage;
|
|
||||||
|
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.text.SimpleDateFormat;
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 存储服务
|
|
||||||
*
|
|
||||||
* @author 阿沐 babamu@126.com
|
|
||||||
*/
|
|
||||||
public interface StorageService {
|
|
||||||
/**
|
|
||||||
* 文件路径
|
|
||||||
* @param prefix 前缀
|
|
||||||
* @param suffix 后缀
|
|
||||||
* @return 返回上传路径
|
|
||||||
*/
|
|
||||||
default String getPath(String prefix, String suffix) {
|
|
||||||
//生成uuid
|
|
||||||
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
|
|
||||||
//文件路径
|
|
||||||
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
|
|
||||||
String path = df.format(new Date()) + "/" + uuid;
|
|
||||||
|
|
||||||
if(StringUtils.hasText(prefix)){
|
|
||||||
path = prefix + "/" + path;
|
|
||||||
}
|
|
||||||
|
|
||||||
return path + "." + suffix;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 文件上传
|
|
||||||
* @param data 文件字节数组
|
|
||||||
* @param path 文件路径,包含文件名
|
|
||||||
* @return 返回http地址
|
|
||||||
*/
|
|
||||||
String upload(byte[] data, String path);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 文件上传
|
|
||||||
* @param data 文件字节数组
|
|
||||||
* @param suffix 后缀
|
|
||||||
* @return 返回http地址
|
|
||||||
*/
|
|
||||||
String uploadSuffix(byte[] data, String suffix);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 文件上传
|
|
||||||
* @param inputStream 字节流
|
|
||||||
* @param path 文件路径,包含文件名
|
|
||||||
* @return 返回http地址
|
|
||||||
*/
|
|
||||||
String upload(InputStream inputStream, String path);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 文件上传
|
|
||||||
* @param inputStream 字节流
|
|
||||||
* @param suffix 后缀
|
|
||||||
* @return 返回http地址
|
|
||||||
*/
|
|
||||||
String uploadSuffix(InputStream inputStream, String suffix);
|
|
||||||
}
|
|
|
@ -1,4 +1,5 @@
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>net.maku</groupId>
|
<groupId>net.maku</groupId>
|
||||||
<artifactId>fast-boot</artifactId>
|
<artifactId>fast-boot</artifactId>
|
||||||
|
@ -29,6 +30,22 @@
|
||||||
<artifactId>aliyun-sdk-oss</artifactId>
|
<artifactId>aliyun-sdk-oss</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
|
<groupId>com.qcloud</groupId>
|
||||||
|
<artifactId>cos_api</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.qiniu</groupId>
|
||||||
|
<artifactId>qiniu-java-sdk</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.huaweicloud</groupId>
|
||||||
|
<artifactId>esdk-obs-java-bundle</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.minio</groupId>
|
||||||
|
<artifactId>minio</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
<groupId>com.github.whvcse</groupId>
|
<groupId>com.github.whvcse</groupId>
|
||||||
<artifactId>easy-captcha</artifactId>
|
<artifactId>easy-captcha</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
|
@ -2,7 +2,6 @@ package net.maku.storage.service;
|
||||||
|
|
||||||
import com.aliyun.oss.OSS;
|
import com.aliyun.oss.OSS;
|
||||||
import com.aliyun.oss.OSSClientBuilder;
|
import com.aliyun.oss.OSSClientBuilder;
|
||||||
import net.maku.api.module.storage.StorageService;
|
|
||||||
import net.maku.framework.common.exception.FastException;
|
import net.maku.framework.common.exception.FastException;
|
||||||
import net.maku.storage.properties.StorageProperties;
|
import net.maku.storage.properties.StorageProperties;
|
||||||
|
|
||||||
|
@ -14,8 +13,7 @@ import java.io.InputStream;
|
||||||
*
|
*
|
||||||
* @author 阿沐 babamu@126.com
|
* @author 阿沐 babamu@126.com
|
||||||
*/
|
*/
|
||||||
public class AliyunStorageService implements StorageService {
|
public class AliyunStorageService extends StorageService {
|
||||||
private final StorageProperties properties;
|
|
||||||
|
|
||||||
public AliyunStorageService(StorageProperties properties) {
|
public AliyunStorageService(StorageProperties properties) {
|
||||||
this.properties = properties;
|
this.properties = properties;
|
||||||
|
@ -27,11 +25,6 @@ public class AliyunStorageService implements StorageService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String uploadSuffix(byte[] data, String suffix) {
|
|
||||||
return upload(data, getPath(properties.getConfig().getPrefix(), suffix));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String upload(InputStream inputStream, String path) {
|
public String upload(InputStream inputStream, String path) {
|
||||||
OSS client = new OSSClientBuilder().build(properties.getAliyun().getEndPoint(),
|
OSS client = new OSSClientBuilder().build(properties.getAliyun().getEndPoint(),
|
||||||
properties.getAliyun().getAccessKeyId(), properties.getAliyun().getAccessKeySecret());
|
properties.getAliyun().getAccessKeyId(), properties.getAliyun().getAccessKeySecret());
|
||||||
|
@ -48,8 +41,4 @@ public class AliyunStorageService implements StorageService {
|
||||||
return properties.getConfig().getDomain() + "/" + path;
|
return properties.getConfig().getDomain() + "/" + path;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String uploadSuffix(InputStream inputStream, String suffix) {
|
|
||||||
return upload(inputStream, getPath(properties.getConfig().getPrefix(), suffix));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package net.maku.storage.service;
|
package net.maku.storage.service;
|
||||||
|
|
||||||
import net.maku.api.module.storage.StorageService;
|
|
||||||
import net.maku.framework.common.exception.FastException;
|
import net.maku.framework.common.exception.FastException;
|
||||||
import net.maku.storage.properties.StorageProperties;
|
import net.maku.storage.properties.StorageProperties;
|
||||||
import org.springframework.util.FileCopyUtils;
|
import org.springframework.util.FileCopyUtils;
|
||||||
|
@ -16,8 +15,7 @@ import java.nio.file.Files;
|
||||||
*
|
*
|
||||||
* @author 阿沐 babamu@126.com
|
* @author 阿沐 babamu@126.com
|
||||||
*/
|
*/
|
||||||
public class LocalStorageService implements StorageService {
|
public class LocalStorageService extends StorageService {
|
||||||
private final StorageProperties properties;
|
|
||||||
|
|
||||||
public LocalStorageService(StorageProperties properties) {
|
public LocalStorageService(StorageProperties properties) {
|
||||||
this.properties = properties;
|
this.properties = properties;
|
||||||
|
@ -28,10 +26,6 @@ public class LocalStorageService implements StorageService {
|
||||||
return upload(new ByteArrayInputStream(data), path);
|
return upload(new ByteArrayInputStream(data), path);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String uploadSuffix(byte[] data, String suffix) {
|
|
||||||
return upload(data, getPath(properties.getConfig().getPrefix(), suffix));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String upload(InputStream inputStream, String path) {
|
public String upload(InputStream inputStream, String path) {
|
||||||
|
@ -42,7 +36,7 @@ public class LocalStorageService implements StorageService {
|
||||||
// 没有目录,则自动创建目录
|
// 没有目录,则自动创建目录
|
||||||
File parent = file.getParentFile();
|
File parent = file.getParentFile();
|
||||||
if (parent != null && !parent.mkdirs() && !parent.isDirectory()) {
|
if (parent != null && !parent.mkdirs() && !parent.isDirectory()) {
|
||||||
throw new IOException("Directory '" + parent + "' could not be created");
|
throw new IOException("目录 '" + parent + "' 创建失败");
|
||||||
}
|
}
|
||||||
|
|
||||||
FileCopyUtils.copy(inputStream, Files.newOutputStream(file.toPath()));
|
FileCopyUtils.copy(inputStream, Files.newOutputStream(file.toPath()));
|
||||||
|
@ -52,9 +46,4 @@ public class LocalStorageService implements StorageService {
|
||||||
|
|
||||||
return properties.getConfig().getDomain() + "/" + properties.getLocal().getUrl() + "/" + path;
|
return properties.getConfig().getDomain() + "/" + properties.getLocal().getUrl() + "/" + path;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String uploadSuffix(InputStream inputStream, String suffix) {
|
|
||||||
return upload(inputStream, getPath(properties.getConfig().getPrefix(), suffix));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,81 @@
|
||||||
|
package net.maku.storage.service;
|
||||||
|
|
||||||
|
import cn.hutool.core.date.DateUtil;
|
||||||
|
import cn.hutool.core.io.file.FileNameUtil;
|
||||||
|
import net.maku.storage.properties.StorageProperties;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 存储服务
|
||||||
|
*
|
||||||
|
* @author 阿沐 babamu@126.com
|
||||||
|
*/
|
||||||
|
public abstract class StorageService {
|
||||||
|
public StorageProperties properties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据文件名,生成带时间戳的新文件名
|
||||||
|
*
|
||||||
|
* @param fileName 文件名
|
||||||
|
* @return 返回带时间戳的文件名
|
||||||
|
*/
|
||||||
|
public String getNewFileName(String fileName) {
|
||||||
|
// 主文件名,不包含扩展名
|
||||||
|
String prefix = FileNameUtil.getPrefix(fileName);
|
||||||
|
// 文件扩展名
|
||||||
|
String suffix = FileNameUtil.getSuffix(fileName);
|
||||||
|
// 把当天HH:mm:ss,转换成秒
|
||||||
|
long time = DateUtil.timeToSecond(DateUtil.formatTime(new Date()));
|
||||||
|
// 新文件名
|
||||||
|
return prefix + "_" + time + "." + suffix;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成路径,不包含文件名
|
||||||
|
*
|
||||||
|
* @return 返回生成的路径
|
||||||
|
*/
|
||||||
|
public String getPath() {
|
||||||
|
// 文件路径
|
||||||
|
String path = DateUtil.format(new Date(), "yyyyMMdd");
|
||||||
|
|
||||||
|
// 如果有前缀,则也带上
|
||||||
|
if (StringUtils.hasText(properties.getConfig().getPrefix())) {
|
||||||
|
path = properties.getConfig().getPrefix() + "/" + path;
|
||||||
|
}
|
||||||
|
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据文件名,生成路径
|
||||||
|
*
|
||||||
|
* @param fileName 文件名
|
||||||
|
* @return 生成文件路径
|
||||||
|
*/
|
||||||
|
public String getPath(String fileName) {
|
||||||
|
return getPath() + "/" + getNewFileName(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件上传
|
||||||
|
*
|
||||||
|
* @param data 文件字节数组
|
||||||
|
* @param path 文件路径,包含文件名
|
||||||
|
* @return 返回http地址
|
||||||
|
*/
|
||||||
|
public abstract String upload(byte[] data, String path);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件上传
|
||||||
|
*
|
||||||
|
* @param inputStream 字节流
|
||||||
|
* @param path 文件路径,包含文件名
|
||||||
|
* @return 返回http地址
|
||||||
|
*/
|
||||||
|
public abstract String upload(InputStream inputStream, String path);
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user