Skip to content

Commit 6205540

Browse files
committed
v1.0
1 parent 37f1989 commit 6205540

File tree

5 files changed

+212
-85
lines changed

5 files changed

+212
-85
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99

1010
# Output of the go coverage tool, specifically when used with LiteIDE
1111
*.out
12+
out/
1213
tinypng
1314

1415
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
1516
.glide/
17+
18+
# Others
1619
.DS_Store

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Go语言编写的 TinyPNG 客户端 v1.0
2+
3+
# 使用方法
4+
```
5+
tinypng 1.png 2.jpg 3.png
6+
tinypng -w 1.png 2.jpg 3.png
7+
```

build.sh

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
name="tinypng"
2+
version="v1.0"
3+
4+
output="out/"
5+
6+
Build() {
7+
echo "Building $1..."
8+
export GOOS=$2 GOARCH=$3 GO386=sse2 CGO_ENABLED=0
9+
if [ $2 = "windows" ];then
10+
go build -ldflags "-s -w" -o "$output/$1/$name.exe"
11+
else
12+
go build -ldflags "-s -w" -o "$output/$1/$name"
13+
fi
14+
15+
Pack $1
16+
}
17+
18+
ArmBuild() {
19+
echo "Building $1..."
20+
export GOOS=$2 GOARCH=$3 GOARM=$4 CGO_ENABLED=1
21+
go build -ldflags '-s -w -linkmode=external -extldflags=-pie' -o "$output/$1/$name"
22+
if [ $2 = "darwin" -a $3 = "arm64" ];then
23+
ldid -S "$output/$1/$name"
24+
fi
25+
26+
Pack $1
27+
}
28+
29+
# 打包
30+
Pack() {
31+
mkdir "$output/$1/download"
32+
cp README.md "$output/$1"
33+
34+
cd $output
35+
zip -q -r "$1.zip" "$1"
36+
37+
rm -rf "$1"
38+
cd ..
39+
}
40+
41+
# android
42+
export NDK_INSTALL=$ANDROID_NDK_ROOT/bin
43+
# CC=$NDK_INSTALL/arm-linux-androideabi-4.9/bin/arm-linux-androideabi-gcc ArmBuild $name-$version"-android-16-armv5" android arm 5
44+
# CC=$NDK_INSTALL/arm-linux-androideabi-4.9/bin/arm-linux-androideabi-gcc ArmBuild $name-$version"-android-16-armv6" android arm 6
45+
CC=$NDK_INSTALL/arm-linux-androideabi-4.9/bin/arm-linux-androideabi-gcc ArmBuild $name-$version"-android-16-armv7" android arm 7
46+
CC=$NDK_INSTALL/aarch64-linux-android-4.9/bin/aarch64-linux-android-gcc ArmBuild $name-$version"-android-21-arm64" android arm64 7
47+
CC=$NDK_INSTALL/i686-linux-android-4.9/bin/i686-linux-android-gcc ArmBuild $name-$version"-android-16-386" android 386 7
48+
CC=$NDK_INSTALL/x86_64-linux-android-4.9/bin/x86_64-linux-android-gcc ArmBuild $name-$version"-android-21-amd64" android amd64 7
49+
50+
# ios
51+
CC=/usr/local/go/misc/ios/clangwrap.sh ArmBuild $name-$version"-darwin-ios-5.0-armv7" darwin arm 7
52+
CC=/usr/local/go/misc/ios/clangwrap.sh ArmBuild $name-$version"-darwin-ios-5.0-arm64" darwin arm64 7
53+
54+
# OS X / macOS
55+
Build $name-$version"-darwin-osx-amd64" darwin amd64
56+
# Build $name-$version"-darwin-osx-386" darwin 386
57+
58+
# Windows
59+
Build $name-$version"-windows-x86" windows 386
60+
Build $name-$version"-windows-x64" windows amd64
61+
62+
# Linux
63+
Build $name-$version"-linux-386" linux 386
64+
Build $name-$version"-linux-amd64" linux amd64
65+
Build $name-$version"-linux-arm" linux arm
66+
Build $name-$version"-linux-arm64" linux arm64
67+
# Build $name-$version"-linux-mips" linux mips
68+
# Build $name-$version"-linux-mips64" linux mips64
69+
# Build $name-$version"-linux-mipsel" linux mipsle
70+
# Build $name-$version"-linux-mips64el" linux mips64le
71+
# Build $name-$version"-linux-ppc64" linux ppc64
72+
# Build $name-$version"-linux-ppc64le" linux ppc64le
73+
# Build $name-$version"-linux-s390x" linux s390x
74+
75+
# other
76+
# $name-$version
77+
# Build $name-$version"-solaris-amd64" solaris amd64
78+
Build $name-$version"-freebsd-386" freebsd 386
79+
# Build $name-$version"-freebsd-amd64" freebsd amd64
80+
# Build $name-$version"-freebsd-arm" freebsd arm
81+
# Build $name-$version"-netbsd-386" netbsd 386
82+
# Build $name-$version"-netbsd-amd64" netbsd amd64
83+
# Build $name-$version"-netbsd-arm" netbsd arm
84+
# Build $name-$version"-openbsd-386" openbsd 386
85+
# Build $name-$version"-openbsd-amd64" openbsd amd64
86+
# Build $name-$version"-openbsd-arm" openbsd arm
87+
# Build $name-$version"-plan9-386" plan9 386
88+
# Build $name-$version"-plan9-amd64" plan9 amd64
89+
# Build $name-$version"-plan9-arm" plan9 arm
90+
# Build $name-$version"-nacl-386" nacl 386
91+
# Build $name-$version"-nacl-amd64p32" nacl amd64p32
92+
# Build $name-$version"-nacl-arm" nacl arm
93+
# Build $name-$version"-dragonflybsd-amd64" dragonfly amd64

