1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- package utils
- import (
- "archive/zip"
- "errors"
- "fmt"
- "io"
- "os"
- "path/filepath"
- "runtime"
- "strings"
- "updater/huawei"
- )
- func UploadExe() (string, error) {
-
-
- appExePath, _ := os.Executable()
-
- appExePath = strings.Replace(appExePath, "\\", "/", -1)
-
- appExeDir, _ := filepath.Split(appExePath)
- fmt.Println(appExeDir)
-
-
- zipFile := fmt.Sprintf("%s%s.%s-%s.zip", "updater", Version, runtime.GOOS, runtime.GOARCH)
- zipPath := fmt.Sprintf("%s%s", appExeDir, zipFile)
- err := ZipFile(zipPath, appExePath)
- if err != nil {
- return "", err
- }
-
- obsDir := "pkg"
-
-
- huawei.InitConfig()
- obs, err := huawei.UploadFile(zipPath, obsDir, zipFile)
- if err != nil {
- return "", err
- }
- if len(obs.Url) < 1 {
- fmt.Println("上传zipfile错误")
- return "", errors.New("上传zipfile错误")
- }
- fmt.Println("succ uploaded=>", obs.Url)
- return zipPath, nil
- }
- func ZipFile(zipPath string, exePath string) error {
- os.RemoveAll(zipPath)
- archive, err := os.Create(zipPath)
- if err != nil {
- return err
- }
- defer archive.Close()
- zipWriter := zip.NewWriter(archive)
- f, err := os.Open(exePath)
- if err != nil {
- return err
- }
- defer f.Close()
- _, exeFile := filepath.Split(exePath)
- w, err := zipWriter.Create(exeFile)
- if err != nil {
- return err
- }
- if _, err := io.Copy(w, f); err != nil {
- return err
- }
- zipWriter.Close()
- return nil
- }
|