Skip to content
Merged
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
21 changes: 20 additions & 1 deletion internal/api/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import (
"encoding/json"
"fmt"
"io"
"slices"
"net/http"
openapi_types "github.com/oapi-codegen/runtime/types"

"artifacts/internal/storage"
"artifacts/internal/storage/storage_error"
)

var chartMIMEType = []string{"application/gzip"}

type Server struct{
storageHandler storage.Storage
}
Expand Down Expand Up @@ -88,6 +91,14 @@ func (s Server) AddChart(w http.ResponseWriter, r *http.Request) {

// Extract chart name from form data
name := r.FormValue("name")
if name == "" {
w.WriteHeader(http.StatusBadRequest)
_ = json.NewEncoder(w).Encode(Error{
Code: new(int(http.StatusBadRequest)),
Message: new(string("Missing 'name' parameter")),
})
return
}

// Extract chart bytes from form data
f, fh, err := r.FormFile("chart")
Expand All @@ -101,7 +112,15 @@ func (s Server) AddChart(w http.ResponseWriter, r *http.Request) {
}
defer f.Close()

// TODO: validate file type is .tgz
// Reject unsupported MIME type
if eq := slices.Compare(fh.Header["Content-Type"], chartMIMEType); eq != 0 {
w.WriteHeader(http.StatusUnsupportedMediaType)
_ = json.NewEncoder(w).Encode(Error{
Code: new(int(http.StatusUnsupportedMediaType)),
Message: new(string(fmt.Sprintf("Unsupported type: %v", fh.Header["Content-Type"]))),
})
return
}

data, err := io.ReadAll(f)
if err != nil {
Expand Down