Files
shorebird-server/internal/api/handlers/storage_handler.go
T
Tony 3bb4ab494e feat: implement database and storage abstractions
- Added `internal/db/store.go` to define the Store interface for database operations, including user, organization, app, channel, release, patch, and artifact management.
- Introduced `internal/models/models.go` changes to reflect updated organization and artifact response structures.
- Created `internal/storage/factory.go`, `local.go`, and `s3.go` to implement storage backends for local filesystem and S3-compatible services.
- Removed deprecated `internal/storage/storage.go` and refactored storage interface into `internal/storage/store.go`.
- Updated frontend JavaScript in `web/js/app.js` to display server health information, including storage and database backend details.
2026-06-12 08:45:01 +08:00

57 lines
1.5 KiB
Go

package handlers
import (
"io"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/shorebird-server/internal/storage"
)
// StorageHandler provides upload/download endpoints for local storage.
type StorageHandler struct {
store storage.Store
}
// NewStorageHandler creates a new storage HTTP handler.
func NewStorageHandler(store storage.Store) *StorageHandler {
return &StorageHandler{store: store}
}
// Upload handles POST /storage/upload/{scope}/{key}
func (h *StorageHandler) Upload(w http.ResponseWriter, r *http.Request) {
scope := chi.URLParam(r, "scope")
key := chi.URLParam(r, "*")
isPublic := scope == "patches"
ct := r.Header.Get("Content-Type")
if ct == "" {
ct = "application/octet-stream"
}
if err := h.store.UploadObject(r.Context(), key, r.Body, r.ContentLength, ct, isPublic); err != nil {
respondError(w, http.StatusInternalServerError, "Upload failed: "+err.Error(), nil)
return
}
w.WriteHeader(http.StatusOK)
}
// Download handles GET /storage/dl/{scope}/{key}
func (h *StorageHandler) Download(w http.ResponseWriter, r *http.Request) {
scope := chi.URLParam(r, "scope")
key := chi.URLParam(r, "*")
isPublic := scope == "patches"
reader, err := h.store.GetObject(r.Context(), key, isPublic)
if err != nil {
respondError(w, http.StatusNotFound, "File not found", nil)
return
}
defer reader.Close()
contentType := "application/octet-stream"
w.Header().Set("Content-Type", contentType)
w.Header().Set("Accept-Ranges", "bytes")
io.Copy(w, reader)
}