mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-09-10 20:57:14 +08:00
refactor(system): 优化文件相关类的属性顺序和注释,增强代码可读性
- 优化文件相关类的属性顺序和注释,增强代码可读性 - 优化 File、Message 相关代码的包结构
This commit is contained in:
@@ -61,21 +61,12 @@ 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));
|
||||
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("/")) {
|
||||
if (absPath.endsWith(StringConstants.SLASH)) {
|
||||
String tempAbsPath = absPath.substring(0, absPath.length() - 1);
|
||||
String[] pathArr = tempAbsPath.split(StringConstants.SLASH);
|
||||
if (pathArr.length > 1) {
|
||||
@@ -84,11 +75,20 @@ public class FileRecorderImpl implements FileRecorder {
|
||||
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.setExtension(fileInfo.getExt());
|
||||
file.setType(FileTypeEnum.getByExtension(file.getExtension()));
|
||||
file.setContentType(fileInfo.getContentType());
|
||||
file.setMd5(fileInfo.getHashInfo().getMd5());
|
||||
file.setMetadata(JSONUtil.toJsonStr(fileInfo.getMetadata()));
|
||||
file.setThumbnailUrl(fileInfo.getThUrl());
|
||||
file.setThumbnailSize(fileInfo.getThSize());
|
||||
file.setThumbnailMetadata(JSONUtil.toJsonStr(fileInfo.getThMetadata()));
|
||||
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());
|
||||
fileMapper.insert(file);
|
||||
return true;
|
||||
}
|
||||
@@ -133,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));
|
||||
}
|
||||
}
|
@@ -49,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")),
|
||||
|
||||
/**
|
||||
* 文档
|
||||
@@ -64,8 +64,7 @@ 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;
|
||||
@@ -79,8 +78,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);
|
||||
}
|
||||
}
|
||||
|
@@ -18,7 +18,7 @@ package top.continew.admin.system.mapper;
|
||||
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
import top.continew.admin.system.model.entity.FileDO;
|
||||
import top.continew.admin.system.model.resp.FileStatisticsResp;
|
||||
import top.continew.admin.system.model.resp.file.FileStatisticsResp;
|
||||
import top.continew.starter.data.mp.base.BaseMapper;
|
||||
|
||||
import java.util.List;
|
||||
|
@@ -21,7 +21,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Constants;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import top.continew.admin.system.model.entity.MessageDO;
|
||||
import top.continew.admin.system.model.resp.MessageResp;
|
||||
import top.continew.admin.system.model.resp.message.MessageResp;
|
||||
import top.continew.starter.data.mp.base.BaseMapper;
|
||||
|
||||
/**
|
||||
|
@@ -70,43 +70,43 @@ public class FileDO extends BaseDO {
|
||||
*/
|
||||
private String absPath;
|
||||
|
||||
/**
|
||||
* 文件元数据
|
||||
*/
|
||||
private String metadata;
|
||||
|
||||
/**
|
||||
* 文件类型
|
||||
*/
|
||||
private String contentType;
|
||||
|
||||
/**
|
||||
* 文件md5值
|
||||
*/
|
||||
private String md5;
|
||||
|
||||
/**
|
||||
* 扩展名
|
||||
*/
|
||||
private String extension;
|
||||
|
||||
/**
|
||||
* 内容类型
|
||||
*/
|
||||
private String contentType;
|
||||
|
||||
/**
|
||||
* 类型
|
||||
*/
|
||||
private FileTypeEnum type;
|
||||
|
||||
/**
|
||||
* MD5 值
|
||||
*/
|
||||
private String md5;
|
||||
|
||||
/**
|
||||
* 元数据
|
||||
*/
|
||||
private String metadata;
|
||||
|
||||
/**
|
||||
* 缩略图大小(字节)
|
||||
*/
|
||||
private Long thumbnailSize;
|
||||
|
||||
/**
|
||||
* 缩略图URL
|
||||
* 缩略图 URL
|
||||
*/
|
||||
private String thumbnailUrl;
|
||||
|
||||
/**
|
||||
* 文件元数据
|
||||
* 缩略图元数据
|
||||
*/
|
||||
private String thumbnailMetadata;
|
||||
|
||||
@@ -126,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));
|
||||
@@ -138,8 +138,8 @@ 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));
|
||||
|
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.admin.system.model.resp;
|
||||
package top.continew.admin.system.model.resp.file;
|
||||
|
||||
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
@@ -68,36 +68,36 @@ public class FileResp extends BaseDetailResp {
|
||||
@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;
|
||||
|
||||
/**
|
||||
* 扩展名
|
||||
*/
|
||||
@Schema(description = "扩展名", example = "jpg")
|
||||
private String extension;
|
||||
|
||||
/**
|
||||
* 内容类型
|
||||
*/
|
||||
@Schema(description = "内容类型", example = "image/jpeg")
|
||||
private String contentType;
|
||||
|
||||
/**
|
||||
* 类型
|
||||
*/
|
||||
@Schema(description = "类型", example = "2")
|
||||
private FileTypeEnum type;
|
||||
|
||||
/**
|
||||
* MD5 值
|
||||
*/
|
||||
@Schema(description = "MD5值", example = "193572f83684128a0d0f993e97100f8a")
|
||||
private String md5;
|
||||
|
||||
/**
|
||||
* 元数据
|
||||
*/
|
||||
@Schema(description = "元数据", example = "{width:1024,height:1024}")
|
||||
private String metadata;
|
||||
|
||||
/**
|
||||
* 缩略图大小(字节)
|
||||
*/
|
||||
@@ -111,7 +111,7 @@ public class FileResp extends BaseDetailResp {
|
||||
private String thumbnailUrl;
|
||||
|
||||
/**
|
||||
* 缩略图文件元数据
|
||||
* 缩略图元数据
|
||||
*/
|
||||
@Schema(description = "缩略图文件元数据", example = "{width:100,height:100}")
|
||||
private String thumbnailMetadata;
|
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.admin.system.model.resp;
|
||||
package top.continew.admin.system.model.resp.file;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.admin.system.model.resp;
|
||||
package top.continew.admin.system.model.resp.file;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Builder;
|
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.admin.system.model.resp;
|
||||
package top.continew.admin.system.model.resp.message;
|
||||
|
||||
import cn.crane4j.annotation.Assemble;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.admin.system.model.resp;
|
||||
package top.continew.admin.system.model.resp.message;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.admin.system.model.resp;
|
||||
package top.continew.admin.system.model.resp.message;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
@@ -21,8 +21,8 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
import top.continew.admin.system.model.entity.FileDO;
|
||||
import top.continew.admin.system.model.query.FileQuery;
|
||||
import top.continew.admin.system.model.req.FileReq;
|
||||
import top.continew.admin.system.model.resp.FileResp;
|
||||
import top.continew.admin.system.model.resp.FileStatisticsResp;
|
||||
import top.continew.admin.system.model.resp.file.FileResp;
|
||||
import top.continew.admin.system.model.resp.file.FileStatisticsResp;
|
||||
import top.continew.starter.data.mp.service.IService;
|
||||
import top.continew.starter.extension.crud.service.BaseService;
|
||||
|
||||
|
@@ -18,7 +18,7 @@ package top.continew.admin.system.service;
|
||||
|
||||
import top.continew.admin.system.model.query.MessageQuery;
|
||||
import top.continew.admin.system.model.req.MessageReq;
|
||||
import top.continew.admin.system.model.resp.MessageResp;
|
||||
import top.continew.admin.system.model.resp.message.MessageResp;
|
||||
import top.continew.starter.extension.crud.model.query.PageQuery;
|
||||
import top.continew.starter.extension.crud.model.resp.PageResp;
|
||||
|
||||
|
@@ -16,7 +16,7 @@
|
||||
|
||||
package top.continew.admin.system.service;
|
||||
|
||||
import top.continew.admin.system.model.resp.MessageUnreadResp;
|
||||
import top.continew.admin.system.model.resp.message.MessageUnreadResp;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@@ -36,8 +36,8 @@ import top.continew.admin.system.model.entity.FileDO;
|
||||
import top.continew.admin.system.model.entity.StorageDO;
|
||||
import top.continew.admin.system.model.query.FileQuery;
|
||||
import top.continew.admin.system.model.req.FileReq;
|
||||
import top.continew.admin.system.model.resp.FileResp;
|
||||
import top.continew.admin.system.model.resp.FileStatisticsResp;
|
||||
import top.continew.admin.system.model.resp.file.FileResp;
|
||||
import top.continew.admin.system.model.resp.file.FileStatisticsResp;
|
||||
import top.continew.admin.system.service.FileService;
|
||||
import top.continew.admin.system.service.StorageService;
|
||||
import top.continew.starter.core.constant.StringConstants;
|
||||
@@ -91,12 +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())
|
||||
.setHashCalculatorMd5(true)
|
||||
.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));
|
||||
@@ -155,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()));
|
||||
}
|
||||
|
@@ -29,7 +29,7 @@ import top.continew.admin.system.mapper.MessageMapper;
|
||||
import top.continew.admin.system.model.entity.MessageDO;
|
||||
import top.continew.admin.system.model.query.MessageQuery;
|
||||
import top.continew.admin.system.model.req.MessageReq;
|
||||
import top.continew.admin.system.model.resp.MessageResp;
|
||||
import top.continew.admin.system.model.resp.message.MessageResp;
|
||||
import top.continew.admin.system.service.MessageService;
|
||||
import top.continew.admin.system.service.MessageUserService;
|
||||
import top.continew.starter.core.validation.CheckUtils;
|
||||
|
@@ -22,8 +22,8 @@ import org.springframework.stereotype.Service;
|
||||
import top.continew.admin.system.enums.MessageTypeEnum;
|
||||
import top.continew.admin.system.mapper.MessageUserMapper;
|
||||
import top.continew.admin.system.model.entity.MessageUserDO;
|
||||
import top.continew.admin.system.model.resp.MessageTypeUnreadResp;
|
||||
import top.continew.admin.system.model.resp.MessageUnreadResp;
|
||||
import top.continew.admin.system.model.resp.message.MessageTypeUnreadResp;
|
||||
import top.continew.admin.system.model.resp.message.MessageUnreadResp;
|
||||
import top.continew.admin.system.service.MessageUserService;
|
||||
import top.continew.starter.core.validation.CheckUtils;
|
||||
|
||||
|
@@ -1,7 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||
<mapper namespace="top.continew.admin.system.mapper.MessageMapper">
|
||||
<select id="selectPageByUserId" resultType="top.continew.admin.system.model.resp.MessageResp">
|
||||
<select id="selectPageByUserId" resultType="top.continew.admin.system.model.resp.message.MessageResp">
|
||||
SELECT
|
||||
t1.*,
|
||||
t2.user_id,
|
||||
|
Reference in New Issue
Block a user