Skip to content

Commit

Permalink
dont write to hash if rewritten (#1227)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hitenjain14 authored Aug 30, 2023
1 parent b7df47b commit 248367c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
22 changes: 11 additions & 11 deletions code/go/0chain.net/blobbercore/filestore/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,6 @@ func (fs *FileStore) WriteFile(allocID, conID string, fileData *FileInputData, i
if err != nil {
return nil, common.NewError("file_write_error", err.Error())
}
if !fileData.IsThumbnail {
_, err = f.Seek(fileData.UploadOffset, io.SeekStart)
if err != nil {
return nil, common.NewError("file_seek_error", err.Error())
}

_, err = io.CopyBuffer(fileData.Hasher, f, buf)
if err != nil {
return nil, common.NewError("file_read_error", err.Error())
}
}

finfo, err = f.Stat()
if err != nil {
Expand All @@ -109,6 +98,17 @@ func (fs *FileStore) WriteFile(allocID, conID string, fileData *FileInputData, i

currentSize := finfo.Size()
if currentSize > initialSize { // Is chunk new or rewritten
if !fileData.IsThumbnail {
_, err = f.Seek(fileData.UploadOffset, io.SeekStart)
if err != nil {
return nil, common.NewError("file_seek_error", err.Error())
}

_, err = io.CopyBuffer(fileData.Hasher, f, buf)
if err != nil {
return nil, common.NewError("file_read_error", err.Error())
}
}
fileRef.ChunkUploaded = true
fs.updateAllocTempFileSize(allocID, currentSize-initialSize)
}
Expand Down
12 changes: 9 additions & 3 deletions code/go/0chain.net/blobbercore/handler/file_command_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,20 @@ func (cmd *UpdateFileCommand) ProcessContent(ctx context.Context, req *http.Requ

result.Filename = cmd.fileChanger.Filename

isFinal := cmd.fileChanger.IsFinal
cmd.fileChanger.IsFinal = false
cmd.reloadChange(connectionObj)
if cmd.fileChanger.IsFinal {
return result, nil
}
cmd.fileChanger.IsFinal = isFinal

origfile, _, err := req.FormFile(UploadFile)
if err != nil {
return result, common.NewError("invalid_parameters", "Error Reading multi parts for file."+err.Error())
}
defer origfile.Close()

cmd.reloadChange(connectionObj)

if cmd.fileChanger.Size == 0 {
return result, common.NewError("invalid_parameters", "Invalid parameters. Size cannot be zero")
}
Expand Down Expand Up @@ -199,10 +205,10 @@ func (cmd *UpdateFileCommand) reloadChange(connectionObj *allocation.AllocationC
}

// reload uploaded size from db, it was chunk size from client
cmd.fileChanger.Size = dbFileChanger.Size
cmd.fileChanger.ThumbnailFilename = dbFileChanger.ThumbnailFilename
cmd.fileChanger.ThumbnailSize = dbFileChanger.ThumbnailSize
cmd.fileChanger.ThumbnailHash = dbFileChanger.ThumbnailHash
cmd.fileChanger.IsFinal = dbFileChanger.IsFinal
return
}
}
Expand Down
13 changes: 9 additions & 4 deletions code/go/0chain.net/blobbercore/handler/file_command_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,21 @@ func (cmd *UploadFileCommand) ProcessContent(ctx context.Context, req *http.Requ
return result, common.NewError("invalid_parameters", "Error Reading multi parts for file."+err.Error())
}
defer origfile.Close()

isFinal := cmd.fileChanger.IsFinal
cmd.fileChanger.IsFinal = false
cmd.reloadChange(connectionObj)
if cmd.fileChanger.IsFinal {
result.Filename = cmd.fileChanger.Filename
return result, nil
}
cmd.fileChanger.IsFinal = isFinal

var hasher *filestore.CommitHasher
filePathHash := encryption.Hash(cmd.fileChanger.Path)
if cmd.fileChanger.Size == 0 {
return result, common.NewError("invalid_parameters", "Invalid parameters. Size cannot be zero")
}
fmt.Println("cmd.fileChanger.Size", cmd.fileChanger.Size)

if cmd.fileChanger.UploadOffset == 0 {
hasher = filestore.GetNewCommitHasher(cmd.fileChanger.Size)
allocation.UpdateConnectionObjWithHasher(connectionObj.ID, filePathHash, hasher)
Expand Down Expand Up @@ -207,11 +213,10 @@ func (cmd *UploadFileCommand) reloadChange(connectionObj *allocation.AllocationC
logging.Logger.Error("reloadChange", zap.Error(err))
}

cmd.fileChanger.Size = dbChangeProcessor.Size
cmd.fileChanger.ThumbnailFilename = dbChangeProcessor.ThumbnailFilename
cmd.fileChanger.ThumbnailSize = dbChangeProcessor.ThumbnailSize
cmd.fileChanger.ThumbnailHash = dbChangeProcessor.ThumbnailHash

cmd.fileChanger.IsFinal = dbChangeProcessor.IsFinal
return
}
}
Expand Down

0 comments on commit 248367c

Please sign in to comment.