diff --git a/node/putHandle.go b/node/putHandle.go index 9598955..2af8176 100644 --- a/node/putHandle.go +++ b/node/putHandle.go @@ -218,7 +218,12 @@ func (n *Node) putHandle(c *gin.Context) { return } - for { + blockIdx, _ := strconv.Atoi(c.Request.Header.Get(HTTPHeader_BIdx)) + blockNum, _ := strconv.Atoi(c.Request.Header.Get(HTTPHeader_BNum)) + totalSize, _ := strconv.Atoi(c.Request.Header.Get(HTTPHeader_TSize)) + filename = c.Request.Header.Get(HTTPHeader_Fname) + + for blockNum <= 0 { savedir = filepath.Join(n.GetDirs().FileDir, account, fmt.Sprintf("%s-%s", uuid.New().String(), uuid.New().String())) _, err = os.Stat(savedir) if err != nil { @@ -236,6 +241,26 @@ func (n *Node) putHandle(c *gin.Context) { break } + if blockNum > 0 { + fdir, err := sutils.CalcSHA256(append([]byte(bucketName+filename), []byte(account)...)) + if err != nil { + n.Upfile("err", fmt.Sprintf("[%v] %v", clientIp, err.Error())) + c.JSON(http.StatusInternalServerError, ERR_InternalServer) + return + } + savedir = filepath.Join(n.GetDirs().FileDir, account, fdir) + _, err = os.Stat(savedir) + if err != nil { + err = os.MkdirAll(savedir, pattern.DirMode) + if err != nil { + n.Upfile("err", fmt.Sprintf("[%v] %v", clientIp, err.Error())) + c.JSON(http.StatusInternalServerError, ERR_InternalServer) + return + } + } + fpath = filepath.Join(savedir, filename) + } + formfile, fileHeder, err := c.Request.FormFile("file") if err != nil { n.Upfile("err", fmt.Sprintf("[%v] %v", clientIp, err.Error())) @@ -269,19 +294,25 @@ func (n *Node) putHandle(c *gin.Context) { return } } else { - filename = fileHeder.Filename + if blockNum <= 0 { + filename = fileHeder.Filename + } if len(filename) > pattern.MaxBucketNameLength { n.Upfile("err", fmt.Sprintf("[%v] %v", clientIp, ERR_FileNameTooLang)) c.JSON(http.StatusBadRequest, ERR_FileNameTooLang) return } - f, err := os.Create(fpath) + var f *os.File + if blockNum > 0 && blockIdx > 1 { + f, err = os.OpenFile(fpath, os.O_WRONLY|os.O_APPEND, 0666) + } else { + f, err = os.Create(fpath) + } if err != nil { n.Upfile("err", fmt.Sprintf("[%v] %v", clientIp, err)) c.JSON(http.StatusInternalServerError, ERR_InternalServer) return } - _, err = io.Copy(f, formfile) if err != nil { f.Close() @@ -292,6 +323,26 @@ func (n *Node) putHandle(c *gin.Context) { f.Close() } + if blockNum > 0 && blockIdx < blockNum { + n.Upfile("info", fmt.Sprintf("[%v] uploaded file block %d/%d successfully", clientIp, blockIdx, blockNum)) + c.JSON(http.StatusOK, blockIdx) + return + } + + if blockNum > 0 && blockIdx == blockNum { + stat, err := os.Stat(fpath) + if err != nil { + n.Upfile("err", fmt.Sprintf("[%v] %v", clientIp, err.Error())) + c.JSON(http.StatusInternalServerError, ERR_InternalServer) + return + } + if stat.Size() != int64(totalSize) { + n.Upfile("err", fmt.Sprintf("[%v] %v", clientIp, "file is not the same size as expected")) + c.JSON(http.StatusBadRequest, fmt.Sprintf("file size mismatch,expected %d, actual %d", totalSize, stat.Size())) + return + } + } + if filename == "" { filename = "null" } diff --git a/node/types.go b/node/types.go index 61ac2d8..0526bc6 100644 --- a/node/types.go +++ b/node/types.go @@ -19,6 +19,10 @@ const ( HTTPHeader_Signature = "Signature" HTTPHeader_Fid = "Fid" HTTPHeader_Cipher = "cipher" + HTTPHeader_BIdx = "BlockIndex" + HTTPHeader_BNum = "BlockNumber" + HTTPHeader_Fname = "FileName" + HTTPHeader_TSize = "TotalSize" ) const (