-
Notifications
You must be signed in to change notification settings - Fork 3
/
parser.go
165 lines (140 loc) · 3.27 KB
/
parser.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
package parser
import (
"archive/zip"
"bytes"
"errors"
"image"
"image/png"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"github.com/sirupsen/logrus"
"github.com/andrianbdn/iospng"
"github.com/appflight/apkparser"
"howett.net/plist"
)
var (
reInfoPlist = regexp.MustCompile(`Payload/[^/]+/Info\.plist`)
ErrNoIcon = errors.New("icon not found")
)
const (
iosExt = ".ipa"
androidExt = ".apk"
PlatformIos = 1
PlatformAndroid = 2
)
type AppInfo struct {
Name string
BundleId string
Version string
Build string
Platform uint8
Icon image.Image
Size uint64
}
type androidManifest struct {
Package string `xml:"package,attr"`
VersionName string `xml:"versionName,attr"`
VersionCode string `xml:"versionCode,attr"`
}
type iosPlist struct {
CFBundleName string `plist:"CFBundleName"`
CFBundleDisplayName string `plist:"CFBundleDisplayName"`
CFBundleVersion string `plist:"CFBundleVersion"`
CFBundleShortVersion string `plist:"CFBundleShortVersionString"`
CFBundleIdentifier string `plist:"CFBundleIdentifier"`
}
func NewAppParser(name string) (*AppInfo, error) {
file, err := os.Open(name)
if err != nil {
return nil, err
}
defer file.Close()
stat, err := file.Stat()
if err != nil {
return nil, err
}
reader, err := zip.NewReader(file, stat.Size())
if err != nil {
return nil, err
}
var plistFile, iosIconFile *zip.File
for _, f := range reader.File {
switch {
case reInfoPlist.MatchString(f.Name):
plistFile = f
case strings.Contains(f.Name, "AppIcon"):
iosIconFile = f
}
}
ext := filepath.Ext(stat.Name())
if ext == androidExt {
apkInfo, err := apkparser.ParseApk(name)
if err != nil {
logrus.Error("parseApkIconAndLabel err:", err)
}
var info AppInfo
info.Name = apkInfo.Label
info.BundleId = apkInfo.Package
info.Version = apkInfo.VersionName
info.Build = strconv.FormatInt(int64(apkInfo.VersionCode), 10)
info.Platform = PlatformAndroid
info.Icon = apkInfo.Icon
info.Size = uint64(stat.Size())
return &info, err
}
if ext == iosExt {
info, err := parseIpaFile(plistFile)
icon, err := parseIpaIcon(iosIconFile)
info.Icon = icon
info.Size = uint64(stat.Size())
info.Platform = PlatformIos
return info, err
}
return nil, errors.New("unknown platform")
}
func parseIpaFile(plistFile *zip.File) (*AppInfo, error) {
if plistFile == nil {
return nil, errors.New("info.plist not found")
}
rc, err := plistFile.Open()
if err != nil {
return nil, err
}
defer rc.Close()
buf, err := ioutil.ReadAll(rc)
if err != nil {
return nil, err
}
p := new(iosPlist)
decoder := plist.NewDecoder(bytes.NewReader(buf))
if err := decoder.Decode(p); err != nil {
return nil, err
}
info := new(AppInfo)
if p.CFBundleDisplayName == "" {
info.Name = p.CFBundleName
} else {
info.Name = p.CFBundleDisplayName
}
info.BundleId = p.CFBundleIdentifier
info.Version = p.CFBundleShortVersion
info.Build = p.CFBundleVersion
return info, nil
}
func parseIpaIcon(iconFile *zip.File) (image.Image, error) {
if iconFile == nil {
return nil, ErrNoIcon
}
rc, err := iconFile.Open()
if err != nil {
return nil, err
}
defer rc.Close()
var w bytes.Buffer
iospng.PngRevertOptimization(rc, &w)
return png.Decode(bytes.NewReader(w.Bytes()))
}