-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
413 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package Updater | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
type OnProgress func(bytesWritten, contentLength uint64) | ||
|
||
type WriteCounter struct { | ||
Total uint64 | ||
ContentLength uint64 | ||
OnProgress OnProgress | ||
} | ||
|
||
type Download struct { | ||
Url string | ||
Filepath string | ||
WriteCounter WriteCounter | ||
} | ||
|
||
func (wc *WriteCounter) Write(p []byte) (int, error) { | ||
n := len(p) | ||
wc.Total += uint64(n) | ||
wc.OnProgress(wc.Total, wc.ContentLength) | ||
return n, 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. We pass an io.TeeReader | ||
// into Copy() to report progress on the download. | ||
func (d *Download) DownloadFile() error { | ||
|
||
// Create the file, but give it a tmp file extension, this means we won't overwrite a | ||
// file until it's downloaded, but we'll remove the tmp extension once downloaded. | ||
out, err := os.Create(d.Filepath + ".tmp") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Get the data | ||
resp, err := http.Get(d.Url) | ||
if err != nil { | ||
out.Close() | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
|
||
d.WriteCounter.ContentLength = uint64(resp.ContentLength) | ||
|
||
// Create our progress reporter and pass it to be used alongside our writer | ||
if _, err = io.Copy(out, io.TeeReader(resp.Body, &d.WriteCounter)); err != nil { | ||
out.Close() | ||
return err | ||
} | ||
|
||
// The progress use the same line so print a new line once it's finished downloading | ||
fmt.Print("\n") | ||
|
||
// Close the file without defer so it can happen before Rename() | ||
out.Close() | ||
|
||
if err = os.Rename(d.Filepath+".tmp", d.Filepath); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package Updater | ||
|
||
import ( | ||
"archive/zip" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
func Unzip(src, dest string) error { | ||
r, err := zip.OpenReader(src) | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { | ||
if err := r.Close(); err != nil { | ||
panic(err) | ||
} | ||
}() | ||
|
||
os.MkdirAll(dest, 0755) | ||
|
||
// Closure to address file descriptors issue with all the deferred .Close() methods | ||
extractAndWriteFile := func(f *zip.File) error { | ||
rc, err := f.Open() | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { | ||
if err := rc.Close(); err != nil { | ||
panic(err) | ||
} | ||
}() | ||
|
||
path := filepath.Join(dest, f.Name) | ||
|
||
// Check for ZipSlip (Directory traversal) | ||
if !strings.HasPrefix(path, filepath.Clean(dest)+string(os.PathSeparator)) { | ||
return fmt.Errorf("illegal file path: %s", path) | ||
} | ||
|
||
if f.FileInfo().IsDir() { | ||
os.MkdirAll(path, f.Mode()) | ||
} else { | ||
os.MkdirAll(filepath.Dir(path), f.Mode()) | ||
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode()) | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { | ||
if err := f.Close(); err != nil { | ||
panic(err) | ||
} | ||
}() | ||
|
||
_, err = io.Copy(f, rc) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
for _, f := range r.File { | ||
err := extractAndWriteFile(f) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package Updater | ||
|
||
import ( | ||
"fmt" | ||
"gopkg.in/yaml.v3" | ||
"io" | ||
"log" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
/* example yaml | ||
packages: | ||
app: | ||
version: 1.0.0.16 | ||
urls: | ||
- https://eu2.someurl.com/app1.0.0.16_win.zip | ||
data: | ||
version: 1.0.0.3 | ||
urls: | ||
- https://eu2.someurl.com/data1.0.0.3_win.zip | ||
- https://usc1.someurl.com/data1.0.0.3_win.zip | ||
*/ | ||
|
||
type UpdateInfo struct { | ||
Version string `yaml:"version"` | ||
Urls []string `yaml:"urls"` | ||
} | ||
|
||
func (i *UpdateInfo) WriteYaml(fileName string) { | ||
// marshal the struct to yaml and save as file | ||
yamlFile, err := yaml.Marshal(i) | ||
if err != nil { | ||
log.Printf("error: %v", err) | ||
} | ||
err = os.WriteFile(fileName, yamlFile, 0644) | ||
if err != nil { | ||
log.Printf("error: %v", err) | ||
} | ||
} | ||
|
||
func (i *UpdateInfo) ReadYaml(data []byte) error { | ||
// read the yaml file and unmarshal it to the struct | ||
var err error | ||
err = yaml.Unmarshal(data, i) | ||
if err != nil { | ||
log.Printf("Unmarshal: %v", err) | ||
} | ||
return err | ||
} | ||
|
||
type UpdatePackages struct { | ||
Packages map[string]UpdateInfo `yaml:"packages"` | ||
//DoNotAskAgain bool `yaml:"doNotAskAgain,omitempty"` | ||
} | ||
|
||
func (u *UpdatePackages) parsePackagesFromYaml(data []byte) error { | ||
var err error | ||
err = yaml.Unmarshal(data, u) | ||
if err != nil { | ||
log.Printf("Unmarshal: %v", err) | ||
} | ||
return err | ||
} | ||
|
||
func (u *UpdatePackages) getYaml(url string) ([]byte, error) { | ||
resp, err := http.Get(url) | ||
if err != nil { | ||
return []byte{}, fmt.Errorf("GET error: %v", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return []byte{}, fmt.Errorf("status error: %v", resp.StatusCode) | ||
} | ||
|
||
data, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return []byte{}, fmt.Errorf("read body: %v", err) | ||
} | ||
|
||
return data, nil | ||
} | ||
|
||
func (u *UpdatePackages) WriteYaml(fileName string) { | ||
// marshal the struct to yaml and save as file | ||
yamlFile, err := yaml.Marshal(u) | ||
if err != nil { | ||
log.Printf("error: %v", err) | ||
} | ||
err = os.WriteFile(fileName, yamlFile, 0644) | ||
if err != nil { | ||
log.Printf("error: %v", err) | ||
} | ||
} | ||
|
||
func (u *UpdatePackages) GetUpdateInfo(url string) error { | ||
data, err := u.getYaml(url) | ||
if err != nil { | ||
return err | ||
} | ||
return u.parsePackagesFromYaml(data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.