Fix file upload EOF bug

This commit is contained in:
Chun-Hung Tseng
2023-07-12 18:13:40 +02:00
parent af49c9f19b
commit 916abed02a
2 changed files with 10 additions and 6 deletions

View File

@@ -15,7 +15,6 @@ var (
ErrLinkMustBeActive = errors.New("can not operate on link state other than active")
ErrDownloadedBlockHashVerificationFailed = errors.New("the hash of the downloaded block doesn't match the original hash")
ErrDraftExists = errors.New("a draft exist - usually this means a file is being uploaded at another client, or, there was a failed upload attempt")
ErrWrongEOFAssumption = errors.New("we have a problem in the assumption where EOF comes on its own")
ErrCantFindActiveRevision = errors.New("can't find an active revision")
ErrCantFindDraftRevision = errors.New("can't find a draft revision")
)

15
file.go
View File

@@ -403,18 +403,23 @@ func (protonDrive *ProtonDrive) uploadAndCollectBlockData(ctx context.Context, n
return nil
}
for i := 1; ; i++ {
shouldContinue := true
for i := 1; shouldContinue; i++ {
// read at most data of size UPLOAD_BLOCK_SIZE
data := make([]byte, UPLOAD_BLOCK_SIZE) // FIXME: get block size from the server config instead of hardcoding it
readBytes, err := file.Read(data)
if err != nil {
if err == io.EOF {
if readBytes > 0 {
return nil, 0, ErrWrongEOFAssumption
// might still have data to read!
if readBytes == 0 {
break
}
break
shouldContinue = false
} else {
// all other errors
return nil, 0, err
}
return nil, 0, err
}
data = data[:readBytes]
totalFileSize += int64(readBytes)