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