convert_size.go

-36
This file was deleted.

main.go

+109-49
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,44 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"flag"
5-
"github.com/bitly/go-simplejson"
6-
"github.com/iikira/baidu-tools/util"
6+
"fmt"
7+
"github.com/iikira/BaiduPCS-Go/pcsutil"
8+
"github.com/iikira/BaiduPCS-Go/requester"
9+
"github.com/iikira/BaiduPCS-Go/uploader"
710
"io/ioutil"
811
"log"
12+
"net/http"
913
"os"
1014
"path/filepath"
1115
)
1216

17+
const (
18+
Version = "v1.0"
19+
)
20+
1321
var (
14-
isOverWrite = flag.Bool("w", false, "over write mode")
22+
isOverWrite = false
23+
printVersion = false
1524
)
1625

1726
func init() {
27+
flag.BoolVar(&isOverWrite, "w", false, "overwrite")
28+
flag.BoolVar(&printVersion, "v", false, "print version")
29+
1830
flag.Parse()
19-
baiduUtil.SetLogPrefix()
20-
baiduUtil.SetTimeout(3e11)
31+
32+
if printVersion {
33+
fmt.Printf("TinyPNG client, Version %s\n", Version)
34+
fmt.Println("Copyright (c) 2017-2018, iikira/tinypng: https://github.com/iikira/tinypng")
35+
os.Exit(0)
36+
}
37+
pcsutil.SetLogPrefix()
2138
}
2239

2340
func main() {
2441
if len(os.Args) <= 1 {
25-
log.Println("请输入参数")
2642
flag.Usage()
2743
return
2844
}
@@ -44,64 +60,108 @@ func main() {
4460
}
4561
}
4662
}
63+
}
4764

65+
type UploadedData struct {
66+
Input InputData `json:"input"`
67+
Output OutputData `json:"output"`
68+
Error string `json:"error"`
69+
Message string `json:"message"`
70+
}
71+
72+
type InputData struct {
73+
Size int64 `json:"size"`
74+
Type string `json:"type"`
75+
}
76+
77+
type OutputData struct {
78+
InputData
79+
Width uint `json:"width"`
80+
Height uint `json:"height"`
81+
Ratio float64 `json:"ratio"`
82+
URL string `json:"url"`
4883
}
4984

