From 7bb85618a633e5d1bd9d21f54b7a0f5d2b3da801 Mon Sep 17 00:00:00 2001 From: youahng <2998465706@qq.com> Date: Mon, 12 Aug 2024 10:52:03 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B5=84=E6=BA=90=E6=9B=B4=E6=96=B002?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../teaching/backend/constant/TypeEnum.java | 81 +++++++++++++++++++ .../model/dto/resource/ResourceUploadDto.java | 3 +- .../impl/resource/ResourceServiceImpl.java | 2 +- .../teaching/backend/utils/MinioUtils.java | 3 +- 4 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/teaching/backend/constant/TypeEnum.java diff --git a/src/main/java/com/teaching/backend/constant/TypeEnum.java b/src/main/java/com/teaching/backend/constant/TypeEnum.java new file mode 100644 index 0000000..259d229 --- /dev/null +++ b/src/main/java/com/teaching/backend/constant/TypeEnum.java @@ -0,0 +1,81 @@ +package com.teaching.backend.constant; + +import org.apache.commons.lang3.StringUtils; + +import java.util.HashMap; +import java.util.Map; + +/** + * @Author:youhang + * @Date:2024-08-07-9:14 + * @Description: + */ +public enum TypeEnum { + DEFAULT("default",0), + PNG("png", 1), + JPEG("jpeg", 2), + JPG("jpg", 3), + GIF("gif", 4), + DOCX("docx", 5), + PDF("pdf", 6), + MD("md", 7), + XLSX("xlsx", 8), + TXT("txt", 9), + WBMP("wbmp", 10), + TIFF("tiff", 11), + JFIF("jfif", 12), + TIF("tif", 13), + FAX("fax", 14), + JPE("jpe", 15), + NET("net", 16), + RP("rp", 17), + ICO("ico", 18), + MP4("mp4", 19); + + + private String prefix; + private Integer type; + + private static final Map ENUM_MAP = new HashMap<>(); + + static { + TypeEnum[] values = values(); + for (TypeEnum value : values) { + ENUM_MAP.put(value.getPrefix(), value); + } + } + + + + TypeEnum(String prefix, Integer type) { + this.prefix = prefix; + this.type = type; + } + + public String getPrefix() { + return prefix; + } + public Integer getType() { + return type; + } + + + public static Integer getTypeByPrefix(String prefix) { + if(StringUtils.isEmpty(prefix)){ + return DEFAULT.getType(); + } + prefix = prefix.substring(prefix.lastIndexOf(".") + 1); + + TypeEnum viewContentTypeEnum = ENUM_MAP.get(prefix); + if (viewContentTypeEnum == null) { + return DEFAULT.getType(); + } + Integer type = viewContentTypeEnum.getType(); + if (type != null) { + return type; + } + return DEFAULT.getType(); + } + + +} diff --git a/src/main/java/com/teaching/backend/model/dto/resource/ResourceUploadDto.java b/src/main/java/com/teaching/backend/model/dto/resource/ResourceUploadDto.java index 9f669ef..02fc3aa 100644 --- a/src/main/java/com/teaching/backend/model/dto/resource/ResourceUploadDto.java +++ b/src/main/java/com/teaching/backend/model/dto/resource/ResourceUploadDto.java @@ -18,8 +18,7 @@ public class ResourceUploadDto { private String name; - private Integer num; - + private Integer type; private String objectName; } diff --git a/src/main/java/com/teaching/backend/service/impl/resource/ResourceServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/resource/ResourceServiceImpl.java index d49c716..53378a0 100644 --- a/src/main/java/com/teaching/backend/service/impl/resource/ResourceServiceImpl.java +++ b/src/main/java/com/teaching/backend/service/impl/resource/ResourceServiceImpl.java @@ -78,7 +78,7 @@ public class ResourceServiceImpl implements ResourceService { //添加到图数据库中 Resources resource = new Resources(); - resource.setType(resourceUploadDto.getNum()); + resource.setType(resourceUploadDto.getType()); resource.setName(objectName); resource.setStatus(0); resource.setUrl(url); diff --git a/src/main/java/com/teaching/backend/utils/MinioUtils.java b/src/main/java/com/teaching/backend/utils/MinioUtils.java index e2d4671..32f908a 100644 --- a/src/main/java/com/teaching/backend/utils/MinioUtils.java +++ b/src/main/java/com/teaching/backend/utils/MinioUtils.java @@ -5,6 +5,7 @@ import cn.hutool.json.JSONUtil; import com.teaching.backend.common.BaseResponse; import com.teaching.backend.common.ErrorCode; import com.teaching.backend.common.ResultUtils; +import com.teaching.backend.constant.TypeEnum; import com.teaching.backend.constant.ViewContentTypeEnum; import com.teaching.backend.model.dto.resource.BucketPolicyConfigDto; import com.teaching.backend.model.dto.resource.ResourceUploadDto; @@ -92,7 +93,7 @@ public class MinioUtils { minioUploadDto.setName(filename); minioUploadDto.setUrl(ENDPOINT + "/" + BUCKET_NAME + "/" + objectName); minioUploadDto.setObjectName(objectName); - minioUploadDto.setNum(ViewContentTypeEnum.DEFAULT.getNumByName(objectName)); + minioUploadDto.setType(TypeEnum.getTypeByPrefix(objectName)); } catch (Exception e) { e.printStackTrace(); LOGGER.info("上传发生错误: {}!", e.getMessage());