Skip to content

Commit

Permalink
Only require file ID when generating partial upload url
Browse files Browse the repository at this point in the history
The GetUploadPartURL method previously required the entire StartFile
struct returned by StartLargeFile, when the only value required was the
fileID from that struct. The method has been updated to only require
this field, which now matches the non-partial GetUploadURL method (which
only requires a bucket ID).

Also added a helper function to generate the B2 URLs rather than using
sprintf each time.
  • Loading branch information
benbusby committed Apr 20, 2024
1 parent b61928a commit 6e2977c
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 30 deletions.
2 changes: 1 addition & 1 deletion b2_test/upload_large_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func uploadLargeFile(service Service) (LargeFile, error) {
log.Printf("Attempt #%d", attempt+1)
}

partInfo, err := service.GetUploadPartURL(startFile)
partInfo, err := service.GetUploadPartURL(startFile.FileID)
if err != nil {
return false, err
}
Expand Down
5 changes: 2 additions & 3 deletions delete_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ func (b2Service Service) DeleteFile(b2ID string, name string) bool {
"fileName": "%s"
}`, b2ID, name)))

reqURL := fmt.Sprintf(
"%s/%s/%s/%s",
b2Service.APIURL, utils.APIPrefix, b2Service.APIVersion, APIDeleteFile)
reqURL := utils.FormatB2URL(
b2Service.APIURL, b2Service.APIVersion, APIDeleteFile)

req, err := http.NewRequest("POST", reqURL, reqBody)
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions download_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ const APIDownloadById string = "b2_download_file_by_id"
// setupDownload creates an http.Request with the URL for downloading a file,
// as well as the file ID included in the query.
func setupDownload(apiURL, apiVersion, fileID string) (*http.Request, error) {
reqURL := fmt.Sprintf(
"%s/%s/%s/%s",
apiURL, utils.APIPrefix, apiVersion, APIDownloadById)
reqURL := utils.FormatB2URL(
apiURL, apiVersion, APIDownloadById)

req, err := http.NewRequest("GET", reqURL, nil)

Expand Down
5 changes: 2 additions & 3 deletions list_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,8 @@ func (b2Service Service) ListFiles(
return listLocalFiles(b2Service.LocalPath)
}

reqURL := fmt.Sprintf(
"%s/%s/%s/%s",
b2Service.APIURL, utils.APIPrefix, b2Service.APIVersion, APIListFileVersions)
reqURL := utils.FormatB2URL(
b2Service.APIURL, b2Service.APIVersion, APIListFileVersions)

req, err := http.NewRequest("GET", reqURL, nil)
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions upload_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,8 @@ func (b2Service Service) GetUploadURL(bucketID string) (FileInfo, error) {
}, nil
}

reqURL := fmt.Sprintf(
"%s/%s/%s/%s",
b2Service.APIURL, utils.APIPrefix, b2Service.APIVersion, APIGetUploadURL)
reqURL := utils.FormatB2URL(
b2Service.APIURL, b2Service.APIVersion, APIGetUploadURL)

req, err := http.NewRequest("GET", reqURL, nil)

Expand Down
28 changes: 11 additions & 17 deletions upload_large_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,8 @@ func (b2Service Service) StartLargeFile(
"fileName": "%s",
"contentType": "b2/x-auto"
}`, bucketID, filename)))
reqURL := fmt.Sprintf(
"%s/%s/%s/%s",
b2Service.APIURL, utils.APIPrefix, b2Service.APIVersion, APIStartLargeFile)
reqURL := utils.FormatB2URL(
b2Service.APIURL, b2Service.APIVersion, APIStartLargeFile)

req, err := http.NewRequest("POST", reqURL, reqBody)
if err != nil {
Expand Down Expand Up @@ -139,26 +138,23 @@ func (b2Service Service) StartLargeFile(
// GetUploadPartURL generates a URL and token for uploading individual chunks
// of a file to B2. It requires a StartFile struct returned by StartLargeFile,
// which contains the unique file ID for this new file.
func (b2Service Service) GetUploadPartURL(
b2File StartFile,
) (FilePartInfo, error) {
func (b2Service Service) GetUploadPartURL(fileID string) (FilePartInfo, error) {
if b2Service.Dummy {
return FilePartInfo{
FileID: b2File.FileID,
FileID: fileID,
UploadURL: b2Service.LocalPath,
Dummy: true,
StorageMaximum: b2Service.StorageMaximum,
}, nil
}

reqURL := fmt.Sprintf(
"%s/%s/%s/%s",
b2Service.APIURL, utils.APIPrefix, b2Service.APIVersion, APIGetUploadPartURL)
reqURL := utils.FormatB2URL(
b2Service.APIURL, b2Service.APIVersion, APIGetUploadPartURL)

req, err := http.NewRequest("GET", reqURL, nil)

q := req.URL.Query()
q.Add("fileId", b2File.FileID)
q.Add("fileId", fileID)
req.URL.RawQuery = q.Encode()

if err != nil {
Expand Down Expand Up @@ -250,9 +246,8 @@ func (b2Service Service) CancelLargeFile(fileID string) (bool, error) {
"fileId": "%s"
}`, fileID)))

reqURL := fmt.Sprintf(
"%s/%s/%s/%s",
b2Service.APIURL, utils.APIPrefix, b2Service.APIVersion, APICancelLargeFile)
reqURL := utils.FormatB2URL(
b2Service.APIURL, b2Service.APIVersion, APICancelLargeFile)

req, err := http.NewRequest("POST", reqURL, reqBody)
if err != nil {
Expand Down Expand Up @@ -298,9 +293,8 @@ func (b2Service Service) FinishLargeFile(
"partSha1Array": %s
}`, fileID, checksumsString)))

reqURL := fmt.Sprintf(
"%s/%s/%s/%s",
b2Service.APIURL, utils.APIPrefix, b2Service.APIVersion, APIFinishLargeFile)
reqURL := utils.FormatB2URL(
b2Service.APIURL, b2Service.APIVersion, APIFinishLargeFile)

req, err := http.NewRequest("POST", reqURL, reqBody)
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
Expand All @@ -27,3 +28,9 @@ func CheckDirSize(path string) (int64, error) {
})
return size, err
}

func FormatB2URL(apiURL, apiVersion, endpoint string) string {
return fmt.Sprintf(
"%s/%s/%s/%s",
apiURL, APIPrefix, apiVersion, endpoint)
}

0 comments on commit 6e2977c

Please sign in to comment.