songhuangfeng123
2022-07-27 fb8d22263bf8b5a3d5b1e4f62556c8b2c2ba1b4b
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package com.gkhy.safePlatform.minioDemo.service;
 
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.commons.vo.ResultVO;
import com.gkhy.safePlatform.minioDemo.config.MinioConfig;
import com.gkhy.safePlatform.minioDemo.enums.ModuleTypeEnums;
import com.gkhy.safePlatform.minioDemo.utils.FileUtil;
import io.minio.GetPresignedObjectUrlArgs;
import io.minio.MinioClient;
import io.minio.errors.*;
import io.minio.http.Method;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.*;
 
import static com.gkhy.safePlatform.minioDemo.enums.ModuleTypeEnums.*;
 
@Service
public class FileService {
 
    @Resource
    private MinioConfig minioConfig;
 
    @Autowired
    private MinioClient minioClient;
 
    @Resource
    private FileUtil fileUtil;
 
    /**
     * 文件下载
     * @param fileName
     * @param response
     */
    public void downloadFile(String fileName, HttpServletResponse response) {
        fileUtil.downloadFile(fileName,response);
    }
 
    /**
     * 文件上传
     * @param file
     * @param moduleType
     * @return
     */
    public ResultVO<String> uploadFile(MultipartFile file, Integer moduleType) {
        String url = "";
        String moduleName = initModuleName(moduleType);
 
        // 文件名转换
        String time = new SimpleDateFormat("yyyyMMdd").format(new Date());
        String fileSuffixName = Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf(".")+1);
        String fileName = moduleName + "/" + time + "_" + initUUID()+"."+fileSuffixName;
        try {
            url=fileUtil.upload(file, fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultVO<>(ResultCodes.OK, url);
    }
 
    /**
     * 模块名称转换
     */
    private String initModuleName(Integer moduleType){
        String moduleName = "";
        ModuleTypeEnums moduleTypeEnums = ModuleTypeEnums.getReviewStatus(moduleType);
        assert moduleTypeEnums != null;
        switch (moduleTypeEnums) {
            case EMERGENCY:
                moduleName = EMERGENCY.getModuleName();
                break;
            case EQUIPMENT:
                moduleName = EQUIPMENT.getModuleName();
                break;
            case GOAL_MANAGE:
                moduleName = GOAL_MANAGE.getModuleName();
                break;
            case INCIDENT_MANAGE:
                moduleName = INCIDENT_MANAGE.getModuleName();
                break;
            default:
                break;
        }
        return moduleName;
    }
 
    /**
     * 生成8位UUID
     */
    private String initUUID() {
        String[] chars = new String[]{"a", "b", "c", "d", "e", "f",
                "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
                "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
                "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
                "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
                "W", "X", "Y", "Z"};
        StringBuffer shortBuffer = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < 8; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[x % 0x3E]);
        }
        return shortBuffer.toString();
    }
 
 
 
 
 
    public String getPutUrl(String objName) {
        String url = null;
        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(objName)
                    .expiry(60 * 2)
                    .extraQueryParams(reqParams)
                    .build());
        } catch (ErrorResponseException e) {
            e.printStackTrace();
        } catch (InsufficientDataException e) {
            e.printStackTrace();
        } catch (InternalException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidResponseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (XmlParserException e) {
            e.printStackTrace();
        } catch (ServerException e) {
            e.printStackTrace();
        }
        return url;
    }
 
 
}