Skip to content

Commit

Permalink
add breakpoint resume upload function (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiuquxzy authored Apr 8, 2024
1 parent ae6d482 commit e1c9831
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
59 changes: 55 additions & 4 deletions node/putHandle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()))
Expand Down Expand Up @@ -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()
Expand All @@ -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"
}
Expand Down
4 changes: 4 additions & 0 deletions node/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit e1c9831

Please sign in to comment.