diff --git a/fast-boot-api/src/main/java/net/maku/api/module/storage/StorageService.java b/fast-boot-api/src/main/java/net/maku/api/module/storage/StorageService.java deleted file mode 100644 index b115150..0000000 --- a/fast-boot-api/src/main/java/net/maku/api/module/storage/StorageService.java +++ /dev/null @@ -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); -} diff --git a/fast-boot-system/pom.xml b/fast-boot-system/pom.xml index f78befb..d98b6c6 100644 --- a/fast-boot-system/pom.xml +++ b/fast-boot-system/pom.xml @@ -1,37 +1,54 @@ - - - net.maku - fast-boot - ${revision} - - 4.0.0 - fast-boot-system - jar + + + net.maku + fast-boot + ${revision} + + 4.0.0 + fast-boot-system + jar - - - net.maku - fast-framework - ${revision} - - - net.maku - fast-boot-api - ${revision} - - - org.springframework.boot - spring-boot-configuration-processor - true - - - com.aliyun.oss - aliyun-sdk-oss - - - com.github.whvcse - easy-captcha - - + + + net.maku + fast-framework + ${revision} + + + net.maku + fast-boot-api + ${revision} + + + org.springframework.boot + spring-boot-configuration-processor + true + + + com.aliyun.oss + aliyun-sdk-oss + + + com.qcloud + cos_api + + + com.qiniu + qiniu-java-sdk + + + com.huaweicloud + esdk-obs-java-bundle + + + io.minio + minio + + + com.github.whvcse + easy-captcha + + \ No newline at end of file diff --git a/fast-boot-system/src/main/java/net/maku/storage/service/AliyunStorageService.java b/fast-boot-system/src/main/java/net/maku/storage/service/AliyunStorageService.java index aa0a0c6..4051adb 100644 --- a/fast-boot-system/src/main/java/net/maku/storage/service/AliyunStorageService.java +++ b/fast-boot-system/src/main/java/net/maku/storage/service/AliyunStorageService.java @@ -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,9 +13,8 @@ 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)); - } } diff --git a/fast-boot-system/src/main/java/net/maku/storage/service/LocalStorageService.java b/fast-boot-system/src/main/java/net/maku/storage/service/LocalStorageService.java index 2202c3c..4ff6eff 100644 --- a/fast-boot-system/src/main/java/net/maku/storage/service/LocalStorageService.java +++ b/fast-boot-system/src/main/java/net/maku/storage/service/LocalStorageService.java @@ -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)); - } } diff --git a/fast-boot-system/src/main/java/net/maku/storage/service/StorageService.java b/fast-boot-system/src/main/java/net/maku/storage/service/StorageService.java new file mode 100644 index 0000000..beea674 --- /dev/null +++ b/fast-boot-system/src/main/java/net/maku/storage/service/StorageService.java @@ -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); + +}