refactor: 统一OSS插件模块方法注释格式
优化OSS存储插件所有方法的注释格式,统一使用标准化的JavaDoc格式: - 采用 @description [功能描述] 格式 - 统一 @author Leocoder 作者标识 - 涵盖配置类、服务类、工具类等所有方法 - 提升代码可读性和维护性 影响模块: - OssAutoConfiguration: OSS自动配置类 - OssStorageService: 阿里云OSS存储服务 - LocalStorageService: 本地存储服务 - StorageService: 存储服务接口 - StorageServiceFactory: 存储服务工厂 - OssUtil: OSS工具类
This commit is contained in:
parent
1045033b62
commit
530ae64dd6
@ -26,7 +26,8 @@ import org.springframework.core.env.Environment;
|
||||
public class OssAutoConfiguration {
|
||||
|
||||
/**
|
||||
* OSS客户端配置
|
||||
* @description [OSS客户端配置]
|
||||
* @author Leocoder
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnProperty(name = "coder.oss.enabled", havingValue = "true")
|
||||
@ -78,7 +79,8 @@ public class OssAutoConfiguration {
|
||||
}
|
||||
|
||||
/**
|
||||
* OSS存储服务
|
||||
* @description [OSS存储服务]
|
||||
* @author Leocoder
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnProperty(name = "coder.oss.enabled", havingValue = "true")
|
||||
@ -89,7 +91,8 @@ public class OssAutoConfiguration {
|
||||
}
|
||||
|
||||
/**
|
||||
* 本地存储服务(始终可用)
|
||||
* @description [本地存储服务(始终可用)]
|
||||
* @author Leocoder
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ -99,7 +102,8 @@ public class OssAutoConfiguration {
|
||||
}
|
||||
|
||||
/**
|
||||
* 存储服务工厂(始终可用)
|
||||
* @description [存储服务工厂(始终可用)]
|
||||
* @author Leocoder
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
|
||||
@ -27,10 +27,18 @@ public class LocalStorageService implements StorageService {
|
||||
|
||||
private final Environment env;
|
||||
|
||||
/**
|
||||
* @description [获取本地存储基本路径]
|
||||
* @author Leocoder
|
||||
*/
|
||||
private String getBasePath() {
|
||||
return env.getProperty("coder.filePath", "/tmp/coder-files");
|
||||
}
|
||||
|
||||
/**
|
||||
* @description [上传文件到本地存储]
|
||||
* @author Leocoder
|
||||
*/
|
||||
@Override
|
||||
public Map<String, Object> uploadFile(MultipartFile file, String fileName, String folderPath) {
|
||||
try {
|
||||
@ -64,6 +72,10 @@ public class LocalStorageService implements StorageService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description [从本地存储删除文件]
|
||||
* @author Leocoder
|
||||
*/
|
||||
@Override
|
||||
public boolean deleteFile(String filePath) {
|
||||
try {
|
||||
@ -89,6 +101,10 @@ public class LocalStorageService implements StorageService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description [获取文件访问URL]
|
||||
* @author Leocoder
|
||||
*/
|
||||
@Override
|
||||
public String getFileUrl(String filePath) {
|
||||
if (!StringUtils.hasText(filePath)) {
|
||||
@ -121,6 +137,10 @@ public class LocalStorageService implements StorageService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description [检查文件是否存在]
|
||||
* @author Leocoder
|
||||
*/
|
||||
@Override
|
||||
public boolean fileExists(String filePath) {
|
||||
if (!StringUtils.hasText(filePath)) {
|
||||
@ -137,8 +157,13 @@ public class LocalStorageService implements StorageService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description [获取存储服务类型]
|
||||
* @author Leocoder
|
||||
*/
|
||||
@Override
|
||||
public String getStorageType() {
|
||||
return CoderConstants.ONE_STRING; // 本地存储对应数据库中的"1"
|
||||
// 本地存储对应数据库中的"1"
|
||||
return CoderConstants.ONE_STRING;
|
||||
}
|
||||
}
|
||||
@ -34,6 +34,10 @@ public class OssStorageService implements StorageService {
|
||||
private final OSS ossClient;
|
||||
private final OssConfig ossConfig;
|
||||
|
||||
/**
|
||||
* @description [上传文件到阿里云OSS]
|
||||
* @author Leocoder
|
||||
*/
|
||||
@Override
|
||||
public Map<String, Object> uploadFile(MultipartFile file, String fileName, String folderPath) {
|
||||
try {
|
||||
@ -65,8 +69,10 @@ public class OssStorageService implements StorageService {
|
||||
fileMap.put("newName", fileName);
|
||||
fileMap.put("fileSize", FileTypeUtil.getFileSize(file));
|
||||
fileMap.put("suffixName", FileTypeUtil.getFileType(file.getOriginalFilename()));
|
||||
fileMap.put("filePath", objectKey); // OSS对象键,用于删除操作
|
||||
fileMap.put("fileUploadPath", getFileUrl(objectKey)); // 完整的访问URL
|
||||
// OSS对象键,用于删除操作
|
||||
fileMap.put("filePath", objectKey);
|
||||
// 完整的访问URL
|
||||
fileMap.put("fileUploadPath", getFileUrl(objectKey));
|
||||
|
||||
log.info("OSS上传成功: objectKey={}, url={}", objectKey, fileMap.get("fileUploadPath"));
|
||||
return fileMap;
|
||||
@ -77,6 +83,10 @@ public class OssStorageService implements StorageService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description [从阿里云OSS删除文件]
|
||||
* @author Leocoder
|
||||
*/
|
||||
@Override
|
||||
public boolean deleteFile(String objectKey) {
|
||||
try {
|
||||
@ -97,6 +107,10 @@ public class OssStorageService implements StorageService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description [获取文件访问URL]
|
||||
* @author Leocoder
|
||||
*/
|
||||
@Override
|
||||
public String getFileUrl(String objectKey) {
|
||||
if (!StringUtils.hasText(objectKey)) {
|
||||
@ -119,6 +133,10 @@ public class OssStorageService implements StorageService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description [检查文件是否存在]
|
||||
* @author Leocoder
|
||||
*/
|
||||
@Override
|
||||
public boolean fileExists(String objectKey) {
|
||||
try {
|
||||
@ -134,17 +152,19 @@ public class OssStorageService implements StorageService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description [获取存储服务类型]
|
||||
* @author Leocoder
|
||||
*/
|
||||
@Override
|
||||
public String getStorageType() {
|
||||
return "3"; // OSS存储对应数据库中的"3"
|
||||
// OSS存储对应数据库中的"3"
|
||||
return "3";
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建OSS对象键
|
||||
*
|
||||
* @param folderPath 文件夹路径
|
||||
* @param fileName 文件名
|
||||
* @return OSS对象键
|
||||
* @description [构建OSS对象键]
|
||||
* @author Leocoder
|
||||
*/
|
||||
private String buildObjectKey(String folderPath, String fileName) {
|
||||
StringBuilder keyBuilder = new StringBuilder();
|
||||
|
||||
@ -6,50 +6,39 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* 存储服务接口
|
||||
* 提供统一的文件存储操作接口,支持多种存储类型(本地、MinIO、OSS等)
|
||||
* 提供统一的文件存储操作接口,支持多种存储类型(本地、OSS等)
|
||||
*
|
||||
* @author Leocoder
|
||||
*/
|
||||
public interface StorageService {
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
*
|
||||
* @param file 文件对象
|
||||
* @param fileName 文件名
|
||||
* @param folderPath 文件夹路径
|
||||
* @return 文件信息Map,包含fileName、newName、fileSize、suffixName、filePath、fileUploadPath等
|
||||
* @description [上传文件]
|
||||
* @author Leocoder
|
||||
*/
|
||||
Map<String, Object> uploadFile(MultipartFile file, String fileName, String folderPath);
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
*
|
||||
* @param filePath 文件路径
|
||||
* @return 删除结果
|
||||
* @description [删除文件]
|
||||
* @author Leocoder
|
||||
*/
|
||||
boolean deleteFile(String filePath);
|
||||
|
||||
/**
|
||||
* 获取文件访问URL
|
||||
*
|
||||
* @param filePath 文件路径
|
||||
* @return 访问URL
|
||||
* @description [获取文件访问URL]
|
||||
* @author Leocoder
|
||||
*/
|
||||
String getFileUrl(String filePath);
|
||||
|
||||
/**
|
||||
* 检查文件是否存在
|
||||
*
|
||||
* @param filePath 文件路径
|
||||
* @return 是否存在
|
||||
* @description [检查文件是否存在]
|
||||
* @author Leocoder
|
||||
*/
|
||||
boolean fileExists(String filePath);
|
||||
|
||||
/**
|
||||
* 获取存储服务类型
|
||||
*
|
||||
* @return 存储服务类型标识(1-LOCAL,2-MINIO,3-OSS)
|
||||
* @description [获取存储服务类型]
|
||||
* @author Leocoder
|
||||
*/
|
||||
String getStorageType();
|
||||
}
|
||||
@ -20,10 +20,8 @@ public class StorageServiceFactory {
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
/**
|
||||
* 根据存储类型获取存储服务
|
||||
*
|
||||
* @param storageType 存储类型(local、minio、oss)
|
||||
* @return 存储服务实例
|
||||
* @description [根据存储类型获取存储服务]
|
||||
* @author Leocoder
|
||||
*/
|
||||
public StorageService getStorageService(String storageType) {
|
||||
if (storageType == null || storageType.trim().isEmpty()) {
|
||||
@ -50,9 +48,8 @@ public class StorageServiceFactory {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取默认存储服务(OSS优先,如果不可用则使用本地存储)
|
||||
*
|
||||
* @return 默认存储服务实例
|
||||
* @description [获取默认存储服务(OSS优先,如果不可用则使用本地存储)]
|
||||
* @author Leocoder
|
||||
*/
|
||||
public StorageService getDefaultStorageService() {
|
||||
try {
|
||||
@ -70,11 +67,8 @@ public class StorageServiceFactory {
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查存储服务是否匹配指定类型
|
||||
*
|
||||
* @param service 存储服务实例
|
||||
* @param storageType 存储类型
|
||||
* @return 是否匹配
|
||||
* @description [检查存储服务是否匹配指定类型]
|
||||
* @author Leocoder
|
||||
*/
|
||||
private boolean isMatchingStorageType(StorageService service, String storageType) {
|
||||
String serviceClassName = service.getClass().getSimpleName().toLowerCase();
|
||||
@ -82,8 +76,6 @@ public class StorageServiceFactory {
|
||||
switch (storageType) {
|
||||
case "local":
|
||||
return serviceClassName.contains("local");
|
||||
case "minio":
|
||||
return serviceClassName.contains("minio");
|
||||
case "oss":
|
||||
return serviceClassName.contains("oss");
|
||||
default:
|
||||
|
||||
@ -17,11 +17,8 @@ import java.util.UUID;
|
||||
public class OssUtil {
|
||||
|
||||
/**
|
||||
* 生成唯一文件名
|
||||
* 格式:yyyyMMddHHmmss-6位UUID.扩展名
|
||||
*
|
||||
* @param originalFilename 原始文件名
|
||||
* @return 生成的唯一文件名
|
||||
* @description [生成唯一文件名]
|
||||
* @author Leocoder
|
||||
*/
|
||||
public static String generateUniqueFileName(String originalFilename) {
|
||||
if (!StringUtils.hasText(originalFilename)) {
|
||||
@ -42,12 +39,8 @@ public class OssUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建文件夹路径
|
||||
* 格式:folderName/username/yyyy/MM/dd
|
||||
*
|
||||
* @param folderName 文件夹名称
|
||||
* @param username 用户名
|
||||
* @return 文件夹路径
|
||||
* @description [构建文件夹路径]
|
||||
* @author Leocoder
|
||||
*/
|
||||
public static String buildFolderPath(String folderName, String username) {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
@ -74,10 +67,8 @@ public class OssUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证OSS对象键格式
|
||||
*
|
||||
* @param objectKey OSS对象键
|
||||
* @return 是否有效
|
||||
* @description [验证OSS对象键格式]
|
||||
* @author Leocoder
|
||||
*/
|
||||
public static boolean isValidObjectKey(String objectKey) {
|
||||
if (!StringUtils.hasText(objectKey)) {
|
||||
@ -89,7 +80,6 @@ public class OssUtil {
|
||||
return false;
|
||||
}
|
||||
|
||||
// OSS对象名不能包含连续的//
|
||||
if (objectKey.contains("//")) {
|
||||
return false;
|
||||
}
|
||||
@ -103,11 +93,8 @@ public class OssUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 规范化对象键
|
||||
* 移除开头的/,替换连续的//为/
|
||||
*
|
||||
* @param objectKey 原始对象键
|
||||
* @return 规范化后的对象键
|
||||
* @description [规范化对象键]
|
||||
* @author Leocoder
|
||||
*/
|
||||
public static String normalizeObjectKey(String objectKey) {
|
||||
if (!StringUtils.hasText(objectKey)) {
|
||||
@ -130,11 +117,8 @@ public class OssUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件类型编码
|
||||
* 基于文件扩展名返回对应的类型编码
|
||||
*
|
||||
* @param filename 文件名
|
||||
* @return 文件类型编码
|
||||
* @description [获取文件类型编码]
|
||||
* @author Leocoder
|
||||
*/
|
||||
public static String getFileTypeCode(String filename) {
|
||||
return FileTypeUtil.checkFileExtension(FileTypeUtil.getFileType(filename));
|
||||
|
||||
Loading…
Reference in New Issue
Block a user