api + cli
This commit is contained in:
@@ -0,0 +1,338 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"mediarr/document"
|
||||
"mediarr/ident"
|
||||
)
|
||||
|
||||
const defaultStoragePolicyID = "00000000-0000-7000-8000-000000000101"
|
||||
|
||||
type documentDTO struct {
|
||||
ID string `json:"id"`
|
||||
Kind string `json:"kind"`
|
||||
OwnerID string `json:"owner_id"`
|
||||
CreatedBy string `json:"created_by"`
|
||||
ParentID *string `json:"parent_id,omitempty"`
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
Depth int64 `json:"depth"`
|
||||
HeadVersionID *string `json:"head_version_id,omitempty"`
|
||||
Metadata json.RawMessage `json:"metadata"`
|
||||
PolicyID *string `json:"policy_id,omitempty"`
|
||||
Visibility string `json:"visibility,omitempty"`
|
||||
ACLInherit bool `json:"acl_inherit"`
|
||||
Rev int64 `json:"rev"`
|
||||
CreatedAt int64 `json:"created_at"`
|
||||
UpdatedAt int64 `json:"updated_at"`
|
||||
DeletedAt *int64 `json:"deleted_at,omitempty"`
|
||||
}
|
||||
|
||||
type createLibraryRequest struct {
|
||||
Name string `json:"name"`
|
||||
PolicyID string `json:"policy_id,omitempty"`
|
||||
Visibility string `json:"visibility,omitempty"`
|
||||
ACLInherit *bool `json:"acl_inherit,omitempty"`
|
||||
Metadata json.RawMessage `json:"metadata,omitempty"`
|
||||
}
|
||||
|
||||
type createChildRequest struct {
|
||||
Kind string `json:"kind"`
|
||||
Name string `json:"name"`
|
||||
ACLInherit *bool `json:"acl_inherit,omitempty"`
|
||||
Metadata json.RawMessage `json:"metadata,omitempty"`
|
||||
}
|
||||
|
||||
type renameRequest struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type metadataRequest struct {
|
||||
Metadata json.RawMessage `json:"metadata"`
|
||||
}
|
||||
|
||||
type moveRequest struct {
|
||||
ParentID string `json:"parent_id"`
|
||||
}
|
||||
|
||||
func (s *Server) handleListLibraries(w http.ResponseWriter, r *http.Request) {
|
||||
libraries, err := s.node.Documents().ListLibraries(r.Context(), actorID(r))
|
||||
if err != nil {
|
||||
writeErrorFor(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, documentsDTO(libraries))
|
||||
}
|
||||
|
||||
func (s *Server) handleCreateLibrary(w http.ResponseWriter, r *http.Request) {
|
||||
var request createLibraryRequest
|
||||
if err := decodeJSON(w, r, &request); err != nil {
|
||||
writeErrorFor(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
policyID := request.PolicyID
|
||||
if policyID == "" {
|
||||
policyID = defaultStoragePolicyID
|
||||
}
|
||||
parsedPolicyID, err := ident.Parse(policyID)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "policy_id invalide")
|
||||
return
|
||||
}
|
||||
|
||||
metadata, err := metadataString(request.Metadata)
|
||||
if err != nil {
|
||||
writeErrorFor(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
created, err := s.node.Documents().CreateLibrary(r.Context(), actorID(r), document.CreateLibraryParams{
|
||||
OwnerID: actorID(r),
|
||||
CreatedBy: actorID(r),
|
||||
Name: request.Name,
|
||||
PolicyID: parsedPolicyID,
|
||||
Visibility: request.Visibility,
|
||||
ACLInherit: request.ACLInherit,
|
||||
Metadata: metadata,
|
||||
})
|
||||
if err != nil {
|
||||
writeErrorFor(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Location", "/api/documents/"+created.ID.String())
|
||||
writeJSON(w, http.StatusCreated, documentDTOFrom(created))
|
||||
}
|
||||
|
||||
func (s *Server) handleGetDocument(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := idParam(r, "id")
|
||||
if err != nil {
|
||||
writeErrorFor(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
found, err := s.node.Documents().Get(r.Context(), actorID(r), id)
|
||||
if err != nil {
|
||||
writeErrorFor(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, documentDTOFrom(found))
|
||||
}
|
||||
|
||||
func (s *Server) handleListChildren(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := idParam(r, "id")
|
||||
if err != nil {
|
||||
writeErrorFor(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
children, err := s.node.Documents().ListChildren(r.Context(), actorID(r), id)
|
||||
if err != nil {
|
||||
writeErrorFor(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, documentsDTO(children))
|
||||
}
|
||||
|
||||
func (s *Server) handleCreateChild(w http.ResponseWriter, r *http.Request) {
|
||||
parentID, err := idParam(r, "id")
|
||||
if err != nil {
|
||||
writeErrorFor(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
var request createChildRequest
|
||||
if err := decodeJSON(w, r, &request); err != nil {
|
||||
writeErrorFor(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
metadata, err := metadataString(request.Metadata)
|
||||
if err != nil {
|
||||
writeErrorFor(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
created, err := s.node.Documents().CreateChild(r.Context(), actorID(r), document.CreateChildParams{
|
||||
Kind: request.Kind,
|
||||
OwnerID: actorID(r),
|
||||
CreatedBy: actorID(r),
|
||||
ParentID: parentID,
|
||||
Name: request.Name,
|
||||
ACLInherit: request.ACLInherit,
|
||||
Metadata: metadata,
|
||||
})
|
||||
if err != nil {
|
||||
writeErrorFor(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Location", "/api/documents/"+created.ID.String())
|
||||
writeJSON(w, http.StatusCreated, documentDTOFrom(created))
|
||||
}
|
||||
|
||||
func (s *Server) handleRename(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := idParam(r, "id")
|
||||
if err != nil {
|
||||
writeErrorFor(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
var request renameRequest
|
||||
if err := decodeJSON(w, r, &request); err != nil {
|
||||
writeErrorFor(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
updated, err := s.node.Documents().Rename(r.Context(), actorID(r), id, request.Name)
|
||||
if err != nil {
|
||||
writeErrorFor(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, documentDTOFrom(updated))
|
||||
}
|
||||
|
||||
func (s *Server) handleSetMetadata(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := idParam(r, "id")
|
||||
if err != nil {
|
||||
writeErrorFor(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
var request metadataRequest
|
||||
if err := decodeJSON(w, r, &request); err != nil {
|
||||
writeErrorFor(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
metadata, err := metadataString(request.Metadata)
|
||||
if err != nil {
|
||||
writeErrorFor(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
updated, err := s.node.Documents().SetMetadata(r.Context(), actorID(r), id, metadata)
|
||||
if err != nil {
|
||||
writeErrorFor(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, documentDTOFrom(updated))
|
||||
}
|
||||
|
||||
func (s *Server) handleMove(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := idParam(r, "id")
|
||||
if err != nil {
|
||||
writeErrorFor(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
var request moveRequest
|
||||
if err := decodeJSON(w, r, &request); err != nil {
|
||||
writeErrorFor(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
parentID, err := ident.Parse(request.ParentID)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "parent_id invalide")
|
||||
return
|
||||
}
|
||||
|
||||
moved, err := s.node.Documents().Move(r.Context(), actorID(r), id, parentID)
|
||||
if err != nil {
|
||||
writeErrorFor(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, documentDTOFrom(moved))
|
||||
}
|
||||
|
||||
func (s *Server) handleTrash(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := idParam(r, "id")
|
||||
if err != nil {
|
||||
writeErrorFor(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
trashed, err := s.node.Documents().Trash(r.Context(), actorID(r), id)
|
||||
if err != nil {
|
||||
writeErrorFor(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, documentDTOFrom(trashed))
|
||||
}
|
||||
|
||||
func (s *Server) handleRestore(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := idParam(r, "id")
|
||||
if err != nil {
|
||||
writeErrorFor(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
restored, err := s.node.Documents().Restore(r.Context(), actorID(r), id)
|
||||
if err != nil {
|
||||
writeErrorFor(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, documentDTOFrom(restored))
|
||||
}
|
||||
|
||||
func actorID(r *http.Request) ident.UUID {
|
||||
user := currentUser(r)
|
||||
if user == nil {
|
||||
return ident.UUID{}
|
||||
}
|
||||
return user.Principal.ID
|
||||
}
|
||||
|
||||
func documentsDTO(documents []document.Document) []documentDTO {
|
||||
result := make([]documentDTO, 0, len(documents))
|
||||
for i := range documents {
|
||||
result = append(result, documentDTOFrom(&documents[i]))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func documentDTOFrom(doc *document.Document) documentDTO {
|
||||
dto := documentDTO{
|
||||
ID: doc.ID.String(),
|
||||
Kind: doc.Kind,
|
||||
OwnerID: doc.OwnerID.String(),
|
||||
CreatedBy: doc.CreatedBy.String(),
|
||||
Name: doc.Name,
|
||||
Path: doc.Path,
|
||||
Depth: doc.Depth,
|
||||
Metadata: json.RawMessage(doc.Metadata),
|
||||
Visibility: doc.Visibility,
|
||||
ACLInherit: doc.ACLInherit,
|
||||
Rev: doc.Rev,
|
||||
CreatedAt: doc.CreatedAt,
|
||||
UpdatedAt: doc.UpdatedAt,
|
||||
DeletedAt: doc.DeletedAt,
|
||||
}
|
||||
|
||||
if doc.ParentID != nil {
|
||||
value := doc.ParentID.String()
|
||||
dto.ParentID = &value
|
||||
}
|
||||
if doc.HeadVersionID != nil {
|
||||
value := doc.HeadVersionID.String()
|
||||
dto.HeadVersionID = &value
|
||||
}
|
||||
if doc.PolicyID != nil {
|
||||
value := doc.PolicyID.String()
|
||||
dto.PolicyID = &value
|
||||
}
|
||||
|
||||
return dto
|
||||
}
|
||||
Reference in New Issue
Block a user