8f8a30ac34
Introduces repo_type (remote/local) as a separate axis from package_type
so that any package type can be hosted locally. A terraform local repo
is package_type=terraform + repo_type=local.
- Remote model gains RepoType field (defaults to "remote")
- Database schema adds repo_type column with migration for existing DBs
- V1 proxy adds /api/v1/local/{name}/* route for serving local files
- V2 upload via PUT /api/v2/remotes/{name}/files/{path} checks
repo_type=local and returns 409 on duplicate (no overwrites)
- V2 create validates repo_type and requires base_url only for remotes
- Removes the previous terraform-local PackageType approach
144 lines
3.8 KiB
Go
144 lines
3.8 KiB
Go
package v2
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"git.unkin.net/unkin/artifactapi/internal/database"
|
|
"git.unkin.net/unkin/artifactapi/internal/provider"
|
|
"git.unkin.net/unkin/artifactapi/internal/storage"
|
|
"git.unkin.net/unkin/artifactapi/pkg/models"
|
|
)
|
|
|
|
type LocalHandler struct {
|
|
db *database.DB
|
|
store *storage.S3
|
|
cas *storage.CAS
|
|
}
|
|
|
|
func NewLocalHandler(db *database.DB, store *storage.S3) *LocalHandler {
|
|
return &LocalHandler{
|
|
db: db,
|
|
store: store,
|
|
cas: storage.NewCAS(store),
|
|
}
|
|
}
|
|
|
|
func (h *LocalHandler) Routes() chi.Router {
|
|
r := chi.NewRouter()
|
|
r.Put("/*", h.upload)
|
|
r.Get("/*", h.download)
|
|
r.Delete("/*", h.remove)
|
|
return r
|
|
}
|
|
|
|
func (h *LocalHandler) upload(w http.ResponseWriter, r *http.Request) {
|
|
repoName := chi.URLParam(r, "name")
|
|
filePath := chi.URLParam(r, "*")
|
|
|
|
if filePath == "" {
|
|
http.Error(w, "file path required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
remote, err := h.db.GetRemote(r.Context(), repoName)
|
|
if err != nil {
|
|
http.Error(w, fmt.Sprintf("remote %q not found", repoName), http.StatusNotFound)
|
|
return
|
|
}
|
|
if remote.RepoType != models.RepoTypeLocal {
|
|
http.Error(w, "upload only allowed for local repository types", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
existing, err := h.db.GetLocalFile(r.Context(), repoName, filePath)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
if existing != nil {
|
|
http.Error(w, fmt.Sprintf("file %q already exists; overwrites are not allowed", filePath), http.StatusConflict)
|
|
return
|
|
}
|
|
|
|
prov, err := provider.Get(remote.PackageType)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
contentType := prov.ContentType(filePath)
|
|
if ct := r.Header.Get("Content-Type"); ct != "" && ct != "application/octet-stream" {
|
|
contentType = ct
|
|
}
|
|
|
|
result, err := h.cas.Store(r.Context(), r.Body, contentType)
|
|
if err != nil {
|
|
http.Error(w, fmt.Sprintf("store failed: %v", err), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
if err := h.db.UpsertBlob(r.Context(), result.ContentHash, result.S3Key, result.SizeBytes, contentType); err != nil {
|
|
http.Error(w, fmt.Sprintf("record blob: %v", err), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
if err := h.db.CreateLocalFile(r.Context(), repoName, filePath, result.ContentHash); err != nil {
|
|
if errors.Is(err, database.ErrAlreadyExists) {
|
|
http.Error(w, fmt.Sprintf("file %q already exists; overwrites are not allowed", filePath), http.StatusConflict)
|
|
return
|
|
}
|
|
http.Error(w, fmt.Sprintf("record file: %v", err), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusCreated, map[string]any{
|
|
"path": filePath,
|
|
"content_hash": result.ContentHash,
|
|
"size_bytes": result.SizeBytes,
|
|
})
|
|
}
|
|
|
|
func (h *LocalHandler) download(w http.ResponseWriter, r *http.Request) {
|
|
repoName := chi.URLParam(r, "name")
|
|
filePath := chi.URLParam(r, "*")
|
|
|
|
file, err := h.db.GetLocalFile(r.Context(), repoName, filePath)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
if file == nil {
|
|
http.Error(w, "not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
s3Key := storage.BlobKey(file.ContentHash[len("sha256:"):])
|
|
reader, info, err := h.store.Download(r.Context(), s3Key)
|
|
if err != nil {
|
|
http.Error(w, fmt.Sprintf("download failed: %v", err), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
defer reader.Close()
|
|
|
|
w.Header().Set("Content-Type", info.ContentType)
|
|
w.Header().Set("Content-Length", fmt.Sprintf("%d", info.Size))
|
|
w.WriteHeader(http.StatusOK)
|
|
io.Copy(w, reader)
|
|
}
|
|
|
|
func (h *LocalHandler) remove(w http.ResponseWriter, r *http.Request) {
|
|
repoName := chi.URLParam(r, "name")
|
|
filePath := chi.URLParam(r, "*")
|
|
|
|
if err := h.db.DeleteLocalFile(r.Context(), repoName, filePath); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|