Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions api/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ paths:
schema:
type: object
properties:
name:
type: string
chart:
type: string
format: binary
Expand Down
1 change: 1 addition & 0 deletions internal/api/api.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 77 additions & 5 deletions internal/api/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package api
import (
"encoding/json"
"fmt"
"io"
"net/http"
//"path"
openapi_types "github.com/oapi-codegen/runtime/types"

"artifact-store/internal/storage"
"artifact-store/internal/storage/storage_error"
Expand Down Expand Up @@ -75,8 +76,79 @@ func (Server) GetChartVersions(w http.ResponseWriter, r *http.Request, name stri
_ = json.NewEncoder(w).Encode(res)
}

func (Server) AddChart(w http.ResponseWriter, r *http.Request) {
// TODO: interface to storage backend for creating
res := fmt.Sprintf("%v", r)
_ = json.NewEncoder(w).Encode(res)
func (s Server) AddChart(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") // TODO: set globally, or default?

// Set max header size (50MB)
r.Body = http.MaxBytesReader(w, r.Body, 50<<20) // TODO: set globally, or default?

// Limit parsed form size (32MB)
if err := r.ParseMultipartForm(32 << 20); err != nil {
w.WriteHeader(http.StatusBadRequest)
_ = json.NewEncoder(w).Encode(Error{
Code: new(int(http.StatusBadRequest)),
Message: new(string("Invalid form data")),
})
return
}
defer func() {
if r.MultipartForm != nil {
// Clean up temp files
r.MultipartForm.RemoveAll()
}
}()

// Extract chart name from form data
name := r.FormValue("name")

// Extract chart bytes from form data
f, fh, err := r.FormFile("chart")
if err != nil {
w.WriteHeader(http.StatusBadRequest)
_ = json.NewEncoder(w).Encode(Error{
Code: new(int(http.StatusBadRequest)),
Message: new(string("Missing chart")),
})
return
}
defer f.Close()

// TODO: validate file type is .tgz

data, err := io.ReadAll(f)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
_ = json.NewEncoder(w).Encode(Error{
Code: new(int(http.StatusBadRequest)),
Message: new(string("Malformed file contents")),
})
}

// Construct runtime type for compatibility with generated types
file := &openapi_types.File{}
file.InitFromBytes(data, fh.Filename)

bytes, err := file.Bytes()
if err != nil {
w.WriteHeader(http.StatusBadRequest)
_ = json.NewEncoder(w).Encode(Error{
Code: new(int(http.StatusBadRequest)),
Message: new(string("Malformed file upload")),
})
}

if err := s.storageHandler.Write(name, bytes); err != nil { // TODO: return created version
w.WriteHeader(http.StatusInternalServerError)
_ = json.NewEncoder(w).Encode(Error{
Code: new(int(http.StatusInternalServerError)),
Message: new(string("Error occurred during file IO")),
})
return
}

w.WriteHeader(http.StatusCreated)
_ = json.NewEncoder(w).Encode(Error{
Code: new(int(http.StatusCreated)),
Message: new(string(fmt.Sprintf("Chart '%v' created", name))),
})
}
26 changes: 23 additions & 3 deletions internal/storage/backend/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package backend

import (
"io/fs"
"log"
"os"
"path"
"path/filepath"
Expand All @@ -10,6 +11,7 @@ import (
)

type FileSystem struct {
root string
Path fs.FS
}

Expand All @@ -18,14 +20,32 @@ func NewFSBackend(path string) (*FileSystem, error) {
return nil, err
}
return &FileSystem{
root: path,
Path: os.DirFS(path),
}, nil
}

// Implementation of the `Writer` interface
func (f *FileSystem) Write(bytes []byte) error { // TODO: define type `artifact` or similar instead
// TODO: create path if not exists (requires more parameters)
return nil
func (f *FileSystem) Write(name string, bytes []byte) error { // TODO: define type `artifact` or similar instead
path := filepath.Join(f.root, name)

// TODO: to be implemented when we decide to add repositories
// if err := os.MkdirAll(filepath.Dir(<f.root + repo>), 0o744); err != nil {
// return err
// }

file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
log.Fatal(err)
return err
}
defer file.Close()

_, err = file.Write(bytes)
if err != nil {
log.Fatal(err)
}
return err
}

// Implementation of the `Reader` interface
Expand Down
2 changes: 1 addition & 1 deletion internal/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

type Storage interface {
Read(location string, version string) ([]byte, error)
Write(bytes []byte) error
Write(name string, bytes []byte) error
}

func New(config config.StorageConfig) (Storage, error) {
Expand Down