‘liusuyi’
2023-08-10 d954874fad36cb31a910a3bbce8bda04cb6e8be7
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
package com.ruoyi.storage.minio.utils;
 
import com.ruoyi.utils.tools.CmdUtils;
import com.sun.jna.Platform;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @Description:
 * @ClassName: MinioService
 * @Author: 刘苏义
 * @Date: 2023年08月04日10:32:40
 * @Version: 1.0
 **/
@Component
@Slf4j(topic = "cmd")
public class MinioService {
    String processName = "minio.exe";
    @Value("${minio.path}")
    String minioPath;
    @Value("${minio.accessKey}")
    String accessKey;
    @Value("${minio.secretKey}")
    String secretKey;
    @Value("${minio.enabled}")
    Boolean minioEnabled;
 
    @PostConstruct
    public void init() {
        if (minioEnabled) {
            if (Platform.isWindows()) {
                String exePath = System.getProperty("user.dir") + File.separator + "lib" + File.separator + "minio" + File.separator + processName;
                List<String> cmd = new ArrayList<>();
                cmd.add(exePath);
                cmd.add("server");
                cmd.add(minioPath);
                if (CmdUtils.isProcessRunning(processName)) {
                    // 进程已经在运行,结束该进程
                    CmdUtils.stopProcess(processName);
                }
                // 启动后台进程
                Map<String, String> envMap = new HashMap<>();
                envMap.put("MINIO_ROOT_USER", accessKey);
                envMap.put("MINIO_ROOT_PASSWORD", secretKey);
                CmdUtils.commandStart(processName, cmd, envMap);
                // 启动cmd窗口
//            String[] command = {"cmd", "/c", "start", exePath, "-H127.0.0.1:8000", "-o"};
//            CmdUtils.commandStart(command);
            }
        }
    }
 
    @PreDestroy
    public void destroyMediaMtx() {
        if (minioEnabled) {
            log.info("销毁minio");
            if (CmdUtils.isProcessRunning(processName)) {
                // 进程已经在运行,结束该进程
                CmdUtils.stopProcess(processName);
            }
        }
    }
}