-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatbox.go
109 lines (94 loc) · 3.72 KB
/
catbox.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
package catbox
import (
"bytes"
"errors"
"fmt"
"io"
"mime/multipart"
"net"
"net/http"
"time"
)
// UploadFile uploads a file from a bytes.Buffer to CatBox.
// The fileName parameter specifies the name of the file to be uploaded.
// The timeout parameter specifies how long the client should wait for the server to respond.
// The userHash parameter is optional and can be used to upload files to a specific user account.
// The function returns the URL of the uploaded file or an error if the upload failed.
func UploadFile(fileBuffer *bytes.Buffer, fileName string, timeout time.Duration, userHash string) (string, error) {
var buf bytes.Buffer
writer := multipart.NewWriter(&buf)
if part, err := writer.CreateFormFile("fileToUpload", fileName); err != nil {
return "", fmt.Errorf("failed to create form file: %w", err)
} else if _, err = io.Copy(part, fileBuffer); err != nil {
return "", fmt.Errorf("failed to copy file content: %w", err)
}
_ = writer.WriteField("reqtype", "fileupload")
if userHash != "" {
_ = writer.WriteField("userHash", userHash)
}
_ = writer.Close()
req, err := http.NewRequest("POST", "https://catbox.moe/user/api.php", &buf)
if err != nil {
return "", fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", writer.FormDataContentType())
client := &http.Client{Timeout: timeout}
resp, err := client.Do(req)
if err != nil {
var ne net.Error
if errors.As(err, &ne) && ne.Timeout() {
return "", fmt.Errorf("upload request timed out after %d seconds", int(timeout.Seconds()))
}
return "", fmt.Errorf("failed to connect to Catbox: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return "", fmt.Errorf("HTTP error occurred: %s - %s", resp.Status, string(body))
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read response body: %w", err)
}
return string(body), nil
}
// UploadToLitterBox uploads a file from a bytes.Buffer to LitterBox.
// The fileName parameter specifies the name of the file to be uploaded.
// The duration parameter specifies how long the file should be stored on the server, Options: '1h', '12h', '24h', '72h', '1w'.
// The timeout parameter specifies how long the client should wait for the server to respond.
func UploadToLitterBox(fileBuffer *bytes.Buffer, fileName string, duration string, timeout time.Duration) (string, error) {
var buf bytes.Buffer
writer := multipart.NewWriter(&buf)
if part, err := writer.CreateFormFile("fileToUpload", fileName); err != nil {
return "", fmt.Errorf("failed to create form file: %w", err)
} else if _, err = io.Copy(part, fileBuffer); err != nil {
return "", fmt.Errorf("failed to copy file content: %w", err)
}
_ = writer.WriteField("reqtype", "fileupload")
_ = writer.WriteField("time", duration)
_ = writer.Close()
req, err := http.NewRequest("POST", "https://litterbox.catbox.moe/resources/internals/api.php", &buf)
if err != nil {
return "", fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", writer.FormDataContentType())
client := &http.Client{Timeout: timeout}
resp, err := client.Do(req)
if err != nil {
var ne net.Error
if errors.As(err, &ne) && ne.Timeout() {
return "", fmt.Errorf("upload to Litterbox timed out after %d seconds", int(timeout.Seconds()))
}
return "", fmt.Errorf("failed to connect to Litterbox: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return "", fmt.Errorf("HTTP error occurred: %s - %s", resp.Status, string(body))
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read response body: %w", err)
}
return string(body), nil
}