Skip to content

Commit

Permalink
feat: 图片文件支持缩略图 (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
jskils committed May 13, 2024
1 parent 3ddcdf0 commit d320c95
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package top.continew.admin.system.config.file;

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 lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -60,7 +61,9 @@ public boolean save(FileInfo fileInfo) {
file.setUrl(fileInfo.getUrl());
file.setExtension(fileInfo.getExt());
file.setType(FileTypeEnum.getByExtension(file.getExtension()));
StorageDO storage = storageMapper.lambdaQuery().eq(StorageDO::getCode, fileInfo.getPlatform()).one();
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(LoginHelper.getUserId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ public class FileDO extends BaseDO {
*/
private FileTypeEnum type;

/**
* 缩略图大小(字节)
*/
private Long thumbnailSize;

/**
* 缩略图URL
*/
private String thumbnailUrl;

/**
* 存储 ID
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ public class FileResp extends BaseDetailResp {
"5"}, example = "2")
private FileTypeEnum type;

/**
* 缩略图大小(字节)
*/
@Schema(description = "缩略图大小(字节)", example = "1024")
private Long thumbnailSize;

/**
* 缩略图URL
*/
@Schema(description = "缩略图URL", example = "https://examplebucket.oss-cn-hangzhou.aliyuncs.com/example/example.jpg.min.jpg")
private String thumbnailUrl;

/**
* 存储 ID
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
package top.continew.admin.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.core.util.*;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -85,7 +84,11 @@ public FileInfo upload(MultipartFile file, String storageCode) {
storage = storageService.getByCode(storageCode);
CheckUtils.throwIfNotExists(storage, "StorageDO", "Code", storageCode);
}
UploadPretreatment uploadPretreatment = fileStorageService.of(file).setPlatform(storage.getCode());

UploadPretreatment uploadPretreatment = fileStorageService.of(file)
.thumbnail(img -> img.size(100, 100))
.setPlatform(storage.getCode())
.putAttr(ClassUtil.getClassName(StorageDO.class, false), storage);
uploadPretreatment.setProgressMonitor(new ProgressListener() {
@Override
public void start() {
Expand Down Expand Up @@ -133,7 +136,12 @@ protected void fill(Object obj) {
super.fill(obj);
if (obj instanceof FileResp fileResp && !URLUtils.isHttpUrl(fileResp.getUrl())) {
StorageDO storage = storageService.getById(fileResp.getStorageId());
fileResp.setUrl(URLUtil.normalize(storage.getDomain() + StringConstants.SLASH + fileResp.getUrl()));
String url = URLUtil.normalize(storage.getDomain() + StringConstants.SLASH + fileResp.getUrl());
fileResp.setUrl(url);
String thumbnailUrl = StrUtil.isBlank(fileResp.getThumbnailUrl())
? url
: URLUtil.normalize(storage.getDomain() + StringConstants.SLASH + fileResp.getThumbnailUrl());
fileResp.setThumbnailUrl(thumbnailUrl);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ CREATE TABLE IF NOT EXISTS `sys_file` (
`create_time` datetime NOT NULL COMMENT '创建时间',
`update_user` bigint(20) NOT NULL COMMENT '修改人',
`update_time` datetime NOT NULL COMMENT '修改时间',
`thumbnail_size` bigint(20) NULL DEFAULT NULL COMMENT '缩略图大小(字节)',
`thumbnail_url` varchar(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '缩略图URL',
PRIMARY KEY (`id`) USING BTREE,
INDEX `idx_url`(`url`) USING BTREE,
INDEX `idx_type`(`type`) USING BTREE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,8 @@ CREATE TABLE IF NOT EXISTS "sys_file" (
"create_time" timestamp NOT NULL,
"update_user" int8 NOT NULL,
"update_time" timestamp NOT NULL,
"thumbnail_size" int8 DEFAULT NULL,
"thumbnail_url" varchar(512) DEFAULT NULL,
PRIMARY KEY ("id")
);
CREATE INDEX "idx_file_url" ON "sys_file" ("url");
Expand All @@ -458,6 +460,8 @@ COMMENT ON COLUMN "sys_file"."create_user" IS '创建人';
COMMENT ON COLUMN "sys_file"."create_time" IS '创建时间';
COMMENT ON COLUMN "sys_file"."update_user" IS '修改人';
COMMENT ON COLUMN "sys_file"."update_time" IS '修改时间';
COMMENT ON COLUMN "sys_file"."thumbnail_size" IS '缩略图大小(字节)';
COMMENT ON COLUMN "sys_file"."thumbnail_url" IS '缩略图URL';
COMMENT ON TABLE "sys_file" IS '文件表';

CREATE TABLE IF NOT EXISTS "gen_config" (
Expand Down

0 comments on commit d320c95

Please sign in to comment.