ingest documents
This commit is contained in:
@@ -131,3 +131,82 @@ func (s *Service) CreateFolder(ctx context.Context, actorID ident.UUID, params C
|
||||
|
||||
return documentFromDB(created), nil
|
||||
}
|
||||
|
||||
func (s *Service) CreateChild(ctx context.Context, actorID ident.UUID, params CreateChildParams) (*Document, error) {
|
||||
if err := s.requireActive(ctx, actorID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if params.OwnerID != actorID || params.CreatedBy != actorID {
|
||||
return nil, fmt.Errorf("%w: owner_id et created_by doivent être l'acteur", ErrPermissionDenied)
|
||||
}
|
||||
|
||||
name, nameKey, err := normalizeName(params.Name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
parent, err := s.getDocument(ctx, params.ParentID)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrDocumentNotFound) {
|
||||
return nil, fmt.Errorf("%w: %s", ErrParentNotFound, params.ParentID)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if parent.DeletedAt.Valid {
|
||||
return nil, fmt.Errorf("%w: %s", ErrParentDeleted, params.ParentID)
|
||||
}
|
||||
if !isContainerKind(parent.Kind) {
|
||||
return nil, fmt.Errorf("%w: %s", ErrInvalidParent, parent.Kind)
|
||||
}
|
||||
if err := s.can(ctx, actorID, params.ParentID, permission.Create); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
kind, err := s.db.Queries.GetDocumentKind(ctx, params.Kind)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, fmt.Errorf("%w: %s", ErrInvalidKind, params.Kind)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if kind.Kind == KindLibrary || kind.Hidden != 0 {
|
||||
return nil, fmt.Errorf("%w: %s", ErrInvalidKind, params.Kind)
|
||||
}
|
||||
|
||||
metadata, err := normalizeMetadata(params.Metadata)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
id, err := ident.NewV7()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
now := nowMillis()
|
||||
|
||||
created, err := s.db.WriteQueries.CreateDocument(ctx, database.CreateDocumentParams{
|
||||
ID: id,
|
||||
Kind: kind.Kind,
|
||||
OwnerID: params.OwnerID,
|
||||
CreatedBy: params.CreatedBy,
|
||||
ParentID: ¶ms.ParentID,
|
||||
Name: name,
|
||||
NameKey: nameKey,
|
||||
Path: parent.Path + hex.EncodeToString(id[:]) + "/",
|
||||
Depth: parent.Depth + 1,
|
||||
Metadata: metadata,
|
||||
AclInherit: aclInheritValue(params.ACLInherit),
|
||||
Rev: 1,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
})
|
||||
if err != nil {
|
||||
if isUniqueViolation(err) {
|
||||
return nil, fmt.Errorf("%w: %s", ErrNameTaken, name)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return documentFromDB(created), nil
|
||||
}
|
||||
|
||||
@@ -280,3 +280,111 @@ func TestCreateFolderRejectsDuplicateSibling(t *testing.T) {
|
||||
t.Fatalf("erreur = %v, attendu ErrNameTaken", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateChild(t *testing.T) {
|
||||
service := newTestService(t)
|
||||
ctx := context.Background()
|
||||
owner := mustUser(t, service, "owner")
|
||||
library := mustLibrary(t, service, owner, "Photos")
|
||||
|
||||
video, err := service.CreateChild(ctx, owner.ID, CreateChildParams{
|
||||
Kind: KindVideo,
|
||||
OwnerID: owner.ID,
|
||||
CreatedBy: owner.ID,
|
||||
ParentID: library.ID,
|
||||
Name: "film.mkv",
|
||||
Metadata: `{"source_path":"film.mkv"}`,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("CreateChild(video) : %v", err)
|
||||
}
|
||||
if video.Kind != KindVideo {
|
||||
t.Fatalf("kind = %q, attendu %q", video.Kind, KindVideo)
|
||||
}
|
||||
if video.ParentID == nil || *video.ParentID != library.ID {
|
||||
t.Fatalf("parent = %v, attendu %s", video.ParentID, library.ID)
|
||||
}
|
||||
if video.Depth != library.Depth+1 {
|
||||
t.Fatalf("depth = %d, attendu %d", video.Depth, library.Depth+1)
|
||||
}
|
||||
if video.Metadata != `{"source_path":"film.mkv"}` {
|
||||
t.Fatalf("metadata = %q", video.Metadata)
|
||||
}
|
||||
|
||||
folder, err := service.CreateChild(ctx, owner.ID, CreateChildParams{
|
||||
Kind: KindFolder,
|
||||
OwnerID: owner.ID,
|
||||
CreatedBy: owner.ID,
|
||||
ParentID: library.ID,
|
||||
Name: "Extras",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("CreateChild(folder) : %v", err)
|
||||
}
|
||||
if folder.Kind != KindFolder {
|
||||
t.Fatalf("kind = %q, attendu %q", folder.Kind, KindFolder)
|
||||
}
|
||||
if folder.Metadata != "{}" {
|
||||
t.Fatalf("metadata = %q, attendu {}", folder.Metadata)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateChildRejectsInvalidInputs(t *testing.T) {
|
||||
service := newTestService(t)
|
||||
ctx := context.Background()
|
||||
owner := mustUser(t, service, "owner")
|
||||
other := mustUser(t, service, "other")
|
||||
library := mustLibrary(t, service, owner, "Photos")
|
||||
|
||||
if _, err := service.CreateChild(ctx, owner.ID, CreateChildParams{
|
||||
Kind: KindVideo,
|
||||
OwnerID: other.ID,
|
||||
CreatedBy: owner.ID,
|
||||
ParentID: library.ID,
|
||||
Name: "film.mkv",
|
||||
}); !errors.Is(err, ErrPermissionDenied) {
|
||||
t.Fatalf("owner usurpé : erreur = %v, attendu ErrPermissionDenied", err)
|
||||
}
|
||||
|
||||
if _, err := service.CreateChild(ctx, owner.ID, CreateChildParams{
|
||||
Kind: "inconnu",
|
||||
OwnerID: owner.ID,
|
||||
CreatedBy: owner.ID,
|
||||
ParentID: library.ID,
|
||||
Name: "film.mkv",
|
||||
}); !errors.Is(err, ErrInvalidKind) {
|
||||
t.Fatalf("kind invalide : erreur = %v, attendu ErrInvalidKind", err)
|
||||
}
|
||||
|
||||
if _, err := service.CreateChild(ctx, owner.ID, CreateChildParams{
|
||||
Kind: KindVideo,
|
||||
OwnerID: owner.ID,
|
||||
CreatedBy: owner.ID,
|
||||
ParentID: library.ID,
|
||||
Name: "film.mkv",
|
||||
Metadata: "{",
|
||||
}); !errors.Is(err, ErrInvalidMetadata) {
|
||||
t.Fatalf("metadata invalide : erreur = %v, attendu ErrInvalidMetadata", err)
|
||||
}
|
||||
|
||||
video, err := service.CreateChild(ctx, owner.ID, CreateChildParams{
|
||||
Kind: KindVideo,
|
||||
OwnerID: owner.ID,
|
||||
CreatedBy: owner.ID,
|
||||
ParentID: library.ID,
|
||||
Name: "film.mkv",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("CreateChild(video) : %v", err)
|
||||
}
|
||||
|
||||
if _, err := service.CreateChild(ctx, owner.ID, CreateChildParams{
|
||||
Kind: KindVideo,
|
||||
OwnerID: owner.ID,
|
||||
CreatedBy: owner.ID,
|
||||
ParentID: video.ID,
|
||||
Name: "enfant.mkv",
|
||||
}); !errors.Is(err, ErrInvalidParent) {
|
||||
t.Fatalf("parent non conteneur : erreur = %v, attendu ErrInvalidParent", err)
|
||||
}
|
||||
}
|
||||
|
||||
+18
-2
@@ -14,8 +14,12 @@ const (
|
||||
)
|
||||
|
||||
const (
|
||||
KindLibrary = "library"
|
||||
KindFolder = "folder"
|
||||
KindLibrary = "library"
|
||||
KindFolder = "folder"
|
||||
KindVideo = "video"
|
||||
KindSubtitle = "subtitle"
|
||||
KindImage = "image"
|
||||
KindFile = "file"
|
||||
)
|
||||
|
||||
type Document struct {
|
||||
@@ -61,6 +65,16 @@ type CreateFolderParams struct {
|
||||
Metadata string
|
||||
}
|
||||
|
||||
type CreateChildParams struct {
|
||||
Kind string
|
||||
OwnerID ident.UUID
|
||||
CreatedBy ident.UUID
|
||||
ParentID ident.UUID
|
||||
Name string
|
||||
ACLInherit *bool
|
||||
Metadata string
|
||||
}
|
||||
|
||||
var (
|
||||
ErrInvalidName = errors.New("nom de document invalide")
|
||||
ErrNameTaken = errors.New("nom de document déjà utilisé")
|
||||
@@ -72,6 +86,8 @@ var (
|
||||
ErrCannotMoveRoot = errors.New("déplacement d'une bibliothèque racine interdit")
|
||||
ErrInvalidMove = errors.New("déplacement invalide")
|
||||
ErrInvalidVisibility = errors.New("visibilité invalide")
|
||||
ErrInvalidKind = errors.New("type de document invalide")
|
||||
ErrInvalidMetadata = errors.New("métadonnées invalides")
|
||||
ErrPolicyNotFound = errors.New("politique de stockage introuvable")
|
||||
ErrInvalidTrashBatch = errors.New("lot de corbeille invalide")
|
||||
ErrPermissionDenied = errors.New("permission refusée")
|
||||
|
||||
@@ -46,6 +46,37 @@ func (s *Service) Rename(ctx context.Context, actorID, documentID ident.UUID, na
|
||||
return documentFromDB(updated), nil
|
||||
}
|
||||
|
||||
func (s *Service) SetMetadata(ctx context.Context, actorID, documentID ident.UUID, metadata string) (*Document, error) {
|
||||
if err := s.requireActive(ctx, actorID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err := s.getActiveDocument(ctx, documentID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.can(ctx, actorID, documentID, permission.Write); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
normalized, err := normalizeMetadata(metadata)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
updated, err := s.db.WriteQueries.UpdateDocumentMetadata(ctx, database.UpdateDocumentMetadataParams{
|
||||
Metadata: normalized,
|
||||
UpdatedAt: nowMillis(),
|
||||
ID: documentID,
|
||||
})
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, fmt.Errorf("%w: %s", ErrDocumentNotFound, documentID)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return documentFromDB(updated), nil
|
||||
}
|
||||
|
||||
func (s *Service) Move(ctx context.Context, actorID, documentID, newParentID ident.UUID) (*Document, error) {
|
||||
document, err := s.getActiveDocument(ctx, documentID)
|
||||
if err != nil {
|
||||
|
||||
@@ -400,3 +400,26 @@ func TestMoveUpdatesTrashedDescendants(t *testing.T) {
|
||||
t.Fatalf("depth = %d, attendu 2", restored.Depth)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetMetadata(t *testing.T) {
|
||||
service := newTestService(t)
|
||||
ctx := context.Background()
|
||||
owner := mustUser(t, service, "owner")
|
||||
library := mustLibrary(t, service, owner, "Photos")
|
||||
video := mustKindDocument(t, service, owner, library, KindVideo, "film.mkv")
|
||||
|
||||
updated, err := service.SetMetadata(ctx, owner.ID, video.ID, `{"duration_ms":120000}`)
|
||||
if err != nil {
|
||||
t.Fatalf("SetMetadata : %v", err)
|
||||
}
|
||||
if updated.Metadata != `{"duration_ms":120000}` {
|
||||
t.Fatalf("metadata = %q", updated.Metadata)
|
||||
}
|
||||
if updated.Rev != video.Rev+1 {
|
||||
t.Fatalf("rev = %d, attendu %d", updated.Rev, video.Rev+1)
|
||||
}
|
||||
|
||||
if _, err := service.SetMetadata(ctx, owner.ID, video.ID, "{"); !errors.Is(err, ErrInvalidMetadata) {
|
||||
t.Fatalf("metadata invalide : erreur = %v, attendu ErrInvalidMetadata", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package document
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
@@ -136,6 +137,14 @@ func metadataValue(metadata string) string {
|
||||
return metadata
|
||||
}
|
||||
|
||||
func normalizeMetadata(metadata string) (string, error) {
|
||||
value := metadataValue(metadata)
|
||||
if !json.Valid([]byte(value)) {
|
||||
return "", ErrInvalidMetadata
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func aclInheritValue(value *bool) int64 {
|
||||
if value == nil || *value {
|
||||
return 1
|
||||
|
||||
Reference in New Issue
Block a user