-
Notifications
You must be signed in to change notification settings - Fork 375
/
offline_helper.go
330 lines (294 loc) · 8.16 KB
/
offline_helper.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package publish_item
import (
"archive/zip"
"bytes"
"encoding/json"
"image"
"image/jpeg"
"image/png"
"io/ioutil"
"mime/multipart"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/andrianbdn/iospng"
"github.com/pkg/errors"
"github.com/shogo82148/androidbinary/apk"
"github.com/sirupsen/logrus"
"howett.net/plist"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/modules/dicehub/dbclient"
"github.com/erda-project/erda/pkg/template"
)
var (
reInfoPlist = regexp.MustCompile(`Payload/[^/]+/Info\.plist`)
ErrNoIcon = errors.New("icon not found")
)
type IOSPlist struct {
CFBundleName string `plist:"CFBundleName"`
CFBundleDisplayName string `plist:"CFBundleDisplayName"`
CFBundleVersion string `plist:"CFBundleVersion"`
CFBundleShortVersion string `plist:"CFBundleShortVersionString"`
CFBundleIdentifier string `plist:"CFBundleIdentifier"`
CFBundleIcons *CFBundleIcons `plist:"CFBundleIcons"`
}
type CFBundleIcons struct {
CFBundlePrimaryIcon *CFBundlePrimaryIcon `plist:"CFBundlePrimaryIcon"`
}
type CFBundlePrimaryIcon struct {
CFBundleIconFiles []string `plist:"CFBundleIconFiles"`
CFBundleIconName string `plist:"CFBundleIconName"`
}
type IosAppInfo struct {
Name string
BundleId string
Version string
Build string
Icon image.Image
Size int64
IconName string
}
type AndroidAppInfo struct {
PackageName string
Version string
VersionCode int32
Icon image.Image
DisplayName string
}
// isOffLineVersion 判断版本是不是离线包上传的版本
func isOffLineVersion(version *dbclient.PublishItemVersion) bool {
var meta map[string]interface{}
if err := json.Unmarshal([]byte(version.Meta), &meta); err != nil {
logrus.Errorf("Judge is OffLineVersion err: %v", err)
return false
}
if meta["projectName"].(string) == "OFFLINE" {
return true
}
return false
}
func (i *PublishItem) UploadFileFromReader(fileHeader *multipart.FileHeader) (*apistructs.File, error) {
fileName := fileHeader.Filename
if fileName == "" {
fileName = "file"
}
reader, err := fileHeader.Open()
if err != nil {
return nil, err
}
defer reader.Close()
resp, err := i.bdl.UploadFile(apistructs.FileUploadRequest{
From: "release",
IsPublic: true,
FileReader: reader,
FileNameWithExt: fileName,
})
if err != nil {
return nil, err
}
// 兖矿临时修改
v := strings.ReplaceAll(resp.DownloadURL, "http://dice.paas.ykjt.cc", "https://appstore.ykjt.cn")
v = strings.ReplaceAll(v, "https://dice.paas.ykjt.cc", "https://appstore.ykjt.cn")
resp.DownloadURL = v
return resp, nil
}
func (i *PublishItem) UploadFileFromFile(filePath string) (*apistructs.File, error) {
f, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer f.Close()
resp, err := i.bdl.UploadFile(apistructs.FileUploadRequest{
From: "release",
IsPublic: true,
FileReader: f,
FileNameWithExt: filepath.Base(filePath),
})
if err != nil {
return nil, err
}
// 兖矿临时修改
v := strings.ReplaceAll(resp.DownloadURL, "http://dice.paas.ykjt.cc", "https://appstore.ykjt.cn")
v = strings.ReplaceAll(v, "https://dice.paas.ykjt.cc", "https://appstore.ykjt.cn")
resp.DownloadURL = v
return resp, nil
}
func GetAndoridInfo(fileHeader *multipart.FileHeader) (*AndroidAppInfo, error) {
info := &AndroidAppInfo{}
formFile, err := fileHeader.Open()
if err != nil {
return nil, err
}
defer formFile.Close()
pkg, err := apk.OpenZipReader(formFile, fileHeader.Size)
if err != nil {
return nil, err
}
defer pkg.Close()
info.Version = pkg.Manifest().VersionName.MustString()
info.VersionCode = pkg.Manifest().VersionCode.MustInt32()
info.DisplayName = pkg.Manifest().App.Name.MustString()
info.PackageName = pkg.PackageName()
icon, err := pkg.Icon(nil)
if err != nil {
return nil, err
}
info.Icon = icon
return info, nil
}
func GetIosInfo(fileHeader *multipart.FileHeader) (*IosAppInfo, error) {
formFile, err := fileHeader.Open()
if err != nil {
return nil, err
}
defer formFile.Close()
reader, err := zip.NewReader(formFile, fileHeader.Size)
if err != nil {
return nil, err
}
var plistFile *zip.File
for _, f := range reader.File {
if reInfoPlist.MatchString(f.Name) {
plistFile = f
break
}
}
info, err := parseIpaFile(plistFile)
if err != nil {
return nil, err
}
var iosIconFile *zip.File
for _, f := range reader.File {
if strings.Contains(f.Name, info.IconName) {
iosIconFile = f
break
}
}
icon, err := parseIpaIcon(iosIconFile)
if err != nil {
return nil, err
}
info.Icon = icon
info.Size = fileHeader.Size
return info, nil
}
func parseIpaFile(plistFile *zip.File) (*IosAppInfo, 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(IosAppInfo)
if p.CFBundleDisplayName == "" {
info.Name = p.CFBundleName
} else {
info.Name = p.CFBundleDisplayName
}
info.BundleId = p.CFBundleIdentifier
info.Version = p.CFBundleShortVersion
info.Build = p.CFBundleVersion
if p.CFBundleIcons != nil &&
p.CFBundleIcons.CFBundlePrimaryIcon != nil &&
p.CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles != nil &&
len(p.CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles) > 0 {
files := p.CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles
info.IconName = files[len(files)-1]
} else {
info.IconName = "Icon.png"
}
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()))
}
func GenerateInstallPlist(info *IosAppInfo, downloadUrl string) string {
plistTemplate := `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>items</key>
<array>
<dict>
<key>assets</key>
<array>
<dict>
<key>kind</key>
<string>software-package</string>
<key>url</key>
<string>{{appUrl}}</string>
</dict>
</array>
<key>metadata</key>
<dict>
<key>bundle-identifier</key>
<string>{{bundleId}}</string>
<key>bundle-version</key>
<string>{{version}}</string>
<key>kind</key>
<string>software</string>
<key>subtitle</key>
<string>{{displayName}}</string>
<key>title</key>
<string>{{displayName}}</string>
</dict>
</dict>
</array>
</dict>
</plist>`
plistContent := template.Render(plistTemplate, map[string]string{
"bundleId": info.BundleId,
"version": info.Version,
"displayName": info.Name,
"appUrl": downloadUrl,
})
return plistContent
}
func GenrateTmpImagePath(id string) string {
return "/tmp/logo-" + id + ".jpg"
}
func SaveImageToFile(icon image.Image, path string) error {
out, err := os.Create(path)
if err != nil {
return err
}
defer out.Close()
var opt jpeg.Options
opt.Quality = 80
err = jpeg.Encode(out, icon, &opt) // put quality to 80%
return err
}