办学质量监测教学评价系统
ageer
2024-04-01 dea23f13ef2d4918080ce8aeee1ee908188cdb19
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
package com.xmzs.midjourney.support;
 
import cn.hutool.core.text.CharSequenceUtil;
import com.xmzs.midjourney.ProxyProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
 
@Component
@RequiredArgsConstructor
public class DiscordHelper {
    private final ProxyProperties properties;
    /**
     * DISCORD_SERVER_URL.
     */
    public static final String DISCORD_SERVER_URL = "https://discord.com";
    /**
     * DISCORD_CDN_URL.
     */
    public static final String DISCORD_CDN_URL = "https://cdn.discordapp.com";
    /**
     * DISCORD_WSS_URL.
     */
    public static final String DISCORD_WSS_URL = "wss://gateway.discord.gg";
    /**
     * DISCORD_UPLOAD_URL.
     */
    public static final String DISCORD_UPLOAD_URL = "https://discord-attachments-uploads-prd.storage.googleapis.com";
 
    public String getServer() {
        if (CharSequenceUtil.isBlank(this.properties.getNgDiscord().getServer())) {
            return DISCORD_SERVER_URL;
        }
        String serverUrl = this.properties.getNgDiscord().getServer();
        if (serverUrl.endsWith("/")) {
            serverUrl = serverUrl.substring(0, serverUrl.length() - 1);
        }
        return serverUrl;
    }
 
    public String getCdn() {
        if (CharSequenceUtil.isBlank(this.properties.getNgDiscord().getCdn())) {
            return DISCORD_CDN_URL;
        }
        String cdnUrl = this.properties.getNgDiscord().getCdn();
        if (cdnUrl.endsWith("/")) {
            cdnUrl = cdnUrl.substring(0, cdnUrl.length() - 1);
        }
        return cdnUrl;
    }
 
    public String getWss() {
        if (CharSequenceUtil.isBlank(this.properties.getNgDiscord().getWss())) {
            return DISCORD_WSS_URL;
        }
        String wssUrl = this.properties.getNgDiscord().getWss();
        if (wssUrl.endsWith("/")) {
            wssUrl = wssUrl.substring(0, wssUrl.length() - 1);
        }
        return wssUrl;
    }
 
    public String getDiscordUploadUrl(String uploadUrl) {
        if (CharSequenceUtil.isBlank(this.properties.getNgDiscord().getUploadServer()) || CharSequenceUtil.isBlank(uploadUrl)) {
            return uploadUrl;
        }
        String uploadServer = this.properties.getNgDiscord().getUploadServer();
        if (uploadServer.endsWith("/")) {
            uploadServer = uploadServer.substring(0, uploadServer.length() - 1);
        }
        return uploadUrl.replaceFirst(DISCORD_UPLOAD_URL, uploadServer);
    }
 
    public String findTaskIdWithCdnUrl(String url) {
        if (!CharSequenceUtil.startWith(url, DISCORD_CDN_URL)) {
            return null;
        }
        int hashStartIndex = url.lastIndexOf("/");
        String taskId = CharSequenceUtil.subBefore(url.substring(hashStartIndex + 1), ".", true);
        if (CharSequenceUtil.length(taskId) == 16) {
            return taskId;
        }
        return null;
    }
 
    public String getMessageHash(String imageUrl) {
        if (CharSequenceUtil.isBlank(imageUrl)) {
            return null;
        }
        if (CharSequenceUtil.endWith(imageUrl, "_grid_0.webp")) {
            int hashStartIndex = imageUrl.lastIndexOf("/");
            if (hashStartIndex < 0) {
                return null;
            }
            return CharSequenceUtil.sub(imageUrl, hashStartIndex + 1, imageUrl.length() - "_grid_0.webp".length());
        }
        int hashStartIndex = imageUrl.lastIndexOf("_");
        if (hashStartIndex < 0) {
            return null;
        }
        return CharSequenceUtil.subBefore(imageUrl.substring(hashStartIndex + 1), ".", true);
    }
 
}