mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-03 23:20:26 +08:00
提供下载最新边缘节点API
This commit is contained in:
79
internal/installers/deploy_file.go
Normal file
79
internal/installers/deploy_file.go
Normal file
@@ -0,0 +1,79 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package installers
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
// DeployFile 部署文件描述
|
||||
type DeployFile struct {
|
||||
OS string
|
||||
Arch string
|
||||
Version string
|
||||
Path string
|
||||
}
|
||||
|
||||
// Sum 计算概要
|
||||
func (this *DeployFile) Sum() (string, error) {
|
||||
fp, err := os.Open(this.Path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer func() {
|
||||
_ = fp.Close()
|
||||
}()
|
||||
|
||||
m := md5.New()
|
||||
buffer := make([]byte, 128*1024)
|
||||
for {
|
||||
n, err := fp.Read(buffer)
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
_, err = m.Write(buffer[:n])
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
sum := m.Sum(nil)
|
||||
return fmt.Sprintf("%x", sum), nil
|
||||
}
|
||||
|
||||
// Read 读取一个片段数据
|
||||
func (this *DeployFile) Read(offset int64) (data []byte, newOffset int64, err error) {
|
||||
fp, err := os.Open(this.Path)
|
||||
if err != nil {
|
||||
return nil, offset, err
|
||||
}
|
||||
defer func() {
|
||||
_ = fp.Close()
|
||||
}()
|
||||
|
||||
stat, err := fp.Stat()
|
||||
if err != nil {
|
||||
return nil, offset, err
|
||||
}
|
||||
if offset >= stat.Size() {
|
||||
return nil, offset, io.EOF
|
||||
}
|
||||
|
||||
_, err = fp.Seek(offset, io.SeekStart)
|
||||
if err != nil {
|
||||
return nil, offset, err
|
||||
}
|
||||
|
||||
buffer := make([]byte, 128*1024)
|
||||
n, err := fp.Read(buffer)
|
||||
if err != nil {
|
||||
return nil, offset, err
|
||||
}
|
||||
|
||||
return buffer[:n], offset + int64(n), nil
|
||||
}
|
||||
36
internal/installers/deploy_file_test.go
Normal file
36
internal/installers/deploy_file_test.go
Normal file
@@ -0,0 +1,36 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package installers
|
||||
|
||||
import (
|
||||
"io"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDeployFile_Sum(t *testing.T) {
|
||||
d := &DeployFile{Path: "deploy_test.txt"}
|
||||
sum, err := d.Sum()
|
||||
if err != nil {
|
||||
t.Log("err:", err)
|
||||
return
|
||||
}
|
||||
t.Log("sum:", sum)
|
||||
}
|
||||
|
||||
func TestDeployFile_Read(t *testing.T) {
|
||||
d := &DeployFile{Path: "deploy_test.txt"}
|
||||
|
||||
var offset int64
|
||||
for i := 0; i < 3; i++ {
|
||||
data, newOffset, err := d.Read(offset)
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
t.Log("err: ", err)
|
||||
return
|
||||
}
|
||||
t.Log("offset:", newOffset, "data:", string(data))
|
||||
offset = newOffset
|
||||
}
|
||||
}
|
||||
@@ -9,17 +9,11 @@ import (
|
||||
|
||||
var SharedDeployManager = NewDeployManager()
|
||||
|
||||
type DeployFile struct {
|
||||
OS string
|
||||
Arch string
|
||||
Version string
|
||||
Path string
|
||||
}
|
||||
|
||||
type DeployManager struct {
|
||||
dir string
|
||||
}
|
||||
|
||||
// NewDeployManager 节点部署文件管理器
|
||||
func NewDeployManager() *DeployManager {
|
||||
return &DeployManager{
|
||||
dir: Tea.Root + "/deploy",
|
||||
@@ -61,7 +55,15 @@ func (this *DeployManager) LoadNodeFiles() []*DeployFile {
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
// FindNodeFile 查找特别平台的节点文件
|
||||
func (this *DeployManager) FindNodeFile(os string, arch string) *DeployFile {
|
||||
for _, file := range this.LoadNodeFiles() {
|
||||
if file.OS == os && file.Arch == arch {
|
||||
return file
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadNSNodeFiles 加载所有文件
|
||||
func (this *DeployManager) LoadNSNodeFiles() []*DeployFile {
|
||||
@@ -96,4 +98,4 @@ func (this *DeployManager) LoadNSNodeFiles() []*DeployFile {
|
||||
result = append(result, v)
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user