Skip to content

Commit

Permalink
Updated upload & download
Browse files Browse the repository at this point in the history
  • Loading branch information
Dliv3 committed Aug 22, 2019
1 parent 11fbc7c commit e13049c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
6 changes: 6 additions & 0 deletions admin/dispather/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,12 @@ func SendUploadCmd(peerNode *node.Node, localPath string, remotePath string) boo
fmt.Println("local file does not exists")
return false
}

if utils.IsDir(localPath) {
fmt.Println("local file cannot be a folder")
return false
}

localFile, err := os.Open(localPath)
if err != nil {
fmt.Println(err)
Expand Down
26 changes: 15 additions & 11 deletions agent/dispather/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var ERR_PROTOCOL_SEPARATOR = errors.New("unknown separator")
var ERR_TARGET_NODE = errors.New("can not find target node")
var ERR_FILE_EXISTS = errors.New("remote file already exists")
var ERR_FILE_NOT_EXISTS = errors.New("remote file not exists")
var ERR_PATH_IS_DIR = errors.New("remote file cannot be a folder")

// AgentClient Admin节点作为Client
func AgentClient(conn net.Conn) {
Expand Down Expand Up @@ -220,26 +221,29 @@ func handleDownloadCmd() {
var downloadPacketCmd protocol.DownloadPacketCmd

node.CurrentNode.CommandBuffers[protocol.DOWNLOAD].ReadPacket(&packetHeader, &downloadPacketCmd)

adminNode := node.Nodes[utils.Array32ToUUID(packetHeader.SrcHashID)]

filePath := string(downloadPacketCmd.Path)

var downloadPacketRet protocol.DownloadPacketRet
var file *os.File
var fileSize int64
// 如果文件存在,则下载
if utils.FileExists(filePath) {
var err error
file, err = os.Open(filePath)
if err != nil {
downloadPacketRet.Success = 0
downloadPacketRet.Msg = []byte(fmt.Sprintf("%s", err))
if !utils.IsDir(filePath) {
var err error
file, err = os.Open(filePath)
if err != nil {
downloadPacketRet.Success = 0
downloadPacketRet.Msg = []byte(fmt.Sprintf("%s", err))
} else {
defer file.Close()
downloadPacketRet.Success = 1
fileSize = utils.GetFileSize(filePath)
downloadPacketRet.FileLen = uint64(fileSize)
}
} else {
defer file.Close()
downloadPacketRet.Success = 1
fileSize = utils.GetFileSize(filePath)
downloadPacketRet.FileLen = uint64(fileSize)
downloadPacketRet.Success = 0
downloadPacketRet.Msg = []byte(fmt.Sprintf("%s", ERR_PATH_IS_DIR))
}
} else {
downloadPacketRet.Success = 0
Expand Down
9 changes: 9 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ func FileExists(filename string) bool {
return exist
}

// IsDir check if path is dir
func IsDir(path string) bool {
s, err := os.Stat(path)
if err != nil {
return false
}
return s.IsDir()
}

// GetFileSize Get the size of a single file
func GetFileSize(path string) int64 {
fileInfo, err := os.Stat(path)
Expand Down

0 comments on commit e13049c

Please sign in to comment.