-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
258 lines (217 loc) · 6.64 KB
/
main.go
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package main
import (
"crypto/md5"
"fmt"
"html"
"io"
"net/http"
"net/url"
"os"
"path"
"regexp"
"strings"
)
func main() {
// Check if HTML file is provided as argument
if len(os.Args) < 2 {
fmt.Println("Usage: go run main.go <html-file> :3")
os.Exit(1)
}
htmlFile := os.Args[1]
// Read HTML content from file
data, err := os.ReadFile(htmlFile)
if err != nil {
fmt.Printf("Error reading file: %v\n", err)
os.Exit(1)
}
content := string(data)
// Create "static" directory if it doesn't exist
if _, err := os.Stat("static"); os.IsNotExist(err) {
err = os.Mkdir("static", 0755)
if err != nil {
fmt.Printf("Error creating static directory: %v\n", err)
os.Exit(1)
}
fmt.Println("Created directory: static :3")
}
// Improved regex to match Discord CDN image URLs
// Match the URL up to any HTML tag character
re := regexp.MustCompile(`https://cdn\.discordapp\.com/[^<>"'\s]+`)
matches := re.FindAllString(content, -1)
// Map to avoid duplicate downloads and track replacements
replacements := make(map[string]string)
// Process found URLs
for _, rawURL := range matches {
// Skip if already processed
if _, exists := replacements[rawURL]; exists {
continue
}
// Clean the URL from any trailing HTML but keep all query parameters
cleanedURL := extractCompleteURL(rawURL)
// Remove trailing ampersand if present
if strings.HasSuffix(cleanedURL, "&") {
cleanedURL = cleanedURL[:len(cleanedURL)-1]
}
// Decode HTML entities like &
decodedURL := html.UnescapeString(cleanedURL)
// Print the full URL we're attempting to download
fmt.Printf("Downloading: %s\n", decodedURL)
// Try the file with different URL variants if needed
filename, err := tryDownloadWithVariants(decodedURL)
if err != nil {
fmt.Printf("Error downloading %s: %v\n", decodedURL, err)
continue
}
localPath := "static/" + filename
replacements[rawURL] = localPath
// Also map the cleaned and decoded URL to the same local path
if cleanedURL != rawURL {
replacements[cleanedURL] = localPath
}
if decodedURL != cleanedURL {
replacements[decodedURL] = localPath
}
fmt.Printf("Will replace with local file: %s\n", localPath)
}
// Sort URLs by length (longest first) to avoid partial replacements
var urls []string
for url := range replacements {
urls = append(urls, url)
}
for i := 0; i < len(urls); i++ {
for j := i + 1; j < len(urls); j++ {
if len(urls[i]) < len(urls[j]) {
urls[i], urls[j] = urls[j], urls[i]
}
}
}
// Perform the replacements in order
for _, url := range urls {
content = strings.ReplaceAll(content, url, replacements[url])
fmt.Printf("Replaced URL: %s -> %s\n", url, replacements[url])
}
// Write the updated HTML content back to the file
err = os.WriteFile(htmlFile, []byte(content), 0644)
if err != nil {
fmt.Printf("Error writing updated file: %v\n", err)
os.Exit(1)
}
fmt.Println("HTML file updated successfully!")
}
// extractCompleteURL properly extracts the complete Discord CDN URL
// keeping ALL query parameters intact
func extractCompleteURL(rawURL string) string {
// Define common HTML markers that might terminate the URL
htmlTerminators := []string{"<", ">", "\"", "'", "</a>", "</span>"}
cleanURL := rawURL
// Find the earliest HTML terminator position
earliestPos := len(cleanURL)
for _, marker := range htmlTerminators {
pos := strings.Index(cleanURL, marker)
if pos > 0 && pos < earliestPos {
earliestPos = pos
}
}
// Trim at the earliest HTML terminator
if earliestPos < len(cleanURL) {
cleanURL = cleanURL[:earliestPos]
}
return cleanURL
}
// tryDownloadWithVariants attempts to download with different URL variants
// such as with/without trailing ampersand
func tryDownloadWithVariants(imageURL string) (string, error) {
variants := []string{imageURL}
// Add a variant without a trailing ampersand if needed
if strings.HasSuffix(imageURL, "&") {
variants = append(variants, imageURL[:len(imageURL)-1])
}
// Try all URL variants
var lastErr error
for _, variant := range variants {
filename, err := downloadImage(variant)
if err == nil {
return filename, nil
}
lastErr = err
fmt.Printf("Failed with variant %s: %v, trying next...\n", variant, err)
}
return "", lastErr
}
// downloadImage downloads the image from imageURL and saves it in the "static" folder
// with a hash-based filename to prevent path length issues
func downloadImage(imageURL string) (string, error) {
// Create a client with timeout that correctly handles redirects
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return nil
},
}
// Perform HTTP GET request to fetch the image
req, err := http.NewRequest("GET", imageURL, nil)
if err != nil {
return "", err
}
// Add a user agent
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("bad status: %s", resp.Status)
}
// Parse the URL to get the file extension
parsedURL, err := url.Parse(imageURL)
if err != nil {
return "", err
}
// Get the file extension from the URL path
ext := path.Ext(parsedURL.Path)
if ext == "" {
// If no extension found, try to determine from Content-Type
contentType := resp.Header.Get("Content-Type")
ext = getExtensionFromContentType(contentType)
}
// Generate a short filename using MD5 hash of the URL
hasher := md5.New()
hasher.Write([]byte(imageURL))
hash := fmt.Sprintf("%x", hasher.Sum(nil))
// Create a short filename with the hash and extension
filename := hash + ext
// Create the file in the "static" directory
filePath := "static/" + filename
out, err := os.Create(filePath)
if err != nil {
return "", err
}
defer out.Close()
// Write image content to file
_, err = io.Copy(out, resp.Body)
if err != nil {
return "", err
}
return filename, nil
}
// getExtensionFromContentType tries to determine file extension from MIME type
func getExtensionFromContentType(contentType string) string {
switch {
case strings.Contains(contentType, "image/jpeg"):
return ".jpg"
case strings.Contains(contentType, "image/png"):
return ".png"
case strings.Contains(contentType, "image/gif"):
return ".gif"
case strings.Contains(contentType, "image/webp"):
return ".webp"
case strings.Contains(contentType, "video/mp4"):
return ".mp4"
case strings.Contains(contentType, "video/quicktime"):
return ".mov"
case strings.Contains(contentType, "audio/mpeg"):
return ".mp3"
default:
return ".bin" // Generic binary extension as fallback
}
}