-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
178 lines (150 loc) · 3.79 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
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/iikira/BaiduPCS-Go/pcsutil"
"github.com/iikira/BaiduPCS-Go/pcsutil/converter"
"github.com/iikira/BaiduPCS-Go/requester"
"github.com/iikira/BaiduPCS-Go/requester/rio"
"github.com/iikira/BaiduPCS-Go/requester/uploader"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
)
const (
// Version 版本号
Version = "v1.0"
)
var (
isOverWrite = false
printVersion = false
outputDir string
)
func init() {
flag.BoolVar(&isOverWrite, "w", false, "overwrite")
flag.BoolVar(&printVersion, "v", false, "print version")
flag.StringVar(&outputDir, "o", ".", "output directory")
flag.Parse()
if printVersion {
fmt.Printf("TinyPNG client, Version %s\n", Version)
fmt.Println("Copyright (c) 2017-2018, iikira/tinypng: https://github.com/iikira/tinypng")
os.Exit(0)
}
pcsutil.SetLogPrefix()
}
func main() {
if len(os.Args) <= 1 {
flag.Usage()
return
}
for k := range flag.Args() {
for_1:
for errTimes := 0; errTimes < 3; errTimes++ {
code := do(flag.Arg(k))
switch code {
case 0:
fallthrough
case 1: // 系统错误
break for_1
case 2: // 网络错误
fallthrough
case 3: // json 解析错误
continue
}
}
}
}
type UploadedData struct {
Input InputData `json:"input"`
Output OutputData `json:"output"`
Error string `json:"error"`
Message string `json:"message"`
}
type InputData struct {
Size int64 `json:"size"`
Type string `json:"type"`
}
type OutputData struct {
InputData
Width uint `json:"width"`
Height uint `json:"height"`
Ratio float64 `json:"ratio"`
URL string `json:"url"`
}
func do(filename string) (code int) {
var savePath string
if isOverWrite {
savePath = filename
} else {
savePath = filepath.Clean(outputDir + string(os.PathSeparator) + filepath.Dir(filename) + string(os.PathSeparator) + "tinified-" + filepath.Base(filename))
_, err := os.Stat(savePath)
if err == nil { // 文件已存在
log.Printf("[%s] 已存在\n", savePath)
return 1
}
}
log.Printf("[%s] 正在上传图片\n", filename)
file, err := os.OpenFile(filename, os.O_RDONLY, 0666)
if err != nil {
log.Println(err)
return 1
}
// 保留权限
mode := os.FileMode(0666)
info, err := file.Stat()
if err == nil {
mode = info.Mode()
}
uploader.DoUpload("https://tinypng.com/web/shrink", rio.NewFileReaderLen64(file), func(resp *http.Response, err error) {
file.Close()
fmt.Println()
if err != nil {
log.Println(err)
code = 2
return
}
imgJSON, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
code = 1
return
}
resp.Body.Close()
data := new(UploadedData)
err = json.Unmarshal(imgJSON, data)
if err != nil {
log.Println(err)
code = 3
return
}
if data.Error != "" {
log.Printf("[%s] Error, %s: %s\n", filename, data.Error, data.Message)
code = 1
return
}
log.Printf("[%s] 上传图片成功, 正在下载压缩后的图片...\n", filename)
img, err := requester.Fetch("GET", data.Output.URL, nil, nil)
if err != nil {
log.Println(err)
code = 2
return
}
imgLen := int64(len(img))
if imgLen != data.Output.Size {
log.Printf("[%s] 图片下载失败, 文件大小不一致, 已下载: %d, 远程: %d\n", filename, imgLen, data.Output.Size)
code = 2
return
}
err = ioutil.WriteFile(savePath, img, mode)
if err != nil {
log.Println(err)
code = 1
return
}
log.Printf("[%s] 图片保存成功, 保存位置: %s, 图片类型: %s, 图片宽度: %d, 图片高度: %d, 原始图片大小: %s, 压缩后图片大小: %s, 压缩比率: %f%%\n", filename, savePath, data.Output.Type, data.Output.Width, data.Output.Height, converter.ConvertFileSize(data.Input.Size), converter.ConvertFileSize(data.Output.Size), data.Output.Ratio*100)
})
return code
}