Skip to content

Commit

Permalink
chore: uploads are deferred by default (#4318)
Browse files Browse the repository at this point in the history
  • Loading branch information
istae authored Sep 14, 2023
1 parent 86b2f77 commit cf00cf5
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 16 deletions.
9 changes: 9 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -895,3 +895,12 @@ func calculateNumberOfChunks(contentLength int64, isEncrypted bool) int64 {

return int64(totalChunks) + 1
}

// defaultUploadMethod returns true for deferred when the defered header is not present.
func defaultUploadMethod(deffered *bool) bool {
if deffered == nil {
return true
}

return *deffered
}
12 changes: 7 additions & 5 deletions pkg/api/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (s *Service) bytesUploadHandler(w http.ResponseWriter, r *http.Request) {
BatchID []byte `map:"Swarm-Postage-Batch-Id" validate:"required"`
SwarmTag uint64 `map:"Swarm-Tag"`
Pin bool `map:"Swarm-Pin"`
Deferred bool `map:"Swarm-Deferred-Upload"`
Deferred *bool `map:"Swarm-Deferred-Upload"`
Encrypt bool `map:"Swarm-Encrypt"`
}{}
if response := s.mapStructure(r.Header, &headers); response != nil {
Expand All @@ -41,10 +41,12 @@ func (s *Service) bytesUploadHandler(w http.ResponseWriter, r *http.Request) {
}

var (
tag uint64
err error
tag uint64
err error
deferred = defaultUploadMethod(headers.Deferred)
)
if headers.Deferred || headers.Pin {

if deferred || headers.Pin {
tag, err = s.getOrCreateSessionID(headers.SwarmTag)
if err != nil {
logger.Debug("get or create tag failed", "error", err)
Expand All @@ -63,7 +65,7 @@ func (s *Service) bytesUploadHandler(w http.ResponseWriter, r *http.Request) {
BatchID: headers.BatchID,
TagID: tag,
Pin: headers.Pin,
Deferred: headers.Deferred,
Deferred: deferred,
})
if err != nil {
logger.Debug("get putter failed", "error", err)
Expand Down
4 changes: 3 additions & 1 deletion pkg/api/bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,9 @@ func TestBytesInvalidStamp(t *testing.T) {
func TestBytesUploadHandlerInvalidInputs(t *testing.T) {
t.Parallel()

client, _, _, _ := newTestServer(t, testServerOptions{})
client, _, _, _ := newTestServer(t, testServerOptions{
Storer: mockstorer.New(),
})

tests := []struct {
name string
Expand Down
12 changes: 7 additions & 5 deletions pkg/api/bzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (s *Service) bzzUploadHandler(w http.ResponseWriter, r *http.Request) {
BatchID []byte `map:"Swarm-Postage-Batch-Id" validate:"required"`
SwarmTag uint64 `map:"Swarm-Tag"`
Pin bool `map:"Swarm-Pin"`
Deferred bool `map:"Swarm-Deferred-Upload"`
Deferred *bool `map:"Swarm-Deferred-Upload"`
Encrypt bool `map:"Swarm-Encrypt"`
IsDir bool `map:"Swarm-Collection"`
}{}
Expand All @@ -50,10 +50,12 @@ func (s *Service) bzzUploadHandler(w http.ResponseWriter, r *http.Request) {
}

var (
tag uint64
err error
tag uint64
err error
deferred = defaultUploadMethod(headers.Deferred)
)
if headers.Deferred || headers.Pin {

if deferred || headers.Pin {
tag, err = s.getOrCreateSessionID(headers.SwarmTag)
if err != nil {
logger.Debug("get or create tag failed", "error", err)
Expand All @@ -72,7 +74,7 @@ func (s *Service) bzzUploadHandler(w http.ResponseWriter, r *http.Request) {
BatchID: headers.BatchID,
TagID: tag,
Pin: headers.Pin,
Deferred: headers.Deferred,
Deferred: deferred,
})
if err != nil {
logger.Debug("putter failed", "error", err)
Expand Down
11 changes: 6 additions & 5 deletions pkg/api/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,19 @@ func (s *Service) feedPostHandler(w http.ResponseWriter, r *http.Request) {
headers := struct {
BatchID []byte `map:"Swarm-Postage-Batch-Id" validate:"required"`
Pin bool `map:"Swarm-Pin"`
Deferred bool `map:"Swarm-Deferred-Upload"`
Deferred *bool `map:"Swarm-Deferred-Upload"`
}{}
if response := s.mapStructure(r.Header, &headers); response != nil {
response("invalid header params", logger, w)
return
}

var (
tag storer.SessionInfo
err error
tag storer.SessionInfo
err error
deferred = defaultUploadMethod(headers.Deferred)
)
if headers.Deferred || headers.Pin {
if deferred || headers.Pin {
tag, err = s.storer.NewSession()
if err != nil {
logger.Debug("get or create tag failed", "error", err)
Expand All @@ -169,7 +170,7 @@ func (s *Service) feedPostHandler(w http.ResponseWriter, r *http.Request) {
BatchID: headers.BatchID,
TagID: tag.TagID,
Pin: headers.Pin,
Deferred: headers.Deferred,
Deferred: deferred,
})
if err != nil {
logger.Debug("get putter failed", "error", err)
Expand Down

0 comments on commit cf00cf5

Please sign in to comment.