This commit is contained in:
1-6
2023-07-25 10:51:27 +08:00
parent 1b9311c5e6
commit df190339a3
+47 -4
View File
@@ -1,10 +1,15 @@
package core package core
import ( import (
"archive/zip"
"fmt"
"io" "io"
"io/fs"
"net/http" "net/http"
"os" "os"
"path/filepath"
"runtime" "runtime"
"strings"
"github.com/cdle/sillyplus/utils" "github.com/cdle/sillyplus/utils"
) )
@@ -23,14 +28,14 @@ var languages = []Language{
Version: "20230725", Version: "20230725",
Os: "linux", Os: "linux",
Arch: "amd64", Arch: "amd64",
Links: []string{"https://gitee.com/sillybot/binary/releases/download/20230725/node_linux_amd64"}, Links: []string{"https://gitee.com/sillybot/binary/releases/download/20230725/node_linux_amd64.zip"},
}, },
{ {
Name: "node", Name: "node",
Version: "20230725", Version: "20230725",
Os: "darwin", Os: "darwin",
Arch: "arm64", Arch: "arm64",
Links: []string{"https://gitee.com/sillybot/binary/releases/download/20230725/node_darwin_arm64"}, Links: []string{"https://gitee.com/sillybot/binary/releases/download/20230725/node_darwin_arm64.zip"},
}, },
} }
@@ -53,17 +58,55 @@ func init() {
return return
} }
defer resp.Body.Close() defer resp.Body.Close()
f, err := os.OpenFile(dir+"/"+item.Name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755) zipfile := dir + "/" + item.Name + ".zip"
f, err := os.OpenFile(zipfile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil { if err != nil {
return return
} }
defer f.Close() defer f.Close()
_, err = io.Copy(f, resp.Body) _, err = io.Copy(f, resp.Body)
if err != nil { if err != nil {
// fmt.Println(err)
return return
} }
os.WriteFile(dir+"/version", []byte(item.Version), 0755) defer os.Remove(zipfile)
if err := unzip(zipfile, 0755); err == nil {
os.WriteFile(dir+"/version", []byte(item.Version), 0755)
} else {
// fmt.Println(err)
}
}() }()
} }
}() }()
} }
func unzip(filename string, perm fs.FileMode) error {
zipFile, err := zip.OpenReader(filename)
dir := filepath.Dir(filename)
fmt.Println(filename, err)
if err != nil {
return err
}
defer zipFile.Close()
for _, file := range zipFile.File {
// 忽略以 "__MACOSX/" 开头的文件
if strings.HasPrefix(file.Name, "__MACOSX/") {
continue
}
zipFile, err := file.Open()
if err != nil {
return err
}
defer zipFile.Close()
localFile, err := os.OpenFile(dir+"/"+file.Name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
if err != nil {
return err
}
defer localFile.Close()
_, err = io.Copy(localFile, zipFile)
if err != nil {
return err
}
}
return err
}