-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
imagedownloader.go
167 lines (150 loc) · 4.46 KB
/
imagedownloader.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
package imagemounter
import (
"fmt"
"github.com/Masterminds/semver"
"github.com/danielpaulus/go-ios/ios"
"github.com/danielpaulus/go-ios/ios/zipconduit"
log "github.com/sirupsen/logrus"
"io"
"net/http"
"os"
"path"
"path/filepath"
"strings"
)
const repo = "https://github.com/haikieu/xcode-developer-disk-image-all-platforms/raw/master/DiskImages/iPhoneOS.platform/DeviceSupport/%s.zip"
const imagepath = "devimages"
const developerDiskImageDmg = "DeveloperDiskImage.dmg"
var availableVersions = []string{"10.0", "10.1", "10.2", "10.3", "11.0", "11.1", "11.2", "11.3", "11.4", "12.0", "12.1", "12.2", "12.3", "13.0", "13.1", "13.1.2", "13.2", "13.3", "13.4", "13.5", "13.6", "13.7", "14.0", "14.2", "14.3", "14.4", "14.5", "14.6", "14.7", "15.0", "15.2", "8.0", "8.1", "8.2", "8.3", "8.4", "9.0", "9.1", "9.2", "9.3"}
const v12_2 = "12.2 (16E226)"
func MatchAvailable(version string) string {
log.Debugf("device version: %s ", version)
ver := semver.MustParse(version)
var bestMatch string
var lastVersion string
for _, availableVersion := range availableVersions {
parsedAV := semver.MustParse(availableVersion)
if ver.Major() > parsedAV.Major() {
continue
}
if ver.Major() < parsedAV.Major() && bestMatch == "" {
bestMatch = lastVersion
break
}
lastVersion = availableVersion
if ver.Major() < parsedAV.Major() {
continue
}
if ver.Minor() == parsedAV.Minor() && ver.Patch() == parsedAV.Patch() {
bestMatch = availableVersion
break
}
if ver.Minor() == parsedAV.Minor() {
bestMatch = availableVersion
continue
}
}
log.Debugf("device version: %s bestMatch: %s", version, bestMatch)
if bestMatch == "12.2" {
return v12_2
}
return bestMatch
}
func DownloadImageFor(device ios.DeviceEntry, baseDir string) (string, error) {
allValues, err := ios.GetValues(device)
if err != nil {
return "", err
}
version := MatchAvailable(allValues.Value.ProductVersion)
log.Infof("getting developer image for iOS %s", version)
imageDownloaded, err := validateBaseDirAndLookForImage(baseDir, version)
if err != nil {
return "", err
}
if imageDownloaded != "" {
log.Infof("%s already downloaded from https://github.com/haikieu/", imageDownloaded)
return imageDownloaded, nil
}
downloadUrl := fmt.Sprintf(repo, version)
log.Infof("downloading from: %s", downloadUrl)
log.Info("thank you haikieu for making these images available :-)")
zipFileName := path.Join(baseDir, imagepath, fmt.Sprintf("%s.zip", version))
err = downloadFile(zipFileName, downloadUrl)
if err != nil {
return "", err
}
files, size, err := zipconduit.Unzip(zipFileName, path.Join(baseDir, imagepath))
if err != nil {
return "", err
}
err = os.Remove(zipFileName)
if err != nil {
log.Warnf("failed deleting: '%s' with err: %+v", zipFileName, err)
}
log.Infof("downloaded: %+v totalbytes: %d", files, size)
downloadedDmgPath, err := findImage(path.Join(baseDir, imagepath), version)
if err != nil {
return "", err
}
os.RemoveAll(path.Join(baseDir, imagepath, "__MACOSX"))
log.Infof("Done extracting: %s", downloadedDmgPath)
return downloadedDmgPath, nil
}
func findImage(dir string, version string) (string, error) {
imageToFind := fmt.Sprintf("%s/%s", version, developerDiskImageDmg)
var imageWeFound string
err := filepath.Walk(dir,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if strings.HasSuffix(path, imageToFind) {
imageWeFound = path
}
return nil
})
if err != nil {
return "", err
}
if imageWeFound != "" {
return imageWeFound, nil
}
return "", fmt.Errorf("image not found")
}
func validateBaseDirAndLookForImage(baseDir string, version string) (string, error) {
images := path.Join(baseDir, imagepath)
dirHandle, err := os.Open(images)
defer dirHandle.Close()
if err != nil {
err := os.MkdirAll(images, 0777)
if err != nil {
return "", err
}
return "", nil
}
dmgPath, err := findImage(baseDir, version)
if err != nil {
return "", nil
}
return dmgPath, nil
}
// DownloadFile will download a url to a local file. It's efficient because it will
// write as it downloads and not load the whole file into memory.
// PS: Taken from golangcode.com
func downloadFile(filepath string, url string) error {
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
return err
}