-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
156 lines (134 loc) · 3.33 KB
/
Copy pathmain.go
File metadata and controls
156 lines (134 loc) · 3.33 KB
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
package main
import (
"bytes"
"database/sql"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
_ "github.com/go-sql-driver/mysql"
)
const numberImages = 6
var configuration struct {
MySQLUser string
MySQLPassoword string
MySQLHost string
MySQLDB string
MySQLQuery string
}
var errors []string
func main() {
//filename is the path to the json config file
file, err := os.Open("config.json")
if err != nil {
fmt.Println(err)
}
decoder := json.NewDecoder(file)
err = decoder.Decode(&configuration)
if err != nil {
fmt.Println(err)
}
db, _ := sql.Open("mysql", configuration.MySQLUser+":"+configuration.MySQLPassoword+"@("+configuration.MySQLHost+")/"+configuration.MySQLDB)
rows, _ := db.Query(configuration.MySQLQuery)
defer rows.Close()
var wg sync.WaitGroup
for rows.Next() {
var ip string
err := rows.Scan(&ip)
if err != nil {
log.Panicln(err)
}
wg.Add(1)
go setImages(ip, &wg)
time.Sleep(time.Millisecond * 50)
}
wg.Wait()
for _, ip := range errors {
fmt.Println("Couldn't connect to: ", ip)
}
}
func setImages(ip string, thisWait *sync.WaitGroup) {
_, err := http.Get("http://" + ip + "/api/config")
if err != nil {
errors = append(errors, ip)
thisWait.Done()
return
}
fmt.Println("posting ", ip)
client := &http.Client{}
var files []string
root := "img"
err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
files = append(files, path)
return nil
})
if err != nil {
fmt.Println(err)
}
files = slicePop(files, 0)
for i := 0; i < numberImages; i++ {
rand.Seed(time.Now().UnixNano())
random := rand.Intn(len(files))
fmt.Println(files[random])
index := strconv.Itoa(i)
writeImage(client, "http://"+ip+"/api/config/splashbackground/"+index, files[random])
files = slicePop(files, random)
time.Sleep(time.Millisecond * 25)
}
thisWait.Done()
}
func writeImage(client *http.Client, url string, filePath string) {
file, err := os.Open(filePath)
if err != nil {
fmt.Println(err)
}
defer file.Close()
var requestBody bytes.Buffer
multiPartWriter := multipart.NewWriter(&requestBody)
fileName := strings.Split(filePath, "/")
fieldWriter, err := multiPartWriter.CreateFormFile("file", fileName[1])
if err != nil {
fmt.Println("****ERROR Could not create request****", err)
return
}
_, err = io.Copy(fieldWriter, file)
if err != nil {
fmt.Println("****ERROR couldnt copy file****", err)
return
}
_, err = fieldWriter.Write([]byte("Value"))
if err != nil {
fmt.Println("****ERROR couldnt write file****", err)
return
}
multiPartWriter.Close()
req, err := http.NewRequest("POST", url, &requestBody)
if err != nil {
fmt.Println("****ERROR could not create reqiest****", err)
return
}
req.Header.Set("Content-Type", multiPartWriter.FormDataContentType())
resp, err := client.Do(req)
if err != nil {
fmt.Println("****ERROR coudn't do request****", err)
return
}
body, err := ioutil.ReadAll(resp.Body)
bodyString := string(body)
fmt.Println(bodyString)
}
func slicePop(slice []string, index int) []string {
slice[index] = slice[len(slice)-1] // Copy last element to index i.
slice[len(slice)-1] = "" // Erase last element (write zero value).
return slice[:len(slice)-1] // Truncate slice.
}