增加上传与接收文件

This commit is contained in:
2025-10-23 19:05:16 +08:00
parent 329384d8ef
commit 10c349b82f
12 changed files with 173 additions and 14 deletions

View File

@@ -4,7 +4,9 @@ import (
"context"
"time"
"github.com/ayflying/p2p/internal/consts"
"github.com/ayflying/p2p/internal/controller/p2p"
"github.com/ayflying/p2p/internal/controller/system"
"github.com/ayflying/p2p/internal/service"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
@@ -22,14 +24,16 @@ func init() {
}
var (
s = g.Server()
Main = gcmd.Command{
Name: "main",
Usage: "main",
Brief: "start http server",
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
g.Log().Debug(ctx, "开始执行main")
Version, err := g.Cfg("hack").Get(ctx, "gfcli.build.version")
g.Log().Debugf(ctx, "当前启动的版本为:%v", Version)
s := g.Server(consts.Name)
parser, err = gcmd.Parse(g.MapStrBool{
"p,port": true,
@@ -49,6 +53,7 @@ var (
group.Middleware(ghttp.MiddlewareHandlerResponse)
group.Bind(
p2p.NewV1(),
system.NewV1(),
)
})
@@ -79,6 +84,9 @@ var (
})
// 启动系统托盘
service.OS().Load(consts.Name, consts.Name+"服务端", "manifest/images/favicon.ico")
s.Run()
return nil
},

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"time"
"github.com/ayflying/p2p/internal/consts"
"github.com/ayflying/p2p/internal/service"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gcmd"
@@ -25,6 +26,7 @@ var (
// Func 为命令的执行函数,接收上下文和参数解析器
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
g.Log().Debug(ctx, "开始执行dht")
s := g.Server(consts.Name)
parser, err = gcmd.Parse(g.MapStrBool{
"p,port": true,

View File

@@ -7,6 +7,7 @@ import (
"path"
"runtime"
systemV1 "github.com/ayflying/p2p/api/system/v1"
"github.com/ayflying/p2p/internal/service"
"github.com/gogf/gf/v2/crypto/gsha1"
"github.com/gogf/gf/v2/encoding/gcompress"
@@ -45,8 +46,8 @@ var (
var filePath = path.Join(pathMain, version, platform, name)
dirList, _ := gfile.ScanDir(path.Join(pathMain, version), "*", false)
for _, v := range dirList {
updateFilename := gfile.Name(v)
updateFilePath := path.Join("server_update", name, version, updateFilename)
updatePlatform := gfile.Name(v)
updateFilePath := path.Join("server_update", name, version, updatePlatform)
var obj bytes.Buffer
g.Log().Debugf(ctx, "读取目录成功:%v", v)
@@ -54,14 +55,14 @@ var (
g.Log().Debugf(ctx, "判断当前文件是否存在:%v", fileMian)
if gfile.IsFile(fileMian) {
// 写入文件哈希
versionFile[updateFilePath+".gz"] = gsha1.MustEncryptFile(fileMian)
versionFile[updatePlatform] = gsha1.MustEncryptFile(fileMian)
err = gcompress.GzipPathWriter(fileMian, &obj)
service.S3().PutObject(ctx, &obj, updateFilePath+".gz")
g.Log().Debugf(ctx, "成功上传文件到:%v", updateFilePath+".gz")
}
if gfile.IsFile(fileMian + ".exe") {
// 写入文件哈希
versionFile[updateFilePath+".gz"] = gsha1.MustEncryptFile(fileMian + ".exe")
versionFile[updatePlatform] = gsha1.MustEncryptFile(fileMian + ".exe")
err = gcompress.GzipPathWriter(fileMian+".exe", &obj)
service.S3().PutObject(ctx, &obj, updateFilePath+".gz")
g.Log().Debugf(ctx, "成功上传文件到:%v", updateFilePath+".gz")
@@ -69,13 +70,38 @@ var (
// 写入文件版本文件
fileByte := gjson.MustEncode(versionFile)
service.S3().PutObject(ctx, bytes.NewReader(fileByte), path.Join("server_update", name, version, "version.json"))
service.S3().PutObject(ctx, bytes.NewReader(fileByte), path.Join("server_update", name, "version.json"))
if err != nil {
g.Log().Error(ctx, err)
}
}
g.Log().Debugf(ctx, "当前获取到的地址为:%v", filePath)
versionUrl := service.S3().GetCdnUrl(path.Join("server_update", name, "version.json"))
listVar := g.Cfg().MustGet(ctx, "p2p.list")
var p2pItem []struct {
Host string `json:"host"`
Port int `json:"port"`
SSL bool `json:"ssl"`
Ws string `json:"ws"`
}
listVar.Scan(&p2pItem)
for _, v := range p2pItem {
url := "http"
if v.SSL == true {
url = "https"
}
url = fmt.Sprintf("%s://%s:%d/system/update", url, v.Host, v.Port)
g.Log().Debugf(ctx, "开始上传到服务器:%v,file=%v", url, versionUrl)
_, err := g.Client().Get(ctx, url, systemV1.UpdateReq{
Url: versionUrl,
})
if err != nil {
g.Log().Error(ctx, err)
}
}
return
}}
)