feat(shared): 优化本地文件服务实现
- 添加文件上传和删除方法的代码注释 - 使用 Hutool 的 DatePattern 优化日期格式化 - 修复文件路径分隔符问题,提高代码兼容性
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package com.youlai.boot.shared.file.service.impl;
|
package com.youlai.boot.shared.file.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.date.DatePattern;
|
||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
import cn.hutool.core.io.FileUtil;
|
import cn.hutool.core.io.FileUtil;
|
||||||
import cn.hutool.core.util.IdUtil;
|
import cn.hutool.core.util.IdUtil;
|
||||||
@@ -7,6 +8,7 @@ import com.youlai.boot.shared.file.model.FileInfo;
|
|||||||
import com.youlai.boot.shared.file.service.FileService;
|
import com.youlai.boot.shared.file.service.FileService;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
@@ -23,30 +25,37 @@ import java.time.LocalDateTime;
|
|||||||
* @author Theo
|
* @author Theo
|
||||||
* @since 2024-12-09 17:11
|
* @since 2024-12-09 17:11
|
||||||
*/
|
*/
|
||||||
|
@Data
|
||||||
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
@ConditionalOnProperty(value = "oss.type", havingValue = "local")
|
@ConditionalOnProperty(value = "oss.type", havingValue = "local")
|
||||||
@ConfigurationProperties(prefix = "oss.local")
|
@ConfigurationProperties(prefix = "oss.local")
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@Data
|
|
||||||
public class LocalFileService implements FileService {
|
public class LocalFileService implements FileService {
|
||||||
|
|
||||||
@Value("${oss.local.storage-path}")
|
@Value("${oss.local.storage-path}")
|
||||||
private String storagePath;
|
private String storagePath;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传文件方法
|
||||||
|
*
|
||||||
|
* @param file 表单文件对象
|
||||||
|
* @return 文件信息
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public FileInfo uploadFile(MultipartFile file) {
|
public FileInfo uploadFile(MultipartFile file) {
|
||||||
// 生成文件名(日期文件夹)
|
// 生成文件名(日期文件夹)
|
||||||
String suffix = FileUtil.getSuffix(file.getOriginalFilename());
|
String suffix = FileUtil.getSuffix(file.getOriginalFilename());
|
||||||
String uuid = IdUtil.simpleUUID();
|
String uuid = IdUtil.simpleUUID();
|
||||||
String folder = DateUtil.format(LocalDateTime.now(), "yyyyMMdd") + File.separator;
|
String folder = DateUtil.format(LocalDateTime.now(), DatePattern.PURE_DATE_PATTERN);
|
||||||
String fileName = uuid + "." + suffix;
|
String fileName = uuid + "." + suffix;
|
||||||
String filePrefix = storagePath.endsWith(File.separator) ? storagePath : storagePath + File.separator;
|
String filePrefix = storagePath.endsWith(File.separator) ? storagePath : storagePath + File.separator;
|
||||||
// try-with-resource 语法糖自动释放流
|
// try-with-resource 语法糖自动释放流
|
||||||
try (InputStream inputStream = file.getInputStream()) {
|
try (InputStream inputStream = file.getInputStream()) {
|
||||||
// 上传文件
|
// 上传文件
|
||||||
FileUtil.writeFromStream(inputStream, filePrefix +folder+ fileName);
|
FileUtil.writeFromStream(inputStream, filePrefix + folder + File.separator + fileName);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
log.error("文件上传失败", e);
|
||||||
throw new RuntimeException("文件上传失败");
|
throw new RuntimeException("文件上传失败");
|
||||||
}
|
}
|
||||||
// 获取文件访问路径,因为这里是本地存储,所以直接返回文件的相对路径,需要前端自行处理访问前缀
|
// 获取文件访问路径,因为这里是本地存储,所以直接返回文件的相对路径,需要前端自行处理访问前缀
|
||||||
@@ -57,6 +66,12 @@ public class LocalFileService implements FileService {
|
|||||||
return fileInfo;
|
return fileInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除文件
|
||||||
|
* @param filePath 文件完整URL
|
||||||
|
* @return 是否删除成功
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean deleteFile(String filePath) {
|
public boolean deleteFile(String filePath) {
|
||||||
//判断文件是否为空
|
//判断文件是否为空
|
||||||
|
|||||||
Reference in New Issue
Block a user