|
| 1 | +package chrome |
| 2 | + |
| 3 | +import ( |
| 4 | + "archive/zip" |
| 5 | + "bytes" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "io/ioutil" |
| 9 | + "net/http" |
| 10 | + "os" |
| 11 | + "path" |
| 12 | + "path/filepath" |
| 13 | + "runtime" |
| 14 | + |
| 15 | + "github.com/pkg/errors" |
| 16 | +) |
| 17 | + |
| 18 | +// ChromiumRevision chromium revision |
| 19 | +const ChromiumRevision = "518818" |
| 20 | + |
| 21 | +// DownloadHost download host |
| 22 | +const DownloadHost = "https://storage.googleapis.com" |
| 23 | + |
| 24 | +var downloadURLs = map[string]string{ |
| 25 | + "linux": "%s/chromium-browser-snapshots/Linux_x64/%s/chrome-linux.zip", |
| 26 | + "mac": "%s/chromium-browser-snapshots/Mac/%s/chrome-mac.zip", |
| 27 | + "win32": "%s/chromium-browser-snapshots/Win/%s/chrome-win32.zip", |
| 28 | + "win64": "%s/chromium-browser-snapshots/Win_x64/%s/chrome-win32.zip", |
| 29 | +} |
| 30 | + |
| 31 | +// Find locally or remotely |
| 32 | +func Find(dir string) (string, error) { |
| 33 | + filepath, err := chromiumPath(dir) |
| 34 | + if err != nil { |
| 35 | + return "", errors.Wrapf(err, "error getting chromium path") |
| 36 | + } |
| 37 | + |
| 38 | + if _, err := os.Stat(filepath); !os.IsNotExist(err) { |
| 39 | + return filepath, nil |
| 40 | + } |
| 41 | + |
| 42 | + if err := download(dir); err != nil { |
| 43 | + return "", errors.Wrapf(err, "error downloading") |
| 44 | + } |
| 45 | + |
| 46 | + return filepath, nil |
| 47 | +} |
| 48 | + |
| 49 | +// Download path |
| 50 | +func download(dir string) error { |
| 51 | + var url, platform string |
| 52 | + |
| 53 | + switch runtime.GOOS { |
| 54 | + case "darwin": |
| 55 | + platform = "mac" |
| 56 | + url = fmt.Sprintf(downloadURLs[platform], DownloadHost, ChromiumRevision) |
| 57 | + case "linux": |
| 58 | + platform = "linux" |
| 59 | + url = fmt.Sprintf(downloadURLs[platform], DownloadHost, ChromiumRevision) |
| 60 | + case "windows": |
| 61 | + platform = "windows" |
| 62 | + // TODO: 32 vs 64 |
| 63 | + url = fmt.Sprintf(downloadURLs["win64"], DownloadHost, ChromiumRevision) |
| 64 | + default: |
| 65 | + return errors.New("unsupported operating system") |
| 66 | + } |
| 67 | + |
| 68 | + res, err := http.Get(url) |
| 69 | + if err != nil { |
| 70 | + return errors.Wrapf(err, "error getting download") |
| 71 | + } |
| 72 | + defer res.Body.Close() |
| 73 | + |
| 74 | + src, err := ioutil.ReadAll(res.Body) |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + |
| 79 | + folderPath := path.Join(dir, platform+"-"+ChromiumRevision) |
| 80 | + if err := os.MkdirAll(folderPath, 0775); err != nil { |
| 81 | + return errors.Wrapf(err, "error making directory") |
| 82 | + } |
| 83 | + |
| 84 | + return unzip(src, folderPath) |
| 85 | +} |
| 86 | + |
| 87 | +// Path to the executable |
| 88 | +// TODO: test on windows |
| 89 | +func chromiumPath(dir string) (string, error) { |
| 90 | + var platform string |
| 91 | + switch runtime.GOOS { |
| 92 | + case "darwin": |
| 93 | + platform = "mac" |
| 94 | + return path.Join(dir, platform+"-"+ChromiumRevision, "chrome-mac", "Chromium.app", "Contents", "MacOS", "Chromium"), nil |
| 95 | + case "linux": |
| 96 | + platform = "linux" |
| 97 | + return path.Join(dir, platform+"-"+ChromiumRevision, "chrome-linux", "chrome"), nil |
| 98 | + case "windows": |
| 99 | + platform = "windows" |
| 100 | + return path.Join(dir, platform+"-"+ChromiumRevision, "chrome-win32", "chrome.exe"), nil |
| 101 | + default: |
| 102 | + return "", errors.New("unsupported operating system") |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// Unzip a file to a destination and preserve the symlinks |
| 107 | +// Based on http://stackoverflow.com/a/24792688/842097 with symlink additions |
| 108 | +func unzip(src []byte, dest string) error { |
| 109 | + rdr := bytes.NewReader(src) |
| 110 | + |
| 111 | + r, err := zip.NewReader(rdr, rdr.Size()) |
| 112 | + if err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + |
| 116 | + os.MkdirAll(dest, 0755) |
| 117 | + |
| 118 | + // Closure to address file descriptors issue with all the deferred .Close() methods |
| 119 | + extractAndWriteFile := func(f *zip.File) error { |
| 120 | + rc, err := f.Open() |
| 121 | + if err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + defer func() { |
| 125 | + if err := rc.Close(); err != nil { |
| 126 | + panic(err) |
| 127 | + } |
| 128 | + }() |
| 129 | + |
| 130 | + path := filepath.Join(dest, f.Name) |
| 131 | + |
| 132 | + if f.FileInfo().IsDir() { |
| 133 | + os.MkdirAll(path, f.Mode()) |
| 134 | + } else if f.FileInfo().Mode()&os.ModeSymlink != 0 { |
| 135 | + buffer := make([]byte, f.FileInfo().Size()) |
| 136 | + size, err := rc.Read(buffer) |
| 137 | + if err != nil { |
| 138 | + return err |
| 139 | + } |
| 140 | + |
| 141 | + target := string(buffer[:size]) |
| 142 | + |
| 143 | + err = os.Symlink(target, path) |
| 144 | + if err != nil { |
| 145 | + return err |
| 146 | + } |
| 147 | + } else { |
| 148 | + os.MkdirAll(filepath.Dir(path), f.Mode()) |
| 149 | + f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode()) |
| 150 | + if err != nil { |
| 151 | + return err |
| 152 | + } |
| 153 | + defer func() { |
| 154 | + if err = f.Close(); err != nil { |
| 155 | + panic(err) |
| 156 | + } |
| 157 | + }() |
| 158 | + |
| 159 | + _, err = io.Copy(f, rc) |
| 160 | + if err != nil { |
| 161 | + return err |
| 162 | + } |
| 163 | + } |
| 164 | + return nil |
| 165 | + } |
| 166 | + |
| 167 | + for _, f := range r.File { |
| 168 | + err := extractAndWriteFile(f) |
| 169 | + if err != nil { |
| 170 | + return err |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + return nil |
| 175 | +} |
0 commit comments