郑永安
2023-06-19 7a6abd05683528032687c75e80e0bd2030a3e46c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.gkhy.safePlatform.specialWork.service.impl;
 
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.gkhy.safePlatform.commons.config.file.MinioConfig;
import com.gkhy.safePlatform.commons.enums.E;
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.commons.exception.AusinessException;
import com.gkhy.safePlatform.commons.utils.StringUtils;
import com.gkhy.safePlatform.commons.utils.UUIDUtil;
import com.gkhy.safePlatform.commons.vo.ResultVO;
import com.gkhy.safePlatform.specialWork.service.SpecialWorkMinoService;
 
import io.minio.GetPresignedObjectUrlArgs;
import io.minio.MinioClient;
import io.minio.RemoveObjectArgs;
import io.minio.RemoveObjectsArgs;
import io.minio.errors.*;
import io.minio.http.Method;
import io.minio.messages.DeleteObject;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.*;
 
 
@Service
public class SpecialWorkMinoServiceImpl implements SpecialWorkMinoService {
 
    private Logger logger = LoggerFactory.getLogger(this.getClass());
 
    @Autowired
    private MinioClient minioClient;
 
    @Autowired
    private MinioConfig minioConfig;
 
    @Value("${specailworkmino.floders.specialwork}")
    private String minioFloders;
 
    /**
     * 获取对象上传路径
     */
    @Override
    public ResultVO getPresignUrl(String suffixName){
 
        String uuid= UUIDUtil.initUUID();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String objName = formatter.format(new Date())+"-"+uuid+"."+suffixName;
        String url = null;
        Map<String,String> resMap = new HashMap<>();
        resMap.put("fileName",objName);
        try {
            Map<String, String> reqParams = new HashMap<>();
            reqParams.put("response-content-type", "application/json");
            url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
                    .method(Method.PUT)
                    .bucket(minioConfig.getBucketName())
                    .object(minioFloders+objName)
                    .expiry(60*2)
                    .extraQueryParams(reqParams)
                    .build());
            resMap.put("uploadUrl",url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultVO<>(ResultCodes.OK,resMap);
    }
 
    /**
     * @description 获取文件下载地址
     */
    @Override
    public ResultVO viewFile(String fileName){
        String url = null;
        try {
            url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
                    .method(Method.GET)
                    .bucket(minioConfig.getBucketName())
                    .object(minioFloders+fileName)
                    .expiry(60*5).build());
        } catch (Exception e) {
            e.printStackTrace();
        }
        url = minioConfig.getUrlPrefix() + url.substring((url.indexOf("//") + 2)).substring(url.substring((url.indexOf("//") + 2)).indexOf(
                        "/"));
        return new ResultVO(ResultCodes.OK,url);
    }
 
    /**
     * @description 删除文件
     */
    @Override
    public ResultVO deleteFile(String fileName) {
        try {
            minioClient.removeObject(RemoveObjectArgs.builder()
                    .bucket(minioConfig.getBucketName())
                    .object(minioFloders+fileName)
                    .build());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultVO<>(ResultCodes.OK);
    }
 
    /**
     * 批量获取文件
     */
    public String viewFiles(String fileNames){
 
        StringBuffer stringBuffer = new StringBuffer();
        if(StringUtils.isNotBlank(fileNames)){
            String[] str = fileNames.split(",");
            for (int i = 0; i < str.length; i++) {
                String url = null;
                try {
                    url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
                            .method(Method.GET)
                            .bucket(minioConfig.getBucketName())
                            .object(minioFloders+str[i])
                            .expiry(60*5).build());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                url = minioConfig.getUrlPrefix() + url.substring((url.indexOf("//") + 2)).substring(url.substring((url.indexOf("//") + 2)).indexOf(
                        "/"));
                stringBuffer.append(url).append(",");
            }
            stringBuffer.deleteCharAt(stringBuffer.length() - 1);
 
        }
        return stringBuffer.toString();
    }
 
}