优化文件存储
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,37 +1,54 @@
|
|||
<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>
|
||||
<groupId>net.maku</groupId>
|
||||
<artifactId>fast-boot</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>fast-boot-system</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<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>
|
||||
<groupId>net.maku</groupId>
|
||||
<artifactId>fast-boot</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>fast-boot-system</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>net.maku</groupId>
|
||||
<artifactId>fast-framework</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.maku</groupId>
|
||||
<artifactId>fast-boot-api</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.aliyun.oss</groupId>
|
||||
<artifactId>aliyun-sdk-oss</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.whvcse</groupId>
|
||||
<artifactId>easy-captcha</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>net.maku</groupId>
|
||||
<artifactId>fast-framework</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.maku</groupId>
|
||||
<artifactId>fast-boot-api</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.aliyun.oss</groupId>
|
||||
<artifactId>aliyun-sdk-oss</artifactId>
|
||||
</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>
|
||||
<artifactId>easy-captcha</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -2,7 +2,6 @@ package net.maku.storage.service;
|
|||
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSClientBuilder;
|
||||
import net.maku.api.module.storage.StorageService;
|
||||
import net.maku.framework.common.exception.FastException;
|
||||
import net.maku.storage.properties.StorageProperties;
|
||||
|
||||
|
@ -14,8 +13,7 @@ import java.io.InputStream;
|
|||
*
|
||||
* @author 阿沐 babamu@126.com
|
||||
*/
|
||||
public class AliyunStorageService implements StorageService {
|
||||
private final StorageProperties properties;
|
||||
public class AliyunStorageService extends StorageService {
|
||||
|
||||
public AliyunStorageService(StorageProperties properties) {
|
||||
this.properties = properties;
|
||||
|
@ -27,17 +25,12 @@ public class AliyunStorageService implements StorageService {
|
|||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(byte[] data, String suffix) {
|
||||
return upload(data, getPath(properties.getConfig().getPrefix(), suffix));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(InputStream inputStream, String path) {
|
||||
OSS client = new OSSClientBuilder().build(properties.getAliyun().getEndPoint(),
|
||||
properties.getAliyun().getAccessKeyId(), properties.getAliyun().getAccessKeySecret());
|
||||
try {
|
||||
client.putObject(properties.getAliyun().getBucketName(), path, inputStream);
|
||||
}catch (Exception e){
|
||||
} catch (Exception e) {
|
||||
throw new FastException("上传文件失败:", e);
|
||||
} finally {
|
||||
if (client != null) {
|
||||
|
@ -48,8 +41,4 @@ public class AliyunStorageService implements StorageService {
|
|||
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;
|
||||
|
||||
import net.maku.api.module.storage.StorageService;
|
||||
import net.maku.framework.common.exception.FastException;
|
||||
import net.maku.storage.properties.StorageProperties;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
|
@ -16,8 +15,7 @@ import java.nio.file.Files;
|
|||
*
|
||||
* @author 阿沐 babamu@126.com
|
||||
*/
|
||||
public class LocalStorageService implements StorageService {
|
||||
private final StorageProperties properties;
|
||||
public class LocalStorageService extends StorageService {
|
||||
|
||||
public LocalStorageService(StorageProperties properties) {
|
||||
this.properties = properties;
|
||||
|
@ -28,10 +26,6 @@ public class LocalStorageService implements StorageService {
|
|||
return upload(new ByteArrayInputStream(data), path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(byte[] data, String suffix) {
|
||||
return upload(data, getPath(properties.getConfig().getPrefix(), suffix));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(InputStream inputStream, String path) {
|
||||
|
@ -42,7 +36,7 @@ public class LocalStorageService implements StorageService {
|
|||
// 没有目录,则自动创建目录
|
||||
File parent = file.getParentFile();
|
||||
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()));
|
||||
|
@ -52,9 +46,4 @@ public class LocalStorageService implements StorageService {
|
|||
|
||||
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