-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
279 lines (258 loc) · 7.53 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package main
import (
"crypto/md5"
"fmt"
"github.com/skip2/go-qrcode"
"gopkg.in/ini.v1"
"imgSearch/framework/db"
"imgSearch/framework/httpServer"
"imgSearch/framework/public"
"io"
"math/rand"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
)
func main() {
//task := goticker.New(100, false)
//_ = task.AddTaskCallBackFunc(fileCoinUpFile, 10, "cli") // 每间隔3秒执行一次 fileCoinUpFile 函数
//_ = task.AddTaskCallBackFunc(qrRandom, 60*5, "cli") // 每间隔8秒执行一次 qrRandom
//_ = task.AddTaskCallBackFunc(doImg, 60*1, "cli")
httpServer.Start()
}
var mutex sync.Mutex
//filecoin文件上传
func fileCoinUpFile(param interface{}) {
mutex.Lock()
defer mutex.Unlock()
//读取配置文件
cfg, err := ini.Load("./config.ini")
if err != nil {
fmt.Println("文件读取错误", err)
return
}
minerIDs := cfg.Section("").Key("minerID")
rows, err := db.Query_sql("select name,id,info,cid from dfs where info='0' limit 1")
if err == nil {
var name string
var id string
var info string
var cid string
if rows.Next() {
_ = rows.Scan(&name, &id, &info, &cid)
rows.Close()
if info == "1" {
println("文件已经上传过:", name)
} else {
println("上传文件:", name)
dir, err := filepath.Abs(name)
if err != nil {
println("文件不存在:", err)
return
}
println("上传文件到filecoin:", dir)
res := public.ExecShell("lotus client import " + dir)
res = strings.Replace(res, "\n", "", -1) //去掉尾部换行符
println("shell res :", res)
//println("--:", strings.Index(res, "Import"))
if strings.Index(res, "Import") == -1 {
println("上传出错:" + dir)
return
}
resSplit := strings.Split(res, " ")
if len(resSplit) > 2 {
println("cid: ", resSplit[3])
cid = resSplit[3]
}
}
if cid == "0" {
println("上传出错,获取不到CID")
return
}
//更新上传状态
_, err := db.Update_sql("update dfs set info='1' where id='" + id + "'")
//更新CID
RowsAffected, err := db.Update_sql("update dfs set cid='" + cid + "' where id='" + id + "'")
if err == nil {
if RowsAffected > 0 {
println("上传到filcoin成功: ", cid)
println("开始交易: ", cid)
println("minerIDs:", minerIDs.Value())
minerIDSplit := strings.Split(minerIDs.Value(), ",")
for _, val := range minerIDSplit {
if strings.Replace(val, " ", "", -1) != "" {
println("执行脚本:", "lotus client deal "+cid+" "+val+" 1afil 1036800")
res_deal := public.ExecShell("lotus client deal " + cid + " " + val + " 1afil 1036800")
res_deal = strings.Replace(res_deal, "\n", "", -1) //去掉尾部换行符
if len(res_deal) == 59 {
println("交易成功")
} else {
println("交易失败:", res_deal)
}
}
}
}
} else {
println("上传到filcoin失败: ", cid)
}
} else {
//println("暂无新数据")
}
} else {
println(err.Error())
}
//matex.Unlock() //解锁
}
var mutex_doImg sync.Mutex
func doImg(param interface{}) {
mutex_doImg.Lock()
defer mutex_doImg.Unlock()
//读取配置文件
cfg, err := ini.Load("./config.ini")
if err != nil {
fmt.Println("文件读取错误", err)
return
}
minerIDs := cfg.Section("").Key("minerID") //minerID集合
//读取数据库文件
rows, err := db.Query_sql("select name,id from img_tab where state=0 limit 1")
var name string
var id int64
var dir = ""
if rows.Next() {
_ = rows.Scan(&name, &id)
rows.Close()
dir = name
//lsimg := strings.Replace(dir, ".jpg", ".png", 1)
//_ = os.Remove(lsimg)
}
//var FileInfo []os.FileInfo
//relativePath := "./upload/tmpimg/"
//if FileInfo, err = ioutil.ReadDir(relativePath); err != nil {
// fmt.Println("读取 img 文件夹出错")
// return
//}
//
//var dir = ""
//
//for _, fileInfo := range FileInfo {
// fmt.Println("fileInfoName:", fileInfo.Name())
// fmt.Println("fileInfoSize:", fileInfo.Size())
// if fileInfo.Size() > (1024 * 1024 * 15) {
// dir = relativePath + fileInfo.Name()
// lsimg := strings.Replace(dir, ".jpg", ".png", 1)
// println("删除:", lsimg)
// _ = os.Remove(lsimg)
// break
// }
//}
if dir == "" {
//println("没有新文件")
return
}
//println("上传文件到filecoin:", dir)
res := public.ExecShell("lotus client import " + dir)
res = strings.Replace(res, "\n", "", -1) //去掉尾部换行符
println("shell res :", res)
//println("--:", strings.Index(res, "Import"))
if strings.Index(res, "Import") == -1 {
println("上传出错:" + dir)
return
}
var cid = "0"
resSplit := strings.Split(res, " ")
if len(resSplit) > 2 {
//println("cid: ", resSplit[3])
cid = resSplit[3]
}
if cid == "0" {
println("上传出错,获取不到CID")
return
}
println("上传到filcoin成功: ", cid)
//更新数据库
update_id, err := db.Update_sql("update img_tab set state=1,cid='" + cid + "' where id=" + strconv.FormatInt(id, 10) + "")
if update_id > 0 {
//println("更新数据库成功:" + dir)
}
println("开始交易: ", cid)
//println("minerIDs:", minerIDs.Value())
minerIDSplit := strings.Split(minerIDs.Value(), ",")
for _, val := range minerIDSplit {
if strings.Replace(val, " ", "", -1) != "" {
//println("执行脚本:", "lotus client deal "+cid+" "+val+" 1afil 1050000")
println("执行脚本:", "lotus client deal "+cid+" "+val+" 0.0000000001FIL 1040000")
res_deal := public.ExecShell("lotus client deal " + cid + " " + val + " 0.0000000001 1040000")
res_deal = strings.Replace(res_deal, "\n", "", -1) //去掉尾部换行符
if len(res_deal) == 59 {
println("交易成功")
} else {
println("交易失败:", res_deal)
}
}
}
}
var mutex_qrRandom sync.Mutex
//生成随机图片
func qrRandom(param interface{}) {
mutex_qrRandom.Lock()
defer mutex_qrRandom.Unlock()
strRandom := getRandomString(256)
strRandomMd5 := md5str(strRandom)
//println("成功的随机数:", strRandomMd5)
imgName := "./upload/tmpimg/" + strRandomMd5 + ".png"
//生成二维码
qrcode.WriteFile(strRandom, qrcode.Medium, 100, imgName)
time.Sleep(time.Second * 3)
//合并文件
println("开始合并文件")
public.ExecShell("cat ./upload/tmp/tmp.iso >> " + imgName + "")
println("合并文件成功", imgName)
////合成照片
//tmpimgB, _ := os.Open("./upload/tmp/tmp.jpg")
//tmpimg, _ := jpeg.Decode(tmpimgB)
//defer tmpimgB.Close()
//
//hcImgB, _ := os.Open(imgName)
//hcImg, _ := png.Decode(hcImgB)
//defer hcImgB.Close()
//
//offset := image.Pt(0, 0)
//b := tmpimg.Bounds()
//m := image.NewRGBA(b)
//draw.Draw(m, b, tmpimg, image.ZP, draw.Src)
//draw.Draw(m, hcImg.Bounds().Add(offset), hcImg, image.ZP, draw.Over)
//imgNewName := "./upload/tmpimg/" + strRandomMd5 + ".jpg"
//imgw, _ := os.Create(imgNewName)
//jpeg.Encode(imgw, m, &jpeg.Options{100})
//defer imgw.Close()
//把图片信息写入数据库
insert_id, _ := db.Insert_sql("insert into img_tab (name) values ('" + imgName + "')")
if insert_id > 0 {
println("插入数据库成功:" + imgName)
} else {
println("插入数据库失败:" + imgName)
}
}
//字符串 md5 和 SHA-256 的校验
func md5str(data string) string {
h := md5.New()
io.WriteString(h, data)
sum := fmt.Sprintf("%x", h.Sum(nil))
return sum
}
//生成随机数
func getRandomString(l int) string {
str := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
bytes := []byte(str)
result := []byte{}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < l; i++ {
result = append(result, bytes[r.Intn(len(bytes))])
}
return string(result)
}
func ss() {
}