diff --git a/app/src/main/resources/sql/h2/create_resources_ddl_2025_0902.sql b/app/src/main/resources/sql/h2/create_resources_ddl_2025_0902.sql index 4dda7424..00e6e670 100644 --- a/app/src/main/resources/sql/h2/create_resources_ddl_2025_0902.sql +++ b/app/src/main/resources/sql/h2/create_resources_ddl_2025_0902.sql @@ -6,6 +6,7 @@ create table `t_resource` `app_id` int not null comment '关联appId', `platform_id` int not null comment '关联设计器id', `name` varchar(255) not null comment '名称', + `thumbnail_name` varchar(255) not null comment '缩略图名称', `resource_url` varchar(255) comment '资源url', `thumbnail_url` varchar(255) comment '缩略图url', `category` varchar(255) not null comment '分类', diff --git a/app/src/main/resources/sql/mysql/create_resources_ddl_2025_0902.sql b/app/src/main/resources/sql/mysql/create_resources_ddl_2025_0902.sql index 4dda7424..00e6e670 100644 --- a/app/src/main/resources/sql/mysql/create_resources_ddl_2025_0902.sql +++ b/app/src/main/resources/sql/mysql/create_resources_ddl_2025_0902.sql @@ -6,6 +6,7 @@ create table `t_resource` `app_id` int not null comment '关联appId', `platform_id` int not null comment '关联设计器id', `name` varchar(255) not null comment '名称', + `thumbnail_name` varchar(255) not null comment '缩略图名称', `resource_url` varchar(255) comment '资源url', `thumbnail_url` varchar(255) comment '缩略图url', `category` varchar(255) not null comment '分类', diff --git a/base/src/main/java/com/tinyengine/it/common/utils/Utils.java b/base/src/main/java/com/tinyengine/it/common/utils/Utils.java index 538273f9..b5b7c337 100644 --- a/base/src/main/java/com/tinyengine/it/common/utils/Utils.java +++ b/base/src/main/java/com/tinyengine/it/common/utils/Utils.java @@ -458,6 +458,34 @@ public static T decodeBase64ToObject(String encodedString, Class clazz) { } } + /** + * 判断是资源图还是缩略图 + * + * @param name 名称 + * @return boolean 是否是自愿图 + */ + public static boolean isResource(String name) { + if (name == null) { + return false; + } + int endIndex = Math.min(name.length(), 10); + return !name.substring(0, endIndex).equals("thumbnail_"); + } + + /** + * 判断是否返回下载 + * + * @param name 名称 + * @return boolean 是否下载 + */ + public static boolean isDownload(String name) { + if (name == null) { + return false; + } + int endIndex = Math.min(name.length(), 5); + return name.substring(0, endIndex).equals("image"); + } + /** * 将标准Base64转换为URL安全格式 */ @@ -484,4 +512,5 @@ private static String fromUrlSafe(String urlSafeBase64) { return standard; } + } diff --git a/base/src/main/java/com/tinyengine/it/controller/ResourceController.java b/base/src/main/java/com/tinyengine/it/controller/ResourceController.java index 3a020c03..204b40c1 100644 --- a/base/src/main/java/com/tinyengine/it/controller/ResourceController.java +++ b/base/src/main/java/com/tinyengine/it/controller/ResourceController.java @@ -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.Utils; import com.tinyengine.it.model.entity.Resource; import com.tinyengine.it.service.material.ResourceService; import io.swagger.v3.oas.annotations.Operation; @@ -292,7 +293,6 @@ public Result detail(@PathVariable Integer id) { * 获取资源 * * @param name the name - * @param isResource the isResource * @return the result */ @Operation(summary = "获取资源", description = "获取资源", @@ -305,28 +305,31 @@ public Result detail(@PathVariable Integer id) { @ApiResponse(responseCode = "400", description = "请求失败") }) @SystemControllerLog(description = "获取资源") - @GetMapping("/resource/download") - public void getResource(@RequestParam String name, @RequestParam boolean isResource, - @RequestParam(required = false) boolean isChat, HttpServletResponse response) throws Exception { + @GetMapping("/resource/download/{name}") + public void getResource(@PathVariable String name, + HttpServletResponse response) throws Exception { + if(name == null) { + throw new ServiceException(ExceptionEnum.CM009.getResultCode(),ExceptionEnum.CM009.getResultMsg()); + } Resource resource = resourceService.queryResourceByName(name); if(resource == null) { throw new ServiceException(ExceptionEnum.CM009.getResultCode(),ExceptionEnum.CM009.getResultMsg()); } - String base64Data = isResource ? resource.getResourceData() : resource.getThumbnailData(); + + String base64Data = Utils.isResource(name) ? resource.getResourceData() : resource.getThumbnailData(); String cleanBase64 = ImageThumbnailGenerator.extractCleanBase64(base64Data); byte[] imageBytes = Base64.getDecoder().decode(cleanBase64); String detectedType = ImageThumbnailGenerator.extractContentType(base64Data); - String fileName = isResource ? resource.getName() : "thumbnail_" + resource.getName(); // URL编码文件名 - String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8) + String encodedFileName = URLEncoder.encode(name, StandardCharsets.UTF_8) .replace("+", "%20"); response.setContentType(detectedType); // 只使用 filename* 格式,避免中文字符直接出现在header中 response.setHeader("Content-Disposition", "inline; filename*=UTF-8''" + encodedFileName); - if(isChat){ + if(Utils.isDownload(name)){ response.setHeader("Content-Disposition", "attachment ; filename*=UTF-8''" + encodedFileName); } try (OutputStream out = response.getOutputStream()) { diff --git a/base/src/main/java/com/tinyengine/it/model/entity/Resource.java b/base/src/main/java/com/tinyengine/it/model/entity/Resource.java index c055e1d6..310acef1 100644 --- a/base/src/main/java/com/tinyengine/it/model/entity/Resource.java +++ b/base/src/main/java/com/tinyengine/it/model/entity/Resource.java @@ -43,6 +43,9 @@ public class Resource extends BaseEntity { @Schema(name = "name", description = "名称") private String name; + @Schema(name = "thumbnailName", description = "缩略图名称") + private String thumbnailName; + @Schema(name = "resourceUrl", description = "资源url") private String resourceUrl; diff --git a/base/src/main/java/com/tinyengine/it/service/material/impl/ResourceServiceImpl.java b/base/src/main/java/com/tinyengine/it/service/material/impl/ResourceServiceImpl.java index c868c174..7ff8879e 100644 --- a/base/src/main/java/com/tinyengine/it/service/material/impl/ResourceServiceImpl.java +++ b/base/src/main/java/com/tinyengine/it/service/material/impl/ResourceServiceImpl.java @@ -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.Utils; import com.tinyengine.it.mapper.ResourceGroupResourceMapper; import com.tinyengine.it.mapper.ResourceMapper; import com.tinyengine.it.model.entity.Resource; @@ -61,7 +62,7 @@ public List queryAllResource() { * 模糊查询表Resource信息 * * @param name the name - * @param des the des + * @param des the des * @return Resource信息列表 */ @Override @@ -93,7 +94,12 @@ public Result queryResourceById(Integer id) { public Resource queryResourceByName(String name) { QueryWrapper queryWrapper = new QueryWrapper<>(); - queryWrapper.eq("name", name); + if (Utils.isResource(name)) { + queryWrapper.eq("name", name); + } else { + queryWrapper.eq("thumbnail_name", name); + } + queryWrapper.eq("app_id", loginUserContext.getAppId()); return this.baseMapper.selectOne(queryWrapper); @@ -175,17 +181,22 @@ public Resource createResource(Resource resource) throws Exception { @SystemServiceLog(description = "图片上传") public Resource resourceUpload(Resource resource) { String imageName = Instant.now().toEpochMilli() + resource.getName(); - resource.setName(imageName); - String resourceData = resource.getResourceData(); - String tinyEngineUrl = System.getenv("TINY_ENGINE_URL"); - String encodedName = URLEncoder.encode(imageName, StandardCharsets.UTF_8); - String resourceUrl = tinyEngineUrl + "?name=" + encodedName + "&isResource=" + true; - String thumbnailUrl = tinyEngineUrl + "?name=" + encodedName + "&isResource=" + false; if (resource.getCategory() == null) { - resourceUrl = tinyEngineUrl + "?name=" + encodedName + "&isResource=" + true + "&isChat=" + true; - thumbnailUrl = tinyEngineUrl + "?name=" + encodedName + "&isResource=" + false + "&isChat=" + true; resource.setCategory("image"); + imageName = "image" + Instant.now().toEpochMilli() + resource.getName(); } + + String thumbnailName = "thumbnail_" + imageName; + resource.setName(imageName); + resource.setThumbnailName(thumbnailName); + String resourceData = resource.getResourceData(); + String tinyEngineUrl = System.getenv("TINY_ENGINE_URL"); + String imageEncodedName = URLEncoder.encode(imageName, StandardCharsets.UTF_8); + String thumbnailEncodedName = URLEncoder.encode(thumbnailName, StandardCharsets.UTF_8); + + String resourceUrl = tinyEngineUrl + "/" + imageEncodedName; + String thumbnailUrl = tinyEngineUrl + "/" + thumbnailEncodedName; + if (!StringUtils.isEmpty(resourceData)) { resource.setResourceUrl(resourceUrl); resource.setThumbnailUrl(thumbnailUrl); @@ -225,5 +236,4 @@ public List createBatchResource(List resources) throws Excep } return resourceList; } - } diff --git a/base/src/main/resources/mappers/ResourceGroupMapper.xml b/base/src/main/resources/mappers/ResourceGroupMapper.xml index 8a8c1d04..d352fc81 100644 --- a/base/src/main/resources/mappers/ResourceGroupMapper.xml +++ b/base/src/main/resources/mappers/ResourceGroupMapper.xml @@ -105,6 +105,7 @@ + @@ -142,6 +143,7 @@ r.app_id as resource_app_id, r.platform_id as resource_platform_id, r.name as resource_name, + r.thumbnail_name, r.resource_url, r.thumbnail_url, r.category, @@ -189,6 +191,7 @@ r.app_id as resource_app_id, r.platform_id as resource_platform_id, r.name as resource_name, + r.thumbnail_name, r.resource_url, r.thumbnail_url, r.category, @@ -239,6 +242,7 @@ r.app_id as resource_app_id, r.platform_id as resource_platform_id, r.name as resource_name, + r.thumbnail_name, r.resource_url, r.thumbnail_url, r.category, diff --git a/base/src/main/resources/mappers/ResourceMapper.xml b/base/src/main/resources/mappers/ResourceMapper.xml index 1ff0fa73..2bf0c081 100644 --- a/base/src/main/resources/mappers/ResourceMapper.xml +++ b/base/src/main/resources/mappers/ResourceMapper.xml @@ -7,7 +7,7 @@ id - , app_id, platform_id, `name`, resource_url, thumbnail_url, category, description, thumbnail_data, resource_data, + , app_id, platform_id, `name`, thumbnail_name, resource_url, thumbnail_url, 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 @@ -19,10 +19,12 @@ AND platform_id = #{platformId} - AND `name` = #{name} + + AND thumbnail_name = #{thumbnailName} + AND resource_url = #{resourceUrl} @@ -81,6 +83,9 @@ `name` = #{name}, + + thumbnail_name = #{thumbnailName}, + resource_url = #{resourceUrl}, @@ -135,6 +140,7 @@ + @@ -246,6 +252,7 @@ , app_id , platform_id , name + , thumbnail_name , resource_url , thumbnail_url , category @@ -265,6 +272,7 @@ , #{appId} , #{platformId} , #{name} + , #{thumbnailName} , #{resourceUrl} , #{thumbnailUrl} , #{category} diff --git a/docker-deploy-data/mysql/init/create_resources_ddl_2025_0902.sql b/docker-deploy-data/mysql/init/create_resources_ddl_2025_0902.sql index 4dda7424..00e6e670 100644 --- a/docker-deploy-data/mysql/init/create_resources_ddl_2025_0902.sql +++ b/docker-deploy-data/mysql/init/create_resources_ddl_2025_0902.sql @@ -6,6 +6,7 @@ create table `t_resource` `app_id` int not null comment '关联appId', `platform_id` int not null comment '关联设计器id', `name` varchar(255) not null comment '名称', + `thumbnail_name` varchar(255) not null comment '缩略图名称', `resource_url` varchar(255) comment '资源url', `thumbnail_url` varchar(255) comment '缩略图url', `category` varchar(255) not null comment '分类',