mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-09-09 20:57:21 +08:00
refactor(system/file): 文件添加路径和md5值 (#138)
This commit is contained in:
@@ -20,6 +20,7 @@ import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.ClassUtil;
|
||||
import cn.hutool.core.util.EscapeUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -60,19 +61,34 @@ public class FileRecorderImpl implements FileRecorder {
|
||||
fileInfo.setId(id.longValue() + "");
|
||||
String originalFilename = EscapeUtil.unescape(fileInfo.getOriginalFilename());
|
||||
file.setName(StrUtil.contains(originalFilename, StringConstants.DOT)
|
||||
? StrUtil.subBefore(originalFilename, StringConstants.DOT, true)
|
||||
: originalFilename);
|
||||
? StrUtil.subBefore(originalFilename, StringConstants.DOT, true)
|
||||
: originalFilename);
|
||||
file.setUrl(fileInfo.getUrl());
|
||||
file.setSize(fileInfo.getSize());
|
||||
file.setExtension(fileInfo.getExt());
|
||||
file.setType(FileTypeEnum.getByExtension(file.getExtension()));
|
||||
file.setThumbnailUrl(fileInfo.getThUrl());
|
||||
file.setThumbnailSize(fileInfo.getThSize());
|
||||
StorageDO storage = (StorageDO)fileInfo.getAttr().get(ClassUtil.getClassName(StorageDO.class, false));
|
||||
StorageDO storage = (StorageDO) fileInfo.getAttr().get(ClassUtil.getClassName(StorageDO.class, false));
|
||||
file.setStorageId(storage.getId());
|
||||
file.setCreateTime(DateUtil.toLocalDateTime(fileInfo.getCreateTime()));
|
||||
file.setUpdateUser(UserContextHolder.getUserId());
|
||||
file.setUpdateTime(file.getCreateTime());
|
||||
String absPath = fileInfo.getPath();
|
||||
if (absPath.endsWith("/")) {
|
||||
String tempAbsPath = absPath.substring(0, absPath.length() - 1);
|
||||
String[] pathArr = tempAbsPath.split(StringConstants.SLASH);
|
||||
if (pathArr.length > 1) {
|
||||
file.setParentPath(pathArr[pathArr.length - 1]);
|
||||
} else {
|
||||
file.setParentPath(StringConstants.SLASH);
|
||||
}
|
||||
}
|
||||
file.setMd5(fileInfo.getHashInfo().getMd5());
|
||||
file.setAbsPath(fileInfo.getPath());
|
||||
file.setMetadata(JSONUtil.toJsonStr(fileInfo.getMetadata()));
|
||||
file.setThumbnailMetadata(JSONUtil.toJsonStr(fileInfo.getThMetadata()));
|
||||
file.setContentType(fileInfo.getContentType());
|
||||
fileMapper.insert(file);
|
||||
return true;
|
||||
}
|
||||
@@ -117,8 +133,8 @@ public class FileRecorderImpl implements FileRecorder {
|
||||
private FileDO getFileByUrl(String url) {
|
||||
Optional<FileDO> fileOptional = fileMapper.lambdaQuery().eq(FileDO::getUrl, url).oneOpt();
|
||||
return fileOptional.orElseGet(() -> fileMapper.lambdaQuery()
|
||||
.likeLeft(FileDO::getUrl, StrUtil.subAfter(url, StringConstants.SLASH, true))
|
||||
.oneOpt()
|
||||
.orElse(null));
|
||||
.likeLeft(FileDO::getUrl, StrUtil.subAfter(url, StringConstants.SLASH, true))
|
||||
.oneOpt()
|
||||
.orElse(null));
|
||||
}
|
||||
}
|
@@ -35,6 +35,11 @@ import java.util.List;
|
||||
@RequiredArgsConstructor
|
||||
public enum FileTypeEnum implements BaseEnum<Integer> {
|
||||
|
||||
/**
|
||||
* 目录
|
||||
*/
|
||||
DIR(0, "目录", Collections.emptyList()),
|
||||
|
||||
/**
|
||||
* 其他
|
||||
*/
|
||||
@@ -44,7 +49,7 @@ public enum FileTypeEnum implements BaseEnum<Integer> {
|
||||
* 图片
|
||||
*/
|
||||
IMAGE(2, "图片", List
|
||||
.of("jpg", "jpeg", "png", "gif", "bmp", "webp", "ico", "psd", "tiff", "dwg", "jxr", "apng", "xcf")),
|
||||
.of("jpg", "jpeg", "png", "gif", "bmp", "webp", "ico", "psd", "tiff", "dwg", "jxr", "apng", "xcf")),
|
||||
|
||||
/**
|
||||
* 文档
|
||||
@@ -59,7 +64,8 @@ public enum FileTypeEnum implements BaseEnum<Integer> {
|
||||
/**
|
||||
* 音频
|
||||
*/
|
||||
AUDIO(5, "音频", List.of("mp3", "flac", "wav", "ogg", "midi", "m4a", "aac", "amr", "ac3", "aiff")),;
|
||||
AUDIO(5, "音频", List.of("mp3", "flac", "wav", "ogg", "midi", "m4a", "aac", "amr", "ac3", "aiff")),
|
||||
;
|
||||
|
||||
private final Integer value;
|
||||
private final String description;
|
||||
@@ -73,8 +79,8 @@ public enum FileTypeEnum implements BaseEnum<Integer> {
|
||||
*/
|
||||
public static FileTypeEnum getByExtension(String extension) {
|
||||
return Arrays.stream(FileTypeEnum.values())
|
||||
.filter(t -> t.getExtensions().contains(StrUtil.emptyIfNull(extension).toLowerCase()))
|
||||
.findFirst()
|
||||
.orElse(FileTypeEnum.UNKNOWN);
|
||||
.filter(t -> t.getExtensions().contains(StrUtil.emptyIfNull(extension).toLowerCase()))
|
||||
.findFirst()
|
||||
.orElse(FileTypeEnum.UNKNOWN);
|
||||
}
|
||||
}
|
||||
|
@@ -17,18 +17,20 @@
|
||||
package top.continew.admin.system.model.entity;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
import lombok.SneakyThrows;
|
||||
import org.dromara.x.file.storage.core.FileInfo;
|
||||
import top.continew.admin.common.model.entity.BaseDO;
|
||||
import top.continew.admin.system.enums.FileTypeEnum;
|
||||
import top.continew.admin.system.enums.StorageTypeEnum;
|
||||
import top.continew.starter.core.constant.StringConstants;
|
||||
import top.continew.starter.core.util.StrUtils;
|
||||
import top.continew.admin.common.model.entity.BaseDO;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.net.URL;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 文件实体
|
||||
@@ -58,6 +60,31 @@ public class FileDO extends BaseDO {
|
||||
*/
|
||||
private String url;
|
||||
|
||||
/**
|
||||
* 上级目录
|
||||
*/
|
||||
private String parentPath;
|
||||
|
||||
/**
|
||||
* 绝对路径
|
||||
*/
|
||||
private String absPath;
|
||||
|
||||
/**
|
||||
* 文件元数据
|
||||
*/
|
||||
private String metadata;
|
||||
|
||||
/**
|
||||
* 文件类型
|
||||
*/
|
||||
private String contentType;
|
||||
|
||||
/**
|
||||
* 文件md5值
|
||||
*/
|
||||
private String md5;
|
||||
|
||||
/**
|
||||
* 扩展名
|
||||
*/
|
||||
@@ -78,6 +105,11 @@ public class FileDO extends BaseDO {
|
||||
*/
|
||||
private String thumbnailUrl;
|
||||
|
||||
/**
|
||||
* 文件元数据
|
||||
*/
|
||||
private String thumbnailMetadata;
|
||||
|
||||
/**
|
||||
* 存储 ID
|
||||
*/
|
||||
@@ -94,10 +126,10 @@ public class FileDO extends BaseDO {
|
||||
fileInfo.setUrl(this.url);
|
||||
fileInfo.setSize(this.size);
|
||||
fileInfo.setFilename(StrUtil.contains(this.url, StringConstants.SLASH)
|
||||
? StrUtil.subAfter(this.url, StringConstants.SLASH, true)
|
||||
: this.url);
|
||||
? StrUtil.subAfter(this.url, StringConstants.SLASH, true)
|
||||
: this.url);
|
||||
fileInfo.setOriginalFilename(StrUtils
|
||||
.blankToDefault(this.extension, this.name, ex -> this.name + StringConstants.DOT + ex));
|
||||
.blankToDefault(this.extension, this.name, ex -> this.name + StringConstants.DOT + ex));
|
||||
fileInfo.setBasePath(StringConstants.EMPTY);
|
||||
// 优化 path 处理
|
||||
fileInfo.setPath(extractRelativePath(this.url, storageDO));
|
||||
@@ -106,9 +138,15 @@ public class FileDO extends BaseDO {
|
||||
fileInfo.setPlatform(storageDO.getCode());
|
||||
fileInfo.setThUrl(this.thumbnailUrl);
|
||||
fileInfo.setThFilename(StrUtil.contains(this.thumbnailUrl, StringConstants.SLASH)
|
||||
? StrUtil.subAfter(this.thumbnailUrl, StringConstants.SLASH, true)
|
||||
: this.thumbnailUrl);
|
||||
? StrUtil.subAfter(this.thumbnailUrl, StringConstants.SLASH, true)
|
||||
: this.thumbnailUrl);
|
||||
fileInfo.setThSize(this.thumbnailSize);
|
||||
if (StrUtil.isNotBlank(this.thumbnailMetadata)) {
|
||||
fileInfo.setThMetadata(JSONUtil.toBean(this.thumbnailMetadata, Map.class));
|
||||
}
|
||||
if (StrUtil.isNotBlank(this.metadata)) {
|
||||
fileInfo.setMetadata(JSONUtil.toBean(this.metadata, Map.class));
|
||||
}
|
||||
return fileInfo;
|
||||
}
|
||||
|
||||
|
@@ -45,6 +45,13 @@ public class FileQuery implements Serializable {
|
||||
@Query(type = QueryType.LIKE)
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 绝对路径
|
||||
*/
|
||||
@Schema(description = "绝对路径", example = "/2025")
|
||||
@Query(type = QueryType.EQ)
|
||||
private String absPath;
|
||||
|
||||
/**
|
||||
* 类型
|
||||
*/
|
||||
|
@@ -56,6 +56,36 @@ public class FileResp extends BaseDetailResp {
|
||||
@Schema(description = "URL", example = "https://examplebucket.oss-cn-hangzhou.aliyuncs.com/example/example.jpg")
|
||||
private String url;
|
||||
|
||||
/**
|
||||
* 上级目录
|
||||
*/
|
||||
@Schema(description = "上级目录", example = "25")
|
||||
private String parentPath;
|
||||
|
||||
/**
|
||||
* 绝对路径
|
||||
*/
|
||||
@Schema(description = "绝对路径", example = "/2025/2/25")
|
||||
private String absPath;
|
||||
|
||||
/**
|
||||
* 文件元数据
|
||||
*/
|
||||
@Schema(description = "文件元数据", example = "{width:1024,height:1024}")
|
||||
private String metadata;
|
||||
|
||||
/**
|
||||
* 文件md5值
|
||||
*/
|
||||
@Schema(description = "文件md5值", example = "abcdefghijklmnopqrstuvwxyz0123456789")
|
||||
private String md5;
|
||||
|
||||
/**
|
||||
* 文件类型
|
||||
*/
|
||||
@Schema(description = "文件md5值", example = "abcdefghijklmnopqrstuvwxyz0123456789")
|
||||
private String contentType;
|
||||
|
||||
/**
|
||||
* 扩展名
|
||||
*/
|
||||
@@ -80,6 +110,12 @@ public class FileResp extends BaseDetailResp {
|
||||
@Schema(description = "缩略图 URL", example = "https://examplebucket.oss-cn-hangzhou.aliyuncs.com/example/example.jpg.min.jpg")
|
||||
private String thumbnailUrl;
|
||||
|
||||
/**
|
||||
* 缩略图文件元数据
|
||||
*/
|
||||
@Schema(description = "缩略图文件元数据", example = "{width:100,height:100}")
|
||||
private String thumbnailMetadata;
|
||||
|
||||
/**
|
||||
* 存储 ID
|
||||
*/
|
||||
|
@@ -91,11 +91,12 @@ public class FileServiceImpl extends BaseServiceImpl<FileMapper, FileDO, FileRes
|
||||
}
|
||||
LocalDate today = LocalDate.now();
|
||||
String path = today.getYear() + StringConstants.SLASH + today.getMonthValue() + StringConstants.SLASH + today
|
||||
.getDayOfMonth() + StringConstants.SLASH;
|
||||
.getDayOfMonth() + StringConstants.SLASH;
|
||||
UploadPretreatment uploadPretreatment = fileStorageService.of(file)
|
||||
.setPlatform(storage.getCode())
|
||||
.putAttr(ClassUtil.getClassName(StorageDO.class, false), storage)
|
||||
.setPath(path);
|
||||
.setPlatform(storage.getCode())
|
||||
.setHashCalculatorMd5(true)
|
||||
.putAttr(ClassUtil.getClassName(StorageDO.class, false), storage)
|
||||
.setPath(path);
|
||||
// 图片文件生成缩略图
|
||||
if (FileTypeEnum.IMAGE.getExtensions().contains(FileNameUtil.extName(file.getOriginalFilename()))) {
|
||||
uploadPretreatment.thumbnail(img -> img.size(100, 100));
|
||||
@@ -154,7 +155,7 @@ public class FileServiceImpl extends BaseServiceImpl<FileMapper, FileDO, FileRes
|
||||
String url = URLUtil.normalize(prefix + fileResp.getUrl());
|
||||
fileResp.setUrl(url);
|
||||
String thumbnailUrl = StrUtils.blankToDefault(fileResp.getThumbnailUrl(), url, thUrl -> URLUtil
|
||||
.normalize(prefix + thUrl));
|
||||
.normalize(prefix + thUrl));
|
||||
fileResp.setThumbnailUrl(thumbnailUrl);
|
||||
fileResp.setStorageName("%s (%s)".formatted(storage.getName(), storage.getCode()));
|
||||
}
|
||||
|
Reference in New Issue
Block a user