diff --git a/luoo_tag/pom.xml b/luoo_tag/pom.xml index 0ecea77..4d39336 100644 --- a/luoo_tag/pom.xml +++ b/luoo_tag/pom.xml @@ -59,6 +59,11 @@ com.h2database h2 + + + software.amazon.awssdk + s3 + app diff --git a/luoo_tag/src/main/java/com/luoo/tag/pojo/Tag.java b/luoo_tag/src/main/java/com/luoo/tag/pojo/Tag.java index 3c1b4ee..75e5fa6 100644 --- a/luoo_tag/src/main/java/com/luoo/tag/pojo/Tag.java +++ b/luoo_tag/src/main/java/com/luoo/tag/pojo/Tag.java @@ -53,6 +53,16 @@ public class Tag implements Serializable { */ private Integer state; + /** + * 说明文案 + */ + private String desc; + + /** + * 图片路径 + */ + private String image; + /** * 创建人用户ID */ diff --git a/luoo_tag/src/main/java/com/luoo/tag/pojo/TagCreateReq.java b/luoo_tag/src/main/java/com/luoo/tag/pojo/TagCreateReq.java index 0771443..ec044c6 100644 --- a/luoo_tag/src/main/java/com/luoo/tag/pojo/TagCreateReq.java +++ b/luoo_tag/src/main/java/com/luoo/tag/pojo/TagCreateReq.java @@ -24,4 +24,10 @@ public class TagCreateReq implements Serializable { @ApiModelProperty(value = "标签英文名") @NotBlank(message = "标签英文名称必填") private String nameEn; + + @ApiModelProperty(value = "文案") + private String desc; + + @ApiModelProperty(value = "图片路径") + private String image; } diff --git a/luoo_tag/src/main/java/com/luoo/tag/pojo/TagDTO.java b/luoo_tag/src/main/java/com/luoo/tag/pojo/TagDTO.java index 1492497..9b002db 100644 --- a/luoo_tag/src/main/java/com/luoo/tag/pojo/TagDTO.java +++ b/luoo_tag/src/main/java/com/luoo/tag/pojo/TagDTO.java @@ -31,6 +31,12 @@ public class TagDTO implements Serializable { @ApiModelProperty(value = "标签状态 0=禁用,1=启用") private Integer state; + @ApiModelProperty(value = "说明文案") + private String desc; + + @ApiModelProperty(value = "图片") + private String image; + @ApiModelProperty(value = "子标签数量") private Long childTagCount = 0L; diff --git a/luoo_tag/src/main/java/com/luoo/tag/pojo/TagUpdateReq.java b/luoo_tag/src/main/java/com/luoo/tag/pojo/TagUpdateReq.java index a65a3b5..326306a 100644 --- a/luoo_tag/src/main/java/com/luoo/tag/pojo/TagUpdateReq.java +++ b/luoo_tag/src/main/java/com/luoo/tag/pojo/TagUpdateReq.java @@ -31,4 +31,10 @@ public class TagUpdateReq implements Serializable { @ApiModelProperty(value = "标签英文名") @NotBlank(message = "标签英文名称必填") private String nameEn; + + @ApiModelProperty(value = "文案") + private String desc; + + @ApiModelProperty(value = "图片路径") + private String image; } diff --git a/luoo_tag/src/main/java/com/luoo/tag/service/TagS3Service.java b/luoo_tag/src/main/java/com/luoo/tag/service/TagS3Service.java new file mode 100644 index 0000000..0641673 --- /dev/null +++ b/luoo_tag/src/main/java/com/luoo/tag/service/TagS3Service.java @@ -0,0 +1,74 @@ +package com.luoo.tag.service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import software.amazon.awssdk.http.SdkHttpResponse; +import software.amazon.awssdk.services.s3.S3Client; +import software.amazon.awssdk.services.s3.model.CopyObjectRequest; +import software.amazon.awssdk.services.s3.model.CopyObjectResponse; +import util.IdWorker; + +import javax.annotation.Resource; + +/** + * ClassName: TagImageUtil + * Description: TagImageUtil + * Date: 2024-02-04 + * + * @author zjb + */ +@Service +public class TagS3Service { + + @Resource + private S3Client s3Client; + + + public static final String DOT = "."; + public static final String TAG_RESOURCE_PREFIX = "http://cdn.indie.cn/tag/"; + public static final String BUCKET = "indie"; + public static final String TEMP_KEY_PREFIX = "temp/"; + + + public String moveTagImage(String srcKey, String oldImage, String tagId) { + String image = oldImage; + String suffix = image.substring(image.lastIndexOf(DOT)); + String destKeySuffix = String.format("%s%s", tagId, suffix); + String destKey = TAG_RESOURCE_PREFIX + destKeySuffix; + int copy = copy(BUCKET, srcKey, destKey); + if (copy > 0) { + return destKeySuffix; + } + return null; + } + + /** + * s3复制文件 + * + * @param bucket 桶 + * @param srcKey 源key + * @param destKey 目标key + * @return - + */ + public int copy(String bucket, String srcKey, String destKey) { + try { + CopyObjectRequest copyObjectRequest = CopyObjectRequest.builder() + .sourceBucket(bucket).sourceKey(srcKey).destinationBucket(bucket).destinationKey(destKey).build(); + CopyObjectResponse copyObjectResponse = s3Client.copyObject(copyObjectRequest); + SdkHttpResponse sdkHttpResponse = copyObjectResponse.sdkHttpResponse(); + if (!sdkHttpResponse.isSuccessful()) { + return -1; + } else { + return 1; + } + } catch (Exception e) { + return -1; + } finally { + s3Client.close(); + } + } + + public static String getSrcKey(String srcFullPath) { + return srcFullPath.substring(srcFullPath.indexOf(TEMP_KEY_PREFIX)); + } +} diff --git a/luoo_tag/src/main/java/com/luoo/tag/service/TagService.java b/luoo_tag/src/main/java/com/luoo/tag/service/TagService.java index 25abbef..9ff24c6 100644 --- a/luoo_tag/src/main/java/com/luoo/tag/service/TagService.java +++ b/luoo_tag/src/main/java/com/luoo/tag/service/TagService.java @@ -40,6 +40,7 @@ public class TagService { private final AdminUserClient userClient; private final JournalTagDao journalTagDao; private final SongTagDao songTagDao; + private final TagS3Service tagS3Service; /** @@ -99,8 +100,15 @@ public class TagService { tag.setLevel(StringUtils.isBlank(parentId) ? TagLevelEnum.L1.getCode() : TagLevelEnum.L2.getCode()); tag.setState(TagStateEnum.ENABLE.getCode()); LocalDateTime now = LocalDateTime.now(); - tag.setCreateTime(now); - tag.setUpdateTime(now); + tag.setCreateTime(now); + tag.setUpdateTime(now); + if (null != createReq.getImage() && !createReq.getImage().isEmpty()) { + tag.setImage(tagS3Service.moveTagImage( + TagS3Service.getSrcKey(createReq.getImage()), createReq.getImage(), tag.getId() + )); + } + + // // UserInfo userInfo = RequestContext.get(); // tag.setCreatorId(userInfo.getId()); @@ -113,6 +121,7 @@ public class TagService { * @param updateReq 更新参数 */ public void update(TagUpdateReq updateReq){ + // TODO: 添加desc和image String id = updateReq.getId(); Optional tagOptional = tagDao.findById(id); AssertUtil.mustTrue(tagOptional.isPresent(), "标签记录不存在"); @@ -134,6 +143,21 @@ public class TagService { tag.setNameEn(updateReq.getNameEn()); tag.setUpdateTime(LocalDateTime.now()); // tag.setUpdaterId(RequestContext.get().getId()); + + String srcKey = null; + if (null == updateReq.getImage()) { + tag.setImage(null); + } + if (updateReq.getImage().contains(TagS3Service.TEMP_KEY_PREFIX)) { + srcKey = updateReq.getImage().substring(updateReq.getImage().indexOf(TagS3Service.TEMP_KEY_PREFIX)); + } + if (StringUtils.isNotBlank(srcKey)) { + tag.setImage( + tagS3Service.moveTagImage(srcKey, updateReq.getImage(), id) + ); + } + + tagDao.save(tag); }