Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/src/main/resources/sql/h2/update_table_ddl_2025_1217.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE t_resource
ADD COLUMN hash VARCHAR(100) AFTER thumbnail_url;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE t_resource
ADD COLUMN hash VARCHAR(100) AFTER thumbnail_url;
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,11 @@ public enum ExceptionEnum implements IBaseError {
/**
* Cm 337 exception enum.
*/
CM337("CM337", "组织不存在");
CM337("CM337", "组织不存在"),
/**
* Cm 338 exception enum.
*/
CM338("CM338", "用户不存在,请重新注册");

/**
* 错误码
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* Copyright (c) 2023 - present TinyEngine Authors.
* Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
*
* Use of this source code is governed by an MIT-style license.
*
* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
*
*/

package com.tinyengine.it.common.utils;

import org.springframework.web.multipart.MultipartFile;
import java.security.MessageDigest;
import java.io.InputStream;

/**
* The type MultipartFileHashUtils.
*
* @since 2025-12-17
*/
public class MultipartFileHashUtils {

/**
* 计算MultipartFile的MD5哈希
*/
public static String getMultipartFileMD5(MultipartFile file) throws Exception {
return getMultipartFileHash(file, "MD5");
}

/**
* 计算MultipartFile的SHA-256哈希
*/
public static String getMultipartFileSHA256(MultipartFile file) throws Exception {
return getMultipartFileHash(file, "SHA-256");
}

/**
* 通用方法:计算MultipartFile的哈希值
*/
public static String getMultipartFileHash(MultipartFile file, String algorithm)
throws Exception {

MessageDigest digest = MessageDigest.getInstance(algorithm);

try (InputStream inputStream = file.getInputStream()) {
byte[] buffer = new byte[8192];
int bytesRead;

while ((bytesRead = inputStream.read(buffer)) != -1) {
digest.update(buffer, 0, bytesRead);
}
}

return bytesToHex(digest.digest());
}

/**
* 字节数组转十六进制字符串
*/
private static String bytesToHex(byte[] bytes) {
StringBuilder hexString = new StringBuilder();
for (byte b : bytes) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.tinyengine.it.common.exception.ServiceException;
import com.tinyengine.it.common.log.SystemControllerLog;
import com.tinyengine.it.common.utils.ImageThumbnailGenerator;
import com.tinyengine.it.common.utils.MultipartFileHashUtils;
import com.tinyengine.it.common.utils.Utils;
import com.tinyengine.it.model.entity.Resource;
import com.tinyengine.it.service.material.ResourceService;
Expand Down Expand Up @@ -200,10 +201,12 @@ public Result<Resource> resourceUpload(@RequestParam MultipartFile file, Integer
}
// 将文件转为 Base64
String base64 = ImageThumbnailGenerator.convertToBase64(file);
String imageHash = MultipartFileHashUtils.getMultipartFileMD5(file);
Resource resource = new Resource();
resource.setName(fileName);
resource.setResourceData(base64);
resource.setAppId(appId);
resource.setHash(imageHash);
Resource result = resourceService.resourceUpload(resource);
return Result.success(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ public Result<SSOTicket> login(@RequestBody User user) throws Exception {
User userParam = new User();
userParam.setUsername(user.getUsername());
List<User> users = userService.queryUserByCondition(userParam);
if (users.isEmpty()) {
Result.failed(ExceptionEnum.CM004);
if (users == null || users.isEmpty()) {
Result.failed(ExceptionEnum.CM338);
}
User userResult = users.get(0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public User createUser(User user) throws Exception {
authUsersUnitsRoles.setUnitType("tenant");
authUsersUnitsRoles.setUnitId(1);
authUsersUnitsRoles.setUserId(Integer.valueOf(user.getId()));
authUsersUnitsRoles.setCreatedBy(user.getId());
authUsersUnitsRoles.setLastUpdatedBy(user.getId());
authUsersUnitsRolesMapper.createAuthUsersUnitsRoles(authUsersUnitsRoles);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public class Resource extends BaseEntity {
@Schema(name = "thumbnailUrl", description = "缩略图url")
private String thumbnailUrl;

@Schema(name = "hash", description = "图片内容hash")
private String hash;

@Schema(name = "category", description = "分类")
private String category;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.tinyengine.it.common.exception.ServiceException;
import com.tinyengine.it.common.log.SystemServiceLog;
import com.tinyengine.it.common.utils.ImageThumbnailGenerator;
import com.tinyengine.it.common.utils.MultipartFileHashUtils;
import com.tinyengine.it.common.utils.Utils;
import com.tinyengine.it.mapper.ResourceGroupResourceMapper;
import com.tinyengine.it.mapper.ResourceMapper;
Expand Down Expand Up @@ -201,8 +202,7 @@ public Resource resourceUpload(Resource resource) {
resource.setThumbnailData(ImageThumbnailGenerator.createThumbnail(resource.getResourceData(), 200, 200));
}
QueryWrapper<Resource> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("name", resource.getName());
queryWrapper.eq("category", resource.getCategory());
queryWrapper.eq("hash", resource.getHash());
// 接入租户系统需添加租户id查询
Resource resourceResult = this.baseMapper.selectOne(queryWrapper);
if (resourceResult != null) {
Expand Down
11 changes: 10 additions & 1 deletion base/src/main/resources/mappers/ResourceMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<!-- 通用查询列 -->
<sql id="Base_Column_List">
id
, app_id, platform_id, `name`, thumbnail_name, resource_url, thumbnail_url, category, description, thumbnail_data, resource_data,
, app_id, platform_id, `name`, thumbnail_name, resource_url, thumbnail_url, hash, category, description, thumbnail_data, resource_data,
public_status, is_default, created_by, last_updated_by, created_time, last_updated_time, tenant_id, renter_id, site_id
</sql>

Expand All @@ -28,6 +28,9 @@
<if test="resourceUrl!=null and resourceUrl!=''">
AND resource_url = #{resourceUrl}
</if>
<if test="hash!=null and hash!=''">
AND hash = #{hash}
</if>
<if test="thumbnailUrl!=null and thumbnailUrl!=''">
AND thumbnail_url = #{thumbnailUrl}
</if>
Expand Down Expand Up @@ -92,6 +95,9 @@
<if test="thumbnailUrl!=null and thumbnailUrl!=''">
thumbnail_url = #{thumbnailUrl},
</if>
<if test="hash!=null and hash!=''">
hash = #{hash},
</if>
<if test="category!=null and category!=''">
category = #{category},
</if>
Expand Down Expand Up @@ -143,6 +149,7 @@
<result column="thumbnail_name" property="thumbnailName"/>
<result column="resource_url" property="resourceUrl"/>
<result column="thumbnail_url" property="thumbnailUrl"/>
<result column="hash" property="hash"/>
<result column="category" property="category"/>
<result column="description" property="description"/>
<result column="thumbnail_data" property="thumbnailData"/>
Expand Down Expand Up @@ -255,6 +262,7 @@
, thumbnail_name
, resource_url
, thumbnail_url
, hash
, category
, description
, thumbnail_data
Expand All @@ -275,6 +283,7 @@
, #{thumbnailName}
, #{resourceUrl}
, #{thumbnailUrl}
, #{hash}
, #{category}
, #{description}
, #{thumbnailData}
Expand Down
2 changes: 2 additions & 0 deletions docker-deploy-data/mysql/init/update_table_ddl_2025_1217.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE t_resource
ADD COLUMN hash VARCHAR(100) AFTER thumbnail_url;
Loading