5085
func do(filename string) (code int) {
5186
log.Printf("[%s] 正在上传图片\n", filename)
52-
data, err := ioutil.ReadFile(filename)
87+
file, err := os.OpenFile(filename, os.O_RDONLY, 0666)
5388
if err != nil {
5489
log.Println(err)
5590
return 1
5691
}
5792

58-
imgJSON, err := baiduUtil.Fetch("POST", "https://tinypng.com/web/shrink", nil, data, map[string]string{
59-
// "Content-Type": "application/x-www-form-urlencoded",
60-
"Content-Encoding": "gzip",
61-
})
62-
if err != nil {
63-
log.Println(err)
64-
return 2
93+
// 保留权限
94+
mode := os.FileMode(0666)
95+
info, err := file.Stat()
96+
if err == nil {
97+
mode = info.Mode()
6598
}
6699

67-
json, err := simplejson.NewJson(imgJSON)
68-
if err != nil {
69-
log.Println(err)
70-
return 3
71-
}
100+
uploader.DoUpload("https://tinypng.com/web/shrink", false, uploader.NewFileReaderLen(file), func(resp *http.Response, err error) {
101+
file.Close()
102+
fmt.Println()
72103

73-
if j, ok := json.CheckGet("error"); ok {
74-
log.Printf("[%s] Error, %s: %s\n", filename, j.MustString(), json.Get("message").MustString())
75-
return 1
76-
}
104+
if err != nil {
105+
log.Println(err)
106+
code = 2
107+
return
108+
}
77109

78-
outputJSON := json.Get("output")
79-
url := outputJSON.Get("url").MustString()
110+
imgJSON, err := ioutil.ReadAll(resp.Body)
111+
if err != nil {
112+
log.Println(err)
113+
code = 1
114+
return
115+
}
80116

81-
log.Printf("[%s] 上传图片成功, 正在下载压缩后的图片...\n", filename)
82-
img, err := baiduUtil.Fetch("GET", url, nil, nil, nil)
83-
if err != nil {
84-
log.Println(err)
85-
return 2
86-
}
117+
resp.Body.Close()
87118

88-
outputSize := outputJSON.Get("size").MustFloat64()
89-
if len(img) != int(outputSize) {
90-
log.Printf("[%s] 图片下载失败, 文件大小不一致\n", filename)
91-
return 2
92-
}
119+
data := new(UploadedData)
93120

94-
var newName string
95-
if *isOverWrite {
96-
newName = filename
97-
} else {
98-
newName = filepath.Dir(filename) + "/tinified-" + filepath.Base(filename)
99-
}
100-
err = ioutil.WriteFile(newName, img, 0666)
101-
if err != nil {
102-
log.Println(err)
103-
return 1
104-
}
105-
log.Printf("[%s] 图片保存成功, 保存位置: %s, 图片类型: %s, 原始图片大小: %s, 压缩后图片大小: %s, 压缩比率: %f%%\n", filename, newName, outputJSON.Get("type").MustString(), convertSize(json.GetPath("input", "size").MustFloat64()), convertSize(outputSize), outputJSON.Get("ratio").MustFloat64()*100)
106-
return 0
121+
err = json.Unmarshal(imgJSON, data)
122+
if err != nil {
123+
log.Println(err)
124+
code = 3
125+
return
126+
}
127+
128+
if data.Error != "" {
129+
log.Printf("[%s] Error, %s: %s\n", filename, data.Error, data.Message)
130+
code = 1
131+
return
132+
}
133+
134+
log.Printf("[%s] 上传图片成功, 正在下载压缩后的图片...\n", filename)
135+
136+
img, err := requester.DefaultClient.Fetch("GET", data.Output.URL, nil, nil)
137+
if err != nil {
138+
log.Println(err)
139+
code = 2
140+
return
141+
}
142+
143+
imgLen := int64(len(img))
144+
if imgLen != data.Output.Size {
145+
log.Printf("[%s] 图片下载失败, 文件大小不一致, 已下载: %d, 远程: %d\n", filename, imgLen, data.Output.Size)
146+
code = 2
147+
return
148+
}
149+
150+
var newName string
151+
if isOverWrite {
152+
newName = filename
153+
} else {
154+
newName = filepath.Dir(filename) + string(os.PathSeparator) + "tinified-" + filepath.Base(filename)
155+
}
156+
157+
err = ioutil.WriteFile(newName, img, mode)
158+
if err != nil {
159+
log.Println(err)
160+
code = 1
161+
return
162+
}
163+
log.Printf("[%s] 图片保存成功, 保存位置: %s, 图片类型: %s, 图片宽度: %d, 图片高度: %d, 原始图片大小: %s, 压缩后图片大小: %s, 压缩比率: %f%%\n", filename, newName, data.Output.Type, data.Output.Width, data.Output.Height, pcsutil.ConvertFileSize(data.Input.Size), pcsutil.ConvertFileSize(data.Output.Size), data.Output.Ratio*100)
164+
})
165+
166+
return code
107167
}

0 commit comments

Comments
 (0)