-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupload_founds.go
90 lines (75 loc) · 1.98 KB
/
upload_founds.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"time"
)
// upload founds
func uploadFounds(apiKey string) error {
fmt.Println("Upload Founds:\n")
filePath, hashPlaintext := selectFile()
var file io.Reader
var filename string
if filePath == "PASTE" {
file = bytes.NewBufferString(hashPlaintext)
filename = time.Now().Format("150405") + ".txt"
} else if filePath == "" {
return nil
} else {
f, err := os.Open(filePath)
if err != nil {
return fmt.Errorf("An error occurred: failed to open file: %v", err)
}
defer f.Close()
file = f
filename = filePath
}
fmt.Println("Enter algorithm ID:")
var algo string
fmt.Scanln(&algo)
var requestBody bytes.Buffer
writer := multipart.NewWriter(&requestBody)
_ = writer.WriteField("key", apiKey)
_ = writer.WriteField("algo", algo)
filePart, err := writer.CreateFormFile("userfile", filename)
if err != nil {
return fmt.Errorf("An error occurred: failed to create form file: %v", err)
}
_, err = io.Copy(filePart, file)
if err != nil {
return fmt.Errorf("An error occurred: failed to copy file: %v", err)
}
err = writer.Close()
if err != nil {
return fmt.Errorf("An error occurred: failed to close writer: %v", err)
}
url := "https://hashes.com/en/api/founds"
req, err := http.NewRequest(http.MethodPost, url, &requestBody)
if err != nil {
return fmt.Errorf("An error occurred: failed to create request: %v", err)
}
req.Header.Set("Content-Type", writer.FormDataContentType())
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("An error occurred: failed to send request: %v", err)
}
defer resp.Body.Close()
var response struct {
Success bool `json:"success"`
}
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return fmt.Errorf("An error occurred: failed to decode response: %v", err)
}
if !response.Success {
return fmt.Errorf("Upload failed")
}
fmt.Println("Upload Successful")
return nil
}