Kaynağa Gözat

feat;upload 增加中继

v1.0.3
yuan_rh 4 yıl önce
ebeveyn
işleme
417e190d99
1 değiştirilmiş dosya ile 47 ekleme ve 5 silme
  1. +47
    -5
      handle/handle.go

+ 47
- 5
handle/handle.go Dosyayı Görüntüle

@@ -2,6 +2,7 @@ package handle

import (
"bufio"
"context"
"encoding/json"
"errors"
"fmt"
@@ -387,9 +388,26 @@ func UploadCommand(conn *websocket.Conn,absolutePath,fileName,projectName,dir,cu
}()

go func(){
first := true
millSeconds := time.Now().UnixNano() / 1e6
current :=time.Now().UnixNano() / 1e6
for content := range uploadProgress { // 通道关闭后会退出for range循环
current :=time.Now().UnixNano() / 1e6
if first {
projson,err := contentToJSONByte(content)
if projson==nil && err==nil{
continue
}

if err != nil {
log.Println("json.Marshal error %s\n", err)
}
uploading=true
if err := conn.WriteMessage(websocket.TextMessage, projson); err != nil {
break
}
millSeconds = current
first=false
}
if current-millSeconds>500{
projson,err := contentToJSONByte(content)
if projson==nil && err==nil{
@@ -405,7 +423,7 @@ func UploadCommand(conn *websocket.Conn,absolutePath,fileName,projectName,dir,cu
}
millSeconds = current
}
if strings.Index(content,"100.00%")!=-1{
if (strings.Index(content,"90.00%")!=-1){

projson,err := contentToJSONByte(content)
if projson==nil && err==nil{
@@ -492,16 +510,40 @@ func UploadCommand(conn *websocket.Conn,absolutePath,fileName,projectName,dir,cu
}

//serverSh.SetTimeout(time.Duration(600)*time.Second)
//判断备份节点对等节点是否已添加连接
idOut,err :=sh.ID()
localId :=idOut.ID
swarmConnInfos,err :=serverSh.SwarmPeers(context.Background())
hasConnect := false
for _,swarmconn := range swarmConnInfos.Peers {
if swarmconn.Peer==localId{
hasConnect=true
break
}
}

if !hasConnect{
log.Println("中继处理")
swarmConnectAddr :="/ipfs/"+"12D3KooWER8uoGrzeLuJHTXoTMnR4jjHfpcA6ZcpXBaQNJvG5jMP"+"/p2p-circuit/ipfs/"+localId
errT := serverSh.SwarmConnect(context.Background(),swarmConnectAddr)
if errT!=nil{
log.Println("中继失败,引导节点备份失败")
log.Println(err)
if err := conn.WriteMessage(websocket.TextMessage, []byte("-1")); err != nil {
return err
}
}
}

err = serverSh.Pin(prog.Hash)
if err != nil {
if err!=nil{
log.Println("引导节点备份失败")
log.Println(err)
if err := conn.WriteMessage(websocket.TextMessage, []byte("-1")); err != nil {
return err
}
return err
}

log.Println("引导节点文件备份成功")

//文件不存在则进行本地文件夹拷贝
@@ -642,7 +684,7 @@ func commitRecord(path,currentHistoryHash,hash,note,creator string, milestone bo
}

serverSh :=shell.NewShell(config.ServerIpfsUrl)
serverSh.SetTimeout(5*time.Second)
serverSh.SetTimeout(60*time.Second)
err = serverSh.Pin(historyHash)
if err != nil {
log.Println("历史版本管理文件备份失败")


Yükleniyor…
İptal
Kaydet