api + cli
This commit is contained in:
+149
@@ -0,0 +1,149 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"mediarr/auth"
|
||||
"mediarr/content"
|
||||
"mediarr/document"
|
||||
"mediarr/ident"
|
||||
"mediarr/identity"
|
||||
"mediarr/storage"
|
||||
)
|
||||
|
||||
const (
|
||||
maxJSONBody = 1 << 20
|
||||
maxUploadBody = 2 << 30
|
||||
)
|
||||
|
||||
func writeJSON(w http.ResponseWriter, status int, value any) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(status)
|
||||
if status == http.StatusNoContent {
|
||||
return
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(value)
|
||||
}
|
||||
|
||||
func writeError(w http.ResponseWriter, status int, message string) {
|
||||
if message == "" {
|
||||
message = http.StatusText(status)
|
||||
}
|
||||
writeJSON(w, status, map[string]string{"error": message})
|
||||
}
|
||||
|
||||
func writeErrorFor(w http.ResponseWriter, err error) {
|
||||
status := statusForError(err)
|
||||
message := err.Error()
|
||||
switch status {
|
||||
case http.StatusInternalServerError:
|
||||
message = "erreur interne"
|
||||
case http.StatusRequestEntityTooLarge:
|
||||
message = "requête trop volumineuse"
|
||||
}
|
||||
writeError(w, status, message)
|
||||
}
|
||||
|
||||
func statusForError(err error) int {
|
||||
var maxBytes *http.MaxBytesError
|
||||
if errors.As(err, &maxBytes) {
|
||||
return http.StatusRequestEntityTooLarge
|
||||
}
|
||||
|
||||
switch {
|
||||
case errors.Is(err, auth.ErrInvalidCredentials),
|
||||
errors.Is(err, auth.ErrInvalidSession):
|
||||
return http.StatusUnauthorized
|
||||
case errors.Is(err, auth.ErrAccountLocked),
|
||||
errors.Is(err, auth.ErrAccountDisabled),
|
||||
errors.Is(err, document.ErrPermissionDenied):
|
||||
return http.StatusForbidden
|
||||
case errors.Is(err, document.ErrDocumentNotFound),
|
||||
errors.Is(err, document.ErrParentNotFound),
|
||||
errors.Is(err, document.ErrPolicyNotFound),
|
||||
errors.Is(err, content.ErrVersionNotFound),
|
||||
errors.Is(err, content.ErrContentUnavailable),
|
||||
errors.Is(err, storage.ErrNotFound):
|
||||
return http.StatusNotFound
|
||||
case errors.Is(err, document.ErrNameTaken),
|
||||
errors.Is(err, document.ErrDocumentDeleted),
|
||||
errors.Is(err, document.ErrParentDeleted),
|
||||
errors.Is(err, auth.ErrLoginTaken),
|
||||
errors.Is(err, content.ErrVersionIsHead),
|
||||
errors.Is(err, content.ErrVersionMismatch),
|
||||
errors.Is(err, content.ErrBlobUnavailable),
|
||||
errors.Is(err, content.ErrBlobCorrupt):
|
||||
return http.StatusConflict
|
||||
case errors.Is(err, content.ErrContentPurged):
|
||||
return http.StatusGone
|
||||
case errors.Is(err, content.ErrStorageNotConfigured),
|
||||
errors.Is(err, content.ErrStorageBackendMissing):
|
||||
return http.StatusServiceUnavailable
|
||||
case errors.Is(err, document.ErrInvalidName),
|
||||
errors.Is(err, document.ErrInvalidKind),
|
||||
errors.Is(err, document.ErrInvalidMetadata),
|
||||
errors.Is(err, document.ErrInvalidParent),
|
||||
errors.Is(err, document.ErrInvalidMove),
|
||||
errors.Is(err, document.ErrCannotMoveRoot),
|
||||
errors.Is(err, document.ErrInvalidVisibility),
|
||||
errors.Is(err, document.ErrInvalidTrashBatch),
|
||||
errors.Is(err, auth.ErrInvalidLogin),
|
||||
errors.Is(err, auth.ErrWeakPassword),
|
||||
errors.Is(err, identity.ErrInvalidRole),
|
||||
errors.Is(err, content.ErrInvalidOrigin),
|
||||
errors.Is(err, content.ErrInvalidSource),
|
||||
errors.Is(err, content.ErrNoContentKind),
|
||||
errors.Is(err, content.ErrNoStoragePolicy),
|
||||
errors.Is(err, content.ErrInvalidRetention):
|
||||
return http.StatusBadRequest
|
||||
default:
|
||||
return http.StatusInternalServerError
|
||||
}
|
||||
}
|
||||
|
||||
func decodeJSON(w http.ResponseWriter, r *http.Request, destination any) error {
|
||||
r.Body = http.MaxBytesReader(w, r.Body, maxJSONBody)
|
||||
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
decoder.DisallowUnknownFields()
|
||||
|
||||
if err := decoder.Decode(destination); err != nil {
|
||||
if errors.Is(err, io.EOF) {
|
||||
return errors.New("corps JSON requis")
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
var extra any
|
||||
if err := decoder.Decode(&extra); !errors.Is(err, io.EOF) {
|
||||
return errors.New("corps JSON invalide")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func metadataString(raw json.RawMessage) (string, error) {
|
||||
if len(raw) == 0 {
|
||||
return "{}", nil
|
||||
}
|
||||
if !json.Valid(raw) {
|
||||
return "", fmt.Errorf("%w", document.ErrInvalidMetadata)
|
||||
}
|
||||
return string(raw), nil
|
||||
}
|
||||
|
||||
func idParam(r *http.Request, name string) (ident.UUID, error) {
|
||||
value := r.PathValue(name)
|
||||
if value == "" {
|
||||
return ident.UUID{}, errors.New("identifiant manquant")
|
||||
}
|
||||
id, err := ident.Parse(value)
|
||||
if err != nil {
|
||||
return ident.UUID{}, fmt.Errorf("identifiant invalide: %w", err)
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
Reference in New Issue
Block a user