Skip to content

Commit

Permalink
fix empty chunk on eof
Browse files Browse the repository at this point in the history
  • Loading branch information
Hitenjain14 committed May 6, 2024
1 parent f8f3928 commit cfc7404
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 43 deletions.
10 changes: 5 additions & 5 deletions zboxcore/sdk/chunked_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,10 +476,7 @@ func (su *ChunkedUpload) process() error {
return thrown.New("upload_failed", "Upload failed. Uploaded size does not match with actual size: "+fmt.Sprintf("%d != %d", su.fileMeta.ActualSize, su.progress.ReadLength))
}
}
if chunks.totalReadSize == 0 {
logger.Logger.Info("isFinal: ", chunks.isFinal, " totalReadSize: ", su.progress.ReadLength)
break
}

//chunk has not be uploaded yet
if chunks.chunkEndIndex > su.progress.ChunkIndex {
err = su.processUpload(
Expand Down Expand Up @@ -653,7 +650,10 @@ func (su *ChunkedUpload) processUpload(chunkStartIndex, chunkEndIndex int,

wgErrors := make(chan error, len(su.blobbers))
if len(fileShards) == 0 {
return thrown.New("upload_failed", "Upload failed. No data to upload")
if !isFinal {
return thrown.New("upload_failed", "Upload failed. No data to upload")
}
fileShards = make([]blobberShards, len(su.blobbers))
}

for i := su.uploadMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) {
Expand Down
15 changes: 0 additions & 15 deletions zboxcore/sdk/chunked_upload_blobber.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,6 @@ func (sb *ChunkedUploadBlobber) sendUploadRequest(
}
}()

if formData.FileBytesLen == 0 {
//fixed fileRef in last chunk on stream. io.EOF with nil bytes
if isFinal {
sb.fileRef.ChunkSize = su.chunkSize
sb.fileRef.Size = su.shardUploadedSize
sb.fileRef.Path = su.fileMeta.RemotePath
sb.fileRef.ActualFileHash = su.fileMeta.ActualHash
sb.fileRef.ActualFileSize = su.fileMeta.ActualSize

sb.fileRef.EncryptedKey = encryptedKey
sb.fileRef.CalculateHash()
consensus.Done()
}
}

eg, _ := errgroup.WithContext(ctx)

for dataInd := 0; dataInd < len(dataBuffers); dataInd++ {
Expand Down
46 changes: 23 additions & 23 deletions zboxcore/sdk/chunked_upload_form_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,13 @@ func (b *chunkedUploadFormBuilder) Build(

var res blobberData

if len(fileChunksData) == 0 {
return res, nil
}

numBodies := len(fileChunksData) / MAX_BLOCKS
if len(fileChunksData)%MAX_BLOCKS > 0 {
numBodies++
}
if len(fileChunksData) == 0 {
numBodies = 1
}
dataBuffers := make([]*bytes.Buffer, 0, numBodies)
contentSlice := make([]string, 0, numBodies)

Expand Down Expand Up @@ -103,33 +102,34 @@ func (b *chunkedUploadFormBuilder) Build(
body := bytes.NewBuffer(bodyBuf)
formWriter := multipart.NewWriter(body)
defer formWriter.Close()

uploadFile, err := formWriter.CreateFormFile("uploadFile", formData.Filename)
if err != nil {
return res, err
}

for _, chunkBytes := range fileChunksData[startRange:endRange] {
_, err = uploadFile.Write(chunkBytes)
if endRange > startRange {
uploadFile, err := formWriter.CreateFormFile("uploadFile", formData.Filename)
if err != nil {
return res, err
}

err = hasher.WriteToFixedMT(chunkBytes)
if err != nil {
return res, err
}
for _, chunkBytes := range fileChunksData[startRange:endRange] {
_, err = uploadFile.Write(chunkBytes)
if err != nil {
return res, err
}

err = hasher.WriteToValidationMT(chunkBytes)
if err != nil {
return res, err
}
err = hasher.WriteToFixedMT(chunkBytes)
if err != nil {
return res, err
}

err = hasher.WriteToValidationMT(chunkBytes)
if err != nil {
return res, err
}

metadata.FileBytesLen += len(chunkBytes)
metadata.FileBytesLen += len(chunkBytes)
}
}

if isFinal && i == numBodies-1 {
err = hasher.Finalize()
err := hasher.Finalize()
if err != nil {
return res, err
}
Expand Down Expand Up @@ -202,7 +202,7 @@ func (b *chunkedUploadFormBuilder) Build(
formData.UploadOffset = formData.UploadOffset + chunkSize*int64(MAX_BLOCKS)
}

err = formWriter.WriteField("connection_id", connectionID)
err := formWriter.WriteField("connection_id", connectionID)
if err != nil {
return res, err
}
Expand Down

0 comments on commit cfc7404

Please sign in to comment.