Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions pkg/api/bzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
package api

import (
"bytes"
"context"
"encoding/hex"
"errors"
"fmt"
"io"
"net/http"
"path"
"path/filepath"
Expand Down Expand Up @@ -51,6 +53,9 @@
largeFileBufferSize = 16 * 32 * 1024

largeBufferFilesizeThreshold = 10 * 1000000 // ten megs

// contentTypeSniffLen is the max bytes used by http.DetectContentType.
contentTypeSniffLen = 512
)

func lookaheadBufferSize(size int64) int {
Expand All @@ -65,7 +70,7 @@
defer span.Finish()

headers := struct {
ContentType string `map:"Content-Type,mimeMediaType" validate:"required"`
ContentType string `map:"Content-Type,mimeMediaType"`
BatchID []byte `map:"Swarm-Postage-Batch-Id" validate:"required"`
SwarmTag uint64 `map:"Swarm-Tag"`
Pin bool `map:"Swarm-Pin"`
Expand Down Expand Up @@ -138,6 +143,12 @@
}

if headers.IsDir || headers.ContentType == multiPartFormData {
if headers.ContentType == "" {
logger.Debug("content-type required for directory upload")
logger.Error(nil, "content-type required for directory upload")
jsonhttp.BadRequest(w, errInvalidContentType)
return
}
s.dirUploadHandler(ctx, logger, span, ow, r, putter, r.Header.Get(ContentTypeHeader), headers.Encrypt, tag, headers.RLevel, headers.Act, headers.HistoryAddress)
return
}
Expand Down Expand Up @@ -174,8 +185,20 @@

p := requestPipelineFn(putter, encrypt, rLevel)

sniffBuf := make([]byte, contentTypeSniffLen)
n, err := io.ReadFull(r.Body, sniffBuf)
sniffBuf = sniffBuf[:n]
if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {

Check failure on line 191 in pkg/api/bzz.go

View workflow job for this annotation

GitHub Actions / Lint

comparing with != will fail on wrapped errors. Use errors.Is to check for a specific error (errorlint)
logger.Debug("body read failed", "file_name", queries.FileName, "error", err)
logger.Error(nil, "body read failed", "file_name", queries.FileName)
jsonhttp.BadRequest(w, "failed to read request body")
return
}
contentType := http.DetectContentType(sniffBuf)
bodyForStore := io.MultiReader(bytes.NewReader(sniffBuf), r.Body)

// first store the file and get its reference
fr, err := p(ctx, r.Body)
fr, err := p(ctx, bodyForStore)
if err != nil {
logger.Debug("file store failed", "file_name", queries.FileName, "error", err)
logger.Error(nil, "file store failed", "file_name", queries.FileName)
Expand Down Expand Up @@ -240,7 +263,7 @@
}

fileMtdt := map[string]string{
manifest.EntryMetadataContentTypeKey: r.Header.Get(ContentTypeHeader), // Content-Type has already been validated.
manifest.EntryMetadataContentTypeKey: contentType,
manifest.EntryMetadataFilenameKey: queries.FileName,
}

Expand Down
Loading