Skip to content

Commit 3603e8f

Browse files
authored
fix: modify resource upload (#277)
1 parent e1e25eb commit 3603e8f

File tree

11 files changed

+107
-6
lines changed

11 files changed

+107
-6
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE t_resource
2+
ADD COLUMN hash VARCHAR(100) AFTER thumbnail_url;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE t_resource
2+
ADD COLUMN hash VARCHAR(100) AFTER thumbnail_url;

base/src/main/java/com/tinyengine/it/common/exception/ExceptionEnum.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,11 @@ public enum ExceptionEnum implements IBaseError {
311311
/**
312312
* Cm 337 exception enum.
313313
*/
314-
CM337("CM337", "组织不存在");
314+
CM337("CM337", "组织不存在"),
315+
/**
316+
* Cm 338 exception enum.
317+
*/
318+
CM338("CM338", "用户不存在,请重新注册");
315319

316320
/**
317321
* 错误码
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* Copyright (c) 2023 - present TinyEngine Authors.
3+
* Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
4+
*
5+
* Use of this source code is governed by an MIT-style license.
6+
*
7+
* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
8+
* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
9+
* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
10+
*
11+
*/
12+
13+
package com.tinyengine.it.common.utils;
14+
15+
import org.springframework.web.multipart.MultipartFile;
16+
import java.security.MessageDigest;
17+
import java.io.InputStream;
18+
19+
/**
20+
* The type MultipartFileHashUtils.
21+
*
22+
* @since 2025-12-17
23+
*/
24+
public class MultipartFileHashUtils {
25+
26+
/**
27+
* 计算MultipartFile的MD5哈希
28+
*/
29+
public static String getMultipartFileMD5(MultipartFile file) throws Exception {
30+
return getMultipartFileHash(file, "MD5");
31+
}
32+
33+
/**
34+
* 计算MultipartFile的SHA-256哈希
35+
*/
36+
public static String getMultipartFileSHA256(MultipartFile file) throws Exception {
37+
return getMultipartFileHash(file, "SHA-256");
38+
}
39+
40+
/**
41+
* 通用方法:计算MultipartFile的哈希值
42+
*/
43+
public static String getMultipartFileHash(MultipartFile file, String algorithm)
44+
throws Exception {
45+
46+
MessageDigest digest = MessageDigest.getInstance(algorithm);
47+
48+
try (InputStream inputStream = file.getInputStream()) {
49+
byte[] buffer = new byte[8192];
50+
int bytesRead;
51+
52+
while ((bytesRead = inputStream.read(buffer)) != -1) {
53+
digest.update(buffer, 0, bytesRead);
54+
}
55+
}
56+
57+
return bytesToHex(digest.digest());
58+
}
59+
60+
/**
61+
* 字节数组转十六进制字符串
62+
*/
63+
private static String bytesToHex(byte[] bytes) {
64+
StringBuilder hexString = new StringBuilder();
65+
for (byte b : bytes) {
66+
String hex = Integer.toHexString(0xff & b);
67+
if (hex.length() == 1) {
68+
hexString.append('0');
69+
}
70+
hexString.append(hex);
71+
}
72+
return hexString.toString();
73+
}
74+
}

base/src/main/java/com/tinyengine/it/controller/ResourceController.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.tinyengine.it.common.exception.ServiceException;
1919
import com.tinyengine.it.common.log.SystemControllerLog;
2020
import com.tinyengine.it.common.utils.ImageThumbnailGenerator;
21+
import com.tinyengine.it.common.utils.MultipartFileHashUtils;
2122
import com.tinyengine.it.common.utils.Utils;
2223
import com.tinyengine.it.model.entity.Resource;
2324
import com.tinyengine.it.service.material.ResourceService;
@@ -200,10 +201,12 @@ public Result<Resource> resourceUpload(@RequestParam MultipartFile file, Integer
200201
}
201202
// 将文件转为 Base64
202203
String base64 = ImageThumbnailGenerator.convertToBase64(file);
204+
String imageHash = MultipartFileHashUtils.getMultipartFileMD5(file);
203205
Resource resource = new Resource();
204206
resource.setName(fileName);
205207
resource.setResourceData(base64);
206208
resource.setAppId(appId);
209+
resource.setHash(imageHash);
207210
Resource result = resourceService.resourceUpload(resource);
208211
return Result.success(result);
209212
}

base/src/main/java/com/tinyengine/it/login/controller/LoginController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ public Result<SSOTicket> login(@RequestBody User user) throws Exception {
143143
User userParam = new User();
144144
userParam.setUsername(user.getUsername());
145145
List<User> users = userService.queryUserByCondition(userParam);
146-
if (users.isEmpty()) {
147-
Result.failed(ExceptionEnum.CM004);
146+
if (users == null || users.isEmpty()) {
147+
return Result.failed(ExceptionEnum.CM338);
148148
}
149149
User userResult = users.get(0);
150150

base/src/main/java/com/tinyengine/it/login/service/impl/LoginServiceImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ public User createUser(User user) throws Exception {
7676
authUsersUnitsRoles.setUnitType("tenant");
7777
authUsersUnitsRoles.setUnitId(1);
7878
authUsersUnitsRoles.setUserId(Integer.valueOf(user.getId()));
79+
authUsersUnitsRoles.setCreatedBy(user.getId());
80+
authUsersUnitsRoles.setLastUpdatedBy(user.getId());
7981
authUsersUnitsRolesMapper.createAuthUsersUnitsRoles(authUsersUnitsRoles);
8082
return result;
8183
}

base/src/main/java/com/tinyengine/it/model/entity/Resource.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public class Resource extends BaseEntity {
5252
@Schema(name = "thumbnailUrl", description = "缩略图url")
5353
private String thumbnailUrl;
5454

55+
@Schema(name = "hash", description = "图片内容hash")
56+
private String hash;
57+
5558
@Schema(name = "category", description = "分类")
5659
private String category;
5760

base/src/main/java/com/tinyengine/it/service/material/impl/ResourceServiceImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.tinyengine.it.common.exception.ServiceException;
2121
import com.tinyengine.it.common.log.SystemServiceLog;
2222
import com.tinyengine.it.common.utils.ImageThumbnailGenerator;
23+
import com.tinyengine.it.common.utils.MultipartFileHashUtils;
2324
import com.tinyengine.it.common.utils.Utils;
2425
import com.tinyengine.it.mapper.ResourceGroupResourceMapper;
2526
import com.tinyengine.it.mapper.ResourceMapper;
@@ -201,8 +202,7 @@ public Resource resourceUpload(Resource resource) {
201202
resource.setThumbnailData(ImageThumbnailGenerator.createThumbnail(resource.getResourceData(), 200, 200));
202203
}
203204
QueryWrapper<Resource> queryWrapper = new QueryWrapper<>();
204-
queryWrapper.eq("name", resource.getName());
205-
queryWrapper.eq("category", resource.getCategory());
205+
queryWrapper.eq("hash", resource.getHash());
206206
// 接入租户系统需添加租户id查询
207207
Resource resourceResult = this.baseMapper.selectOne(queryWrapper);
208208
if (resourceResult != null) {

base/src/main/resources/mappers/ResourceMapper.xml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<!-- 通用查询列 -->
88
<sql id="Base_Column_List">
99
id
10-
, app_id, platform_id, `name`, thumbnail_name, resource_url, thumbnail_url, category, description, thumbnail_data, resource_data,
10+
, app_id, platform_id, `name`, thumbnail_name, resource_url, thumbnail_url, hash, category, description, thumbnail_data, resource_data,
1111
public_status, is_default, created_by, last_updated_by, created_time, last_updated_time, tenant_id, renter_id, site_id
1212
</sql>
1313

@@ -28,6 +28,9 @@
2828
<if test="resourceUrl!=null and resourceUrl!=''">
2929
AND resource_url = #{resourceUrl}
3030
</if>
31+
<if test="hash!=null and hash!=''">
32+
AND hash = #{hash}
33+
</if>
3134
<if test="thumbnailUrl!=null and thumbnailUrl!=''">
3235
AND thumbnail_url = #{thumbnailUrl}
3336
</if>
@@ -92,6 +95,9 @@
9295
<if test="thumbnailUrl!=null and thumbnailUrl!=''">
9396
thumbnail_url = #{thumbnailUrl},
9497
</if>
98+
<if test="hash!=null and hash!=''">
99+
hash = #{hash},
100+
</if>
95101
<if test="category!=null and category!=''">
96102
category = #{category},
97103
</if>
@@ -143,6 +149,7 @@
143149
<result column="thumbnail_name" property="thumbnailName"/>
144150
<result column="resource_url" property="resourceUrl"/>
145151
<result column="thumbnail_url" property="thumbnailUrl"/>
152+
<result column="hash" property="hash"/>
146153
<result column="category" property="category"/>
147154
<result column="description" property="description"/>
148155
<result column="thumbnail_data" property="thumbnailData"/>
@@ -255,6 +262,7 @@
255262
, thumbnail_name
256263
, resource_url
257264
, thumbnail_url
265+
, hash
258266
, category
259267
, description
260268
, thumbnail_data
@@ -275,6 +283,7 @@
275283
, #{thumbnailName}
276284
, #{resourceUrl}
277285
, #{thumbnailUrl}
286+
, #{hash}
278287
, #{category}
279288
, #{description}
280289
, #{thumbnailData}

0 commit comments

Comments
 (0)