‘liusuyi’
2023-09-21 9c3645f8239c596838def5c9951ce714fd000c57
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package com.ruoyi.storage.minio.service.impl;
 
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
 
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
 
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.storage.minio.domain.jsonbean.*;
import com.ruoyi.utils.process.CmdUtils;
import com.sun.jna.Platform;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.ruoyi.storage.minio.mapper.StorageMinioEventMapper;
import com.ruoyi.storage.minio.domain.StorageMinioEvent;
import com.ruoyi.storage.minio.service.IStorageMinioEventService;
 
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
 
/**
 * 存储事件Service业务层处理
 *
 * @author ard
 * @date 2023-08-05
 */
@Service
@Slf4j(topic = "minio")
public class StorageMinioEventServiceImpl implements IStorageMinioEventService {
 
    @Resource
    private StorageMinioEventMapper storageMinioEventMapper;
    @Value("${minio.accessKey}")
    String accessKey;
    @Value("${minio.secretKey}")
    String secretKey;
    @Value("${minio.path}")
    String path;
    @Value("${minio.enabled}")
    Boolean enabled;
    String processName = "minio.exe";
    @PostConstruct
    public void initMinio() {
        if (enabled) {
            log.debug("初始化启动minio");
 
            if (Platform.isWindows()) {
                String exePath = System.getProperty("user.dir") + File.separator + "lib" + File.separator + "minio" + File.separator + processName;
                Map<String, String> env=new HashMap<>();
                env.put("MINIO_ROOT_USER",accessKey);
                env.put("MINIO_ROOT_PASSWORD",secretKey);
                List<String> cmd = new ArrayList<>();
                cmd.add(exePath);
                cmd.add("server");
                cmd.add(path);
                cmd.add("--console-address=0.0.0.0:9000");
                cmd.add("--address=0.0.0.0:9001");
                if (CmdUtils.isProcessRunning(processName)) {
                    // 进程已经在运行,结束该进程
                    CmdUtils.stopProcess(processName);
                }
                // 启动后台进程
                CmdUtils.commandStart(processName, cmd, env);
                // 启动cmd窗口
                //String[] command = {"cmd", "/c", "start", exePath};
                //CmdUtils.commandStart(command);
            }
        }
    }
    @PreDestroy
    public void destroyMinio() {
        if (enabled) {
            log.info("销毁minio");
            if (CmdUtils.isProcessRunning(processName)) {
                // 进程已经在运行,结束该进程
                CmdUtils.stopProcess(processName);
            }
        }
    }
 
    /**
     * 查询存储事件
     *
     * @param id 存储事件主键
     * @return 存储事件
     */
    @Override
    public StorageMinioEvent selectStorageMinioEventById(String id) {
        return storageMinioEventMapper.selectStorageMinioEventById(id);
    }
 
    /**
     * 查询存储事件列表
     *
     * @param storageMinioEvent 存储事件
     * @return 存储事件
     */
    @Override
    public List<StorageMinioEvent> selectStorageMinioEventList(StorageMinioEvent storageMinioEvent) {
        return storageMinioEventMapper.selectStorageMinioEventList(storageMinioEvent);
    }
 
    /**
     * 新增存储事件
     *
     * @param storageMinioEvent 存储事件
     * @return 结果
     */
    @Override
    public int insertStorageMinioEvent(StorageMinioEvent storageMinioEvent) {
        storageMinioEvent.setId(IdUtils.simpleUUID());
        storageMinioEvent.setCreateTime(DateUtils.getNowDate());
        return storageMinioEventMapper.insertStorageMinioEvent(storageMinioEvent);
    }
 
    /**
     * 修改存储事件
     *
     * @param storageMinioEvent 存储事件
     * @return 结果
     */
    @Override
    public int updateStorageMinioEvent(StorageMinioEvent storageMinioEvent) {
        return storageMinioEventMapper.updateStorageMinioEvent(storageMinioEvent);
    }
 
    /**
     * 批量删除存储事件
     *
     * @param ids 需要删除的存储事件主键
     * @return 结果
     */
    @Override
    public int deleteStorageMinioEventByIds(String[] ids) {
        return storageMinioEventMapper.deleteStorageMinioEventByIds(ids);
    }
 
    /**
     * 删除存储事件信息
     *
     * @param id 存储事件主键
     * @return 结果
     */
    @Override
    public int deleteStorageMinioEventById(String id) {
        return storageMinioEventMapper.deleteStorageMinioEventById(id);
    }
 
    @Async
    @Override
    public void parseStorageMinioEvent(String message) {
        try {
            JsonsRootBean jsonsRootBean = JSONObject.parseObject(message, JsonsRootBean.class);
            if (jsonsRootBean != null) {
                Records records = jsonsRootBean.getRecords().get(0);
                StorageMinioEvent storageMinioEvent = new StorageMinioEvent();
                storageMinioEvent.setEventTime(records.getEventTime());
                String eventType = records.getEventName().substring(0, records.getEventName().indexOf(":", records.getEventName().indexOf(":") + 1));//不包含本身位置
                storageMinioEvent.setEventType(eventType);
                storageMinioEvent.setBucketName(records.getS3().getBucket().getName());
                String encode = null;
                try {
                    encode = URLDecoder.decode(records.getS3().getMObject().getKey(), "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                storageMinioEvent.setObjectName(encode);
                storageMinioEvent.setObjectSize(records.getS3().getMObject().getSize());
                storageMinioEvent.setObjectType(records.getS3().getMObject().getContentType());
                storageMinioEvent.setHost(records.getSource().getHost());
                storageMinioEvent.setEndpoint(records.getResponseElements().getXMinioOriginEndpoint());
                storageMinioEvent.setUserName(records.getRequestParameters().getPrincipalid());
 
                int i = insertStorageMinioEvent(storageMinioEvent);
                if (i > 0) {
                    log.debug("minio操作日志入库成功!【" + storageMinioEvent.getEventType() + "】");
                }
            }
        } catch (Exception ex) {
            log.error("minio事件格式化异常:" + ex.getMessage());
        }
    }
}