Compare commits
11
Commits
276a9fe320
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2beba7d010 | ||
|
|
8d48fadd01 | ||
|
|
4f18d94158 | ||
|
|
01233c216e | ||
|
|
7b3f20b8d7 | ||
|
|
cc0ddd0dfb | ||
|
|
610192e2d5 | ||
|
|
29a6ab40f0 | ||
|
|
03da7d9fff | ||
|
|
9d4f827d38 | ||
|
|
f16363cb60 |
@@ -3,3 +3,5 @@
|
|||||||
*.db-shm
|
*.db-shm
|
||||||
*.key
|
*.key
|
||||||
.cluster-key-*
|
.cluster-key-*
|
||||||
|
data
|
||||||
|
suggestions
|
||||||
+50
@@ -0,0 +1,50 @@
|
|||||||
|
package acl
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"mediarr/ident"
|
||||||
|
"mediarr/permission"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Permission = permission.Permission
|
||||||
|
|
||||||
|
const (
|
||||||
|
PermissionRead = permission.Read
|
||||||
|
PermissionWrite = permission.Write
|
||||||
|
PermissionCreate = permission.Create
|
||||||
|
PermissionDelete = permission.Delete
|
||||||
|
PermissionShare = permission.Share
|
||||||
|
PermissionAdmin = permission.Admin
|
||||||
|
PermissionRequest = permission.Request
|
||||||
|
PermissionAll = permission.All
|
||||||
|
)
|
||||||
|
|
||||||
|
type Entry struct {
|
||||||
|
ID ident.UUID
|
||||||
|
DocumentID ident.UUID
|
||||||
|
SubjectID ident.UUID
|
||||||
|
Permissions Permission
|
||||||
|
Propagate bool
|
||||||
|
ExpiresAt *time.Time
|
||||||
|
GrantedBy ident.UUID
|
||||||
|
CreatedAt time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
type GrantParams struct {
|
||||||
|
DocumentID ident.UUID
|
||||||
|
SubjectID ident.UUID
|
||||||
|
Permissions Permission
|
||||||
|
Propagate bool
|
||||||
|
ExpiresAt *time.Time
|
||||||
|
GrantedBy ident.UUID
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrInvalidPermissions = errors.New("permissions invalides")
|
||||||
|
ErrDocumentNotFound = errors.New("document introuvable")
|
||||||
|
ErrSubjectNotFound = errors.New("sujet ACL introuvable")
|
||||||
|
ErrEntryNotFound = errors.New("entrée ACL introuvable")
|
||||||
|
ErrDocumentCycle = errors.New("cycle dans l'arborescence des documents")
|
||||||
|
)
|
||||||
+123
@@ -0,0 +1,123 @@
|
|||||||
|
package acl
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"mediarr/database"
|
||||||
|
"mediarr/ident"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s *Service) Grant(ctx context.Context, params GrantParams) (*Entry, error) {
|
||||||
|
if !params.Permissions.Valid() {
|
||||||
|
return nil, fmt.Errorf("%w: %d", ErrInvalidPermissions, params.Permissions)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := s.db.Queries.GetDocument(ctx, params.DocumentID); err != nil {
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return nil, fmt.Errorf("%w: %s", ErrDocumentNotFound, params.DocumentID)
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := s.loadSubject(ctx, params.SubjectID); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
id, err := ident.NewV7()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
|
||||||
|
entry, err := s.db.WriteQueries.UpsertACLEntry(ctx, database.UpsertACLEntryParams{
|
||||||
|
ID: id,
|
||||||
|
DocumentID: params.DocumentID,
|
||||||
|
SubjectID: params.SubjectID,
|
||||||
|
Permissions: int64(params.Permissions),
|
||||||
|
Propagate: boolToInt(params.Propagate),
|
||||||
|
ExpiresAt: timeToNull(params.ExpiresAt),
|
||||||
|
GrantedBy: params.GrantedBy,
|
||||||
|
CreatedAt: now.UnixMilli(),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return entryFromDB(entry), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) Revoke(ctx context.Context, documentID, subjectID ident.UUID) error {
|
||||||
|
_, err := s.db.WriteQueries.RevokeACLEntry(ctx, database.RevokeACLEntryParams{
|
||||||
|
RevokedAt: sql.NullInt64{
|
||||||
|
Int64: time.Now().UnixMilli(),
|
||||||
|
Valid: true,
|
||||||
|
},
|
||||||
|
DocumentID: documentID,
|
||||||
|
SubjectID: subjectID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return ErrEntryNotFound
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) List(ctx context.Context, documentID ident.UUID) ([]Entry, error) {
|
||||||
|
rows, err := s.db.Queries.ListActiveACLEntriesForDocument(ctx, database.ListActiveACLEntriesForDocumentParams{
|
||||||
|
DocumentID: documentID,
|
||||||
|
Now: time.Now().UnixMilli(),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
entries := make([]Entry, 0, len(rows))
|
||||||
|
for _, row := range rows {
|
||||||
|
entries = append(entries, *entryFromDB(row))
|
||||||
|
}
|
||||||
|
|
||||||
|
return entries, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func entryFromDB(entry database.AclEntry) *Entry {
|
||||||
|
return &Entry{
|
||||||
|
ID: entry.ID,
|
||||||
|
DocumentID: entry.DocumentID,
|
||||||
|
SubjectID: entry.SubjectID,
|
||||||
|
Permissions: Permission(entry.Permissions),
|
||||||
|
Propagate: entry.Propagate != 0,
|
||||||
|
ExpiresAt: nullToTime(entry.ExpiresAt),
|
||||||
|
GrantedBy: entry.GrantedBy,
|
||||||
|
CreatedAt: time.UnixMilli(entry.CreatedAt),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func boolToInt(value bool) int64 {
|
||||||
|
if value {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func timeToNull(value *time.Time) sql.NullInt64 {
|
||||||
|
if value == nil {
|
||||||
|
return sql.NullInt64{}
|
||||||
|
}
|
||||||
|
return sql.NullInt64{Int64: value.UnixMilli(), Valid: true}
|
||||||
|
}
|
||||||
|
|
||||||
|
func nullToTime(value sql.NullInt64) *time.Time {
|
||||||
|
if !value.Valid {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
result := time.UnixMilli(value.Int64)
|
||||||
|
return &result
|
||||||
|
}
|
||||||
@@ -0,0 +1,279 @@
|
|||||||
|
package acl
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"mediarr/ident"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGrantAndList(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
alice := mustUser(t, service, "alice")
|
||||||
|
documentID := mustDocument(t, service, owner.ID, nil, "Photos", "", true)
|
||||||
|
|
||||||
|
expiresAt := time.Now().Add(time.Hour).Truncate(time.Millisecond)
|
||||||
|
|
||||||
|
entry, err := service.Grant(ctx, GrantParams{
|
||||||
|
DocumentID: documentID,
|
||||||
|
SubjectID: alice.ID,
|
||||||
|
Permissions: PermissionRead | PermissionWrite,
|
||||||
|
Propagate: true,
|
||||||
|
ExpiresAt: &expiresAt,
|
||||||
|
GrantedBy: owner.ID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Grant : %v", err)
|
||||||
|
}
|
||||||
|
if entry.SubjectID != alice.ID {
|
||||||
|
t.Fatalf("subject = %s, attendu %s", entry.SubjectID, alice.ID)
|
||||||
|
}
|
||||||
|
if entry.Permissions != PermissionRead|PermissionWrite {
|
||||||
|
t.Fatalf("permissions = %d, attendu %d", entry.Permissions, PermissionRead|PermissionWrite)
|
||||||
|
}
|
||||||
|
if !entry.Propagate {
|
||||||
|
t.Fatal("propagate = false, attendu true")
|
||||||
|
}
|
||||||
|
if entry.ExpiresAt == nil || !entry.ExpiresAt.Equal(expiresAt) {
|
||||||
|
t.Fatalf("expires_at = %v, attendu %v", entry.ExpiresAt, expiresAt)
|
||||||
|
}
|
||||||
|
|
||||||
|
entries, err := service.List(ctx, documentID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("List : %v", err)
|
||||||
|
}
|
||||||
|
if len(entries) != 1 {
|
||||||
|
t.Fatalf("entrées = %d, attendu 1", len(entries))
|
||||||
|
}
|
||||||
|
if entries[0].ID != entry.ID {
|
||||||
|
t.Fatalf("id = %s, attendu %s", entries[0].ID, entry.ID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGrantUpsertsExistingEntry(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
alice := mustUser(t, service, "alice")
|
||||||
|
documentID := mustDocument(t, service, owner.ID, nil, "Photos", "", true)
|
||||||
|
|
||||||
|
if _, err := service.Grant(ctx, GrantParams{
|
||||||
|
DocumentID: documentID,
|
||||||
|
SubjectID: alice.ID,
|
||||||
|
Permissions: PermissionRead,
|
||||||
|
GrantedBy: owner.ID,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("premier Grant : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := service.Grant(ctx, GrantParams{
|
||||||
|
DocumentID: documentID,
|
||||||
|
SubjectID: alice.ID,
|
||||||
|
Permissions: PermissionWrite,
|
||||||
|
GrantedBy: owner.ID,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("second Grant : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
entries, err := service.List(ctx, documentID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("List : %v", err)
|
||||||
|
}
|
||||||
|
if len(entries) != 1 {
|
||||||
|
t.Fatalf("entrées = %d, attendu 1", len(entries))
|
||||||
|
}
|
||||||
|
if entries[0].Permissions != PermissionWrite {
|
||||||
|
t.Fatalf("permissions = %d, attendu %d", entries[0].Permissions, PermissionWrite)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGrantRejectsInvalidPermissions(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
alice := mustUser(t, service, "alice")
|
||||||
|
documentID := mustDocument(t, service, owner.ID, nil, "Photos", "", true)
|
||||||
|
|
||||||
|
for _, permissions := range []Permission{0, PermissionAll + 1} {
|
||||||
|
_, err := service.Grant(ctx, GrantParams{
|
||||||
|
DocumentID: documentID,
|
||||||
|
SubjectID: alice.ID,
|
||||||
|
Permissions: permissions,
|
||||||
|
GrantedBy: owner.ID,
|
||||||
|
})
|
||||||
|
if !errors.Is(err, ErrInvalidPermissions) {
|
||||||
|
t.Fatalf("permissions %d : erreur = %v, attendu ErrInvalidPermissions", permissions, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGrantRejectsMissingDocument(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
alice := mustUser(t, service, "alice")
|
||||||
|
missing, err := ident.NewV7()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewV7 : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = service.Grant(ctx, GrantParams{
|
||||||
|
DocumentID: missing,
|
||||||
|
SubjectID: alice.ID,
|
||||||
|
Permissions: PermissionRead,
|
||||||
|
GrantedBy: owner.ID,
|
||||||
|
})
|
||||||
|
if !errors.Is(err, ErrDocumentNotFound) {
|
||||||
|
t.Fatalf("erreur = %v, attendu ErrDocumentNotFound", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGrantRejectsMissingSubject(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
documentID := mustDocument(t, service, owner.ID, nil, "Photos", "", true)
|
||||||
|
missing, err := ident.NewV7()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewV7 : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = service.Grant(ctx, GrantParams{
|
||||||
|
DocumentID: documentID,
|
||||||
|
SubjectID: missing,
|
||||||
|
Permissions: PermissionRead,
|
||||||
|
GrantedBy: owner.ID,
|
||||||
|
})
|
||||||
|
if !errors.Is(err, ErrSubjectNotFound) {
|
||||||
|
t.Fatalf("erreur = %v, attendu ErrSubjectNotFound", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGrantRejectsInactiveSubject(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
alice := mustUser(t, service, "alice")
|
||||||
|
documentID := mustDocument(t, service, owner.ID, nil, "Photos", "", true)
|
||||||
|
|
||||||
|
disablePrincipal(t, service, alice.ID)
|
||||||
|
|
||||||
|
_, err := service.Grant(ctx, GrantParams{
|
||||||
|
DocumentID: documentID,
|
||||||
|
SubjectID: alice.ID,
|
||||||
|
Permissions: PermissionRead,
|
||||||
|
GrantedBy: owner.ID,
|
||||||
|
})
|
||||||
|
if !errors.Is(err, ErrSubjectNotFound) {
|
||||||
|
t.Fatalf("erreur = %v, attendu ErrSubjectNotFound", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRevoke(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
alice := mustUser(t, service, "alice")
|
||||||
|
documentID := mustDocument(t, service, owner.ID, nil, "Photos", "", true)
|
||||||
|
|
||||||
|
if _, err := service.Grant(ctx, GrantParams{
|
||||||
|
DocumentID: documentID,
|
||||||
|
SubjectID: alice.ID,
|
||||||
|
Permissions: PermissionRead,
|
||||||
|
GrantedBy: owner.ID,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("Grant : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := service.Revoke(ctx, documentID, alice.ID); err != nil {
|
||||||
|
t.Fatalf("Revoke : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
entries, err := service.List(ctx, documentID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("List : %v", err)
|
||||||
|
}
|
||||||
|
if len(entries) != 0 {
|
||||||
|
t.Fatalf("entrées = %d, attendu 0", len(entries))
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := service.Revoke(ctx, documentID, alice.ID); !errors.Is(err, ErrEntryNotFound) {
|
||||||
|
t.Fatalf("erreur = %v, attendu ErrEntryNotFound", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRevokeThenGrantAgain(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
alice := mustUser(t, service, "alice")
|
||||||
|
documentID := mustDocument(t, service, owner.ID, nil, "Photos", "", true)
|
||||||
|
|
||||||
|
if _, err := service.Grant(ctx, GrantParams{
|
||||||
|
DocumentID: documentID,
|
||||||
|
SubjectID: alice.ID,
|
||||||
|
Permissions: PermissionRead,
|
||||||
|
GrantedBy: owner.ID,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("Grant : %v", err)
|
||||||
|
}
|
||||||
|
if err := service.Revoke(ctx, documentID, alice.ID); err != nil {
|
||||||
|
t.Fatalf("Revoke : %v", err)
|
||||||
|
}
|
||||||
|
if _, err := service.Grant(ctx, GrantParams{
|
||||||
|
DocumentID: documentID,
|
||||||
|
SubjectID: alice.ID,
|
||||||
|
Permissions: PermissionWrite,
|
||||||
|
GrantedBy: owner.ID,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("Grant après révocation : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
permissions, err := service.Effective(ctx, alice.ID, documentID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Effective : %v", err)
|
||||||
|
}
|
||||||
|
if permissions != PermissionWrite {
|
||||||
|
t.Fatalf("permissions = %d, attendu %d", permissions, PermissionWrite)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestListIgnoresExpiredEntries(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
alice := mustUser(t, service, "alice")
|
||||||
|
documentID := mustDocument(t, service, owner.ID, nil, "Photos", "", true)
|
||||||
|
|
||||||
|
expiredAt := time.Now().Add(-time.Hour)
|
||||||
|
if _, err := service.Grant(ctx, GrantParams{
|
||||||
|
DocumentID: documentID,
|
||||||
|
SubjectID: alice.ID,
|
||||||
|
Permissions: PermissionRead,
|
||||||
|
ExpiresAt: &expiredAt,
|
||||||
|
GrantedBy: owner.ID,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("Grant : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
entries, err := service.List(ctx, documentID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("List : %v", err)
|
||||||
|
}
|
||||||
|
if len(entries) != 0 {
|
||||||
|
t.Fatalf("entrées = %d, attendu 0", len(entries))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,142 @@
|
|||||||
|
package acl
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/hex"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"mediarr/database"
|
||||||
|
"mediarr/document"
|
||||||
|
"mediarr/ident"
|
||||||
|
"mediarr/identity"
|
||||||
|
)
|
||||||
|
|
||||||
|
var testPolicyID = ident.MustParse("00000000-0000-7000-8000-000000000101")
|
||||||
|
|
||||||
|
func newTestService(t *testing.T) *Service {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
t.Chdir(t.TempDir())
|
||||||
|
|
||||||
|
err, db := database.NewDatabase(database.NewDatabaseParams{})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewDatabase : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Cleanup(func() {
|
||||||
|
if err := db.Close(); err != nil {
|
||||||
|
t.Errorf("Close : %v", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return NewService(*db)
|
||||||
|
}
|
||||||
|
|
||||||
|
func mustPrincipal(t *testing.T, service *Service, kind, name string) *identity.Principal {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
principal, err := service.identity.CreatePrincipal(context.Background(), identity.CreatePrincipalParams{
|
||||||
|
Kind: kind,
|
||||||
|
Name: name,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreatePrincipal(%q) : %v", name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return principal
|
||||||
|
}
|
||||||
|
|
||||||
|
func mustUser(t *testing.T, service *Service, name string) *identity.Principal {
|
||||||
|
return mustPrincipal(t, service, identity.KindUser, name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func mustGroup(t *testing.T, service *Service, name string) *identity.Principal {
|
||||||
|
return mustPrincipal(t, service, identity.KindGroup, name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func addMember(t *testing.T, service *Service, groupID, memberID ident.UUID) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
if err := service.identity.AddMember(context.Background(), groupID, memberID, identity.RoleMember); err != nil {
|
||||||
|
t.Fatalf("AddMember : %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func disablePrincipal(t *testing.T, service *Service, principalID ident.UUID) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
_, err := service.db.WriteQueries.UpdatePrincipalStatus(context.Background(), database.UpdatePrincipalStatusParams{
|
||||||
|
Status: identity.StatusDisabled,
|
||||||
|
ID: principalID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("UpdatePrincipalStatus : %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func mustDocument(t *testing.T, service *Service, ownerID ident.UUID, parentID *ident.UUID, name, visibility string, aclInherit bool) ident.UUID {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
id, err := ident.NewV7()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewV7 : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if visibility == "" {
|
||||||
|
visibility = document.VisibilityPrivate
|
||||||
|
}
|
||||||
|
|
||||||
|
kind := "library"
|
||||||
|
path := hex.EncodeToString(id[:]) + "/"
|
||||||
|
depth := int64(0)
|
||||||
|
|
||||||
|
var parentValue any
|
||||||
|
var policy any = testPolicyID
|
||||||
|
var visibilityValue any = visibility
|
||||||
|
|
||||||
|
if parentID != nil {
|
||||||
|
parent, err := service.db.Queries.GetDocument(ctx, *parentID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetDocument(parent) : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
kind = "folder"
|
||||||
|
path = parent.Path + hex.EncodeToString(id[:]) + "/"
|
||||||
|
depth = parent.Depth + 1
|
||||||
|
parentValue = *parentID
|
||||||
|
policy = nil
|
||||||
|
visibilityValue = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
now := time.Now().UnixMilli()
|
||||||
|
|
||||||
|
_, err = service.db.WriteDB.ExecContext(ctx, `
|
||||||
|
INSERT INTO document (
|
||||||
|
id, kind, owner_id, created_by, parent_id, name, name_key, path, depth,
|
||||||
|
policy_id, visibility, acl_inherit, created_at, updated_at
|
||||||
|
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||||
|
id,
|
||||||
|
kind,
|
||||||
|
ownerID,
|
||||||
|
ownerID,
|
||||||
|
parentValue,
|
||||||
|
name,
|
||||||
|
strings.ToLower(name),
|
||||||
|
path,
|
||||||
|
depth,
|
||||||
|
policy,
|
||||||
|
visibilityValue,
|
||||||
|
boolToInt(aclInherit),
|
||||||
|
now,
|
||||||
|
now,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("insertion du document %q : %v", name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return id
|
||||||
|
}
|
||||||
@@ -0,0 +1,113 @@
|
|||||||
|
package acl
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"errors"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"mediarr/database"
|
||||||
|
"mediarr/document"
|
||||||
|
"mediarr/ident"
|
||||||
|
"mediarr/identity"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s *Service) Effective(ctx context.Context, principalID, documentID ident.UUID) (Permission, error) {
|
||||||
|
principal, err := s.identity.GetPrincipal(ctx, principalID)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return 0, ErrSubjectNotFound
|
||||||
|
}
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if principal.Status != identity.StatusActive {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
chain, err := s.documentChain(ctx, documentID)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
target := chain[0]
|
||||||
|
if target.OwnerID == principalID {
|
||||||
|
return PermissionAll, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
subjects := map[ident.UUID]struct{}{principalID: {}}
|
||||||
|
groups, err := s.identity.ListGroups(ctx, principalID)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
for _, group := range groups {
|
||||||
|
if group.Status == identity.StatusActive {
|
||||||
|
subjects[group.ID] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var effective Permission
|
||||||
|
now := time.Now().UnixMilli()
|
||||||
|
inherit := target.AclInherit != 0
|
||||||
|
|
||||||
|
for depth, current := range chain {
|
||||||
|
if depth > 0 && !inherit {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
entries, err := s.db.Queries.ListActiveACLEntriesForDocument(ctx, database.ListActiveACLEntriesForDocumentParams{
|
||||||
|
DocumentID: current.ID,
|
||||||
|
Now: now,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, entry := range entries {
|
||||||
|
if depth > 0 && entry.Propagate == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if _, matches := subjects[entry.SubjectID]; matches {
|
||||||
|
effective |= Permission(entry.Permissions)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if depth == 0 {
|
||||||
|
inherit = current.AclInherit != 0
|
||||||
|
} else {
|
||||||
|
inherit = inherit && current.AclInherit != 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
switch effectiveVisibility(chain) {
|
||||||
|
case document.VisibilityCluster:
|
||||||
|
if principal.Kind == identity.KindUser || principal.Kind == identity.KindGuest {
|
||||||
|
effective |= PermissionRead
|
||||||
|
}
|
||||||
|
case document.VisibilityPublic:
|
||||||
|
effective |= PermissionRead
|
||||||
|
}
|
||||||
|
|
||||||
|
return effective, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) Can(ctx context.Context, principalID, documentID ident.UUID, required Permission) (bool, error) {
|
||||||
|
if required == 0 {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
permissions, err := s.Effective(ctx, principalID, documentID)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return permissions.Has(required), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func effectiveVisibility(chain []database.Document) string {
|
||||||
|
for _, document := range chain {
|
||||||
|
if document.Visibility.Valid {
|
||||||
|
return document.Visibility.String
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
@@ -0,0 +1,454 @@
|
|||||||
|
package acl
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"mediarr/document"
|
||||||
|
"mediarr/ident"
|
||||||
|
"mediarr/identity"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPermissionValidAndHas(t *testing.T) {
|
||||||
|
if PermissionAdmin != 32 || PermissionRequest != 64 {
|
||||||
|
t.Fatalf("bits = admin %d, request %d ; attendu 32 et 64", PermissionAdmin, PermissionRequest)
|
||||||
|
}
|
||||||
|
if !PermissionAll.Valid() {
|
||||||
|
t.Fatal("PermissionAll devrait être valide")
|
||||||
|
}
|
||||||
|
if Permission(0).Valid() {
|
||||||
|
t.Fatal("Permission(0) devrait être invalide")
|
||||||
|
}
|
||||||
|
if (PermissionAll + 1).Valid() {
|
||||||
|
t.Fatal("PermissionAll+1 devrait être invalide")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !PermissionAdmin.Has(PermissionRead) {
|
||||||
|
t.Fatal("PermissionAdmin devrait impliquer PermissionRead")
|
||||||
|
}
|
||||||
|
if !PermissionAll.Has(PermissionRequest) {
|
||||||
|
t.Fatal("PermissionAll devrait contenir PermissionRequest")
|
||||||
|
}
|
||||||
|
if PermissionRequest.Has(PermissionRead) {
|
||||||
|
t.Fatal("PermissionRequest ne devrait pas impliquer PermissionRead")
|
||||||
|
}
|
||||||
|
if PermissionRead.Has(PermissionWrite) {
|
||||||
|
t.Fatal("PermissionRead ne devrait pas impliquer PermissionWrite")
|
||||||
|
}
|
||||||
|
if PermissionAdmin.Has(PermissionAll + 1) {
|
||||||
|
t.Fatal("une permission requise invalide ne devrait pas être satisfaite")
|
||||||
|
}
|
||||||
|
if !Permission(0).Has(0) {
|
||||||
|
t.Fatal("aucune permission requise devrait toujours être satisfaite")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOwnerHasAllPermissions(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
documentID := mustDocument(t, service, owner.ID, nil, "Photos", "", true)
|
||||||
|
|
||||||
|
permissions, err := service.Effective(ctx, owner.ID, documentID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Effective : %v", err)
|
||||||
|
}
|
||||||
|
if permissions != PermissionAll {
|
||||||
|
t.Fatalf("permissions = %d, attendu %d", permissions, PermissionAll)
|
||||||
|
}
|
||||||
|
|
||||||
|
allowed, err := service.Can(ctx, owner.ID, documentID, PermissionAdmin)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Can : %v", err)
|
||||||
|
}
|
||||||
|
if !allowed {
|
||||||
|
t.Fatal("le propriétaire devrait avoir PermissionAdmin")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEffectiveDirectAndGroup(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
alice := mustUser(t, service, "alice")
|
||||||
|
group := mustGroup(t, service, "family")
|
||||||
|
documentID := mustDocument(t, service, owner.ID, nil, "Photos", "", true)
|
||||||
|
|
||||||
|
addMember(t, service, group.ID, alice.ID)
|
||||||
|
|
||||||
|
if _, err := service.Grant(ctx, GrantParams{
|
||||||
|
DocumentID: documentID,
|
||||||
|
SubjectID: group.ID,
|
||||||
|
Permissions: PermissionRead,
|
||||||
|
GrantedBy: owner.ID,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("Grant(groupe) : %v", err)
|
||||||
|
}
|
||||||
|
if _, err := service.Grant(ctx, GrantParams{
|
||||||
|
DocumentID: documentID,
|
||||||
|
SubjectID: alice.ID,
|
||||||
|
Permissions: PermissionWrite,
|
||||||
|
GrantedBy: owner.ID,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("Grant(alice) : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
permissions, err := service.Effective(ctx, alice.ID, documentID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Effective : %v", err)
|
||||||
|
}
|
||||||
|
if permissions != PermissionRead|PermissionWrite {
|
||||||
|
t.Fatalf("permissions = %d, attendu %d", permissions, PermissionRead|PermissionWrite)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEffectiveDisabledGroup(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
alice := mustUser(t, service, "alice")
|
||||||
|
group := mustGroup(t, service, "family")
|
||||||
|
documentID := mustDocument(t, service, owner.ID, nil, "Photos", "", true)
|
||||||
|
|
||||||
|
addMember(t, service, group.ID, alice.ID)
|
||||||
|
|
||||||
|
if _, err := service.Grant(ctx, GrantParams{
|
||||||
|
DocumentID: documentID,
|
||||||
|
SubjectID: group.ID,
|
||||||
|
Permissions: PermissionRead,
|
||||||
|
GrantedBy: owner.ID,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("Grant : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
disablePrincipal(t, service, group.ID)
|
||||||
|
|
||||||
|
permissions, err := service.Effective(ctx, alice.ID, documentID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Effective : %v", err)
|
||||||
|
}
|
||||||
|
if permissions != 0 {
|
||||||
|
t.Fatalf("permissions = %d, attendu 0", permissions)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEffectiveAdminImpliesAll(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
alice := mustUser(t, service, "alice")
|
||||||
|
documentID := mustDocument(t, service, owner.ID, nil, "Photos", "", true)
|
||||||
|
|
||||||
|
if _, err := service.Grant(ctx, GrantParams{
|
||||||
|
DocumentID: documentID,
|
||||||
|
SubjectID: alice.ID,
|
||||||
|
Permissions: PermissionAdmin,
|
||||||
|
GrantedBy: owner.ID,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("Grant : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
allowed, err := service.Can(ctx, alice.ID, documentID, PermissionRead|PermissionDelete)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Can : %v", err)
|
||||||
|
}
|
||||||
|
if !allowed {
|
||||||
|
t.Fatal("PermissionAdmin devrait impliquer toutes les permissions")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEffectiveInheritance(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
alice := mustUser(t, service, "alice")
|
||||||
|
|
||||||
|
root := mustDocument(t, service, owner.ID, nil, "Root", "", true)
|
||||||
|
folder := mustDocument(t, service, owner.ID, &root, "Folder", "", true)
|
||||||
|
child := mustDocument(t, service, owner.ID, &folder, "Child", "", true)
|
||||||
|
|
||||||
|
if _, err := service.Grant(ctx, GrantParams{
|
||||||
|
DocumentID: root,
|
||||||
|
SubjectID: alice.ID,
|
||||||
|
Permissions: PermissionRead,
|
||||||
|
Propagate: true,
|
||||||
|
GrantedBy: owner.ID,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("Grant(root) : %v", err)
|
||||||
|
}
|
||||||
|
if _, err := service.Grant(ctx, GrantParams{
|
||||||
|
DocumentID: folder,
|
||||||
|
SubjectID: alice.ID,
|
||||||
|
Permissions: PermissionWrite,
|
||||||
|
Propagate: true,
|
||||||
|
GrantedBy: owner.ID,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("Grant(folder) : %v", err)
|
||||||
|
}
|
||||||
|
if _, err := service.Grant(ctx, GrantParams{
|
||||||
|
DocumentID: child,
|
||||||
|
SubjectID: alice.ID,
|
||||||
|
Permissions: PermissionDelete,
|
||||||
|
GrantedBy: owner.ID,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("Grant(child) : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
permissions, err := service.Effective(ctx, alice.ID, child)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Effective : %v", err)
|
||||||
|
}
|
||||||
|
expected := PermissionRead | PermissionWrite | PermissionDelete
|
||||||
|
if permissions != expected {
|
||||||
|
t.Fatalf("permissions = %d, attendu %d", permissions, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEffectivePropagationDisabled(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
alice := mustUser(t, service, "alice")
|
||||||
|
|
||||||
|
root := mustDocument(t, service, owner.ID, nil, "Root", "", true)
|
||||||
|
child := mustDocument(t, service, owner.ID, &root, "Child", "", true)
|
||||||
|
|
||||||
|
if _, err := service.Grant(ctx, GrantParams{
|
||||||
|
DocumentID: root,
|
||||||
|
SubjectID: alice.ID,
|
||||||
|
Permissions: PermissionRead,
|
||||||
|
Propagate: false,
|
||||||
|
GrantedBy: owner.ID,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("Grant : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
permissions, err := service.Effective(ctx, alice.ID, child)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Effective : %v", err)
|
||||||
|
}
|
||||||
|
if permissions != 0 {
|
||||||
|
t.Fatalf("permissions = %d, attendu 0", permissions)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEffectiveAclInheritBreak(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
alice := mustUser(t, service, "alice")
|
||||||
|
|
||||||
|
root := mustDocument(t, service, owner.ID, nil, "Root", "", true)
|
||||||
|
middle := mustDocument(t, service, owner.ID, &root, "Middle", "", false)
|
||||||
|
leaf := mustDocument(t, service, owner.ID, &middle, "Leaf", "", true)
|
||||||
|
|
||||||
|
if _, err := service.Grant(ctx, GrantParams{
|
||||||
|
DocumentID: root,
|
||||||
|
SubjectID: alice.ID,
|
||||||
|
Permissions: PermissionRead,
|
||||||
|
Propagate: true,
|
||||||
|
GrantedBy: owner.ID,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("Grant(root) : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
permissions, err := service.Effective(ctx, alice.ID, leaf)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Effective : %v", err)
|
||||||
|
}
|
||||||
|
if permissions != 0 {
|
||||||
|
t.Fatalf("permissions = %d, attendu 0", permissions)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := service.Grant(ctx, GrantParams{
|
||||||
|
DocumentID: leaf,
|
||||||
|
SubjectID: alice.ID,
|
||||||
|
Permissions: PermissionWrite,
|
||||||
|
GrantedBy: owner.ID,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("Grant(leaf) : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
permissions, err = service.Effective(ctx, alice.ID, leaf)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Effective après grant direct : %v", err)
|
||||||
|
}
|
||||||
|
if permissions != PermissionWrite {
|
||||||
|
t.Fatalf("permissions = %d, attendu %d", permissions, PermissionWrite)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEffectiveExpiredAndRevoked(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
alice := mustUser(t, service, "alice")
|
||||||
|
documentID := mustDocument(t, service, owner.ID, nil, "Photos", "", true)
|
||||||
|
|
||||||
|
expiredAt := time.Now().Add(-time.Hour)
|
||||||
|
if _, err := service.Grant(ctx, GrantParams{
|
||||||
|
DocumentID: documentID,
|
||||||
|
SubjectID: alice.ID,
|
||||||
|
Permissions: PermissionRead,
|
||||||
|
ExpiresAt: &expiredAt,
|
||||||
|
GrantedBy: owner.ID,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("Grant expiré : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
permissions, err := service.Effective(ctx, alice.ID, documentID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Effective : %v", err)
|
||||||
|
}
|
||||||
|
if permissions != 0 {
|
||||||
|
t.Fatalf("permissions = %d, attendu 0", permissions)
|
||||||
|
}
|
||||||
|
|
||||||
|
future := time.Now().Add(time.Hour)
|
||||||
|
if _, err := service.Grant(ctx, GrantParams{
|
||||||
|
DocumentID: documentID,
|
||||||
|
SubjectID: alice.ID,
|
||||||
|
Permissions: PermissionWrite,
|
||||||
|
ExpiresAt: &future,
|
||||||
|
GrantedBy: owner.ID,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("Grant futur : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
permissions, err = service.Effective(ctx, alice.ID, documentID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Effective : %v", err)
|
||||||
|
}
|
||||||
|
if permissions != PermissionWrite {
|
||||||
|
t.Fatalf("permissions = %d, attendu %d", permissions, PermissionWrite)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := service.Revoke(ctx, documentID, alice.ID); err != nil {
|
||||||
|
t.Fatalf("Revoke : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
permissions, err = service.Effective(ctx, alice.ID, documentID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Effective après révocation : %v", err)
|
||||||
|
}
|
||||||
|
if permissions != 0 {
|
||||||
|
t.Fatalf("permissions = %d, attendu 0", permissions)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEffectiveVisibility(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
alice := mustUser(t, service, "alice")
|
||||||
|
guest := mustPrincipal(t, service, identity.KindGuest, "jean")
|
||||||
|
system := mustPrincipal(t, service, identity.KindSystem, "system-test")
|
||||||
|
|
||||||
|
clusterDoc := mustDocument(t, service, owner.ID, nil, "Cluster", document.VisibilityCluster, true)
|
||||||
|
privateDoc := mustDocument(t, service, owner.ID, nil, "Private", document.VisibilityPrivate, true)
|
||||||
|
publicDoc := mustDocument(t, service, owner.ID, nil, "Public", document.VisibilityPublic, true)
|
||||||
|
federatedDoc := mustDocument(t, service, owner.ID, nil, "Federated", document.VisibilityFederated, true)
|
||||||
|
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
principalID ident.UUID
|
||||||
|
documentID ident.UUID
|
||||||
|
expected Permission
|
||||||
|
}{
|
||||||
|
{"cluster/user", alice.ID, clusterDoc, PermissionRead},
|
||||||
|
{"cluster/guest", guest.ID, clusterDoc, PermissionRead},
|
||||||
|
{"cluster/system", system.ID, clusterDoc, 0},
|
||||||
|
{"private/user", alice.ID, privateDoc, 0},
|
||||||
|
{"public/system", system.ID, publicDoc, PermissionRead},
|
||||||
|
{"federated/user", alice.ID, federatedDoc, 0},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, testCase := range cases {
|
||||||
|
permissions, err := service.Effective(ctx, testCase.principalID, testCase.documentID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%s : Effective : %v", testCase.name, err)
|
||||||
|
}
|
||||||
|
if permissions != testCase.expected {
|
||||||
|
t.Fatalf("%s : permissions = %d, attendu %d", testCase.name, permissions, testCase.expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEffectiveInactivePrincipal(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
alice := mustUser(t, service, "alice")
|
||||||
|
documentID := mustDocument(t, service, owner.ID, nil, "Photos", "", true)
|
||||||
|
|
||||||
|
if _, err := service.Grant(ctx, GrantParams{
|
||||||
|
DocumentID: documentID,
|
||||||
|
SubjectID: alice.ID,
|
||||||
|
Permissions: PermissionRead,
|
||||||
|
GrantedBy: owner.ID,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("Grant : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
disablePrincipal(t, service, alice.ID)
|
||||||
|
|
||||||
|
permissions, err := service.Effective(ctx, alice.ID, documentID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Effective : %v", err)
|
||||||
|
}
|
||||||
|
if permissions != 0 {
|
||||||
|
t.Fatalf("permissions = %d, attendu 0", permissions)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEffectiveMissingDocument(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
alice := mustUser(t, service, "alice")
|
||||||
|
missing, err := ident.NewV7()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewV7 : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = service.Effective(ctx, alice.ID, missing)
|
||||||
|
if !errors.Is(err, ErrDocumentNotFound) {
|
||||||
|
t.Fatalf("erreur = %v, attendu ErrDocumentNotFound", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsActive(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
alice := mustUser(t, service, "alice")
|
||||||
|
|
||||||
|
active, err := service.IsActive(ctx, alice.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("IsActive : %v", err)
|
||||||
|
}
|
||||||
|
if !active {
|
||||||
|
t.Fatal("alice devrait être active")
|
||||||
|
}
|
||||||
|
|
||||||
|
disablePrincipal(t, service, alice.ID)
|
||||||
|
|
||||||
|
active, err = service.IsActive(ctx, alice.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("IsActive après désactivation : %v", err)
|
||||||
|
}
|
||||||
|
if active {
|
||||||
|
t.Fatal("alice ne devrait plus être active")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
package acl
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"mediarr/database"
|
||||||
|
"mediarr/ident"
|
||||||
|
"mediarr/identity"
|
||||||
|
)
|
||||||
|
|
||||||
|
const maxDocumentDepth = 256
|
||||||
|
|
||||||
|
type Service struct {
|
||||||
|
db database.DatabaseClient
|
||||||
|
identity *identity.Service
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewService(db database.DatabaseClient) *Service {
|
||||||
|
return &Service{
|
||||||
|
db: db,
|
||||||
|
identity: identity.NewService(db),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) documentChain(ctx context.Context, documentID ident.UUID) ([]database.Document, error) {
|
||||||
|
chain := make([]database.Document, 0, 8)
|
||||||
|
seen := make(map[ident.UUID]struct{})
|
||||||
|
current := documentID
|
||||||
|
|
||||||
|
for {
|
||||||
|
if _, exists := seen[current]; exists {
|
||||||
|
return nil, ErrDocumentCycle
|
||||||
|
}
|
||||||
|
seen[current] = struct{}{}
|
||||||
|
|
||||||
|
document, err := s.db.Queries.GetDocument(ctx, current)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return nil, fmt.Errorf("%w: %s", ErrDocumentNotFound, current)
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
chain = append(chain, document)
|
||||||
|
if document.ParentID == nil {
|
||||||
|
return chain, nil
|
||||||
|
}
|
||||||
|
if len(chain) >= maxDocumentDepth {
|
||||||
|
return nil, ErrDocumentCycle
|
||||||
|
}
|
||||||
|
|
||||||
|
current = *document.ParentID
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) loadSubject(ctx context.Context, subjectID ident.UUID) (*identity.Principal, error) {
|
||||||
|
subject, err := s.identity.GetPrincipal(ctx, subjectID)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return nil, fmt.Errorf("%w: %s", ErrSubjectNotFound, subjectID)
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if subject.Status != identity.StatusActive {
|
||||||
|
return nil, fmt.Errorf("%w: %s", ErrSubjectNotFound, subjectID)
|
||||||
|
}
|
||||||
|
|
||||||
|
return subject, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) IsActive(ctx context.Context, principalID ident.UUID) (bool, error) {
|
||||||
|
if _, err := s.loadSubject(ctx, principalID); err != nil {
|
||||||
|
if errors.Is(err, ErrSubjectNotFound) {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
+56
@@ -0,0 +1,56 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"mediarr/node"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Server struct {
|
||||||
|
node *node.Node
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewServer(n *node.Node) *Server {
|
||||||
|
return &Server{node: n}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) Handler() http.Handler {
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
|
mux.HandleFunc("GET /api/health", s.handleHealth)
|
||||||
|
mux.HandleFunc("POST /api/login", s.handleLogin)
|
||||||
|
|
||||||
|
mux.Handle("POST /api/logout", s.requireAuth(http.HandlerFunc(s.handleLogout)))
|
||||||
|
mux.Handle("GET /api/me", s.requireAuth(http.HandlerFunc(s.handleMe)))
|
||||||
|
mux.Handle("POST /api/users", s.requireAuth(s.requireAdmin(http.HandlerFunc(s.handleCreateUser))))
|
||||||
|
|
||||||
|
mux.Handle("GET /api/libraries", s.requireAuth(http.HandlerFunc(s.handleListLibraries)))
|
||||||
|
mux.Handle("POST /api/libraries", s.requireAuth(http.HandlerFunc(s.handleCreateLibrary)))
|
||||||
|
|
||||||
|
mux.Handle("GET /api/documents/{id}", s.requireAuth(http.HandlerFunc(s.handleGetDocument)))
|
||||||
|
mux.Handle("GET /api/documents/{id}/children", s.requireAuth(http.HandlerFunc(s.handleListChildren)))
|
||||||
|
mux.Handle("POST /api/documents/{id}/children", s.requireAuth(http.HandlerFunc(s.handleCreateChild)))
|
||||||
|
mux.Handle("POST /api/documents/{id}/rename", s.requireAuth(http.HandlerFunc(s.handleRename)))
|
||||||
|
mux.Handle("PUT /api/documents/{id}/metadata", s.requireAuth(http.HandlerFunc(s.handleSetMetadata)))
|
||||||
|
mux.Handle("POST /api/documents/{id}/move", s.requireAuth(http.HandlerFunc(s.handleMove)))
|
||||||
|
mux.Handle("DELETE /api/documents/{id}", s.requireAuth(http.HandlerFunc(s.handleTrash)))
|
||||||
|
mux.Handle("POST /api/documents/{id}/restore", s.requireAuth(http.HandlerFunc(s.handleRestore)))
|
||||||
|
|
||||||
|
mux.Handle("GET /api/documents/{id}/versions", s.requireAuth(http.HandlerFunc(s.handleListVersions)))
|
||||||
|
mux.Handle("POST /api/documents/{id}/versions", s.requireAuth(http.HandlerFunc(s.handleUploadVersion)))
|
||||||
|
mux.Handle("GET /api/documents/{id}/head", s.requireAuth(http.HandlerFunc(s.handleGetHead)))
|
||||||
|
mux.Handle("POST /api/documents/{id}/head", s.requireAuth(http.HandlerFunc(s.handleSetHead)))
|
||||||
|
mux.Handle("POST /api/documents/{id}/retention", s.requireAuth(http.HandlerFunc(s.handleApplyRetention)))
|
||||||
|
|
||||||
|
mux.Handle("GET /api/versions/{id}", s.requireAuth(http.HandlerFunc(s.handleDownloadVersion)))
|
||||||
|
mux.Handle("DELETE /api/versions/{id}", s.requireAuth(http.HandlerFunc(s.handlePurgeVersion)))
|
||||||
|
mux.Handle("POST /api/versions/{id}/verify", s.requireAuth(http.HandlerFunc(s.handleVerifyVersion)))
|
||||||
|
|
||||||
|
mux.Handle("POST /api/maintenance/gc", s.requireAuth(s.requireAdmin(http.HandlerFunc(s.handleCollectGarbage))))
|
||||||
|
|
||||||
|
return mux
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleHealth(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
writeJSON(w, http.StatusOK, map[string]string{"status": "ok"})
|
||||||
|
}
|
||||||
+411
@@ -0,0 +1,411 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"mime/multipart"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"mediarr/auth"
|
||||||
|
"mediarr/ident"
|
||||||
|
"mediarr/identity"
|
||||||
|
"mediarr/node"
|
||||||
|
)
|
||||||
|
|
||||||
|
type apiTestEnv struct {
|
||||||
|
node node.Node
|
||||||
|
server *httptest.Server
|
||||||
|
}
|
||||||
|
|
||||||
|
func newAPITestEnv(t *testing.T) *apiTestEnv {
|
||||||
|
t.Helper()
|
||||||
|
t.Chdir(t.TempDir())
|
||||||
|
|
||||||
|
err, n := node.NewNode(node.NewNodeParams{
|
||||||
|
DatabaseName: "api",
|
||||||
|
StoragePath: "store",
|
||||||
|
ClusterKeyPath: "cluster.key",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewNode : %v", err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() {
|
||||||
|
if err := n.Close(); err != nil {
|
||||||
|
t.Errorf("Close : %v", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if err := n.Up(); err != nil {
|
||||||
|
t.Fatalf("Up : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
server := httptest.NewServer(NewServer(&n).Handler())
|
||||||
|
t.Cleanup(server.Close)
|
||||||
|
|
||||||
|
return &apiTestEnv{node: n, server: server}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAPIWorkflow(t *testing.T) {
|
||||||
|
env := newAPITestEnv(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
client := env.server.Client()
|
||||||
|
|
||||||
|
if _, err := env.node.Auth().CreateUser(ctx, auth.CreateUserParams{
|
||||||
|
Login: "admin",
|
||||||
|
Password: "password123",
|
||||||
|
Role: identity.RoleAdmin,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("CreateUser(admin) : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
response, body := doJSON(t, client, http.MethodGet, env.server.URL+"/api/health", "", nil)
|
||||||
|
requireStatus(t, response, body, http.StatusOK)
|
||||||
|
|
||||||
|
response, body = doJSON(t, client, http.MethodGet, env.server.URL+"/api/me", "", nil)
|
||||||
|
requireStatus(t, response, body, http.StatusUnauthorized)
|
||||||
|
|
||||||
|
response, body = doJSON(t, client, http.MethodPost, env.server.URL+"/api/login", "", map[string]string{
|
||||||
|
"login": "admin",
|
||||||
|
"password": "password123",
|
||||||
|
})
|
||||||
|
requireStatus(t, response, body, http.StatusOK)
|
||||||
|
login := decodeBody[loginResponse](t, body)
|
||||||
|
if login.Token == "" || login.Principal.ID == "" || login.Account.Login != "admin" {
|
||||||
|
t.Fatalf("login = %+v", login)
|
||||||
|
}
|
||||||
|
if len(response.Cookies()) == 0 {
|
||||||
|
t.Fatal("cookie de session absent")
|
||||||
|
}
|
||||||
|
token := login.Token
|
||||||
|
|
||||||
|
response, body = doJSON(t, client, http.MethodGet, env.server.URL+"/api/me", token, nil)
|
||||||
|
requireStatus(t, response, body, http.StatusOK)
|
||||||
|
me := decodeBody[userDTO](t, body)
|
||||||
|
if me.Principal.ID != login.Principal.ID || me.Account.Login != "admin" {
|
||||||
|
t.Fatalf("me = %+v", me)
|
||||||
|
}
|
||||||
|
|
||||||
|
response, body = doJSON(t, client, http.MethodPost, env.server.URL+"/api/libraries", token, map[string]any{
|
||||||
|
"name": "Films",
|
||||||
|
})
|
||||||
|
requireStatus(t, response, body, http.StatusCreated)
|
||||||
|
library := decodeBody[documentDTO](t, body)
|
||||||
|
if library.Kind != "library" || library.Name != "Films" || library.PolicyID == nil {
|
||||||
|
t.Fatalf("bibliothèque = %+v", library)
|
||||||
|
}
|
||||||
|
|
||||||
|
response, body = doJSON(t, client, http.MethodGet, env.server.URL+"/api/libraries", token, nil)
|
||||||
|
requireStatus(t, response, body, http.StatusOK)
|
||||||
|
libraries := decodeBody[[]documentDTO](t, body)
|
||||||
|
if len(libraries) != 1 || libraries[0].ID != library.ID {
|
||||||
|
t.Fatalf("bibliothèques = %+v", libraries)
|
||||||
|
}
|
||||||
|
|
||||||
|
response, body = doJSON(t, client, http.MethodPost, env.server.URL+"/api/documents/"+library.ID+"/children", token, map[string]any{
|
||||||
|
"kind": "video",
|
||||||
|
"name": "film.mkv",
|
||||||
|
})
|
||||||
|
requireStatus(t, response, body, http.StatusCreated)
|
||||||
|
video := decodeBody[documentDTO](t, body)
|
||||||
|
if video.Kind != "video" || video.ParentID == nil || *video.ParentID != library.ID {
|
||||||
|
t.Fatalf("vidéo = %+v", video)
|
||||||
|
}
|
||||||
|
|
||||||
|
response, body = doUpload(t, client, env.server.URL+"/api/documents/"+video.ID+"/versions", token, "film.mkv", []byte("contenu v1"))
|
||||||
|
requireStatus(t, response, body, http.StatusCreated)
|
||||||
|
first := decodeBody[versionDTO](t, body)
|
||||||
|
if first.Seq != 1 || first.Size != int64(len("contenu v1")) || first.DocumentID != video.ID {
|
||||||
|
t.Fatalf("version 1 = %+v", first)
|
||||||
|
}
|
||||||
|
|
||||||
|
response, body = doJSON(t, client, http.MethodGet, env.server.URL+"/api/documents/"+video.ID+"/versions", token, nil)
|
||||||
|
requireStatus(t, response, body, http.StatusOK)
|
||||||
|
versions := decodeBody[[]versionDTO](t, body)
|
||||||
|
if len(versions) != 1 || versions[0].ID != first.ID {
|
||||||
|
t.Fatalf("versions = %+v", versions)
|
||||||
|
}
|
||||||
|
|
||||||
|
response, body = doJSON(t, client, http.MethodGet, env.server.URL+"/api/documents/"+video.ID+"/head", token, nil)
|
||||||
|
requireStatus(t, response, body, http.StatusOK)
|
||||||
|
head := decodeBody[struct {
|
||||||
|
Version *versionDTO `json:"version"`
|
||||||
|
}](t, body)
|
||||||
|
if head.Version == nil || head.Version.ID != first.ID {
|
||||||
|
t.Fatalf("head = %+v", head)
|
||||||
|
}
|
||||||
|
|
||||||
|
response, downloaded := doRaw(t, client, http.MethodGet, env.server.URL+"/api/versions/"+first.ID, token, nil, "")
|
||||||
|
requireStatus(t, response, downloaded, http.StatusOK)
|
||||||
|
if string(downloaded) != "contenu v1" {
|
||||||
|
t.Fatalf("contenu téléchargé = %q", downloaded)
|
||||||
|
}
|
||||||
|
|
||||||
|
response, body = doJSON(t, client, http.MethodPost, env.server.URL+"/api/documents/"+video.ID+"/head", token, map[string]string{
|
||||||
|
"version_id": first.ID,
|
||||||
|
})
|
||||||
|
requireStatus(t, response, body, http.StatusOK)
|
||||||
|
if decodeBody[versionDTO](t, body).ID != first.ID {
|
||||||
|
t.Fatal("version tête incohérente")
|
||||||
|
}
|
||||||
|
|
||||||
|
response, body = doUpload(t, client, env.server.URL+"/api/documents/"+video.ID+"/versions", token, "film.mkv", []byte("contenu v2"))
|
||||||
|
requireStatus(t, response, body, http.StatusCreated)
|
||||||
|
second := decodeBody[versionDTO](t, body)
|
||||||
|
if second.Seq != 2 {
|
||||||
|
t.Fatalf("version 2 = %+v", second)
|
||||||
|
}
|
||||||
|
|
||||||
|
response, body = doJSON(t, client, http.MethodPost, env.server.URL+"/api/documents/"+video.ID+"/head", token, map[string]string{
|
||||||
|
"version_id": second.ID,
|
||||||
|
})
|
||||||
|
requireStatus(t, response, body, http.StatusOK)
|
||||||
|
|
||||||
|
response, body = doJSON(t, client, http.MethodDelete, env.server.URL+"/api/versions/"+first.ID, token, nil)
|
||||||
|
requireStatus(t, response, body, http.StatusNoContent)
|
||||||
|
|
||||||
|
response, body = doJSON(t, client, http.MethodGet, env.server.URL+"/api/versions/"+first.ID, token, nil)
|
||||||
|
requireStatus(t, response, body, http.StatusGone)
|
||||||
|
|
||||||
|
response, body = doJSON(t, client, http.MethodPost, env.server.URL+"/api/documents/"+video.ID+"/retention", token, nil)
|
||||||
|
requireStatus(t, response, body, http.StatusOK)
|
||||||
|
retention := decodeBody[retentionDTO](t, body)
|
||||||
|
if retention.Kept < 1 {
|
||||||
|
t.Fatalf("rétention = %+v", retention)
|
||||||
|
}
|
||||||
|
|
||||||
|
response, body = doJSON(t, client, http.MethodPost, env.server.URL+"/api/versions/"+second.ID+"/verify", token, nil)
|
||||||
|
requireStatus(t, response, body, http.StatusNoContent)
|
||||||
|
|
||||||
|
response, body = doJSON(t, client, http.MethodPost, env.server.URL+"/api/documents/"+video.ID+"/rename", token, map[string]string{
|
||||||
|
"name": "film-renamed.mkv",
|
||||||
|
})
|
||||||
|
requireStatus(t, response, body, http.StatusOK)
|
||||||
|
if decodeBody[documentDTO](t, body).Name != "film-renamed.mkv" {
|
||||||
|
t.Fatal("renommage incohérent")
|
||||||
|
}
|
||||||
|
|
||||||
|
response, body = doJSON(t, client, http.MethodPut, env.server.URL+"/api/documents/"+video.ID+"/metadata", token, map[string]any{
|
||||||
|
"metadata": map[string]any{"tag": "test"},
|
||||||
|
})
|
||||||
|
requireStatus(t, response, body, http.StatusOK)
|
||||||
|
if string(decodeBody[documentDTO](t, body).Metadata) != `{"tag":"test"}` {
|
||||||
|
t.Fatalf("métadonnées = %s", body)
|
||||||
|
}
|
||||||
|
|
||||||
|
response, body = doJSON(t, client, http.MethodPost, env.server.URL+"/api/documents/"+library.ID+"/children", token, map[string]any{
|
||||||
|
"kind": "folder",
|
||||||
|
"name": "Dossier",
|
||||||
|
})
|
||||||
|
requireStatus(t, response, body, http.StatusCreated)
|
||||||
|
folder := decodeBody[documentDTO](t, body)
|
||||||
|
|
||||||
|
response, body = doJSON(t, client, http.MethodPost, env.server.URL+"/api/documents/"+video.ID+"/move", token, map[string]string{
|
||||||
|
"parent_id": folder.ID,
|
||||||
|
})
|
||||||
|
requireStatus(t, response, body, http.StatusOK)
|
||||||
|
moved := decodeBody[documentDTO](t, body)
|
||||||
|
if moved.ParentID == nil || *moved.ParentID != folder.ID {
|
||||||
|
t.Fatalf("déplacement = %+v", moved)
|
||||||
|
}
|
||||||
|
|
||||||
|
response, body = doJSON(t, client, http.MethodDelete, env.server.URL+"/api/documents/"+video.ID, token, nil)
|
||||||
|
requireStatus(t, response, body, http.StatusOK)
|
||||||
|
trashed := decodeBody[documentDTO](t, body)
|
||||||
|
if trashed.DeletedAt == nil {
|
||||||
|
t.Fatal("document non mis à la corbeille")
|
||||||
|
}
|
||||||
|
|
||||||
|
response, body = doJSON(t, client, http.MethodPost, env.server.URL+"/api/documents/"+video.ID+"/restore", token, nil)
|
||||||
|
requireStatus(t, response, body, http.StatusOK)
|
||||||
|
if decodeBody[documentDTO](t, body).DeletedAt != nil {
|
||||||
|
t.Fatal("document non restauré")
|
||||||
|
}
|
||||||
|
|
||||||
|
response, body = doJSON(t, client, http.MethodPost, env.server.URL+"/api/users", token, map[string]string{
|
||||||
|
"login": "member",
|
||||||
|
"password": "password123",
|
||||||
|
"role": identity.RoleMember,
|
||||||
|
})
|
||||||
|
requireStatus(t, response, body, http.StatusCreated)
|
||||||
|
|
||||||
|
response, body = doJSON(t, client, http.MethodPost, env.server.URL+"/api/login", "", map[string]string{
|
||||||
|
"login": "member",
|
||||||
|
"password": "password123",
|
||||||
|
})
|
||||||
|
requireStatus(t, response, body, http.StatusOK)
|
||||||
|
memberToken := decodeBody[loginResponse](t, body).Token
|
||||||
|
|
||||||
|
response, body = doJSON(t, client, http.MethodPost, env.server.URL+"/api/maintenance/gc", memberToken, nil)
|
||||||
|
requireStatus(t, response, body, http.StatusForbidden)
|
||||||
|
|
||||||
|
response, body = doJSON(t, client, http.MethodPost, env.server.URL+"/api/maintenance/gc", token, nil)
|
||||||
|
requireStatus(t, response, body, http.StatusOK)
|
||||||
|
|
||||||
|
response, body = doJSON(t, client, http.MethodPost, env.server.URL+"/api/logout", token, nil)
|
||||||
|
requireStatus(t, response, body, http.StatusNoContent)
|
||||||
|
|
||||||
|
response, body = doJSON(t, client, http.MethodGet, env.server.URL+"/api/me", token, nil)
|
||||||
|
requireStatus(t, response, body, http.StatusUnauthorized)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAPILoginRejectsInvalidCredentials(t *testing.T) {
|
||||||
|
env := newAPITestEnv(t)
|
||||||
|
client := env.server.Client()
|
||||||
|
|
||||||
|
response, body := doJSON(t, client, http.MethodPost, env.server.URL+"/api/login", "", map[string]string{
|
||||||
|
"login": "missing",
|
||||||
|
"password": "password123",
|
||||||
|
})
|
||||||
|
requireStatus(t, response, body, http.StatusUnauthorized)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAPIDocumentNotFound(t *testing.T) {
|
||||||
|
env := newAPITestEnv(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
client := env.server.Client()
|
||||||
|
|
||||||
|
if _, err := env.node.Auth().CreateUser(ctx, auth.CreateUserParams{
|
||||||
|
Login: "admin",
|
||||||
|
Password: "password123",
|
||||||
|
Role: identity.RoleAdmin,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("CreateUser : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, body := doJSON(t, client, http.MethodPost, env.server.URL+"/api/login", "", map[string]string{
|
||||||
|
"login": "admin",
|
||||||
|
"password": "password123",
|
||||||
|
})
|
||||||
|
token := decodeBody[loginResponse](t, body).Token
|
||||||
|
|
||||||
|
missing := ident.MustParse("00000000-0000-7000-8000-000000000999").String()
|
||||||
|
response, body := doJSON(t, client, http.MethodGet, env.server.URL+"/api/documents/"+missing, token, nil)
|
||||||
|
requireStatus(t, response, body, http.StatusNotFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
func doJSON(t *testing.T, client *http.Client, method, url, token string, body any) (*http.Response, []byte) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
var reader io.Reader
|
||||||
|
if body != nil {
|
||||||
|
encoded, err := json.Marshal(body)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Marshal : %v", err)
|
||||||
|
}
|
||||||
|
reader = bytes.NewReader(encoded)
|
||||||
|
}
|
||||||
|
|
||||||
|
request, err := http.NewRequest(method, url, reader)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewRequest : %v", err)
|
||||||
|
}
|
||||||
|
if body != nil {
|
||||||
|
request.Header.Set("Content-Type", "application/json")
|
||||||
|
}
|
||||||
|
if token != "" {
|
||||||
|
request.Header.Set("Authorization", "Bearer "+token)
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := client.Do(request)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%s %s : %v", method, url, err)
|
||||||
|
}
|
||||||
|
data, err := io.ReadAll(response.Body)
|
||||||
|
if err != nil {
|
||||||
|
_ = response.Body.Close()
|
||||||
|
t.Fatalf("ReadAll : %v", err)
|
||||||
|
}
|
||||||
|
_ = response.Body.Close()
|
||||||
|
|
||||||
|
return response, data
|
||||||
|
}
|
||||||
|
|
||||||
|
func doUpload(t *testing.T, client *http.Client, url, token, filename string, content []byte) (*http.Response, []byte) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
body := &bytes.Buffer{}
|
||||||
|
writer := multipart.NewWriter(body)
|
||||||
|
part, err := writer.CreateFormFile("file", filename)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreateFormFile : %v", err)
|
||||||
|
}
|
||||||
|
if _, err := part.Write(content); err != nil {
|
||||||
|
t.Fatalf("Write : %v", err)
|
||||||
|
}
|
||||||
|
if err := writer.WriteField("message", "test"); err != nil {
|
||||||
|
t.Fatalf("WriteField : %v", err)
|
||||||
|
}
|
||||||
|
if err := writer.Close(); err != nil {
|
||||||
|
t.Fatalf("Close : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
request, err := http.NewRequest(http.MethodPost, url, body)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewRequest : %v", err)
|
||||||
|
}
|
||||||
|
request.Header.Set("Content-Type", writer.FormDataContentType())
|
||||||
|
request.Header.Set("Authorization", "Bearer "+token)
|
||||||
|
|
||||||
|
response, err := client.Do(request)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("POST %s : %v", url, err)
|
||||||
|
}
|
||||||
|
data, err := io.ReadAll(response.Body)
|
||||||
|
if err != nil {
|
||||||
|
_ = response.Body.Close()
|
||||||
|
t.Fatalf("ReadAll : %v", err)
|
||||||
|
}
|
||||||
|
_ = response.Body.Close()
|
||||||
|
|
||||||
|
return response, data
|
||||||
|
}
|
||||||
|
|
||||||
|
func doRaw(t *testing.T, client *http.Client, method, url, token string, body io.Reader, contentType string) (*http.Response, []byte) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
request, err := http.NewRequest(method, url, body)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewRequest : %v", err)
|
||||||
|
}
|
||||||
|
if contentType != "" {
|
||||||
|
request.Header.Set("Content-Type", contentType)
|
||||||
|
}
|
||||||
|
if token != "" {
|
||||||
|
request.Header.Set("Authorization", "Bearer "+token)
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := client.Do(request)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%s %s : %v", method, url, err)
|
||||||
|
}
|
||||||
|
data, err := io.ReadAll(response.Body)
|
||||||
|
if err != nil {
|
||||||
|
_ = response.Body.Close()
|
||||||
|
t.Fatalf("ReadAll : %v", err)
|
||||||
|
}
|
||||||
|
_ = response.Body.Close()
|
||||||
|
|
||||||
|
return response, data
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeBody[T any](t *testing.T, body []byte) T {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
var value T
|
||||||
|
if err := json.Unmarshal(body, &value); err != nil {
|
||||||
|
t.Fatalf("Unmarshal %s : %v", body, err)
|
||||||
|
}
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
func requireStatus(t *testing.T, response *http.Response, body []byte, want int) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
if response.StatusCode != want {
|
||||||
|
t.Fatalf("status = %d, attendu %d: %s", response.StatusCode, want, body)
|
||||||
|
}
|
||||||
|
}
|
||||||
+262
@@ -0,0 +1,262 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"mediarr/auth"
|
||||||
|
"mediarr/identity"
|
||||||
|
)
|
||||||
|
|
||||||
|
const sessionCookieName = "mediarr_session"
|
||||||
|
|
||||||
|
type contextKey int
|
||||||
|
|
||||||
|
const (
|
||||||
|
userContextKey contextKey = iota
|
||||||
|
tokenContextKey
|
||||||
|
)
|
||||||
|
|
||||||
|
type loginRequest struct {
|
||||||
|
Login string `json:"login"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
Label string `json:"label,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type loginResponse struct {
|
||||||
|
Token string `json:"token"`
|
||||||
|
Principal principalDTO `json:"principal"`
|
||||||
|
Account accountDTO `json:"account"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type createUserRequest struct {
|
||||||
|
Login string `json:"login"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
Role string `json:"role,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type userDTO struct {
|
||||||
|
Principal principalDTO `json:"principal"`
|
||||||
|
Account accountDTO `json:"account"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type principalDTO struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Kind string `json:"kind"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Status string `json:"status"`
|
||||||
|
CreatedAt int64 `json:"created_at"`
|
||||||
|
Groups []groupDTO `json:"groups"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type groupDTO struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Status string `json:"status"`
|
||||||
|
Role string `json:"role"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type accountDTO struct {
|
||||||
|
Login string `json:"login"`
|
||||||
|
State string `json:"state"`
|
||||||
|
MustChangePassword bool `json:"must_change_password"`
|
||||||
|
LastLoginAt *int64 `json:"last_login_at,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleLogin(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var request loginRequest
|
||||||
|
if err := decodeJSON(w, r, &request); err != nil {
|
||||||
|
writeErrorFor(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
user, err := s.node.Auth().Authenticate(r.Context(), request.Login, request.Password)
|
||||||
|
if err != nil {
|
||||||
|
writeErrorFor(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, token, err := s.node.Auth().CreateSession(r.Context(), auth.CreateSessionParams{
|
||||||
|
PrincipalID: user.Principal.ID,
|
||||||
|
Kind: "web",
|
||||||
|
Label: request.Label,
|
||||||
|
UserAgent: r.UserAgent(),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
writeErrorFor(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
http.SetCookie(w, &http.Cookie{
|
||||||
|
Name: sessionCookieName,
|
||||||
|
Value: token,
|
||||||
|
Path: "/",
|
||||||
|
MaxAge: int((30 * 24 * time.Hour).Seconds()),
|
||||||
|
HttpOnly: true,
|
||||||
|
SameSite: http.SameSiteLaxMode,
|
||||||
|
})
|
||||||
|
|
||||||
|
dto := userDTOFrom(user)
|
||||||
|
writeJSON(w, http.StatusOK, loginResponse{
|
||||||
|
Token: token,
|
||||||
|
Principal: dto.Principal,
|
||||||
|
Account: dto.Account,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleLogout(w http.ResponseWriter, r *http.Request) {
|
||||||
|
token := currentToken(r)
|
||||||
|
if token != "" {
|
||||||
|
if err := s.node.Auth().RevokeSessionByToken(r.Context(), token); err != nil && !errors.Is(err, auth.ErrInvalidSession) {
|
||||||
|
writeErrorFor(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
http.SetCookie(w, &http.Cookie{
|
||||||
|
Name: sessionCookieName,
|
||||||
|
Value: "",
|
||||||
|
Path: "/",
|
||||||
|
MaxAge: -1,
|
||||||
|
HttpOnly: true,
|
||||||
|
SameSite: http.SameSiteLaxMode,
|
||||||
|
})
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleMe(w http.ResponseWriter, r *http.Request) {
|
||||||
|
user := currentUser(r)
|
||||||
|
if user == nil {
|
||||||
|
writeError(w, http.StatusUnauthorized, auth.ErrInvalidSession.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
writeJSON(w, http.StatusOK, userDTOFrom(user))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleCreateUser(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var request createUserRequest
|
||||||
|
if err := decodeJSON(w, r, &request); err != nil {
|
||||||
|
writeErrorFor(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
role := request.Role
|
||||||
|
if role == "" {
|
||||||
|
role = identity.RoleMember
|
||||||
|
}
|
||||||
|
|
||||||
|
created, err := s.node.Auth().CreateUser(r.Context(), auth.CreateUserParams{
|
||||||
|
Login: request.Login,
|
||||||
|
Password: request.Password,
|
||||||
|
Role: role,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
writeErrorFor(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
writeJSON(w, http.StatusCreated, userDTOFrom(created))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) requireAuth(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
token := extractToken(r)
|
||||||
|
if token == "" {
|
||||||
|
writeError(w, http.StatusUnauthorized, auth.ErrInvalidSession.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, user, err := s.node.Auth().ValidateSession(r.Context(), token)
|
||||||
|
if err != nil {
|
||||||
|
writeErrorFor(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := context.WithValue(r.Context(), userContextKey, user)
|
||||||
|
ctx = context.WithValue(ctx, tokenContextKey, token)
|
||||||
|
next.ServeHTTP(w, r.WithContext(ctx))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) requireAdmin(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
user := currentUser(r)
|
||||||
|
if user == nil || !isAdmin(user) {
|
||||||
|
writeError(w, http.StatusForbidden, "droits administrateur requis")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func extractToken(r *http.Request) string {
|
||||||
|
if header := r.Header.Get("Authorization"); header != "" {
|
||||||
|
if strings.HasPrefix(strings.ToLower(header), "bearer ") {
|
||||||
|
return strings.TrimSpace(header[len("bearer "):])
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
cookie, err := r.Cookie(sessionCookieName)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return cookie.Value
|
||||||
|
}
|
||||||
|
|
||||||
|
func currentUser(r *http.Request) *auth.AuthenticatedUser {
|
||||||
|
user, _ := r.Context().Value(userContextKey).(*auth.AuthenticatedUser)
|
||||||
|
return user
|
||||||
|
}
|
||||||
|
|
||||||
|
func currentToken(r *http.Request) string {
|
||||||
|
token, _ := r.Context().Value(tokenContextKey).(string)
|
||||||
|
return token
|
||||||
|
}
|
||||||
|
|
||||||
|
func isAdmin(user *auth.AuthenticatedUser) bool {
|
||||||
|
for _, group := range user.Principal.Groups {
|
||||||
|
if group.Role == identity.RoleAdmin || group.Name == "admins" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func userDTOFrom(user *auth.AuthenticatedUser) userDTO {
|
||||||
|
groups := make([]groupDTO, 0, len(user.Principal.Groups))
|
||||||
|
for _, group := range user.Principal.Groups {
|
||||||
|
groups = append(groups, groupDTO{
|
||||||
|
ID: group.ID.String(),
|
||||||
|
Name: group.Name,
|
||||||
|
Status: group.Status,
|
||||||
|
Role: group.Role,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
var lastLoginAt *int64
|
||||||
|
if user.Account.LastLoginAt.Valid {
|
||||||
|
value := user.Account.LastLoginAt.Int64
|
||||||
|
lastLoginAt = &value
|
||||||
|
}
|
||||||
|
|
||||||
|
return userDTO{
|
||||||
|
Principal: principalDTO{
|
||||||
|
ID: user.Principal.ID.String(),
|
||||||
|
Kind: user.Principal.Kind,
|
||||||
|
Name: user.Principal.Name,
|
||||||
|
Status: user.Principal.Status,
|
||||||
|
CreatedAt: user.Principal.CreatedAt,
|
||||||
|
Groups: groups,
|
||||||
|
},
|
||||||
|
Account: accountDTO{
|
||||||
|
Login: user.Account.Login,
|
||||||
|
State: user.Account.State,
|
||||||
|
MustChangePassword: user.Account.MustChangePassword,
|
||||||
|
LastLoginAt: lastLoginAt,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
+284
@@ -0,0 +1,284 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"mime"
|
||||||
|
"net/http"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"mediarr/content"
|
||||||
|
"mediarr/ident"
|
||||||
|
)
|
||||||
|
|
||||||
|
type versionDTO struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
DocumentID string `json:"document_id"`
|
||||||
|
Seq int64 `json:"seq"`
|
||||||
|
ContentRoot string `json:"content_root"`
|
||||||
|
Size int64 `json:"size"`
|
||||||
|
Mime string `json:"mime,omitempty"`
|
||||||
|
AuthorID string `json:"author_id"`
|
||||||
|
Origin string `json:"origin"`
|
||||||
|
Message string `json:"message,omitempty"`
|
||||||
|
Pinned bool `json:"pinned"`
|
||||||
|
ContentPurgedAt *int64 `json:"content_purged_at,omitempty"`
|
||||||
|
CreatedAt int64 `json:"created_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type setHeadRequest struct {
|
||||||
|
VersionID string `json:"version_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type retentionDTO struct {
|
||||||
|
Purged []string `json:"purged"`
|
||||||
|
Kept int `json:"kept"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type garbageCollectionDTO struct {
|
||||||
|
Blobs int `json:"blobs"`
|
||||||
|
Locations int `json:"locations"`
|
||||||
|
Bytes int64 `json:"bytes"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleListVersions(w http.ResponseWriter, r *http.Request) {
|
||||||
|
documentID, err := idParam(r, "id")
|
||||||
|
if err != nil {
|
||||||
|
writeErrorFor(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
versions, err := s.node.Content().ListVersions(r.Context(), actorID(r), documentID)
|
||||||
|
if err != nil {
|
||||||
|
writeErrorFor(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
result := make([]versionDTO, 0, len(versions))
|
||||||
|
for i := range versions {
|
||||||
|
result = append(result, versionDTOFrom(&versions[i]))
|
||||||
|
}
|
||||||
|
writeJSON(w, http.StatusOK, result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleUploadVersion(w http.ResponseWriter, r *http.Request) {
|
||||||
|
documentID, err := idParam(r, "id")
|
||||||
|
if err != nil {
|
||||||
|
writeErrorFor(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
r.Body = http.MaxBytesReader(w, r.Body, maxUploadBody)
|
||||||
|
defer func() {
|
||||||
|
if r.MultipartForm != nil {
|
||||||
|
_ = r.MultipartForm.RemoveAll()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
file, header, err := r.FormFile("file")
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, http.StatusBadRequest, "fichier requis")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
origin := r.FormValue("origin")
|
||||||
|
if origin == "" {
|
||||||
|
origin = content.OriginUpload
|
||||||
|
}
|
||||||
|
if origin != content.OriginUpload && origin != content.OriginRestore {
|
||||||
|
writeError(w, http.StatusBadRequest, "origine invalide")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
mimeType := strings.TrimSpace(r.FormValue("mime"))
|
||||||
|
if mimeType == "" {
|
||||||
|
mimeType = mime.TypeByExtension(strings.ToLower(filepath.Ext(header.Filename)))
|
||||||
|
}
|
||||||
|
if mimeType == "" {
|
||||||
|
mimeType = "application/octet-stream"
|
||||||
|
}
|
||||||
|
|
||||||
|
pinned := false
|
||||||
|
if raw := r.FormValue("pinned"); raw != "" {
|
||||||
|
parsed, err := strconv.ParseBool(raw)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, http.StatusBadRequest, "pinned invalide")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
pinned = parsed
|
||||||
|
}
|
||||||
|
|
||||||
|
version, err := s.node.Content().CreateVersion(r.Context(), actorID(r), documentID, content.CreateVersionParams{
|
||||||
|
Source: file,
|
||||||
|
Mime: mimeType,
|
||||||
|
Origin: origin,
|
||||||
|
Message: r.FormValue("message"),
|
||||||
|
Pinned: pinned,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
writeErrorFor(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Location", "/api/versions/"+version.ID.String())
|
||||||
|
writeJSON(w, http.StatusCreated, versionDTOFrom(version))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleDownloadVersion(w http.ResponseWriter, r *http.Request) {
|
||||||
|
versionID, err := idParam(r, "id")
|
||||||
|
if err != nil {
|
||||||
|
writeErrorFor(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
reader, version, err := s.node.Content().OpenVersion(r.Context(), actorID(r), versionID)
|
||||||
|
if err != nil {
|
||||||
|
writeErrorFor(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer reader.Close()
|
||||||
|
|
||||||
|
contentType := version.Mime
|
||||||
|
if contentType == "" {
|
||||||
|
contentType = "application/octet-stream"
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", contentType)
|
||||||
|
w.Header().Set("Content-Length", strconv.FormatInt(version.Size, 10))
|
||||||
|
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", fmt.Sprintf("version-%d", version.Seq)))
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
_, _ = io.Copy(w, reader)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleGetHead(w http.ResponseWriter, r *http.Request) {
|
||||||
|
documentID, err := idParam(r, "id")
|
||||||
|
if err != nil {
|
||||||
|
writeErrorFor(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
version, err := s.node.Content().HeadVersion(r.Context(), actorID(r), documentID)
|
||||||
|
if err != nil {
|
||||||
|
writeErrorFor(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if version == nil {
|
||||||
|
writeJSON(w, http.StatusOK, map[string]any{"version": nil})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
writeJSON(w, http.StatusOK, map[string]any{"version": versionDTOFrom(version)})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleSetHead(w http.ResponseWriter, r *http.Request) {
|
||||||
|
documentID, err := idParam(r, "id")
|
||||||
|
if err != nil {
|
||||||
|
writeErrorFor(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var request setHeadRequest
|
||||||
|
if err := decodeJSON(w, r, &request); err != nil {
|
||||||
|
writeErrorFor(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
versionID, err := ident.Parse(request.VersionID)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, http.StatusBadRequest, "version_id invalide")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
version, err := s.node.Content().SetHeadVersion(r.Context(), actorID(r), documentID, versionID)
|
||||||
|
if err != nil {
|
||||||
|
writeErrorFor(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
writeJSON(w, http.StatusOK, versionDTOFrom(version))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleApplyRetention(w http.ResponseWriter, r *http.Request) {
|
||||||
|
documentID, err := idParam(r, "id")
|
||||||
|
if err != nil {
|
||||||
|
writeErrorFor(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := s.node.Content().ApplyRetention(r.Context(), actorID(r), documentID)
|
||||||
|
if err != nil {
|
||||||
|
writeErrorFor(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
purged := make([]string, 0, len(result.Purged))
|
||||||
|
for _, id := range result.Purged {
|
||||||
|
purged = append(purged, id.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
writeJSON(w, http.StatusOK, retentionDTO{Purged: purged, Kept: result.Kept})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handlePurgeVersion(w http.ResponseWriter, r *http.Request) {
|
||||||
|
versionID, err := idParam(r, "id")
|
||||||
|
if err != nil {
|
||||||
|
writeErrorFor(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.node.Content().PurgeVersion(r.Context(), actorID(r), versionID); err != nil {
|
||||||
|
writeErrorFor(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleVerifyVersion(w http.ResponseWriter, r *http.Request) {
|
||||||
|
versionID, err := idParam(r, "id")
|
||||||
|
if err != nil {
|
||||||
|
writeErrorFor(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.node.Content().VerifyVersion(r.Context(), actorID(r), versionID); err != nil {
|
||||||
|
writeErrorFor(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleCollectGarbage(w http.ResponseWriter, r *http.Request) {
|
||||||
|
result, err := s.node.Content().CollectGarbage(r.Context())
|
||||||
|
if err != nil {
|
||||||
|
writeErrorFor(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
writeJSON(w, http.StatusOK, garbageCollectionDTO{
|
||||||
|
Blobs: result.Blobs,
|
||||||
|
Locations: result.Locations,
|
||||||
|
Bytes: result.Bytes,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func versionDTOFrom(version *content.Version) versionDTO {
|
||||||
|
return versionDTO{
|
||||||
|
ID: version.ID.String(),
|
||||||
|
DocumentID: version.DocumentID.String(),
|
||||||
|
Seq: version.Seq,
|
||||||
|
ContentRoot: hex.EncodeToString(version.ContentRoot),
|
||||||
|
Size: version.Size,
|
||||||
|
Mime: version.Mime,
|
||||||
|
AuthorID: version.AuthorID.String(),
|
||||||
|
Origin: version.Origin,
|
||||||
|
Message: version.Message,
|
||||||
|
Pinned: version.Pinned,
|
||||||
|
ContentPurgedAt: version.ContentPurgedAt,
|
||||||
|
CreatedAt: version.CreatedAt,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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
|
||||||
|
}
|
||||||
+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
|
||||||
|
}
|
||||||
@@ -24,6 +24,12 @@ type Account struct {
|
|||||||
CreatedAt int64
|
CreatedAt int64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
AccountStateActive = "active"
|
||||||
|
AccountStateLocked = "locked"
|
||||||
|
AccountStateDisabled = "disabled"
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrInvalidCredentials = errors.New("identifiants invalides")
|
ErrInvalidCredentials = errors.New("identifiants invalides")
|
||||||
ErrAccountLocked = errors.New("compte verrouillé")
|
ErrAccountLocked = errors.New("compte verrouillé")
|
||||||
|
|||||||
+6
-5
@@ -12,6 +12,7 @@ import (
|
|||||||
|
|
||||||
"mediarr/database"
|
"mediarr/database"
|
||||||
"mediarr/ident"
|
"mediarr/ident"
|
||||||
|
"mediarr/identity"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -57,7 +58,7 @@ func (s *Service) CreateSession(ctx context.Context, params CreateSessionParams)
|
|||||||
}
|
}
|
||||||
return nil, "", err
|
return nil, "", err
|
||||||
}
|
}
|
||||||
if principal.Status != "active" {
|
if principal.Status != identity.StatusActive {
|
||||||
return nil, "", ErrInvalidSession
|
return nil, "", ErrInvalidSession
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,10 +68,10 @@ func (s *Service) CreateSession(ctx context.Context, params CreateSessionParams)
|
|||||||
switch {
|
switch {
|
||||||
case err == nil:
|
case err == nil:
|
||||||
switch account.State {
|
switch account.State {
|
||||||
case "active":
|
case AccountStateActive:
|
||||||
case "disabled":
|
case AccountStateDisabled:
|
||||||
return nil, "", ErrAccountDisabled
|
return nil, "", ErrAccountDisabled
|
||||||
case "locked":
|
case AccountStateLocked:
|
||||||
if !account.LockedUntil.Valid || account.LockedUntil.Int64 > now {
|
if !account.LockedUntil.Valid || account.LockedUntil.Int64 > now {
|
||||||
return nil, "", ErrAccountLocked
|
return nil, "", ErrAccountLocked
|
||||||
}
|
}
|
||||||
@@ -152,7 +153,7 @@ func (s *Service) ValidateSession(ctx context.Context, token string) (*Session,
|
|||||||
}
|
}
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
if user.Principal.Status != "active" || user.Account.State == "disabled" {
|
if user.Principal.Status != identity.StatusActive || user.Account.State == AccountStateDisabled {
|
||||||
return nil, nil, ErrInvalidSession
|
return nil, nil, ErrInvalidSession
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -149,8 +149,8 @@ func TestGuestSession(t *testing.T) {
|
|||||||
if authenticated.Account.Login != "jean" {
|
if authenticated.Account.Login != "jean" {
|
||||||
t.Fatalf("login = %q, attendu jean", authenticated.Account.Login)
|
t.Fatalf("login = %q, attendu jean", authenticated.Account.Login)
|
||||||
}
|
}
|
||||||
if authenticated.Account.State != "active" {
|
if authenticated.Account.State != AccountStateActive {
|
||||||
t.Fatalf("state = %q, attendu active", authenticated.Account.State)
|
t.Fatalf("state = %q, attendu %q", authenticated.Account.State, AccountStateActive)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -161,7 +161,7 @@ func TestCreateSessionRejectsInactivePrincipal(t *testing.T) {
|
|||||||
created := mustCreateUser(t, service, "alice", "correct horse battery staple", identity.RoleMember)
|
created := mustCreateUser(t, service, "alice", "correct horse battery staple", identity.RoleMember)
|
||||||
|
|
||||||
_, err := service.db.WriteQueries.UpdatePrincipalStatus(ctx, database.UpdatePrincipalStatusParams{
|
_, err := service.db.WriteQueries.UpdatePrincipalStatus(ctx, database.UpdatePrincipalStatusParams{
|
||||||
Status: "disabled",
|
Status: identity.StatusDisabled,
|
||||||
ID: created.Principal.ID,
|
ID: created.Principal.ID,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
+8
-8
@@ -70,7 +70,7 @@ func (s *Service) CreateUser(ctx context.Context, params CreateUserParams) (*Aut
|
|||||||
ID: userID,
|
ID: userID,
|
||||||
Kind: identity.KindUser,
|
Kind: identity.KindUser,
|
||||||
Name: display,
|
Name: display,
|
||||||
Status: "active",
|
Status: identity.StatusActive,
|
||||||
CreatedAt: now,
|
CreatedAt: now,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -89,7 +89,7 @@ func (s *Service) CreateUser(ctx context.Context, params CreateUserParams) (*Aut
|
|||||||
Valid: true,
|
Valid: true,
|
||||||
},
|
},
|
||||||
MustChangePassword: 0,
|
MustChangePassword: 0,
|
||||||
State: "active",
|
State: AccountStateActive,
|
||||||
FailedAttempts: 0,
|
FailedAttempts: 0,
|
||||||
CreatedAt: now,
|
CreatedAt: now,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
@@ -135,23 +135,23 @@ func (s *Service) Authenticate(ctx context.Context, login, password string) (*Au
|
|||||||
}
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if principal.Status != "active" {
|
if principal.Status != identity.StatusActive {
|
||||||
return nil, ErrInvalidCredentials
|
return nil, ErrInvalidCredentials
|
||||||
}
|
}
|
||||||
|
|
||||||
now := time.Now().UnixMilli()
|
now := time.Now().UnixMilli()
|
||||||
|
|
||||||
switch account.State {
|
switch account.State {
|
||||||
case "disabled":
|
case AccountStateDisabled:
|
||||||
return nil, ErrAccountDisabled
|
return nil, ErrAccountDisabled
|
||||||
case "locked":
|
case AccountStateLocked:
|
||||||
if !account.LockedUntil.Valid || account.LockedUntil.Int64 > now {
|
if !account.LockedUntil.Valid || account.LockedUntil.Int64 > now {
|
||||||
return nil, ErrAccountLocked
|
return nil, ErrAccountLocked
|
||||||
}
|
}
|
||||||
if _, err := s.db.WriteQueries.UnlockAccount(ctx, account.PrincipalID); err != nil {
|
if _, err := s.db.WriteQueries.UnlockAccount(ctx, account.PrincipalID); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
account.State = "active"
|
account.State = AccountStateActive
|
||||||
account.FailedAttempts = 0
|
account.FailedAttempts = 0
|
||||||
account.LockedUntil = sql.NullInt64{}
|
account.LockedUntil = sql.NullInt64{}
|
||||||
}
|
}
|
||||||
@@ -177,7 +177,7 @@ func (s *Service) Authenticate(ctx context.Context, login, password string) (*Au
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if updated.State == "locked" {
|
if updated.State == AccountStateLocked {
|
||||||
return nil, ErrAccountLocked
|
return nil, ErrAccountLocked
|
||||||
}
|
}
|
||||||
return nil, ErrInvalidCredentials
|
return nil, ErrInvalidCredentials
|
||||||
@@ -241,7 +241,7 @@ func (s *Service) loadAuthenticatedUser(ctx context.Context, id ident.UUID) (*Au
|
|||||||
Account: Account{
|
Account: Account{
|
||||||
PrincipalID: id,
|
PrincipalID: id,
|
||||||
Login: principal.Name,
|
Login: principal.Name,
|
||||||
State: "active",
|
State: AccountStateActive,
|
||||||
},
|
},
|
||||||
}, nil
|
}, nil
|
||||||
default:
|
default:
|
||||||
|
|||||||
+9
-9
@@ -32,11 +32,11 @@ func TestCreateUser(t *testing.T) {
|
|||||||
if created.Principal.Name != "Alice" {
|
if created.Principal.Name != "Alice" {
|
||||||
t.Fatalf("name = %q, attendu Alice", created.Principal.Name)
|
t.Fatalf("name = %q, attendu Alice", created.Principal.Name)
|
||||||
}
|
}
|
||||||
if created.Principal.Status != "active" {
|
if created.Principal.Status != identity.StatusActive {
|
||||||
t.Fatalf("status = %q, attendu active", created.Principal.Status)
|
t.Fatalf("status = %q, attendu %q", created.Principal.Status, identity.StatusActive)
|
||||||
}
|
}
|
||||||
if created.Account.State != "active" {
|
if created.Account.State != AccountStateActive {
|
||||||
t.Fatalf("état du compte = %q, attendu active", created.Account.State)
|
t.Fatalf("état du compte = %q, attendu %q", created.Account.State, AccountStateActive)
|
||||||
}
|
}
|
||||||
if len(created.Principal.Groups) != 1 || created.Principal.Groups[0].Name != "members" {
|
if len(created.Principal.Groups) != 1 || created.Principal.Groups[0].Name != "members" {
|
||||||
t.Fatalf("groupes = %+v, attendu members", created.Principal.Groups)
|
t.Fatalf("groupes = %+v, attendu members", created.Principal.Groups)
|
||||||
@@ -198,8 +198,8 @@ func TestAuthenticateLocksAccountAfterTooManyFailures(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("GetAccount : %v", err)
|
t.Fatalf("GetAccount : %v", err)
|
||||||
}
|
}
|
||||||
if account.State != "locked" {
|
if account.State != AccountStateLocked {
|
||||||
t.Fatalf("state = %q, attendu locked", account.State)
|
t.Fatalf("state = %q, attendu %q", account.State, AccountStateLocked)
|
||||||
}
|
}
|
||||||
if !account.LockedUntil.Valid || account.LockedUntil.Int64 <= time.Now().UnixMilli() {
|
if !account.LockedUntil.Valid || account.LockedUntil.Int64 <= time.Now().UnixMilli() {
|
||||||
t.Fatalf("locked_until = %+v, attendu dans le futur", account.LockedUntil)
|
t.Fatalf("locked_until = %+v, attendu dans le futur", account.LockedUntil)
|
||||||
@@ -232,8 +232,8 @@ func TestAuthenticateUnlocksExpiredAccount(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("GetAccount : %v", err)
|
t.Fatalf("GetAccount : %v", err)
|
||||||
}
|
}
|
||||||
if account.State != "active" {
|
if account.State != AccountStateActive {
|
||||||
t.Fatalf("state = %q, attendu active", account.State)
|
t.Fatalf("state = %q, attendu %q", account.State, AccountStateActive)
|
||||||
}
|
}
|
||||||
if account.FailedAttempts != 0 {
|
if account.FailedAttempts != 0 {
|
||||||
t.Fatalf("failed_attempts = %d, attendu 0", account.FailedAttempts)
|
t.Fatalf("failed_attempts = %d, attendu 0", account.FailedAttempts)
|
||||||
@@ -247,7 +247,7 @@ func TestAuthenticateRejectsDisabledAccount(t *testing.T) {
|
|||||||
created := mustCreateUser(t, service, "alice", "correct horse battery staple", identity.RoleMember)
|
created := mustCreateUser(t, service, "alice", "correct horse battery staple", identity.RoleMember)
|
||||||
|
|
||||||
_, err := service.db.WriteQueries.UpdateAccountState(ctx, database.UpdateAccountStateParams{
|
_, err := service.db.WriteQueries.UpdateAccountState(ctx, database.UpdateAccountStateParams{
|
||||||
State: "disabled",
|
State: AccountStateDisabled,
|
||||||
PrincipalID: created.Principal.ID,
|
PrincipalID: created.Principal.ID,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
+409
-7
@@ -1,22 +1,424 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"mediarr/api"
|
||||||
|
"mediarr/auth"
|
||||||
|
"mediarr/document"
|
||||||
|
"mediarr/ident"
|
||||||
|
"mediarr/identity"
|
||||||
|
"mediarr/ingest"
|
||||||
"mediarr/node"
|
"mediarr/node"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
const defaultStoragePolicyID = "00000000-0000-7000-8000-000000000101"
|
||||||
|
|
||||||
err, nodeA := node.NewNode(node.NewNodeParams{DatabaseName: "database"})
|
func main() {
|
||||||
|
if len(os.Args) < 2 {
|
||||||
|
usage()
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
switch os.Args[1] {
|
||||||
|
case "serve":
|
||||||
|
err = runServe(os.Args[2:])
|
||||||
|
case "user":
|
||||||
|
err = runUser(os.Args[2:])
|
||||||
|
case "library":
|
||||||
|
err = runLibrary(os.Args[2:])
|
||||||
|
case "scan":
|
||||||
|
err = runScan(os.Args[2:])
|
||||||
|
case "import":
|
||||||
|
err = runImport(os.Args[2:])
|
||||||
|
case "help", "-h", "--help":
|
||||||
|
usage()
|
||||||
|
default:
|
||||||
|
usage()
|
||||||
|
err = fmt.Errorf("commande inconnue: %s", os.Args[1])
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
defer nodeA.Close()
|
func usage() {
|
||||||
|
fmt.Fprintln(os.Stderr, `Usage:
|
||||||
|
server serve [options]
|
||||||
|
server user create -login LOGIN -password PASSWORD [-role member|admin]
|
||||||
|
server user password -id UUID -password PASSWORD
|
||||||
|
server library create -owner UUID -name NAME [options]
|
||||||
|
server scan -actor UUID -parent UUID -path PATH [-prune]
|
||||||
|
server import -actor UUID -parent UUID -path PATH [-prune]
|
||||||
|
|
||||||
if err := nodeA.Up(); err != nil {
|
Options de stockage:
|
||||||
log.Fatalln(err)
|
-db NOM nom de la base SQLite (défaut: database)
|
||||||
|
-storage PATH répertoire de stockage (défaut: data)
|
||||||
|
-cluster-key PATH chemin de la clé cluster (défaut: cluster.key)`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type nodeFlags struct {
|
||||||
|
database string
|
||||||
|
storage string
|
||||||
|
clusterKey string
|
||||||
|
}
|
||||||
|
|
||||||
|
func addNodeFlags(fs *flag.FlagSet) *nodeFlags {
|
||||||
|
flags := &nodeFlags{}
|
||||||
|
fs.StringVar(&flags.database, "db", "database", "nom de la base SQLite")
|
||||||
|
fs.StringVar(&flags.storage, "storage", "data", "répertoire de stockage")
|
||||||
|
fs.StringVar(&flags.clusterKey, "cluster-key", "cluster.key", "chemin de la clé cluster")
|
||||||
|
return flags
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *nodeFlags) open() (node.Node, error) {
|
||||||
|
err, n := node.NewNode(node.NewNodeParams{
|
||||||
|
DatabaseName: f.database,
|
||||||
|
StoragePath: f.storage,
|
||||||
|
ClusterKeyPath: f.clusterKey,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return node.Node{}, err
|
||||||
|
}
|
||||||
|
if err := n.Up(); err != nil {
|
||||||
|
_ = n.Close()
|
||||||
|
return node.Node{}, err
|
||||||
|
}
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func runServe(args []string) error {
|
||||||
|
fs := flag.NewFlagSet("serve", flag.ExitOnError)
|
||||||
|
addr := fs.String("addr", ":8080", "adresse HTTP")
|
||||||
|
flags := addNodeFlags(fs)
|
||||||
|
if err := fs.Parse(args); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
n, err := flags.open()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer n.Close()
|
||||||
|
|
||||||
|
server := &http.Server{
|
||||||
|
Addr: *addr,
|
||||||
|
Handler: api.NewServer(&n).Handler(),
|
||||||
|
ReadHeaderTimeout: 10 * time.Second,
|
||||||
|
IdleTimeout: 120 * time.Second,
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||||
|
defer stop()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
<-ctx.Done()
|
||||||
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
_ = server.Shutdown(shutdownCtx)
|
||||||
|
}()
|
||||||
|
|
||||||
|
fmt.Printf("Mediarr écoute sur %s\n", *addr)
|
||||||
|
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func runUser(args []string) error {
|
||||||
|
if len(args) == 0 {
|
||||||
|
return errors.New("usage: server user create|password [options]")
|
||||||
|
}
|
||||||
|
|
||||||
|
switch args[0] {
|
||||||
|
case "create":
|
||||||
|
return runUserCreate(args[1:])
|
||||||
|
case "password":
|
||||||
|
return runUserPassword(args[1:])
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("sous-commande user inconnue: %s", args[0])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func runUserCreate(args []string) error {
|
||||||
|
fs := flag.NewFlagSet("user create", flag.ExitOnError)
|
||||||
|
login := fs.String("login", "", "login")
|
||||||
|
password := fs.String("password", "", "mot de passe (ou MEDIARR_PASSWORD)")
|
||||||
|
role := fs.String("role", identity.RoleMember, "rôle: member ou admin")
|
||||||
|
flags := addNodeFlags(fs)
|
||||||
|
if err := fs.Parse(args); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if *login == "" {
|
||||||
|
return errors.New("login requis")
|
||||||
|
}
|
||||||
|
passwordValue := *password
|
||||||
|
if passwordValue == "" {
|
||||||
|
passwordValue = os.Getenv("MEDIARR_PASSWORD")
|
||||||
|
}
|
||||||
|
if passwordValue == "" {
|
||||||
|
return errors.New("mot de passe requis (-password ou MEDIARR_PASSWORD)")
|
||||||
|
}
|
||||||
|
|
||||||
|
n, err := flags.open()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer n.Close()
|
||||||
|
|
||||||
|
created, err := n.Auth().CreateUser(context.Background(), auth.CreateUserParams{
|
||||||
|
Login: *login,
|
||||||
|
Password: passwordValue,
|
||||||
|
Role: *role,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return printJSON(map[string]any{
|
||||||
|
"id": created.Principal.ID.String(),
|
||||||
|
"login": created.Account.Login,
|
||||||
|
"role": *role,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func runUserPassword(args []string) error {
|
||||||
|
fs := flag.NewFlagSet("user password", flag.ExitOnError)
|
||||||
|
id := fs.String("id", "", "UUID du principal")
|
||||||
|
password := fs.String("password", "", "mot de passe (ou MEDIARR_PASSWORD)")
|
||||||
|
flags := addNodeFlags(fs)
|
||||||
|
if err := fs.Parse(args); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
principalID, err := parseID(*id, "id")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
passwordValue := *password
|
||||||
|
if passwordValue == "" {
|
||||||
|
passwordValue = os.Getenv("MEDIARR_PASSWORD")
|
||||||
|
}
|
||||||
|
if passwordValue == "" {
|
||||||
|
return errors.New("mot de passe requis (-password ou MEDIARR_PASSWORD)")
|
||||||
|
}
|
||||||
|
|
||||||
|
n, err := flags.open()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer n.Close()
|
||||||
|
|
||||||
|
if err := n.Auth().SetPassword(context.Background(), principalID, passwordValue); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return printJSON(map[string]string{"status": "updated", "id": principalID.String()})
|
||||||
|
}
|
||||||
|
|
||||||
|
func runLibrary(args []string) error {
|
||||||
|
if len(args) == 0 || args[0] != "create" {
|
||||||
|
return errors.New("usage: server library create -owner UUID -name NAME [options]")
|
||||||
|
}
|
||||||
|
return runLibraryCreate(args[1:])
|
||||||
|
}
|
||||||
|
|
||||||
|
func runLibraryCreate(args []string) error {
|
||||||
|
fs := flag.NewFlagSet("library create", flag.ExitOnError)
|
||||||
|
owner := fs.String("owner", "", "UUID du propriétaire")
|
||||||
|
name := fs.String("name", "", "nom de la bibliothèque")
|
||||||
|
policy := fs.String("policy", defaultStoragePolicyID, "UUID de la politique de stockage")
|
||||||
|
visibility := fs.String("visibility", document.VisibilityPrivate, "PRIVATE, CLUSTER, FEDERATED ou PUBLIC")
|
||||||
|
aclInherit := fs.Bool("acl-inherit", true, "héritage ACL")
|
||||||
|
metadata := fs.String("metadata", "{}", "métadonnées JSON")
|
||||||
|
flags := addNodeFlags(fs)
|
||||||
|
if err := fs.Parse(args); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
ownerID, err := parseID(*owner, "owner")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
policyID, err := parseID(*policy, "policy")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if *name == "" {
|
||||||
|
return errors.New("name requis")
|
||||||
|
}
|
||||||
|
if !json.Valid([]byte(*metadata)) {
|
||||||
|
return errors.New("metadata doit être un JSON valide")
|
||||||
|
}
|
||||||
|
|
||||||
|
n, err := flags.open()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer n.Close()
|
||||||
|
|
||||||
|
created, err := n.Documents().CreateLibrary(context.Background(), ownerID, document.CreateLibraryParams{
|
||||||
|
OwnerID: ownerID,
|
||||||
|
CreatedBy: ownerID,
|
||||||
|
Name: *name,
|
||||||
|
PolicyID: policyID,
|
||||||
|
Visibility: *visibility,
|
||||||
|
ACLInherit: aclInherit,
|
||||||
|
Metadata: *metadata,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return printJSON(map[string]any{
|
||||||
|
"id": created.ID.String(),
|
||||||
|
"kind": created.Kind,
|
||||||
|
"name": created.Name,
|
||||||
|
"path": created.Path,
|
||||||
|
"owner_id": created.OwnerID.String(),
|
||||||
|
"policy_id": created.PolicyID.String(),
|
||||||
|
"visibility": created.Visibility,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func runScan(args []string) error {
|
||||||
|
fs := flag.NewFlagSet("scan", flag.ExitOnError)
|
||||||
|
actor := fs.String("actor", "", "UUID du principal")
|
||||||
|
parent := fs.String("parent", "", "UUID du document parent")
|
||||||
|
root := fs.String("path", "", "chemin à scanner")
|
||||||
|
prune := fs.Bool("prune", false, "supprimer les documents absents du disque")
|
||||||
|
flags := addNodeFlags(fs)
|
||||||
|
if err := fs.Parse(args); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
actorID, err := parseID(*actor, "actor")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
parentID, err := parseID(*parent, "parent")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(*root) == "" {
|
||||||
|
return errors.New("path requis")
|
||||||
|
}
|
||||||
|
|
||||||
|
n, err := flags.open()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer n.Close()
|
||||||
|
|
||||||
|
result, err := n.Ingest().ScanWithOptions(context.Background(), actorID, parentID, *root, ingest.ScanOptions{Prune: *prune})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := printJSON(scanOutputFrom(result)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if result.HasErrors() {
|
||||||
|
return fmt.Errorf("scan terminé avec %d erreur(s)", len(result.Errors))
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func runImport(args []string) error {
|
||||||
|
fs := flag.NewFlagSet("import", flag.ExitOnError)
|
||||||
|
actor := fs.String("actor", "", "UUID du principal")
|
||||||
|
parent := fs.String("parent", "", "UUID du document parent")
|
||||||
|
root := fs.String("path", "", "fichier ou dossier à importer")
|
||||||
|
prune := fs.Bool("prune", false, "supprimer les documents absents du disque")
|
||||||
|
flags := addNodeFlags(fs)
|
||||||
|
if err := fs.Parse(args); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
actorID, err := parseID(*actor, "actor")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
parentID, err := parseID(*parent, "parent")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(*root) == "" {
|
||||||
|
return errors.New("path requis")
|
||||||
|
}
|
||||||
|
|
||||||
|
n, err := flags.open()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer n.Close()
|
||||||
|
|
||||||
|
result, err := n.Ingest().ImportWithOptions(context.Background(), actorID, parentID, *root, ingest.ScanOptions{Prune: *prune})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := printJSON(scanOutputFrom(result)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if result.HasErrors() {
|
||||||
|
return fmt.Errorf("import terminé avec %d erreur(s)", len(result.Errors))
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type scanOutput struct {
|
||||||
|
Files int `json:"files"`
|
||||||
|
Directories int `json:"directories"`
|
||||||
|
Created int `json:"created"`
|
||||||
|
Updated int `json:"updated"`
|
||||||
|
Unchanged int `json:"unchanged"`
|
||||||
|
Pruned int `json:"pruned"`
|
||||||
|
Errors []string `json:"errors,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func scanOutputFrom(result ingest.ScanResult) scanOutput {
|
||||||
|
output := scanOutput{
|
||||||
|
Files: result.Files,
|
||||||
|
Directories: result.Directories,
|
||||||
|
Created: result.Created,
|
||||||
|
Updated: result.Updated,
|
||||||
|
Unchanged: result.Unchanged,
|
||||||
|
Pruned: result.Pruned,
|
||||||
|
}
|
||||||
|
for _, scanErr := range result.Errors {
|
||||||
|
output.Errors = append(output.Errors, scanErr.Error())
|
||||||
|
}
|
||||||
|
return output
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseID(value, name string) (ident.UUID, error) {
|
||||||
|
if strings.TrimSpace(value) == "" {
|
||||||
|
return ident.UUID{}, fmt.Errorf("%s requis", name)
|
||||||
|
}
|
||||||
|
id, err := ident.Parse(value)
|
||||||
|
if err != nil {
|
||||||
|
return ident.UUID{}, fmt.Errorf("%s invalide: %w", name, err)
|
||||||
|
}
|
||||||
|
return id, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func printJSON(value any) error {
|
||||||
|
encoder := json.NewEncoder(os.Stdout)
|
||||||
|
encoder.SetIndent("", " ")
|
||||||
|
return encoder.Encode(value)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,78 @@
|
|||||||
|
package content
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"mediarr/ident"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
OriginUpload = "upload"
|
||||||
|
OriginImport = "import"
|
||||||
|
OriginMerge = "merge"
|
||||||
|
OriginRestore = "restore"
|
||||||
|
OriginSystem = "system"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
StagingStateStaged = "staged"
|
||||||
|
StagingStateHashing = "hashing"
|
||||||
|
StagingStateCommitted = "committed"
|
||||||
|
StagingStateDiscarded = "discarded"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
BlobStatePresent = "present"
|
||||||
|
BlobStateCorrupt = "corrupt"
|
||||||
|
BlobRoleAuthoritative = "authoritative"
|
||||||
|
BlobTreeStateReady = "ready"
|
||||||
|
BlobEncryptionNone = "none"
|
||||||
|
BlobLocationPresent = "present"
|
||||||
|
BlobLocationMissing = "missing"
|
||||||
|
)
|
||||||
|
|
||||||
|
const blobUnitSize = 1024 * 1024
|
||||||
|
|
||||||
|
type Version struct {
|
||||||
|
ID ident.UUID
|
||||||
|
DocumentID ident.UUID
|
||||||
|
Seq int64
|
||||||
|
ContentRoot []byte
|
||||||
|
Size int64
|
||||||
|
Mime string
|
||||||
|
AuthorID ident.UUID
|
||||||
|
DeviceID *ident.UUID
|
||||||
|
Origin string
|
||||||
|
Message string
|
||||||
|
Pinned bool
|
||||||
|
ContentPurgedAt *int64
|
||||||
|
CreatedAt int64
|
||||||
|
}
|
||||||
|
|
||||||
|
type CreateVersionParams struct {
|
||||||
|
Source io.Reader
|
||||||
|
Mime string
|
||||||
|
Origin string
|
||||||
|
Message string
|
||||||
|
Pinned bool
|
||||||
|
DeviceID *ident.UUID
|
||||||
|
JobRef string
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrStorageNotConfigured = errors.New("stockage non configuré")
|
||||||
|
ErrStorageBackendMissing = errors.New("backend de stockage introuvable")
|
||||||
|
ErrInvalidSource = errors.New("source de contenu invalide")
|
||||||
|
ErrInvalidOrigin = errors.New("origine de version invalide")
|
||||||
|
ErrNoContentKind = errors.New("type de document sans contenu")
|
||||||
|
ErrNoStoragePolicy = errors.New("politique de stockage introuvable")
|
||||||
|
ErrBlobUnavailable = errors.New("blob indisponible")
|
||||||
|
ErrBlobCorrupt = errors.New("blob corrompu")
|
||||||
|
ErrVersionNotFound = errors.New("version introuvable")
|
||||||
|
ErrVersionMismatch = errors.New("version associée à un autre document")
|
||||||
|
ErrVersionIsHead = errors.New("version tête de document")
|
||||||
|
ErrContentPurged = errors.New("contenu purgé")
|
||||||
|
ErrContentUnavailable = errors.New("contenu indisponible")
|
||||||
|
ErrInvalidRetention = errors.New("politique de rétention invalide")
|
||||||
|
)
|
||||||
+106
@@ -0,0 +1,106 @@
|
|||||||
|
package content
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"mediarr/database"
|
||||||
|
"mediarr/document"
|
||||||
|
"mediarr/ident"
|
||||||
|
"mediarr/permission"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s *Service) SetHeadVersion(ctx context.Context, actorID, documentID, versionID ident.UUID) (*Version, error) {
|
||||||
|
documentRow, err := s.getDocument(ctx, documentID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if documentRow.DeletedAt.Valid {
|
||||||
|
return nil, fmt.Errorf("%w: %s", document.ErrDocumentDeleted, documentID)
|
||||||
|
}
|
||||||
|
if err := s.can(ctx, actorID, documentID, permission.Write); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
version, err := s.getVersion(ctx, versionID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if version.DocumentID != documentID {
|
||||||
|
return nil, fmt.Errorf("%w: %s", ErrVersionMismatch, versionID)
|
||||||
|
}
|
||||||
|
if version.ContentPurgedAt.Valid {
|
||||||
|
return nil, fmt.Errorf("%w: %s", ErrContentPurged, versionID)
|
||||||
|
}
|
||||||
|
if documentRow.HeadVersionID != nil && *documentRow.HeadVersionID == versionID {
|
||||||
|
return versionFromDB(*version), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := s.db.WriteQueries.SetDocumentHeadVersion(ctx, database.SetDocumentHeadVersionParams{
|
||||||
|
HeadVersionID: &versionID,
|
||||||
|
UpdatedAt: nowMillis(),
|
||||||
|
ID: documentID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if rows == 0 {
|
||||||
|
return nil, fmt.Errorf("%w: %s", document.ErrDocumentDeleted, documentID)
|
||||||
|
}
|
||||||
|
|
||||||
|
return versionFromDB(*version), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) PurgeVersion(ctx context.Context, actorID, versionID ident.UUID) error {
|
||||||
|
version, err := s.getVersion(ctx, versionID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
documentRow, err := s.getDocument(ctx, version.DocumentID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if documentRow.DeletedAt.Valid {
|
||||||
|
return fmt.Errorf("%w: %s", document.ErrDocumentDeleted, documentRow.ID)
|
||||||
|
}
|
||||||
|
if err := s.can(ctx, actorID, version.DocumentID, permission.Delete); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if version.ContentPurgedAt.Valid {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if documentRow.HeadVersionID != nil && *documentRow.HeadVersionID == version.ID {
|
||||||
|
return fmt.Errorf("%w: %s", ErrVersionIsHead, version.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := s.db.WriteQueries.PurgeDocumentVersion(ctx, database.PurgeDocumentVersionParams{
|
||||||
|
PurgedAt: sql.NullInt64{Int64: nowMillis(), Valid: true},
|
||||||
|
ID: version.ID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if rows == 0 {
|
||||||
|
current, getErr := s.getVersion(ctx, version.ID)
|
||||||
|
if getErr == nil && current.ContentPurgedAt.Valid {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return fmt.Errorf("%w: %s", ErrVersionIsHead, version.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) purgeVersion(ctx context.Context, versionID ident.UUID) (bool, error) {
|
||||||
|
rows, err := s.db.WriteQueries.PurgeDocumentVersion(ctx, database.PurgeDocumentVersionParams{
|
||||||
|
PurgedAt: sql.NullInt64{Int64: nowMillis(), Valid: true},
|
||||||
|
ID: versionID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return rows > 0, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,145 @@
|
|||||||
|
package content
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/hex"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"mediarr/database"
|
||||||
|
"mediarr/document"
|
||||||
|
"mediarr/ident"
|
||||||
|
"mediarr/identity"
|
||||||
|
"mediarr/permission"
|
||||||
|
"mediarr/storage"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
testBackendID = ident.MustParse("00000000-0000-7000-8000-000000000201")
|
||||||
|
testPolicyID = ident.MustParse("00000000-0000-7000-8000-000000000101")
|
||||||
|
)
|
||||||
|
|
||||||
|
type testAuthorizer struct {
|
||||||
|
allowed permission.Permission
|
||||||
|
}
|
||||||
|
|
||||||
|
func allowAll() *testAuthorizer {
|
||||||
|
return &testAuthorizer{allowed: permission.All}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *testAuthorizer) IsActive(context.Context, ident.UUID) (bool, error) {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *testAuthorizer) Can(_ context.Context, _ ident.UUID, _ ident.UUID, required permission.Permission) (bool, error) {
|
||||||
|
return a.allowed.Has(required), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type testEnv struct {
|
||||||
|
service *Service
|
||||||
|
db *database.DatabaseClient
|
||||||
|
store *storage.Local
|
||||||
|
owner *identity.Principal
|
||||||
|
library *document.Document
|
||||||
|
libraryID ident.UUID
|
||||||
|
videoID ident.UUID
|
||||||
|
}
|
||||||
|
|
||||||
|
func newTestEnv(t *testing.T, authorizer document.Authorizer) *testEnv {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
t.Chdir(t.TempDir())
|
||||||
|
|
||||||
|
err, db := database.NewDatabase(database.NewDatabaseParams{})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewDatabase : %v", err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() {
|
||||||
|
if err := db.Close(); err != nil {
|
||||||
|
t.Errorf("Close : %v", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
store, err := storage.NewLocal(filepath.Join("store"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewLocal : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
now := time.Now().UnixMilli()
|
||||||
|
if err := db.WriteQueries.CreateStorageBackend(context.Background(), database.CreateStorageBackendParams{
|
||||||
|
ID: testBackendID,
|
||||||
|
Kind: "hub_fs",
|
||||||
|
Name: "local",
|
||||||
|
Config: `{"root":"store"}`,
|
||||||
|
Enabled: 1,
|
||||||
|
State: "ok",
|
||||||
|
CreatedAt: now,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("CreateStorageBackend : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
owner, err := identity.NewService(*db).CreatePrincipal(ctx, identity.CreatePrincipalParams{
|
||||||
|
Kind: identity.KindUser,
|
||||||
|
Name: "owner",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreatePrincipal : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
documents := document.NewService(*db, allowAll())
|
||||||
|
library, err := documents.CreateLibrary(ctx, owner.ID, document.CreateLibraryParams{
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
CreatedBy: owner.ID,
|
||||||
|
Name: "Bibliothèque",
|
||||||
|
PolicyID: testPolicyID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreateLibrary : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
videoID := createVideo(t, db, owner, library, "film.mkv")
|
||||||
|
|
||||||
|
return &testEnv{
|
||||||
|
service: NewService(*db, store, testBackendID, authorizer),
|
||||||
|
db: db,
|
||||||
|
store: store,
|
||||||
|
owner: owner,
|
||||||
|
library: library,
|
||||||
|
libraryID: library.ID,
|
||||||
|
videoID: videoID,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func createVideo(t *testing.T, db *database.DatabaseClient, owner *identity.Principal, parent *document.Document, name string) ident.UUID {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
id, err := ident.NewV7()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewV7 : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
now := time.Now().UnixMilli()
|
||||||
|
if _, err := db.WriteQueries.CreateDocument(context.Background(), database.CreateDocumentParams{
|
||||||
|
ID: id,
|
||||||
|
Kind: "video",
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
CreatedBy: owner.ID,
|
||||||
|
ParentID: &parent.ID,
|
||||||
|
Name: name,
|
||||||
|
NameKey: strings.ToLower(name),
|
||||||
|
Path: parent.Path + hex.EncodeToString(id[:]) + "/",
|
||||||
|
Depth: parent.Depth + 1,
|
||||||
|
Metadata: "{}",
|
||||||
|
AclInherit: 1,
|
||||||
|
Rev: 1,
|
||||||
|
CreatedAt: now,
|
||||||
|
UpdatedAt: now,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("CreateDocument : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return id
|
||||||
|
}
|
||||||
@@ -0,0 +1,175 @@
|
|||||||
|
package content
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"crypto/sha256"
|
||||||
|
"database/sql"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"mediarr/database"
|
||||||
|
"mediarr/document"
|
||||||
|
"mediarr/ident"
|
||||||
|
"mediarr/permission"
|
||||||
|
"mediarr/storage"
|
||||||
|
)
|
||||||
|
|
||||||
|
type GarbageCollectionResult struct {
|
||||||
|
Blobs int
|
||||||
|
Locations int
|
||||||
|
Bytes int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) CollectGarbage(ctx context.Context) (GarbageCollectionResult, error) {
|
||||||
|
if s.store == nil {
|
||||||
|
return GarbageCollectionResult{}, ErrStorageNotConfigured
|
||||||
|
}
|
||||||
|
|
||||||
|
var blobs []database.Blob
|
||||||
|
var locations []database.BlobLocation
|
||||||
|
var totalBytes int64
|
||||||
|
|
||||||
|
err := s.withTx(ctx, func(q *database.Queries) error {
|
||||||
|
var err error
|
||||||
|
blobs, err = q.ListUnreferencedBlobs(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, blob := range blobs {
|
||||||
|
totalBytes += blob.Size
|
||||||
|
|
||||||
|
blobLocations, err := q.ListBlobLocations(ctx, blob.Root)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
locations = append(locations, blobLocations...)
|
||||||
|
|
||||||
|
if err := q.DeleteBlob(ctx, blob.Root); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return GarbageCollectionResult{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
result := GarbageCollectionResult{
|
||||||
|
Blobs: len(blobs),
|
||||||
|
Bytes: totalBytes,
|
||||||
|
}
|
||||||
|
|
||||||
|
var removeErrors []error
|
||||||
|
for _, location := range locations {
|
||||||
|
if err := s.store.Remove(ctx, location.Locator); err != nil {
|
||||||
|
removeErrors = append(removeErrors, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
result.Locations++
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(removeErrors) > 0 {
|
||||||
|
return result, errors.Join(removeErrors...)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) VerifyVersion(ctx context.Context, actorID, versionID ident.UUID) error {
|
||||||
|
if s.store == nil {
|
||||||
|
return ErrStorageNotConfigured
|
||||||
|
}
|
||||||
|
|
||||||
|
version, err := s.getVersion(ctx, versionID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
documentRow, err := s.getDocument(ctx, version.DocumentID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if documentRow.DeletedAt.Valid {
|
||||||
|
return fmt.Errorf("%w: %s", document.ErrDocumentDeleted, documentRow.ID)
|
||||||
|
}
|
||||||
|
if err := s.can(ctx, actorID, version.DocumentID, permission.Read); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if version.ContentPurgedAt.Valid {
|
||||||
|
return fmt.Errorf("%w: %s", ErrContentPurged, versionID)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := s.db.Queries.GetBlob(ctx, version.ContentRoot); err != nil {
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return fmt.Errorf("%w: %s", ErrContentUnavailable, versionID)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
location, err := s.db.Queries.GetBlobLocation(ctx, database.GetBlobLocationParams{
|
||||||
|
Root: version.ContentRoot,
|
||||||
|
BackendID: s.backendID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return fmt.Errorf("%w: %s", ErrContentUnavailable, versionID)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if location.State != BlobLocationPresent {
|
||||||
|
return fmt.Errorf("%w: %s", ErrContentUnavailable, versionID)
|
||||||
|
}
|
||||||
|
|
||||||
|
reader, err := s.store.Open(ctx, location.Locator)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, storage.ErrNotFound) {
|
||||||
|
unavailable := fmt.Errorf("%w: %s", ErrContentUnavailable, versionID)
|
||||||
|
updateErr := s.db.WriteQueries.UpdateBlobLocationState(ctx, database.UpdateBlobLocationStateParams{
|
||||||
|
State: BlobLocationMissing,
|
||||||
|
Root: version.ContentRoot,
|
||||||
|
BackendID: s.backendID,
|
||||||
|
})
|
||||||
|
if updateErr != nil {
|
||||||
|
return errors.Join(unavailable, updateErr)
|
||||||
|
}
|
||||||
|
return unavailable
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
hasher := sha256.New()
|
||||||
|
size, readErr := io.Copy(hasher, reader)
|
||||||
|
closeErr := reader.Close()
|
||||||
|
if readErr != nil {
|
||||||
|
return readErr
|
||||||
|
}
|
||||||
|
if closeErr != nil {
|
||||||
|
return closeErr
|
||||||
|
}
|
||||||
|
|
||||||
|
now := nowMillis()
|
||||||
|
if size != version.Size || !bytes.Equal(hasher.Sum(nil), version.ContentRoot) {
|
||||||
|
corrupt := fmt.Errorf("%w: %s", ErrBlobCorrupt, versionID)
|
||||||
|
updateErr := s.db.WriteQueries.UpdateBlobVerification(ctx, database.UpdateBlobVerificationParams{
|
||||||
|
State: BlobStateCorrupt,
|
||||||
|
LastVerifiedAt: sql.NullInt64{Int64: now, Valid: true},
|
||||||
|
VerifyError: sql.NullString{String: "empreinte ou taille incohérente", Valid: true},
|
||||||
|
Root: version.ContentRoot,
|
||||||
|
})
|
||||||
|
if updateErr != nil {
|
||||||
|
return errors.Join(corrupt, updateErr)
|
||||||
|
}
|
||||||
|
return corrupt
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.db.WriteQueries.UpdateBlobVerification(ctx, database.UpdateBlobVerificationParams{
|
||||||
|
State: BlobStatePresent,
|
||||||
|
LastVerifiedAt: sql.NullInt64{Int64: now, Valid: true},
|
||||||
|
VerifyError: sql.NullString{},
|
||||||
|
Root: version.ContentRoot,
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,377 @@
|
|||||||
|
package content
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"mediarr/database"
|
||||||
|
"mediarr/document"
|
||||||
|
"mediarr/ident"
|
||||||
|
"mediarr/permission"
|
||||||
|
"mediarr/storage"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSetHeadVersionAndPurge(t *testing.T) {
|
||||||
|
env := newTestEnv(t, allowAll())
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
first := createTestVersion(t, env, env.videoID, "un")
|
||||||
|
second := createTestVersion(t, env, env.videoID, "deux")
|
||||||
|
|
||||||
|
if _, err := env.service.SetHeadVersion(ctx, env.owner.ID, env.videoID, first.ID); err != nil {
|
||||||
|
t.Fatalf("SetHeadVersion : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
documentRow, err := env.db.Queries.GetDocument(ctx, env.videoID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetDocument : %v", err)
|
||||||
|
}
|
||||||
|
if documentRow.HeadVersionID == nil || *documentRow.HeadVersionID != first.ID {
|
||||||
|
t.Fatalf("head_version_id = %v, attendu %s", documentRow.HeadVersionID, first.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
rev := documentRow.Rev
|
||||||
|
if _, err := env.service.SetHeadVersion(ctx, env.owner.ID, env.videoID, first.ID); err != nil {
|
||||||
|
t.Fatalf("SetHeadVersion(idempotent) : %v", err)
|
||||||
|
}
|
||||||
|
documentRow, err = env.db.Queries.GetDocument(ctx, env.videoID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetDocument : %v", err)
|
||||||
|
}
|
||||||
|
if documentRow.Rev != rev {
|
||||||
|
t.Fatalf("rev = %d, attendu %d", documentRow.Rev, rev)
|
||||||
|
}
|
||||||
|
|
||||||
|
otherVideoID := createVideo(t, env.db, env.owner, env.library, "autre.mkv")
|
||||||
|
otherVersion := createTestVersion(t, env, otherVideoID, "autre")
|
||||||
|
if _, err := env.service.SetHeadVersion(ctx, env.owner.ID, env.videoID, otherVersion.ID); !errors.Is(err, ErrVersionMismatch) {
|
||||||
|
t.Fatalf("SetHeadVersion(mauvais document) : erreur = %v, attendu ErrVersionMismatch", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := env.service.PurgeVersion(ctx, env.owner.ID, second.ID); err != nil {
|
||||||
|
t.Fatalf("PurgeVersion : %v", err)
|
||||||
|
}
|
||||||
|
if err := env.service.PurgeVersion(ctx, env.owner.ID, second.ID); err != nil {
|
||||||
|
t.Fatalf("PurgeVersion(idempotent) : %v", err)
|
||||||
|
}
|
||||||
|
if _, err := env.service.SetHeadVersion(ctx, env.owner.ID, env.videoID, second.ID); !errors.Is(err, ErrContentPurged) {
|
||||||
|
t.Fatalf("SetHeadVersion(purgée) : erreur = %v, attendu ErrContentPurged", err)
|
||||||
|
}
|
||||||
|
if err := env.service.PurgeVersion(ctx, env.owner.ID, first.ID); !errors.Is(err, ErrVersionIsHead) {
|
||||||
|
t.Fatalf("PurgeVersion(tête) : erreur = %v, attendu ErrVersionIsHead", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
readOnly := NewService(*env.db, env.store, testBackendID, &testAuthorizer{allowed: permission.Read})
|
||||||
|
if _, err := readOnly.SetHeadVersion(ctx, env.owner.ID, env.videoID, first.ID); !errors.Is(err, document.ErrPermissionDenied) {
|
||||||
|
t.Fatalf("SetHeadVersion sans Write : erreur = %v, attendu ErrPermissionDenied", err)
|
||||||
|
}
|
||||||
|
if err := readOnly.PurgeVersion(ctx, env.owner.ID, second.ID); !errors.Is(err, document.ErrPermissionDenied) {
|
||||||
|
t.Fatalf("PurgeVersion sans Delete : erreur = %v, attendu ErrPermissionDenied", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRetentionPurgeIDs(t *testing.T) {
|
||||||
|
now := time.Date(2026, 1, 31, 12, 0, 0, 0, time.UTC).UnixMilli()
|
||||||
|
at := func(days int) int64 {
|
||||||
|
return now - int64(days)*dayMillis
|
||||||
|
}
|
||||||
|
|
||||||
|
headID := ident.MustParse("00000000-0000-7000-8000-000000000001")
|
||||||
|
versions := []database.DocumentVersion{
|
||||||
|
retentionVersion(t, 1, at(1000), 0, false),
|
||||||
|
retentionVersion(t, 2, at(1000), 1, false),
|
||||||
|
retentionVersion(t, 3, at(10), 0, false),
|
||||||
|
retentionVersion(t, 4, at(40), 0, false),
|
||||||
|
retentionVersion(t, 5, at(40)-60*60*1000, 0, false),
|
||||||
|
retentionVersion(t, 6, at(41), 0, false),
|
||||||
|
retentionVersion(t, 7, at(100), 0, false),
|
||||||
|
retentionVersion(t, 8, at(110), 0, false),
|
||||||
|
retentionVersion(t, 9, at(200), 0, false),
|
||||||
|
retentionVersion(t, 10, at(300), 0, false),
|
||||||
|
retentionVersion(t, 11, at(400), 0, false),
|
||||||
|
retentionVersion(t, 12, at(10), 0, true),
|
||||||
|
}
|
||||||
|
sort.Slice(versions, func(i, j int) bool {
|
||||||
|
return versions[i].CreatedAt > versions[j].CreatedAt
|
||||||
|
})
|
||||||
|
|
||||||
|
purged := retentionPurgeIDs(versions, &headID, retentionPolicy{
|
||||||
|
KeepAllDays: 30,
|
||||||
|
KeepDailyDays: 60,
|
||||||
|
KeepMonthly: 2,
|
||||||
|
}, now)
|
||||||
|
|
||||||
|
expected := map[ident.UUID]bool{
|
||||||
|
ident.MustParse("00000000-0000-7000-8000-000000000005"): true,
|
||||||
|
ident.MustParse("00000000-0000-7000-8000-000000000008"): true,
|
||||||
|
ident.MustParse("00000000-0000-7000-8000-000000000010"): true,
|
||||||
|
ident.MustParse("00000000-0000-7000-8000-000000000011"): true,
|
||||||
|
}
|
||||||
|
if len(purged) != len(expected) {
|
||||||
|
t.Fatalf("versions purgées = %v, attendu %v", purged, expected)
|
||||||
|
}
|
||||||
|
for _, versionID := range purged {
|
||||||
|
if !expected[versionID] {
|
||||||
|
t.Fatalf("version purgée inattendue : %s", versionID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestApplyRetention(t *testing.T) {
|
||||||
|
env := newTestEnv(t, allowAll())
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
if _, err := env.db.WriteDB.ExecContext(ctx,
|
||||||
|
"UPDATE storage_policy SET version_retention = ? WHERE id = ?",
|
||||||
|
`{"keep_all_days":30,"keep_daily_days":0,"keep_monthly":0}`, testPolicyID,
|
||||||
|
); err != nil {
|
||||||
|
t.Fatalf("UPDATE storage_policy : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
first := createTestVersion(t, env, env.videoID, "un")
|
||||||
|
second := createTestVersion(t, env, env.videoID, "deux")
|
||||||
|
head := createTestVersion(t, env, env.videoID, "trois")
|
||||||
|
|
||||||
|
now := time.Now().UnixMilli()
|
||||||
|
setVersionCreatedAt(t, env, first.ID, now-100*dayMillis)
|
||||||
|
setVersionCreatedAt(t, env, second.ID, now-40*dayMillis)
|
||||||
|
setVersionCreatedAt(t, env, head.ID, now)
|
||||||
|
|
||||||
|
result, err := env.service.ApplyRetention(ctx, env.owner.ID, env.videoID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ApplyRetention : %v", err)
|
||||||
|
}
|
||||||
|
if len(result.Purged) != 2 || result.Kept != 1 {
|
||||||
|
t.Fatalf("résultat = %+v, attendu 2 purgées et 1 conservée", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
firstRow, _ := env.db.Queries.GetDocumentVersion(ctx, first.ID)
|
||||||
|
secondRow, _ := env.db.Queries.GetDocumentVersion(ctx, second.ID)
|
||||||
|
headRow, _ := env.db.Queries.GetDocumentVersion(ctx, head.ID)
|
||||||
|
if !firstRow.ContentPurgedAt.Valid || !secondRow.ContentPurgedAt.Valid {
|
||||||
|
t.Fatalf("versions anciennes non purgées : %+v %+v", firstRow, secondRow)
|
||||||
|
}
|
||||||
|
if headRow.ContentPurgedAt.Valid {
|
||||||
|
t.Fatalf("version tête purgée : %+v", headRow)
|
||||||
|
}
|
||||||
|
|
||||||
|
again, err := env.service.ApplyRetention(ctx, env.owner.ID, env.videoID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ApplyRetention(idempotent) : %v", err)
|
||||||
|
}
|
||||||
|
if len(again.Purged) != 0 || again.Kept != 3 {
|
||||||
|
t.Fatalf("second résultat = %+v, attendu 0 purgée et 3 conservées", again)
|
||||||
|
}
|
||||||
|
|
||||||
|
adminDenied := NewService(*env.db, env.store, testBackendID, &testAuthorizer{allowed: permission.Write})
|
||||||
|
if _, err := adminDenied.ApplyRetention(ctx, env.owner.ID, env.videoID); !errors.Is(err, document.ErrPermissionDenied) {
|
||||||
|
t.Fatalf("ApplyRetention sans Admin : erreur = %v, attendu ErrPermissionDenied", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := env.db.WriteDB.ExecContext(ctx,
|
||||||
|
"UPDATE storage_policy SET version_retention = ? WHERE id = ?",
|
||||||
|
`{"keep_all_days":-1}`, testPolicyID,
|
||||||
|
); err != nil {
|
||||||
|
t.Fatalf("UPDATE storage_policy(invalide) : %v", err)
|
||||||
|
}
|
||||||
|
if _, err := env.service.ApplyRetention(ctx, env.owner.ID, env.videoID); !errors.Is(err, ErrInvalidRetention) {
|
||||||
|
t.Fatalf("ApplyRetention(politique invalide) : erreur = %v, attendu ErrInvalidRetention", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCollectGarbage(t *testing.T) {
|
||||||
|
env := newTestEnv(t, allowAll())
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
purged := createTestVersion(t, env, env.videoID, "un")
|
||||||
|
kept := createTestVersion(t, env, env.videoID, "deux")
|
||||||
|
|
||||||
|
location, err := env.db.Queries.GetBlobLocation(ctx, database.GetBlobLocationParams{
|
||||||
|
Root: purged.ContentRoot,
|
||||||
|
BackendID: testBackendID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetBlobLocation : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := env.service.PurgeVersion(ctx, env.owner.ID, purged.ID); err != nil {
|
||||||
|
t.Fatalf("PurgeVersion : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := env.service.CollectGarbage(ctx)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CollectGarbage : %v", err)
|
||||||
|
}
|
||||||
|
if result.Blobs != 1 || result.Locations != 1 || result.Bytes != purged.Size {
|
||||||
|
t.Fatalf("résultat = %+v", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := env.db.Queries.GetBlob(ctx, purged.ContentRoot); !errors.Is(err, sql.ErrNoRows) {
|
||||||
|
t.Fatalf("blob purgé encore présent : %v", err)
|
||||||
|
}
|
||||||
|
if _, err := env.db.Queries.GetBlob(ctx, kept.ContentRoot); err != nil {
|
||||||
|
t.Fatalf("blob conservé : %v", err)
|
||||||
|
}
|
||||||
|
if _, err := env.store.Open(ctx, location.Locator); !errors.Is(err, storage.ErrNotFound) {
|
||||||
|
t.Fatalf("fichier purgé encore présent : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
withoutStore := NewService(*env.db, nil, testBackendID, allowAll())
|
||||||
|
if _, err := withoutStore.CollectGarbage(ctx); !errors.Is(err, ErrStorageNotConfigured) {
|
||||||
|
t.Fatalf("CollectGarbage sans stockage : erreur = %v, attendu ErrStorageNotConfigured", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestVerifyVersion(t *testing.T) {
|
||||||
|
env := newTestEnv(t, allowAll())
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
version := createTestVersion(t, env, env.videoID, "contenu vérifié")
|
||||||
|
if err := env.service.VerifyVersion(ctx, env.owner.ID, version.ID); err != nil {
|
||||||
|
t.Fatalf("VerifyVersion : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
blob, err := env.db.Queries.GetBlob(ctx, version.ContentRoot)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetBlob : %v", err)
|
||||||
|
}
|
||||||
|
if blob.State != BlobStatePresent || !blob.LastVerifiedAt.Valid {
|
||||||
|
t.Fatalf("blob = %+v", blob)
|
||||||
|
}
|
||||||
|
|
||||||
|
location, err := env.db.Queries.GetBlobLocation(ctx, database.GetBlobLocationParams{
|
||||||
|
Root: version.ContentRoot,
|
||||||
|
BackendID: testBackendID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetBlobLocation : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
path := filepath.Join("store", filepath.FromSlash(location.Locator))
|
||||||
|
if err := os.WriteFile(path, []byte("corrompu"), 0o640); err != nil {
|
||||||
|
t.Fatalf("WriteFile : %v", err)
|
||||||
|
}
|
||||||
|
if err := env.service.VerifyVersion(ctx, env.owner.ID, version.ID); !errors.Is(err, ErrBlobCorrupt) {
|
||||||
|
t.Fatalf("VerifyVersion(corrompu) : erreur = %v, attendu ErrBlobCorrupt", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
blob, err = env.db.Queries.GetBlob(ctx, version.ContentRoot)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetBlob(corrompu) : %v", err)
|
||||||
|
}
|
||||||
|
if blob.State != BlobStateCorrupt || !blob.VerifyError.Valid {
|
||||||
|
t.Fatalf("blob corrompu = %+v", blob)
|
||||||
|
}
|
||||||
|
if _, _, err := env.service.OpenVersion(ctx, env.owner.ID, version.ID); !errors.Is(err, ErrContentUnavailable) {
|
||||||
|
t.Fatalf("OpenVersion(corrompu) : erreur = %v, attendu ErrContentUnavailable", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
healed, err := env.service.CreateVersion(ctx, env.owner.ID, env.videoID, CreateVersionParams{
|
||||||
|
Source: strings.NewReader("contenu vérifié"),
|
||||||
|
Mime: "video/mp4",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreateVersion(réparation) : %v", err)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(healed.ContentRoot, version.ContentRoot) {
|
||||||
|
t.Fatalf("content_root réparé = %x, attendu %x", healed.ContentRoot, version.ContentRoot)
|
||||||
|
}
|
||||||
|
if err := env.service.VerifyVersion(ctx, env.owner.ID, healed.ID); err != nil {
|
||||||
|
t.Fatalf("VerifyVersion(réparé) : %v", err)
|
||||||
|
}
|
||||||
|
reader, _, err := env.service.OpenVersion(ctx, env.owner.ID, healed.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("OpenVersion(réparé) : %v", err)
|
||||||
|
}
|
||||||
|
if err := reader.Close(); err != nil {
|
||||||
|
t.Fatalf("Close(réparé) : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
readOnly := NewService(*env.db, env.store, testBackendID, &testAuthorizer{allowed: permission.Write})
|
||||||
|
if err := readOnly.VerifyVersion(ctx, env.owner.ID, version.ID); !errors.Is(err, document.ErrPermissionDenied) {
|
||||||
|
t.Fatalf("VerifyVersion sans Read : erreur = %v, attendu ErrPermissionDenied", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestVerifyVersionMissingFile(t *testing.T) {
|
||||||
|
env := newTestEnv(t, allowAll())
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
version := createTestVersion(t, env, env.videoID, "fichier manquant")
|
||||||
|
location, err := env.db.Queries.GetBlobLocation(ctx, database.GetBlobLocationParams{
|
||||||
|
Root: version.ContentRoot,
|
||||||
|
BackendID: testBackendID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetBlobLocation : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
path := filepath.Join("store", filepath.FromSlash(location.Locator))
|
||||||
|
if err := os.Remove(path); err != nil {
|
||||||
|
t.Fatalf("Remove : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := env.service.VerifyVersion(ctx, env.owner.ID, version.ID); !errors.Is(err, ErrContentUnavailable) {
|
||||||
|
t.Fatalf("VerifyVersion : erreur = %v, attendu ErrContentUnavailable", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
location, err = env.db.Queries.GetBlobLocation(ctx, database.GetBlobLocationParams{
|
||||||
|
Root: version.ContentRoot,
|
||||||
|
BackendID: testBackendID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetBlobLocation(après) : %v", err)
|
||||||
|
}
|
||||||
|
if location.State != BlobLocationMissing {
|
||||||
|
t.Fatalf("state = %q, attendu %q", location.State, BlobLocationMissing)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func createTestVersion(t *testing.T, env *testEnv, documentID ident.UUID, payload string) *Version {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
version, err := env.service.CreateVersion(context.Background(), env.owner.ID, documentID, CreateVersionParams{
|
||||||
|
Source: strings.NewReader(payload),
|
||||||
|
Mime: "video/mp4",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreateVersion : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return version
|
||||||
|
}
|
||||||
|
|
||||||
|
func setVersionCreatedAt(t *testing.T, env *testEnv, versionID ident.UUID, createdAt int64) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
if _, err := env.db.WriteDB.ExecContext(context.Background(),
|
||||||
|
"UPDATE document_version SET created_at = ? WHERE id = ?",
|
||||||
|
createdAt, versionID,
|
||||||
|
); err != nil {
|
||||||
|
t.Fatalf("UPDATE created_at : %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func retentionVersion(t *testing.T, suffix int, createdAt int64, pinned int64, purged bool) database.DocumentVersion {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
version := database.DocumentVersion{
|
||||||
|
ID: ident.MustParse(fmt.Sprintf("00000000-0000-7000-8000-%012d", suffix)),
|
||||||
|
CreatedAt: createdAt,
|
||||||
|
Pinned: pinned,
|
||||||
|
}
|
||||||
|
if purged {
|
||||||
|
version.ContentPurgedAt = sql.NullInt64{Int64: createdAt, Valid: true}
|
||||||
|
}
|
||||||
|
|
||||||
|
return version
|
||||||
|
}
|
||||||
@@ -0,0 +1,153 @@
|
|||||||
|
package content
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"sort"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"mediarr/database"
|
||||||
|
"mediarr/document"
|
||||||
|
"mediarr/ident"
|
||||||
|
"mediarr/permission"
|
||||||
|
)
|
||||||
|
|
||||||
|
const dayMillis int64 = 24 * 60 * 60 * 1000
|
||||||
|
|
||||||
|
type RetentionResult struct {
|
||||||
|
Purged []ident.UUID
|
||||||
|
Kept int
|
||||||
|
}
|
||||||
|
|
||||||
|
type retentionPolicy struct {
|
||||||
|
KeepAllDays int `json:"keep_all_days"`
|
||||||
|
KeepDailyDays int `json:"keep_daily_days"`
|
||||||
|
KeepMonthly int `json:"keep_monthly"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p retentionPolicy) validate() error {
|
||||||
|
if p.KeepAllDays < 0 || p.KeepDailyDays < 0 || p.KeepMonthly < 0 {
|
||||||
|
return ErrInvalidRetention
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) ApplyRetention(ctx context.Context, actorID, documentID ident.UUID) (RetentionResult, error) {
|
||||||
|
documentRow, err := s.getDocument(ctx, documentID)
|
||||||
|
if err != nil {
|
||||||
|
return RetentionResult{}, err
|
||||||
|
}
|
||||||
|
if documentRow.DeletedAt.Valid {
|
||||||
|
return RetentionResult{}, fmt.Errorf("%w: %s", document.ErrDocumentDeleted, documentID)
|
||||||
|
}
|
||||||
|
if err := s.can(ctx, actorID, documentID, permission.Admin); err != nil {
|
||||||
|
return RetentionResult{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
policyBytes, err := s.db.Queries.GetEffectiveDocumentPolicy(ctx, documentID)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return RetentionResult{}, fmt.Errorf("%w: %s", ErrNoStoragePolicy, documentID)
|
||||||
|
}
|
||||||
|
return RetentionResult{}, err
|
||||||
|
}
|
||||||
|
policyID, err := ident.FromBytes(policyBytes)
|
||||||
|
if err != nil {
|
||||||
|
return RetentionResult{}, fmt.Errorf("%w: %v", ErrNoStoragePolicy, err)
|
||||||
|
}
|
||||||
|
storagePolicy, err := s.db.Queries.GetStoragePolicy(ctx, policyID)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return RetentionResult{}, fmt.Errorf("%w: %s", ErrNoStoragePolicy, policyID)
|
||||||
|
}
|
||||||
|
return RetentionResult{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var retention retentionPolicy
|
||||||
|
if err := json.Unmarshal([]byte(storagePolicy.VersionRetention), &retention); err != nil {
|
||||||
|
return RetentionResult{}, fmt.Errorf("%w: %v", ErrInvalidRetention, err)
|
||||||
|
}
|
||||||
|
if err := retention.validate(); err != nil {
|
||||||
|
return RetentionResult{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
versions, err := s.db.Queries.ListDocumentVersions(ctx, documentID)
|
||||||
|
if err != nil {
|
||||||
|
return RetentionResult{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
purgeIDs := retentionPurgeIDs(versions, documentRow.HeadVersionID, retention, nowMillis())
|
||||||
|
result := RetentionResult{
|
||||||
|
Kept: len(versions) - len(purgeIDs),
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, versionID := range purgeIDs {
|
||||||
|
purged, err := s.purgeVersion(ctx, versionID)
|
||||||
|
if err != nil {
|
||||||
|
return result, err
|
||||||
|
}
|
||||||
|
if purged {
|
||||||
|
result.Purged = append(result.Purged, versionID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func retentionPurgeIDs(versions []database.DocumentVersion, headID *ident.UUID, policy retentionPolicy, now int64) []ident.UUID {
|
||||||
|
ordered := append([]database.DocumentVersion(nil), versions...)
|
||||||
|
sort.Slice(ordered, func(i, j int) bool {
|
||||||
|
if ordered[i].CreatedAt == ordered[j].CreatedAt {
|
||||||
|
return ordered[i].Seq > ordered[j].Seq
|
||||||
|
}
|
||||||
|
return ordered[i].CreatedAt > ordered[j].CreatedAt
|
||||||
|
})
|
||||||
|
|
||||||
|
keep := make(map[ident.UUID]bool, len(ordered))
|
||||||
|
dailyKept := make(map[string]bool)
|
||||||
|
monthlyKept := make(map[string]bool)
|
||||||
|
|
||||||
|
for _, version := range ordered {
|
||||||
|
if version.ContentPurgedAt.Valid || version.Pinned != 0 || (headID != nil && *headID == version.ID) {
|
||||||
|
keep[version.ID] = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if version.CreatedAt > now {
|
||||||
|
keep[version.ID] = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
age := now - version.CreatedAt
|
||||||
|
ageDays := age / dayMillis
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case policy.KeepAllDays > 0 && ageDays <= int64(policy.KeepAllDays):
|
||||||
|
keep[version.ID] = true
|
||||||
|
case policy.KeepDailyDays > 0 && ageDays <= int64(policy.KeepDailyDays):
|
||||||
|
day := time.UnixMilli(version.CreatedAt).UTC().Format("2006-01-02")
|
||||||
|
if !dailyKept[day] {
|
||||||
|
dailyKept[day] = true
|
||||||
|
keep[version.ID] = true
|
||||||
|
}
|
||||||
|
case policy.KeepMonthly > 0:
|
||||||
|
month := time.UnixMilli(version.CreatedAt).UTC().Format("2006-01")
|
||||||
|
if !monthlyKept[month] && len(monthlyKept) < policy.KeepMonthly {
|
||||||
|
monthlyKept[month] = true
|
||||||
|
keep[version.ID] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
purge := make([]ident.UUID, 0, len(ordered)-len(keep))
|
||||||
|
for _, version := range ordered {
|
||||||
|
if !keep[version.ID] {
|
||||||
|
purge = append(purge, version.ID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return purge
|
||||||
|
}
|
||||||
@@ -0,0 +1,145 @@
|
|||||||
|
package content
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"mediarr/database"
|
||||||
|
"mediarr/document"
|
||||||
|
"mediarr/ident"
|
||||||
|
"mediarr/permission"
|
||||||
|
"mediarr/storage"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Service struct {
|
||||||
|
db database.DatabaseClient
|
||||||
|
store storage.Store
|
||||||
|
backendID ident.UUID
|
||||||
|
authorizer document.Authorizer
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewService(db database.DatabaseClient, store storage.Store, backendID ident.UUID, authorizer document.Authorizer) *Service {
|
||||||
|
return &Service{
|
||||||
|
db: db,
|
||||||
|
store: store,
|
||||||
|
backendID: backendID,
|
||||||
|
authorizer: authorizer,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) withTx(ctx context.Context, fn func(*database.Queries) error) error {
|
||||||
|
tx, err := s.db.WriteDB.BeginTx(ctx, nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
_ = tx.Rollback()
|
||||||
|
}()
|
||||||
|
|
||||||
|
if err := fn(s.db.WriteQueries.WithTx(tx)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return tx.Commit()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) getDocument(ctx context.Context, id ident.UUID) (*database.Document, error) {
|
||||||
|
row, err := s.db.Queries.GetDocument(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return nil, fmt.Errorf("%w: %s", document.ErrDocumentNotFound, id)
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &row, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) getVersion(ctx context.Context, id ident.UUID) (*database.DocumentVersion, error) {
|
||||||
|
row, err := s.db.Queries.GetDocumentVersion(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return nil, fmt.Errorf("%w: %s", ErrVersionNotFound, id)
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &row, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) can(ctx context.Context, principalID, documentID ident.UUID, required permission.Permission) error {
|
||||||
|
allowed, err := s.authorizer.Can(ctx, principalID, documentID, required)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !allowed {
|
||||||
|
return fmt.Errorf("%w: %s sur %s", document.ErrPermissionDenied, principalID, documentID)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) setStagingState(ctx context.Context, id ident.UUID, state string) error {
|
||||||
|
return s.db.WriteQueries.UpdateStagingItemState(ctx, database.UpdateStagingItemStateParams{
|
||||||
|
State: state,
|
||||||
|
UpdatedAt: nowMillis(),
|
||||||
|
ID: id,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func versionFromDB(version database.DocumentVersion) *Version {
|
||||||
|
var mime string
|
||||||
|
if version.Mime.Valid {
|
||||||
|
mime = version.Mime.String
|
||||||
|
}
|
||||||
|
|
||||||
|
var message string
|
||||||
|
if version.Message.Valid {
|
||||||
|
message = version.Message.String
|
||||||
|
}
|
||||||
|
|
||||||
|
var purgedAt *int64
|
||||||
|
if version.ContentPurgedAt.Valid {
|
||||||
|
value := version.ContentPurgedAt.Int64
|
||||||
|
purgedAt = &value
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Version{
|
||||||
|
ID: version.ID,
|
||||||
|
DocumentID: version.DocumentID,
|
||||||
|
Seq: version.Seq,
|
||||||
|
ContentRoot: version.ContentRoot,
|
||||||
|
Size: version.Size,
|
||||||
|
Mime: mime,
|
||||||
|
AuthorID: version.AuthorID,
|
||||||
|
DeviceID: version.DeviceID,
|
||||||
|
Origin: version.Origin,
|
||||||
|
Message: message,
|
||||||
|
Pinned: version.Pinned != 0,
|
||||||
|
ContentPurgedAt: purgedAt,
|
||||||
|
CreatedAt: version.CreatedAt,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func validOrigin(origin string) bool {
|
||||||
|
switch origin {
|
||||||
|
case OriginUpload, OriginImport, OriginMerge, OriginRestore, OriginSystem:
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func stagingOrigin(origin string) string {
|
||||||
|
if origin == OriginImport {
|
||||||
|
return OriginImport
|
||||||
|
}
|
||||||
|
return OriginUpload
|
||||||
|
}
|
||||||
|
|
||||||
|
func nowMillis() int64 {
|
||||||
|
return time.Now().UnixMilli()
|
||||||
|
}
|
||||||
@@ -0,0 +1,434 @@
|
|||||||
|
package content
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"mediarr/database"
|
||||||
|
"mediarr/document"
|
||||||
|
"mediarr/ident"
|
||||||
|
"mediarr/permission"
|
||||||
|
"mediarr/storage"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s *Service) CreateVersion(ctx context.Context, actorID, documentID ident.UUID, params CreateVersionParams) (*Version, error) {
|
||||||
|
if s.store == nil {
|
||||||
|
return nil, ErrStorageNotConfigured
|
||||||
|
}
|
||||||
|
if params.Source == nil {
|
||||||
|
return nil, ErrInvalidSource
|
||||||
|
}
|
||||||
|
|
||||||
|
origin := params.Origin
|
||||||
|
if origin == "" {
|
||||||
|
origin = OriginUpload
|
||||||
|
}
|
||||||
|
if !validOrigin(origin) {
|
||||||
|
return nil, fmt.Errorf("%w: %q", ErrInvalidOrigin, origin)
|
||||||
|
}
|
||||||
|
|
||||||
|
documentRow, err := s.getDocument(ctx, documentID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if documentRow.DeletedAt.Valid {
|
||||||
|
return nil, fmt.Errorf("%w: %s", document.ErrDocumentDeleted, documentID)
|
||||||
|
}
|
||||||
|
|
||||||
|
kind, err := s.db.Queries.GetDocumentKind(ctx, documentRow.Kind)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return nil, fmt.Errorf("%w: %s", ErrNoContentKind, documentRow.Kind)
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if kind.HasContent == 0 {
|
||||||
|
return nil, fmt.Errorf("%w: %s", ErrNoContentKind, documentRow.Kind)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.can(ctx, actorID, documentID, permission.Write); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
policyBytes, err := s.db.Queries.GetEffectiveDocumentPolicy(ctx, documentID)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return nil, fmt.Errorf("%w: %s", ErrNoStoragePolicy, documentID)
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
policyID, err := ident.FromBytes(policyBytes)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("%w: %v", ErrNoStoragePolicy, err)
|
||||||
|
}
|
||||||
|
if _, err := s.db.Queries.GetStoragePolicy(ctx, policyID); err != nil {
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return nil, fmt.Errorf("%w: %s", ErrNoStoragePolicy, policyID)
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if _, err := s.db.Queries.GetStorageBackend(ctx, s.backendID); err != nil {
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return nil, fmt.Errorf("%w: %s", ErrStorageBackendMissing, s.backendID)
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
stagingID, err := ident.NewV7()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
staged, err := s.store.Stage(ctx, stagingID, params.Source)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
now := nowMillis()
|
||||||
|
jobRef := sql.NullString{String: params.JobRef, Valid: params.JobRef != ""}
|
||||||
|
|
||||||
|
if err := s.db.WriteQueries.CreateStagingItem(ctx, database.CreateStagingItemParams{
|
||||||
|
ID: stagingID,
|
||||||
|
Origin: stagingOrigin(origin),
|
||||||
|
JobRef: jobRef,
|
||||||
|
Locator: staged.Locator,
|
||||||
|
Size: sql.NullInt64{Int64: staged.Size, Valid: true},
|
||||||
|
State: StagingStateStaged,
|
||||||
|
CreatedAt: now,
|
||||||
|
UpdatedAt: now,
|
||||||
|
}); err != nil {
|
||||||
|
_ = s.store.Remove(ctx, staged.Locator)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
blob, err := s.db.Queries.GetBlob(ctx, staged.Root)
|
||||||
|
newBlob := false
|
||||||
|
newLocation := false
|
||||||
|
healBlob := false
|
||||||
|
blobLocator := ""
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case err == nil:
|
||||||
|
if blob.Size != staged.Size {
|
||||||
|
_ = s.store.Remove(ctx, staged.Locator)
|
||||||
|
_ = s.setStagingState(ctx, stagingID, StagingStateDiscarded)
|
||||||
|
return nil, fmt.Errorf("%w: taille incohérente", ErrBlobUnavailable)
|
||||||
|
}
|
||||||
|
|
||||||
|
if blob.State != BlobStatePresent {
|
||||||
|
_, locationErr := s.db.Queries.GetBlobLocation(ctx, database.GetBlobLocationParams{
|
||||||
|
Root: staged.Root,
|
||||||
|
BackendID: s.backendID,
|
||||||
|
})
|
||||||
|
switch {
|
||||||
|
case locationErr == nil:
|
||||||
|
blobLocator, err = s.store.Replace(ctx, staged)
|
||||||
|
if err != nil {
|
||||||
|
_ = s.store.Remove(ctx, staged.Locator)
|
||||||
|
_ = s.setStagingState(ctx, stagingID, StagingStateDiscarded)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
case errors.Is(locationErr, sql.ErrNoRows):
|
||||||
|
blobLocator, err = s.store.Replace(ctx, staged)
|
||||||
|
if err != nil {
|
||||||
|
_ = s.store.Remove(ctx, staged.Locator)
|
||||||
|
_ = s.setStagingState(ctx, stagingID, StagingStateDiscarded)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
newLocation = true
|
||||||
|
default:
|
||||||
|
_ = s.store.Remove(ctx, staged.Locator)
|
||||||
|
_ = s.setStagingState(ctx, stagingID, StagingStateDiscarded)
|
||||||
|
return nil, locationErr
|
||||||
|
}
|
||||||
|
healBlob = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
location, locationErr := s.db.Queries.GetBlobLocation(ctx, database.GetBlobLocationParams{
|
||||||
|
Root: staged.Root,
|
||||||
|
BackendID: s.backendID,
|
||||||
|
})
|
||||||
|
switch {
|
||||||
|
case locationErr == nil:
|
||||||
|
if location.State != BlobLocationPresent {
|
||||||
|
_ = s.store.Remove(ctx, staged.Locator)
|
||||||
|
_ = s.setStagingState(ctx, stagingID, StagingStateDiscarded)
|
||||||
|
return nil, fmt.Errorf("%w: %x", ErrBlobUnavailable, staged.Root)
|
||||||
|
}
|
||||||
|
if err := s.store.Remove(ctx, staged.Locator); err != nil {
|
||||||
|
_ = s.setStagingState(ctx, stagingID, StagingStateDiscarded)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
case errors.Is(locationErr, sql.ErrNoRows):
|
||||||
|
blobLocator, err = s.store.Commit(ctx, staged)
|
||||||
|
if err != nil {
|
||||||
|
_ = s.store.Remove(ctx, staged.Locator)
|
||||||
|
_ = s.setStagingState(ctx, stagingID, StagingStateDiscarded)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
newLocation = true
|
||||||
|
default:
|
||||||
|
_ = s.store.Remove(ctx, staged.Locator)
|
||||||
|
_ = s.setStagingState(ctx, stagingID, StagingStateDiscarded)
|
||||||
|
return nil, locationErr
|
||||||
|
}
|
||||||
|
case errors.Is(err, sql.ErrNoRows):
|
||||||
|
blobLocator, err = s.store.Commit(ctx, staged)
|
||||||
|
if err != nil {
|
||||||
|
_ = s.store.Remove(ctx, staged.Locator)
|
||||||
|
_ = s.setStagingState(ctx, stagingID, StagingStateDiscarded)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
newBlob = true
|
||||||
|
newLocation = true
|
||||||
|
default:
|
||||||
|
_ = s.store.Remove(ctx, staged.Locator)
|
||||||
|
_ = s.setStagingState(ctx, stagingID, StagingStateDiscarded)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var created database.DocumentVersion
|
||||||
|
|
||||||
|
err = s.withTx(ctx, func(q *database.Queries) error {
|
||||||
|
if newBlob {
|
||||||
|
if err := q.CreateBlob(ctx, database.CreateBlobParams{
|
||||||
|
Root: staged.Root,
|
||||||
|
Size: staged.Size,
|
||||||
|
State: BlobStatePresent,
|
||||||
|
Role: BlobRoleAuthoritative,
|
||||||
|
UnitSize: blobUnitSize,
|
||||||
|
TreeState: BlobTreeStateReady,
|
||||||
|
Encryption: BlobEncryptionNone,
|
||||||
|
CreatedAt: now,
|
||||||
|
}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if newLocation {
|
||||||
|
if err := q.CreateBlobLocation(ctx, database.CreateBlobLocationParams{
|
||||||
|
Root: staged.Root,
|
||||||
|
BackendID: s.backendID,
|
||||||
|
Locator: blobLocator,
|
||||||
|
State: BlobLocationPresent,
|
||||||
|
CreatedAt: now,
|
||||||
|
}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if healBlob {
|
||||||
|
if err := q.UpdateBlobVerification(ctx, database.UpdateBlobVerificationParams{
|
||||||
|
State: BlobStatePresent,
|
||||||
|
LastVerifiedAt: sql.NullInt64{Int64: now, Valid: true},
|
||||||
|
VerifyError: sql.NullString{},
|
||||||
|
Root: staged.Root,
|
||||||
|
}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !newLocation {
|
||||||
|
if err := q.UpdateBlobLocationState(ctx, database.UpdateBlobLocationStateParams{
|
||||||
|
State: BlobLocationPresent,
|
||||||
|
Root: staged.Root,
|
||||||
|
BackendID: s.backendID,
|
||||||
|
}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !newBlob {
|
||||||
|
existing, err := q.GetBlob(ctx, staged.Root)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("%w: %x", ErrBlobUnavailable, staged.Root)
|
||||||
|
}
|
||||||
|
if existing.State != BlobStatePresent || existing.Size != staged.Size {
|
||||||
|
return fmt.Errorf("%w: %x", ErrBlobUnavailable, staged.Root)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !newLocation {
|
||||||
|
existingLocation, err := q.GetBlobLocation(ctx, database.GetBlobLocationParams{
|
||||||
|
Root: staged.Root,
|
||||||
|
BackendID: s.backendID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("%w: %x", ErrBlobUnavailable, staged.Root)
|
||||||
|
}
|
||||||
|
if existingLocation.State != BlobLocationPresent {
|
||||||
|
return fmt.Errorf("%w: %x", ErrBlobUnavailable, staged.Root)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
seq, err := q.NextDocumentVersionSeq(ctx, documentID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
versionID, err := ident.NewV7()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
created, err = q.CreateDocumentVersion(ctx, database.CreateDocumentVersionParams{
|
||||||
|
ID: versionID,
|
||||||
|
DocumentID: documentID,
|
||||||
|
Seq: seq,
|
||||||
|
ContentRoot: staged.Root,
|
||||||
|
Size: staged.Size,
|
||||||
|
Mime: sql.NullString{String: params.Mime, Valid: params.Mime != ""},
|
||||||
|
AuthorID: actorID,
|
||||||
|
DeviceID: params.DeviceID,
|
||||||
|
Origin: origin,
|
||||||
|
Message: sql.NullString{String: params.Message, Valid: params.Message != ""},
|
||||||
|
Pinned: boolToInt(params.Pinned),
|
||||||
|
CreatedAt: now,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := q.SetDocumentHeadVersion(ctx, database.SetDocumentHeadVersionParams{
|
||||||
|
HeadVersionID: &versionID,
|
||||||
|
UpdatedAt: now,
|
||||||
|
ID: documentID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if rows == 0 {
|
||||||
|
return fmt.Errorf("%w: %s", document.ErrDocumentDeleted, documentID)
|
||||||
|
}
|
||||||
|
|
||||||
|
return q.UpdateStagingItemState(ctx, database.UpdateStagingItemStateParams{
|
||||||
|
State: StagingStateCommitted,
|
||||||
|
UpdatedAt: now,
|
||||||
|
ID: stagingID,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
_ = s.setStagingState(ctx, stagingID, StagingStateDiscarded)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return versionFromDB(created), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) OpenVersion(ctx context.Context, actorID, versionID ident.UUID) (io.ReadCloser, *Version, error) {
|
||||||
|
if s.store == nil {
|
||||||
|
return nil, nil, ErrStorageNotConfigured
|
||||||
|
}
|
||||||
|
|
||||||
|
version, err := s.getVersion(ctx, versionID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
documentRow, err := s.getDocument(ctx, version.DocumentID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
if documentRow.DeletedAt.Valid {
|
||||||
|
return nil, nil, fmt.Errorf("%w: %s", document.ErrDocumentDeleted, documentRow.ID)
|
||||||
|
}
|
||||||
|
if err := s.can(ctx, actorID, version.DocumentID, permission.Read); err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
if version.ContentPurgedAt.Valid {
|
||||||
|
return nil, nil, fmt.Errorf("%w: %s", ErrContentPurged, versionID)
|
||||||
|
}
|
||||||
|
|
||||||
|
blob, err := s.db.Queries.GetBlob(ctx, version.ContentRoot)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return nil, nil, fmt.Errorf("%w: %s", ErrContentUnavailable, versionID)
|
||||||
|
}
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
if blob.State != BlobStatePresent {
|
||||||
|
return nil, nil, fmt.Errorf("%w: %s", ErrContentUnavailable, versionID)
|
||||||
|
}
|
||||||
|
|
||||||
|
location, err := s.db.Queries.GetBlobLocation(ctx, database.GetBlobLocationParams{
|
||||||
|
Root: version.ContentRoot,
|
||||||
|
BackendID: s.backendID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return nil, nil, fmt.Errorf("%w: %s", ErrContentUnavailable, versionID)
|
||||||
|
}
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
if location.State != BlobLocationPresent {
|
||||||
|
return nil, nil, fmt.Errorf("%w: %s", ErrContentUnavailable, versionID)
|
||||||
|
}
|
||||||
|
|
||||||
|
reader, err := s.store.Open(ctx, location.Locator)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, storage.ErrNotFound) {
|
||||||
|
return nil, nil, fmt.Errorf("%w: %s", ErrContentUnavailable, versionID)
|
||||||
|
}
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return reader, versionFromDB(*version), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) ListVersions(ctx context.Context, actorID, documentID ident.UUID) ([]Version, error) {
|
||||||
|
documentRow, err := s.getDocument(ctx, documentID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if documentRow.DeletedAt.Valid {
|
||||||
|
return nil, fmt.Errorf("%w: %s", document.ErrDocumentDeleted, documentID)
|
||||||
|
}
|
||||||
|
if err := s.can(ctx, actorID, documentID, permission.Read); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := s.db.Queries.ListDocumentVersions(ctx, documentID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
versions := make([]Version, 0, len(rows))
|
||||||
|
for _, row := range rows {
|
||||||
|
versions = append(versions, *versionFromDB(row))
|
||||||
|
}
|
||||||
|
|
||||||
|
return versions, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) HeadVersion(ctx context.Context, actorID, documentID ident.UUID) (*Version, error) {
|
||||||
|
documentRow, err := s.getDocument(ctx, documentID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if documentRow.DeletedAt.Valid {
|
||||||
|
return nil, fmt.Errorf("%w: %s", document.ErrDocumentDeleted, documentID)
|
||||||
|
}
|
||||||
|
if err := s.can(ctx, actorID, documentID, permission.Read); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if documentRow.HeadVersionID == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
version, err := s.getVersion(ctx, *documentRow.HeadVersionID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if version.DocumentID != documentID {
|
||||||
|
return nil, fmt.Errorf("%w: %s", ErrVersionMismatch, version.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
return versionFromDB(*version), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func boolToInt(value bool) int64 {
|
||||||
|
if value {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
@@ -0,0 +1,244 @@
|
|||||||
|
package content
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"mediarr/database"
|
||||||
|
"mediarr/document"
|
||||||
|
"mediarr/ident"
|
||||||
|
"mediarr/permission"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCreateVersionStoresAndDeduplicates(t *testing.T) {
|
||||||
|
env := newTestEnv(t, allowAll())
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
payload := []byte("contenu de test")
|
||||||
|
first, err := env.service.CreateVersion(ctx, env.owner.ID, env.videoID, CreateVersionParams{
|
||||||
|
Source: bytes.NewReader(payload),
|
||||||
|
Mime: "video/mp4",
|
||||||
|
Message: "première",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreateVersion : %v", err)
|
||||||
|
}
|
||||||
|
if first.Seq != 1 || first.Size != int64(len(payload)) {
|
||||||
|
t.Fatalf("version = seq %d taille %d", first.Seq, first.Size)
|
||||||
|
}
|
||||||
|
|
||||||
|
second, err := env.service.CreateVersion(ctx, env.owner.ID, env.videoID, CreateVersionParams{
|
||||||
|
Source: bytes.NewReader(payload),
|
||||||
|
Origin: OriginMerge,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreateVersion(doublon) : %v", err)
|
||||||
|
}
|
||||||
|
if second.Seq != 2 {
|
||||||
|
t.Fatalf("seq = %d, attendu 2", second.Seq)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(second.ContentRoot, first.ContentRoot) {
|
||||||
|
t.Fatalf("content_root = %x, attendu %x", second.ContentRoot, first.ContentRoot)
|
||||||
|
}
|
||||||
|
if second.Origin != OriginMerge {
|
||||||
|
t.Fatalf("origin = %q, attendu %q", second.Origin, OriginMerge)
|
||||||
|
}
|
||||||
|
|
||||||
|
reader, opened, err := env.service.OpenVersion(ctx, env.owner.ID, second.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("OpenVersion : %v", err)
|
||||||
|
}
|
||||||
|
data, err := io.ReadAll(reader)
|
||||||
|
if err != nil {
|
||||||
|
_ = reader.Close()
|
||||||
|
t.Fatalf("ReadAll : %v", err)
|
||||||
|
}
|
||||||
|
if err := reader.Close(); err != nil {
|
||||||
|
t.Fatalf("Close : %v", err)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(data, payload) {
|
||||||
|
t.Fatalf("contenu = %q, attendu %q", data, payload)
|
||||||
|
}
|
||||||
|
if opened.ID != second.ID {
|
||||||
|
t.Fatalf("version = %s, attendu %s", opened.ID, second.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
versions, err := env.service.ListVersions(ctx, env.owner.ID, env.videoID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ListVersions : %v", err)
|
||||||
|
}
|
||||||
|
if len(versions) != 2 || versions[0].ID != second.ID || versions[1].ID != first.ID {
|
||||||
|
t.Fatalf("versions = %+v", versions)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateVersionRejectsContainerKind(t *testing.T) {
|
||||||
|
env := newTestEnv(t, allowAll())
|
||||||
|
|
||||||
|
_, err := env.service.CreateVersion(context.Background(), env.owner.ID, env.libraryID, CreateVersionParams{
|
||||||
|
Source: bytes.NewReader([]byte("x")),
|
||||||
|
})
|
||||||
|
if !errors.Is(err, ErrNoContentKind) {
|
||||||
|
t.Fatalf("erreur = %v, attendu ErrNoContentKind", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateVersionRequiresWrite(t *testing.T) {
|
||||||
|
env := newTestEnv(t, &testAuthorizer{allowed: permission.Read})
|
||||||
|
|
||||||
|
_, err := env.service.CreateVersion(context.Background(), env.owner.ID, env.videoID, CreateVersionParams{
|
||||||
|
Source: bytes.NewReader([]byte("x")),
|
||||||
|
})
|
||||||
|
if !errors.Is(err, document.ErrPermissionDenied) {
|
||||||
|
t.Fatalf("erreur = %v, attendu ErrPermissionDenied", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOpenVersionRequiresRead(t *testing.T) {
|
||||||
|
env := newTestEnv(t, &testAuthorizer{allowed: permission.Write})
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
version, err := env.service.CreateVersion(ctx, env.owner.ID, env.videoID, CreateVersionParams{
|
||||||
|
Source: bytes.NewReader([]byte("x")),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreateVersion : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, _, err := env.service.OpenVersion(ctx, env.owner.ID, version.ID); !errors.Is(err, document.ErrPermissionDenied) {
|
||||||
|
t.Fatalf("erreur = %v, attendu ErrPermissionDenied", err)
|
||||||
|
}
|
||||||
|
if _, err := env.service.ListVersions(ctx, env.owner.ID, env.videoID); !errors.Is(err, document.ErrPermissionDenied) {
|
||||||
|
t.Fatalf("ListVersions : erreur = %v, attendu ErrPermissionDenied", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOpenVersionPurged(t *testing.T) {
|
||||||
|
env := newTestEnv(t, allowAll())
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
version, err := env.service.CreateVersion(ctx, env.owner.ID, env.videoID, CreateVersionParams{
|
||||||
|
Source: bytes.NewReader([]byte("x")),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreateVersion : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := env.db.WriteDB.ExecContext(ctx,
|
||||||
|
"UPDATE document_version SET content_purged_at = ? WHERE id = ?",
|
||||||
|
time.Now().UnixMilli(), version.ID,
|
||||||
|
); err != nil {
|
||||||
|
t.Fatalf("UPDATE content_purged_at : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, _, err := env.service.OpenVersion(ctx, env.owner.ID, version.ID); !errors.Is(err, ErrContentPurged) {
|
||||||
|
t.Fatalf("erreur = %v, attendu ErrContentPurged", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOpenVersionMissingBlob(t *testing.T) {
|
||||||
|
env := newTestEnv(t, allowAll())
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
version, err := env.service.CreateVersion(ctx, env.owner.ID, env.videoID, CreateVersionParams{
|
||||||
|
Source: bytes.NewReader([]byte("x")),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreateVersion : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
location, err := env.db.Queries.GetBlobLocation(ctx, database.GetBlobLocationParams{
|
||||||
|
Root: version.ContentRoot,
|
||||||
|
BackendID: testBackendID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetBlobLocation : %v", err)
|
||||||
|
}
|
||||||
|
if err := env.store.Remove(ctx, location.Locator); err != nil {
|
||||||
|
t.Fatalf("Remove : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, _, err := env.service.OpenVersion(ctx, env.owner.ID, version.ID); !errors.Is(err, ErrContentUnavailable) {
|
||||||
|
t.Fatalf("erreur = %v, attendu ErrContentUnavailable", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateVersionConfigurationErrors(t *testing.T) {
|
||||||
|
env := newTestEnv(t, allowAll())
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
if _, err := env.service.CreateVersion(ctx, env.owner.ID, env.videoID, CreateVersionParams{}); !errors.Is(err, ErrInvalidSource) {
|
||||||
|
t.Fatalf("sans source : erreur = %v, attendu ErrInvalidSource", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := env.service.CreateVersion(ctx, env.owner.ID, env.videoID, CreateVersionParams{
|
||||||
|
Source: bytes.NewReader([]byte("x")),
|
||||||
|
Origin: "inconnu",
|
||||||
|
}); !errors.Is(err, ErrInvalidOrigin) {
|
||||||
|
t.Fatalf("origine invalide : erreur = %v, attendu ErrInvalidOrigin", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
missingBackend := NewService(*env.db, env.store, ident.MustParse("00000000-0000-7000-8000-000000000202"), allowAll())
|
||||||
|
if _, err := missingBackend.CreateVersion(ctx, env.owner.ID, env.videoID, CreateVersionParams{
|
||||||
|
Source: bytes.NewReader([]byte("x")),
|
||||||
|
}); !errors.Is(err, ErrStorageBackendMissing) {
|
||||||
|
t.Fatalf("backend manquant : erreur = %v, attendu ErrStorageBackendMissing", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
withoutStore := NewService(*env.db, nil, testBackendID, allowAll())
|
||||||
|
if _, err := withoutStore.CreateVersion(ctx, env.owner.ID, env.videoID, CreateVersionParams{
|
||||||
|
Source: bytes.NewReader([]byte("x")),
|
||||||
|
}); !errors.Is(err, ErrStorageNotConfigured) {
|
||||||
|
t.Fatalf("sans stockage : erreur = %v, attendu ErrStorageNotConfigured", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHeadVersion(t *testing.T) {
|
||||||
|
env := newTestEnv(t, allowAll())
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
head, err := env.service.HeadVersion(ctx, env.owner.ID, env.videoID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("HeadVersion(sans version) : %v", err)
|
||||||
|
}
|
||||||
|
if head != nil {
|
||||||
|
t.Fatalf("HeadVersion(sans version) = %+v, attendu nil", head)
|
||||||
|
}
|
||||||
|
|
||||||
|
first, err := env.service.CreateVersion(ctx, env.owner.ID, env.videoID, CreateVersionParams{
|
||||||
|
Source: bytes.NewReader([]byte("premier")),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreateVersion : %v", err)
|
||||||
|
}
|
||||||
|
head, err = env.service.HeadVersion(ctx, env.owner.ID, env.videoID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("HeadVersion : %v", err)
|
||||||
|
}
|
||||||
|
if head == nil || head.ID != first.ID {
|
||||||
|
t.Fatalf("HeadVersion = %+v, attendu %s", head, first.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
second, err := env.service.CreateVersion(ctx, env.owner.ID, env.videoID, CreateVersionParams{
|
||||||
|
Source: bytes.NewReader([]byte("second")),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreateVersion(second) : %v", err)
|
||||||
|
}
|
||||||
|
head, err = env.service.HeadVersion(ctx, env.owner.ID, env.videoID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("HeadVersion(second) : %v", err)
|
||||||
|
}
|
||||||
|
if head == nil || head.ID != second.ID {
|
||||||
|
t.Fatalf("HeadVersion = %+v, attendu %s", head, second.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
readerOnly := NewService(*env.db, env.store, testBackendID, &testAuthorizer{allowed: permission.Write})
|
||||||
|
if _, err := readerOnly.HeadVersion(ctx, env.owner.ID, env.videoID); !errors.Is(err, document.ErrPermissionDenied) {
|
||||||
|
t.Fatalf("HeadVersion sans Read : erreur = %v, attendu ErrPermissionDenied", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
+1418
-2
File diff suppressed because it is too large
Load Diff
+432
-1
@@ -155,7 +155,7 @@ WHERE group_id = sqlc.arg(group_id)
|
|||||||
AND member_id = sqlc.arg(member_id);
|
AND member_id = sqlc.arg(member_id);
|
||||||
|
|
||||||
-- name: ListMemberGroupsWithRole :many
|
-- name: ListMemberGroupsWithRole :many
|
||||||
SELECT p.id, p.name, m.role
|
SELECT p.id, p.name, p.status, m.role
|
||||||
FROM membership m
|
FROM membership m
|
||||||
JOIN principal p ON p.id = m.group_id
|
JOIN principal p ON p.id = m.group_id
|
||||||
WHERE m.member_id = sqlc.arg(member_id)
|
WHERE m.member_id = sqlc.arg(member_id)
|
||||||
@@ -351,3 +351,434 @@ ORDER BY created_at DESC;
|
|||||||
DELETE FROM session
|
DELETE FROM session
|
||||||
WHERE expires_at <= sqlc.arg(now)
|
WHERE expires_at <= sqlc.arg(now)
|
||||||
OR idle_expires_at <= sqlc.arg(now);
|
OR idle_expires_at <= sqlc.arg(now);
|
||||||
|
|
||||||
|
-- ============================================================================
|
||||||
|
-- storage_policy
|
||||||
|
-- ============================================================================
|
||||||
|
|
||||||
|
-- name: GetStoragePolicy :one
|
||||||
|
SELECT *
|
||||||
|
FROM storage_policy
|
||||||
|
WHERE id = sqlc.arg(id);
|
||||||
|
|
||||||
|
-- ============================================================================
|
||||||
|
-- storage_backend
|
||||||
|
-- ============================================================================
|
||||||
|
|
||||||
|
-- name: GetStorageBackend :one
|
||||||
|
SELECT *
|
||||||
|
FROM storage_backend
|
||||||
|
WHERE id = sqlc.arg(id);
|
||||||
|
|
||||||
|
-- name: CreateStorageBackend :exec
|
||||||
|
INSERT INTO storage_backend (
|
||||||
|
id,
|
||||||
|
kind,
|
||||||
|
name,
|
||||||
|
config,
|
||||||
|
enabled,
|
||||||
|
state,
|
||||||
|
created_at
|
||||||
|
) VALUES (
|
||||||
|
sqlc.arg(id),
|
||||||
|
sqlc.arg(kind),
|
||||||
|
sqlc.arg(name),
|
||||||
|
sqlc.arg(config),
|
||||||
|
sqlc.arg(enabled),
|
||||||
|
sqlc.arg(state),
|
||||||
|
sqlc.arg(created_at)
|
||||||
|
);
|
||||||
|
|
||||||
|
-- ============================================================================
|
||||||
|
-- blob
|
||||||
|
-- ============================================================================
|
||||||
|
|
||||||
|
-- name: GetBlob :one
|
||||||
|
SELECT *
|
||||||
|
FROM blob
|
||||||
|
WHERE root = sqlc.arg(root);
|
||||||
|
|
||||||
|
-- name: CreateBlob :exec
|
||||||
|
INSERT INTO blob (
|
||||||
|
root,
|
||||||
|
size,
|
||||||
|
state,
|
||||||
|
role,
|
||||||
|
unit_size,
|
||||||
|
tree_state,
|
||||||
|
encryption,
|
||||||
|
created_at
|
||||||
|
) VALUES (
|
||||||
|
sqlc.arg(root),
|
||||||
|
sqlc.arg(size),
|
||||||
|
sqlc.arg(state),
|
||||||
|
sqlc.arg(role),
|
||||||
|
sqlc.arg(unit_size),
|
||||||
|
sqlc.arg(tree_state),
|
||||||
|
sqlc.arg(encryption),
|
||||||
|
sqlc.arg(created_at)
|
||||||
|
);
|
||||||
|
|
||||||
|
-- name: GetBlobLocation :one
|
||||||
|
SELECT *
|
||||||
|
FROM blob_location
|
||||||
|
WHERE root = sqlc.arg(root)
|
||||||
|
AND backend_id = sqlc.arg(backend_id);
|
||||||
|
|
||||||
|
-- name: ListBlobLocations :many
|
||||||
|
SELECT *
|
||||||
|
FROM blob_location
|
||||||
|
WHERE root = sqlc.arg(root)
|
||||||
|
ORDER BY created_at;
|
||||||
|
|
||||||
|
-- name: ListUnreferencedBlobs :many
|
||||||
|
SELECT b.*
|
||||||
|
FROM blob b
|
||||||
|
WHERE NOT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM document_version v
|
||||||
|
WHERE v.content_root = b.root
|
||||||
|
AND v.content_purged_at IS NULL
|
||||||
|
)
|
||||||
|
AND NOT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM export_link e
|
||||||
|
WHERE e.blob_root = b.root
|
||||||
|
AND e.state <> 'stale'
|
||||||
|
)
|
||||||
|
ORDER BY b.created_at;
|
||||||
|
|
||||||
|
-- name: DeleteBlob :exec
|
||||||
|
DELETE FROM blob
|
||||||
|
WHERE root = sqlc.arg(root);
|
||||||
|
|
||||||
|
-- name: UpdateBlobVerification :exec
|
||||||
|
UPDATE blob
|
||||||
|
SET state = sqlc.arg(state),
|
||||||
|
last_verified_at = sqlc.arg(last_verified_at),
|
||||||
|
verify_error = sqlc.narg(verify_error)
|
||||||
|
WHERE root = sqlc.arg(root);
|
||||||
|
|
||||||
|
-- name: UpdateBlobLocationState :exec
|
||||||
|
UPDATE blob_location
|
||||||
|
SET state = sqlc.arg(state)
|
||||||
|
WHERE root = sqlc.arg(root)
|
||||||
|
AND backend_id = sqlc.arg(backend_id);
|
||||||
|
|
||||||
|
-- name: CreateBlobLocation :exec
|
||||||
|
INSERT INTO blob_location (
|
||||||
|
root,
|
||||||
|
backend_id,
|
||||||
|
locator,
|
||||||
|
state,
|
||||||
|
created_at
|
||||||
|
) VALUES (
|
||||||
|
sqlc.arg(root),
|
||||||
|
sqlc.arg(backend_id),
|
||||||
|
sqlc.arg(locator),
|
||||||
|
sqlc.arg(state),
|
||||||
|
sqlc.arg(created_at)
|
||||||
|
);
|
||||||
|
|
||||||
|
-- ============================================================================
|
||||||
|
-- staging_item
|
||||||
|
-- ============================================================================
|
||||||
|
|
||||||
|
-- name: CreateStagingItem :exec
|
||||||
|
INSERT INTO staging_item (
|
||||||
|
id,
|
||||||
|
origin,
|
||||||
|
job_ref,
|
||||||
|
locator,
|
||||||
|
size,
|
||||||
|
state,
|
||||||
|
created_at,
|
||||||
|
updated_at
|
||||||
|
) VALUES (
|
||||||
|
sqlc.arg(id),
|
||||||
|
sqlc.arg(origin),
|
||||||
|
sqlc.narg(job_ref),
|
||||||
|
sqlc.arg(locator),
|
||||||
|
sqlc.narg(size),
|
||||||
|
sqlc.arg(state),
|
||||||
|
sqlc.arg(created_at),
|
||||||
|
sqlc.arg(updated_at)
|
||||||
|
);
|
||||||
|
|
||||||
|
-- name: UpdateStagingItemState :exec
|
||||||
|
UPDATE staging_item
|
||||||
|
SET state = sqlc.arg(state),
|
||||||
|
updated_at = sqlc.arg(updated_at)
|
||||||
|
WHERE id = sqlc.arg(id);
|
||||||
|
|
||||||
|
-- ============================================================================
|
||||||
|
-- document_kind
|
||||||
|
-- ============================================================================
|
||||||
|
|
||||||
|
-- name: GetDocumentKind :one
|
||||||
|
SELECT *
|
||||||
|
FROM document_kind
|
||||||
|
WHERE kind = sqlc.arg(kind);
|
||||||
|
|
||||||
|
-- ============================================================================
|
||||||
|
-- document
|
||||||
|
-- ============================================================================
|
||||||
|
|
||||||
|
-- name: GetDocument :one
|
||||||
|
SELECT *
|
||||||
|
FROM document
|
||||||
|
WHERE id = sqlc.arg(id);
|
||||||
|
|
||||||
|
-- name: CreateDocument :one
|
||||||
|
INSERT INTO document (
|
||||||
|
id,
|
||||||
|
kind,
|
||||||
|
owner_id,
|
||||||
|
created_by,
|
||||||
|
parent_id,
|
||||||
|
name,
|
||||||
|
name_key,
|
||||||
|
path,
|
||||||
|
depth,
|
||||||
|
position,
|
||||||
|
head_version_id,
|
||||||
|
metadata,
|
||||||
|
policy_id,
|
||||||
|
visibility,
|
||||||
|
acl_inherit,
|
||||||
|
rev,
|
||||||
|
created_at,
|
||||||
|
updated_at
|
||||||
|
) VALUES (
|
||||||
|
sqlc.arg(id),
|
||||||
|
sqlc.arg(kind),
|
||||||
|
sqlc.arg(owner_id),
|
||||||
|
sqlc.arg(created_by),
|
||||||
|
sqlc.narg(parent_id),
|
||||||
|
sqlc.arg(name),
|
||||||
|
sqlc.arg(name_key),
|
||||||
|
sqlc.arg(path),
|
||||||
|
sqlc.arg(depth),
|
||||||
|
sqlc.narg(position),
|
||||||
|
sqlc.narg(head_version_id),
|
||||||
|
sqlc.arg(metadata),
|
||||||
|
sqlc.narg(policy_id),
|
||||||
|
sqlc.narg(visibility),
|
||||||
|
sqlc.arg(acl_inherit),
|
||||||
|
sqlc.arg(rev),
|
||||||
|
sqlc.arg(created_at),
|
||||||
|
sqlc.arg(updated_at)
|
||||||
|
)
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: ListDocumentChildren :many
|
||||||
|
SELECT *
|
||||||
|
FROM document
|
||||||
|
WHERE parent_id = sqlc.arg(parent_id)
|
||||||
|
AND deleted_at IS NULL
|
||||||
|
ORDER BY COALESCE(position, ''), name_key;
|
||||||
|
|
||||||
|
-- name: ListLibraries :many
|
||||||
|
SELECT *
|
||||||
|
FROM document
|
||||||
|
WHERE parent_id IS NULL
|
||||||
|
AND deleted_at IS NULL
|
||||||
|
ORDER BY name_key;
|
||||||
|
|
||||||
|
-- name: UpdateDocumentName :one
|
||||||
|
UPDATE document
|
||||||
|
SET name = sqlc.arg(name),
|
||||||
|
name_key = sqlc.arg(name_key),
|
||||||
|
rev = rev + 1,
|
||||||
|
updated_at = sqlc.arg(updated_at)
|
||||||
|
WHERE id = sqlc.arg(id)
|
||||||
|
AND deleted_at IS NULL
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: UpdateDocumentMetadata :one
|
||||||
|
UPDATE document
|
||||||
|
SET metadata = sqlc.arg(metadata),
|
||||||
|
rev = rev + 1,
|
||||||
|
updated_at = sqlc.arg(updated_at)
|
||||||
|
WHERE id = sqlc.arg(id)
|
||||||
|
AND deleted_at IS NULL
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: MoveDocument :one
|
||||||
|
UPDATE document
|
||||||
|
SET parent_id = sqlc.arg(parent_id),
|
||||||
|
path = sqlc.arg(path),
|
||||||
|
depth = sqlc.arg(depth),
|
||||||
|
rev = rev + 1,
|
||||||
|
updated_at = sqlc.arg(updated_at)
|
||||||
|
WHERE id = sqlc.arg(id)
|
||||||
|
AND deleted_at IS NULL
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: ListDocumentDescendants :many
|
||||||
|
SELECT *
|
||||||
|
FROM document
|
||||||
|
WHERE path LIKE CAST(sqlc.arg(prefix) AS TEXT) || '%'
|
||||||
|
AND id <> sqlc.arg(root_id)
|
||||||
|
ORDER BY depth;
|
||||||
|
|
||||||
|
-- name: UpdateDocumentLocation :exec
|
||||||
|
UPDATE document
|
||||||
|
SET path = sqlc.arg(path),
|
||||||
|
depth = sqlc.arg(depth),
|
||||||
|
rev = rev + 1,
|
||||||
|
updated_at = sqlc.arg(updated_at)
|
||||||
|
WHERE id = sqlc.arg(id);
|
||||||
|
|
||||||
|
-- name: TrashDocumentSubtree :execrows
|
||||||
|
UPDATE document
|
||||||
|
SET deleted_at = sqlc.arg(deleted_at),
|
||||||
|
deleted_by = sqlc.arg(deleted_by),
|
||||||
|
trash_batch = sqlc.arg(trash_batch),
|
||||||
|
rev = rev + 1,
|
||||||
|
updated_at = sqlc.arg(updated_at)
|
||||||
|
WHERE path LIKE CAST(sqlc.arg(prefix) AS TEXT) || '%'
|
||||||
|
AND deleted_at IS NULL;
|
||||||
|
|
||||||
|
-- name: RestoreDocumentBatch :execrows
|
||||||
|
UPDATE document
|
||||||
|
SET deleted_at = NULL,
|
||||||
|
deleted_by = NULL,
|
||||||
|
trash_batch = NULL,
|
||||||
|
rev = rev + 1,
|
||||||
|
updated_at = sqlc.arg(updated_at)
|
||||||
|
WHERE trash_batch = sqlc.arg(trash_batch)
|
||||||
|
AND deleted_at IS NOT NULL;
|
||||||
|
|
||||||
|
-- name: GetEffectiveDocumentPolicy :one
|
||||||
|
WITH RECURSIVE chain(id, policy_id, parent_id) AS (
|
||||||
|
SELECT d.id, d.policy_id, d.parent_id
|
||||||
|
FROM document d
|
||||||
|
WHERE d.id = sqlc.arg(id)
|
||||||
|
UNION ALL
|
||||||
|
SELECT d.id, COALESCE(d.policy_id, chain.policy_id), d.parent_id
|
||||||
|
FROM document d
|
||||||
|
JOIN chain ON d.id = chain.parent_id
|
||||||
|
)
|
||||||
|
SELECT policy_id
|
||||||
|
FROM chain
|
||||||
|
WHERE policy_id IS NOT NULL
|
||||||
|
LIMIT 1;
|
||||||
|
|
||||||
|
-- ============================================================================
|
||||||
|
-- document_version
|
||||||
|
-- ============================================================================
|
||||||
|
|
||||||
|
-- name: GetDocumentVersion :one
|
||||||
|
SELECT *
|
||||||
|
FROM document_version
|
||||||
|
WHERE id = sqlc.arg(id);
|
||||||
|
|
||||||
|
-- name: ListDocumentVersions :many
|
||||||
|
SELECT *
|
||||||
|
FROM document_version
|
||||||
|
WHERE document_id = sqlc.arg(document_id)
|
||||||
|
ORDER BY seq DESC;
|
||||||
|
|
||||||
|
-- name: NextDocumentVersionSeq :one
|
||||||
|
SELECT COALESCE(MAX(seq), 0) + 1 AS seq
|
||||||
|
FROM document_version
|
||||||
|
WHERE document_id = sqlc.arg(document_id);
|
||||||
|
|
||||||
|
-- name: CreateDocumentVersion :one
|
||||||
|
INSERT INTO document_version (
|
||||||
|
id,
|
||||||
|
document_id,
|
||||||
|
seq,
|
||||||
|
content_root,
|
||||||
|
size,
|
||||||
|
mime,
|
||||||
|
author_id,
|
||||||
|
device_id,
|
||||||
|
origin,
|
||||||
|
message,
|
||||||
|
pinned,
|
||||||
|
created_at
|
||||||
|
) VALUES (
|
||||||
|
sqlc.arg(id),
|
||||||
|
sqlc.arg(document_id),
|
||||||
|
sqlc.arg(seq),
|
||||||
|
sqlc.arg(content_root),
|
||||||
|
sqlc.arg(size),
|
||||||
|
sqlc.narg(mime),
|
||||||
|
sqlc.arg(author_id),
|
||||||
|
sqlc.narg(device_id),
|
||||||
|
sqlc.arg(origin),
|
||||||
|
sqlc.narg(message),
|
||||||
|
sqlc.arg(pinned),
|
||||||
|
sqlc.arg(created_at)
|
||||||
|
)
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: SetDocumentHeadVersion :execrows
|
||||||
|
UPDATE document
|
||||||
|
SET head_version_id = sqlc.arg(head_version_id),
|
||||||
|
rev = rev + 1,
|
||||||
|
updated_at = sqlc.arg(updated_at)
|
||||||
|
WHERE id = sqlc.arg(id)
|
||||||
|
AND deleted_at IS NULL;
|
||||||
|
|
||||||
|
-- name: PurgeDocumentVersion :execrows
|
||||||
|
UPDATE document_version
|
||||||
|
SET content_purged_at = sqlc.arg(purged_at)
|
||||||
|
WHERE document_version.id = sqlc.arg(id)
|
||||||
|
AND document_version.content_purged_at IS NULL
|
||||||
|
AND NOT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM document d
|
||||||
|
WHERE d.id = document_version.document_id
|
||||||
|
AND d.head_version_id = document_version.id
|
||||||
|
);
|
||||||
|
|
||||||
|
-- ============================================================================
|
||||||
|
-- acl_entry
|
||||||
|
-- ============================================================================
|
||||||
|
|
||||||
|
-- name: UpsertACLEntry :one
|
||||||
|
INSERT INTO acl_entry (
|
||||||
|
id,
|
||||||
|
document_id,
|
||||||
|
subject_id,
|
||||||
|
permissions,
|
||||||
|
propagate,
|
||||||
|
expires_at,
|
||||||
|
granted_by,
|
||||||
|
created_at
|
||||||
|
) VALUES (
|
||||||
|
sqlc.arg(id),
|
||||||
|
sqlc.arg(document_id),
|
||||||
|
sqlc.arg(subject_id),
|
||||||
|
sqlc.arg(permissions),
|
||||||
|
sqlc.arg(propagate),
|
||||||
|
sqlc.narg(expires_at),
|
||||||
|
sqlc.arg(granted_by),
|
||||||
|
sqlc.arg(created_at)
|
||||||
|
)
|
||||||
|
ON CONFLICT(document_id, subject_id) DO UPDATE SET
|
||||||
|
permissions = excluded.permissions,
|
||||||
|
propagate = excluded.propagate,
|
||||||
|
expires_at = excluded.expires_at,
|
||||||
|
granted_by = excluded.granted_by,
|
||||||
|
revoked_at = NULL
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: RevokeACLEntry :one
|
||||||
|
UPDATE acl_entry
|
||||||
|
SET revoked_at = sqlc.arg(revoked_at)
|
||||||
|
WHERE document_id = sqlc.arg(document_id)
|
||||||
|
AND subject_id = sqlc.arg(subject_id)
|
||||||
|
AND revoked_at IS NULL
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: ListActiveACLEntriesForDocument :many
|
||||||
|
SELECT *
|
||||||
|
FROM acl_entry
|
||||||
|
WHERE document_id = sqlc.arg(document_id)
|
||||||
|
AND revoked_at IS NULL
|
||||||
|
AND (expires_at IS NULL OR expires_at > CAST(sqlc.arg(now) AS INTEGER))
|
||||||
|
ORDER BY created_at;
|
||||||
|
|||||||
@@ -0,0 +1,212 @@
|
|||||||
|
package document
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"encoding/hex"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"mediarr/database"
|
||||||
|
"mediarr/ident"
|
||||||
|
"mediarr/permission"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s *Service) CreateLibrary(ctx context.Context, actorID ident.UUID, params CreateLibraryParams) (*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
|
||||||
|
}
|
||||||
|
|
||||||
|
visibility, err := normalizeVisibility(params.Visibility)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := s.db.Queries.GetStoragePolicy(ctx, params.PolicyID); err != nil {
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return nil, fmt.Errorf("%w: %s", ErrPolicyNotFound, params.PolicyID)
|
||||||
|
}
|
||||||
|
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: KindLibrary,
|
||||||
|
OwnerID: params.OwnerID,
|
||||||
|
CreatedBy: params.CreatedBy,
|
||||||
|
ParentID: nil,
|
||||||
|
Name: name,
|
||||||
|
NameKey: nameKey,
|
||||||
|
Path: hex.EncodeToString(id[:]) + "/",
|
||||||
|
Depth: 0,
|
||||||
|
Metadata: metadataValue(params.Metadata),
|
||||||
|
PolicyID: ¶ms.PolicyID,
|
||||||
|
Visibility: sql.NullString{String: visibility, Valid: true},
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) CreateFolder(ctx context.Context, actorID ident.UUID, params CreateFolderParams) (*Document, error) {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
id, err := ident.NewV7()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
now := nowMillis()
|
||||||
|
|
||||||
|
created, err := s.db.WriteQueries.CreateDocument(ctx, database.CreateDocumentParams{
|
||||||
|
ID: id,
|
||||||
|
Kind: KindFolder,
|
||||||
|
OwnerID: params.OwnerID,
|
||||||
|
CreatedBy: params.CreatedBy,
|
||||||
|
ParentID: ¶ms.ParentID,
|
||||||
|
Name: name,
|
||||||
|
NameKey: nameKey,
|
||||||
|
Path: parent.Path + hex.EncodeToString(id[:]) + "/",
|
||||||
|
Depth: parent.Depth + 1,
|
||||||
|
Metadata: metadataValue(params.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
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
@@ -0,0 +1,390 @@
|
|||||||
|
package document
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/hex"
|
||||||
|
"errors"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"mediarr/ident"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCreateLibrary(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
|
||||||
|
library, err := service.CreateLibrary(ctx, owner.ID, CreateLibraryParams{
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
CreatedBy: owner.ID,
|
||||||
|
Name: "Photos",
|
||||||
|
PolicyID: testPolicyID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreateLibrary : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if library.Kind != KindLibrary {
|
||||||
|
t.Fatalf("kind = %q, attendu %q", library.Kind, KindLibrary)
|
||||||
|
}
|
||||||
|
if library.Name != "Photos" || library.NameKey != "photos" {
|
||||||
|
t.Fatalf("nom = %q/%q, attendu Photos/photos", library.Name, library.NameKey)
|
||||||
|
}
|
||||||
|
if library.ParentID != nil {
|
||||||
|
t.Fatalf("parent = %v, attendu nil", library.ParentID)
|
||||||
|
}
|
||||||
|
if library.Depth != 0 {
|
||||||
|
t.Fatalf("depth = %d, attendu 0", library.Depth)
|
||||||
|
}
|
||||||
|
if library.Path != hex.EncodeToString(library.ID[:])+"/" {
|
||||||
|
t.Fatalf("path = %q", library.Path)
|
||||||
|
}
|
||||||
|
if library.PolicyID == nil || *library.PolicyID != testPolicyID {
|
||||||
|
t.Fatalf("policy = %v, attendu %s", library.PolicyID, testPolicyID)
|
||||||
|
}
|
||||||
|
if library.Visibility != VisibilityPrivate {
|
||||||
|
t.Fatalf("visibility = %q, attendu %q", library.Visibility, VisibilityPrivate)
|
||||||
|
}
|
||||||
|
if !library.ACLInherit {
|
||||||
|
t.Fatal("acl_inherit = false, attendu true")
|
||||||
|
}
|
||||||
|
if library.Rev != 1 {
|
||||||
|
t.Fatalf("rev = %d, attendu 1", library.Rev)
|
||||||
|
}
|
||||||
|
if library.Metadata != "{}" {
|
||||||
|
t.Fatalf("metadata = %q, attendu {}", library.Metadata)
|
||||||
|
}
|
||||||
|
if library.CreatedAt == 0 || library.UpdatedAt == 0 {
|
||||||
|
t.Fatal("timestamps non renseignés")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateLibraryExplicitOptions(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
aclInherit := false
|
||||||
|
|
||||||
|
library, err := service.CreateLibrary(ctx, owner.ID, CreateLibraryParams{
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
CreatedBy: owner.ID,
|
||||||
|
Name: "Films",
|
||||||
|
PolicyID: testPolicyID,
|
||||||
|
Visibility: VisibilityCluster,
|
||||||
|
ACLInherit: &aclInherit,
|
||||||
|
Metadata: `{"genre":"test"}`,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreateLibrary : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if library.Visibility != VisibilityCluster {
|
||||||
|
t.Fatalf("visibility = %q, attendu %q", library.Visibility, VisibilityCluster)
|
||||||
|
}
|
||||||
|
if library.ACLInherit {
|
||||||
|
t.Fatal("acl_inherit = true, attendu false")
|
||||||
|
}
|
||||||
|
if library.Metadata != `{"genre":"test"}` {
|
||||||
|
t.Fatalf("metadata = %q", library.Metadata)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateLibraryRejectsInvalidName(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
|
||||||
|
cases := []string{"", " ", "a/b", strings.Repeat("a", 256)}
|
||||||
|
for _, name := range cases {
|
||||||
|
_, err := service.CreateLibrary(ctx, owner.ID, CreateLibraryParams{
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
CreatedBy: owner.ID,
|
||||||
|
Name: name,
|
||||||
|
PolicyID: testPolicyID,
|
||||||
|
})
|
||||||
|
if !errors.Is(err, ErrInvalidName) {
|
||||||
|
t.Fatalf("nom %q : erreur = %v, attendu ErrInvalidName", name, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateLibraryRejectsInvalidVisibility(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
|
||||||
|
_, err := service.CreateLibrary(ctx, owner.ID, CreateLibraryParams{
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
CreatedBy: owner.ID,
|
||||||
|
Name: "Photos",
|
||||||
|
PolicyID: testPolicyID,
|
||||||
|
Visibility: "SECRET",
|
||||||
|
})
|
||||||
|
if !errors.Is(err, ErrInvalidVisibility) {
|
||||||
|
t.Fatalf("erreur = %v, attendu ErrInvalidVisibility", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateLibraryRejectsMissingPolicy(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
missing, err := ident.NewV7()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewV7 : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = service.CreateLibrary(ctx, owner.ID, CreateLibraryParams{
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
CreatedBy: owner.ID,
|
||||||
|
Name: "Photos",
|
||||||
|
PolicyID: missing,
|
||||||
|
})
|
||||||
|
if !errors.Is(err, ErrPolicyNotFound) {
|
||||||
|
t.Fatalf("erreur = %v, attendu ErrPolicyNotFound", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateLibraryRejectsDuplicateName(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
|
||||||
|
mustLibrary(t, service, owner, "Photos")
|
||||||
|
|
||||||
|
_, err := service.CreateLibrary(ctx, owner.ID, CreateLibraryParams{
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
CreatedBy: owner.ID,
|
||||||
|
Name: "photos",
|
||||||
|
PolicyID: testPolicyID,
|
||||||
|
})
|
||||||
|
if !errors.Is(err, ErrNameTaken) {
|
||||||
|
t.Fatalf("erreur = %v, attendu ErrNameTaken", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateFolder(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
|
||||||
|
library := mustLibrary(t, service, owner, "Photos")
|
||||||
|
folder, err := service.CreateFolder(ctx, owner.ID, CreateFolderParams{
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
CreatedBy: owner.ID,
|
||||||
|
ParentID: library.ID,
|
||||||
|
Name: "Vacances",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreateFolder : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if folder.Kind != KindFolder {
|
||||||
|
t.Fatalf("kind = %q, attendu %q", folder.Kind, KindFolder)
|
||||||
|
}
|
||||||
|
if folder.ParentID == nil || *folder.ParentID != library.ID {
|
||||||
|
t.Fatalf("parent = %v, attendu %s", folder.ParentID, library.ID)
|
||||||
|
}
|
||||||
|
if folder.Depth != 1 {
|
||||||
|
t.Fatalf("depth = %d, attendu 1", folder.Depth)
|
||||||
|
}
|
||||||
|
if folder.Path != library.Path+hex.EncodeToString(folder.ID[:])+"/" {
|
||||||
|
t.Fatalf("path = %q", folder.Path)
|
||||||
|
}
|
||||||
|
if folder.Visibility != "" {
|
||||||
|
t.Fatalf("visibility = %q, attendu vide", folder.Visibility)
|
||||||
|
}
|
||||||
|
if folder.PolicyID != nil {
|
||||||
|
t.Fatalf("policy = %v, attendu nil", folder.PolicyID)
|
||||||
|
}
|
||||||
|
if !folder.ACLInherit {
|
||||||
|
t.Fatal("acl_inherit = false, attendu true")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateFolderRejectsMissingParent(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
missing, err := ident.NewV7()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewV7 : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = service.CreateFolder(ctx, owner.ID, CreateFolderParams{
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
CreatedBy: owner.ID,
|
||||||
|
ParentID: missing,
|
||||||
|
Name: "Vacances",
|
||||||
|
})
|
||||||
|
if !errors.Is(err, ErrParentNotFound) {
|
||||||
|
t.Fatalf("erreur = %v, attendu ErrParentNotFound", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateFolderRejectsDeletedParent(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
library := mustLibrary(t, service, owner, "Photos")
|
||||||
|
|
||||||
|
if _, err := service.Trash(ctx, owner.ID, library.ID); err != nil {
|
||||||
|
t.Fatalf("Trash : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := service.CreateFolder(ctx, owner.ID, CreateFolderParams{
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
CreatedBy: owner.ID,
|
||||||
|
ParentID: library.ID,
|
||||||
|
Name: "Vacances",
|
||||||
|
})
|
||||||
|
if !errors.Is(err, ErrParentDeleted) {
|
||||||
|
t.Fatalf("erreur = %v, attendu ErrParentDeleted", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateFolderRejectsInvalidParent(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
library := mustLibrary(t, service, owner, "Photos")
|
||||||
|
movie := mustKindDocument(t, service, owner, library, "movie", "Film")
|
||||||
|
|
||||||
|
_, err := service.CreateFolder(ctx, owner.ID, CreateFolderParams{
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
CreatedBy: owner.ID,
|
||||||
|
ParentID: movie.ID,
|
||||||
|
Name: "Extras",
|
||||||
|
})
|
||||||
|
if !errors.Is(err, ErrInvalidParent) {
|
||||||
|
t.Fatalf("erreur = %v, attendu ErrInvalidParent", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateFolderRejectsDuplicateSibling(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
library := mustLibrary(t, service, owner, "Photos")
|
||||||
|
|
||||||
|
mustFolder(t, service, owner, library.ID, "Vacances")
|
||||||
|
|
||||||
|
_, err := service.CreateFolder(ctx, owner.ID, CreateFolderParams{
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
CreatedBy: owner.ID,
|
||||||
|
ParentID: library.ID,
|
||||||
|
Name: "vacances",
|
||||||
|
})
|
||||||
|
if !errors.Is(err, ErrNameTaken) {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
+87
-3
@@ -1,10 +1,94 @@
|
|||||||
package document
|
package document
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"mediarr/ident"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
VisibilityPrivate = "PRIVATE"
|
||||||
|
VisibilityCluster = "CLUSTER"
|
||||||
|
VisibilityFederated = "FEDERATED"
|
||||||
|
VisibilityPublic = "PUBLIC"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
KindLibrary = "library"
|
||||||
|
KindFolder = "folder"
|
||||||
|
KindVideo = "video"
|
||||||
|
KindSubtitle = "subtitle"
|
||||||
|
KindImage = "image"
|
||||||
|
KindFile = "file"
|
||||||
|
)
|
||||||
|
|
||||||
type Document struct {
|
type Document struct {
|
||||||
|
ID ident.UUID
|
||||||
Kind string
|
Kind string
|
||||||
Hidden string
|
OwnerID ident.UUID
|
||||||
|
CreatedBy ident.UUID
|
||||||
|
ParentID *ident.UUID
|
||||||
|
Name string
|
||||||
|
NameKey string
|
||||||
|
Path string
|
||||||
|
Depth int64
|
||||||
|
Position *string
|
||||||
|
HeadVersionID *ident.UUID
|
||||||
|
Metadata string
|
||||||
|
PolicyID *ident.UUID
|
||||||
|
Visibility string
|
||||||
|
ACLInherit bool
|
||||||
|
Rev int64
|
||||||
|
CreatedAt int64
|
||||||
|
UpdatedAt int64
|
||||||
|
DeletedAt *int64
|
||||||
|
DeletedBy *ident.UUID
|
||||||
|
TrashBatch []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDocument() {
|
type CreateLibraryParams struct {
|
||||||
|
OwnerID ident.UUID
|
||||||
|
CreatedBy ident.UUID
|
||||||
|
Name string
|
||||||
|
PolicyID ident.UUID
|
||||||
|
Visibility string
|
||||||
|
ACLInherit *bool
|
||||||
|
Metadata string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type CreateFolderParams struct {
|
||||||
|
OwnerID ident.UUID
|
||||||
|
CreatedBy ident.UUID
|
||||||
|
ParentID ident.UUID
|
||||||
|
Name string
|
||||||
|
ACLInherit *bool
|
||||||
|
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é")
|
||||||
|
ErrDocumentNotFound = errors.New("document introuvable")
|
||||||
|
ErrDocumentDeleted = errors.New("document supprimé")
|
||||||
|
ErrParentNotFound = errors.New("parent introuvable")
|
||||||
|
ErrParentDeleted = errors.New("parent supprimé")
|
||||||
|
ErrInvalidParent = errors.New("parent invalide : bibliothèque ou dossier attendu")
|
||||||
|
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")
|
||||||
|
)
|
||||||
|
|||||||
@@ -0,0 +1,247 @@
|
|||||||
|
package document
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"mediarr/ident"
|
||||||
|
"mediarr/permission"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestEnforcementCreateLibrary(t *testing.T) {
|
||||||
|
authorizer := newAllowAllAuthorizer()
|
||||||
|
service := newTestServiceWithAuthorizer(t, authorizer)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
other := mustUser(t, service, "other")
|
||||||
|
|
||||||
|
authorizer.allowAll = false
|
||||||
|
authorizer.active = map[ident.UUID]bool{owner.ID: false}
|
||||||
|
|
||||||
|
_, err := service.CreateLibrary(ctx, owner.ID, CreateLibraryParams{
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
CreatedBy: owner.ID,
|
||||||
|
Name: "Photos",
|
||||||
|
PolicyID: testPolicyID,
|
||||||
|
})
|
||||||
|
if !errors.Is(err, ErrPermissionDenied) {
|
||||||
|
t.Fatalf("acteur inactif : erreur = %v, attendu ErrPermissionDenied", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
authorizer.active[owner.ID] = true
|
||||||
|
|
||||||
|
_, err = service.CreateLibrary(ctx, owner.ID, CreateLibraryParams{
|
||||||
|
OwnerID: other.ID,
|
||||||
|
CreatedBy: owner.ID,
|
||||||
|
Name: "Photos",
|
||||||
|
PolicyID: testPolicyID,
|
||||||
|
})
|
||||||
|
if !errors.Is(err, ErrPermissionDenied) {
|
||||||
|
t.Fatalf("owner usurpé : erreur = %v, attendu ErrPermissionDenied", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = service.CreateLibrary(ctx, owner.ID, CreateLibraryParams{
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
CreatedBy: other.ID,
|
||||||
|
Name: "Photos",
|
||||||
|
PolicyID: testPolicyID,
|
||||||
|
})
|
||||||
|
if !errors.Is(err, ErrPermissionDenied) {
|
||||||
|
t.Fatalf("created_by usurpé : erreur = %v, attendu ErrPermissionDenied", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := service.CreateLibrary(ctx, owner.ID, CreateLibraryParams{
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
CreatedBy: owner.ID,
|
||||||
|
Name: "Photos",
|
||||||
|
PolicyID: testPolicyID,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("CreateLibrary : %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEnforcementCreateFolder(t *testing.T) {
|
||||||
|
authorizer := newAllowAllAuthorizer()
|
||||||
|
service := newTestServiceWithAuthorizer(t, authorizer)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
other := mustUser(t, service, "other")
|
||||||
|
library := mustLibrary(t, service, owner, "Photos")
|
||||||
|
|
||||||
|
authorizer.allowAll = false
|
||||||
|
authorizer.active = map[ident.UUID]bool{owner.ID: true, other.ID: true}
|
||||||
|
|
||||||
|
_, err := service.CreateFolder(ctx, owner.ID, CreateFolderParams{
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
CreatedBy: owner.ID,
|
||||||
|
ParentID: library.ID,
|
||||||
|
Name: "Vacances",
|
||||||
|
})
|
||||||
|
if !errors.Is(err, ErrPermissionDenied) {
|
||||||
|
t.Fatalf("sans Create : erreur = %v, attendu ErrPermissionDenied", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
authorizer.grant(owner.ID, library.ID, permission.Create)
|
||||||
|
|
||||||
|
if _, err := service.CreateFolder(ctx, owner.ID, CreateFolderParams{
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
CreatedBy: owner.ID,
|
||||||
|
ParentID: library.ID,
|
||||||
|
Name: "Vacances",
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("CreateFolder : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = service.CreateFolder(ctx, owner.ID, CreateFolderParams{
|
||||||
|
OwnerID: other.ID,
|
||||||
|
CreatedBy: owner.ID,
|
||||||
|
ParentID: library.ID,
|
||||||
|
Name: "Autre",
|
||||||
|
})
|
||||||
|
if !errors.Is(err, ErrPermissionDenied) {
|
||||||
|
t.Fatalf("owner usurpé : erreur = %v, attendu ErrPermissionDenied", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEnforcementGetAndList(t *testing.T) {
|
||||||
|
authorizer := newAllowAllAuthorizer()
|
||||||
|
service := newTestServiceWithAuthorizer(t, authorizer)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
reader := mustUser(t, service, "reader")
|
||||||
|
outsider := mustUser(t, service, "outsider")
|
||||||
|
|
||||||
|
photos := mustLibrary(t, service, owner, "Photos")
|
||||||
|
mustLibrary(t, service, owner, "Films")
|
||||||
|
allowedFolder := mustFolder(t, service, owner, photos.ID, "Allowed")
|
||||||
|
hiddenFolder := mustFolder(t, service, owner, photos.ID, "Hidden")
|
||||||
|
|
||||||
|
authorizer.allowAll = false
|
||||||
|
authorizer.active = map[ident.UUID]bool{
|
||||||
|
owner.ID: true,
|
||||||
|
reader.ID: true,
|
||||||
|
outsider.ID: true,
|
||||||
|
}
|
||||||
|
authorizer.grant(reader.ID, photos.ID, permission.Read)
|
||||||
|
authorizer.grant(reader.ID, allowedFolder.ID, permission.Read)
|
||||||
|
|
||||||
|
if _, err := service.Get(ctx, outsider.ID, photos.ID); !errors.Is(err, ErrPermissionDenied) {
|
||||||
|
t.Fatalf("Get sans Read : erreur = %v, attendu ErrPermissionDenied", err)
|
||||||
|
}
|
||||||
|
if _, err := service.Get(ctx, reader.ID, photos.ID); err != nil {
|
||||||
|
t.Fatalf("Get : %v", err)
|
||||||
|
}
|
||||||
|
if _, err := service.Get(ctx, reader.ID, hiddenFolder.ID); !errors.Is(err, ErrPermissionDenied) {
|
||||||
|
t.Fatalf("Get enfant caché : erreur = %v, attendu ErrPermissionDenied", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
libraries, err := service.ListLibraries(ctx, reader.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ListLibraries : %v", err)
|
||||||
|
}
|
||||||
|
if len(libraries) != 1 || libraries[0].ID != photos.ID {
|
||||||
|
t.Fatalf("bibliothèques = %+v", libraries)
|
||||||
|
}
|
||||||
|
|
||||||
|
children, err := service.ListChildren(ctx, reader.ID, photos.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ListChildren : %v", err)
|
||||||
|
}
|
||||||
|
if len(children) != 1 || children[0].ID != allowedFolder.ID {
|
||||||
|
t.Fatalf("enfants = %+v", children)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := service.ListChildren(ctx, outsider.ID, photos.ID); !errors.Is(err, ErrPermissionDenied) {
|
||||||
|
t.Fatalf("ListChildren sans Read : erreur = %v, attendu ErrPermissionDenied", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEnforcementRename(t *testing.T) {
|
||||||
|
authorizer := newAllowAllAuthorizer()
|
||||||
|
service := newTestServiceWithAuthorizer(t, authorizer)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
writer := mustUser(t, service, "writer")
|
||||||
|
library := mustLibrary(t, service, owner, "Photos")
|
||||||
|
folder := mustFolder(t, service, owner, library.ID, "Action")
|
||||||
|
|
||||||
|
authorizer.allowAll = false
|
||||||
|
authorizer.active = map[ident.UUID]bool{writer.ID: true}
|
||||||
|
|
||||||
|
if _, err := service.Rename(ctx, writer.ID, folder.ID, "Aventure"); !errors.Is(err, ErrPermissionDenied) {
|
||||||
|
t.Fatalf("sans Write : erreur = %v, attendu ErrPermissionDenied", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
authorizer.grant(writer.ID, folder.ID, permission.Request)
|
||||||
|
if _, err := service.Rename(ctx, writer.ID, folder.ID, "Aventure"); !errors.Is(err, ErrPermissionDenied) {
|
||||||
|
t.Fatalf("avec Request seul : erreur = %v, attendu ErrPermissionDenied", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
authorizer.grant(writer.ID, folder.ID, permission.Write)
|
||||||
|
if _, err := service.Rename(ctx, writer.ID, folder.ID, "Aventure"); err != nil {
|
||||||
|
t.Fatalf("Rename : %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEnforcementMove(t *testing.T) {
|
||||||
|
authorizer := newAllowAllAuthorizer()
|
||||||
|
service := newTestServiceWithAuthorizer(t, authorizer)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
mover := mustUser(t, service, "mover")
|
||||||
|
libraryA := mustLibrary(t, service, owner, "Photos")
|
||||||
|
libraryB := mustLibrary(t, service, owner, "Films")
|
||||||
|
folder := mustFolder(t, service, owner, libraryA.ID, "Action")
|
||||||
|
|
||||||
|
authorizer.allowAll = false
|
||||||
|
authorizer.active = map[ident.UUID]bool{mover.ID: true}
|
||||||
|
|
||||||
|
if _, err := service.Move(ctx, mover.ID, folder.ID, libraryB.ID); !errors.Is(err, ErrPermissionDenied) {
|
||||||
|
t.Fatalf("sans Admin : erreur = %v, attendu ErrPermissionDenied", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
authorizer.grant(mover.ID, folder.ID, permission.Admin)
|
||||||
|
if _, err := service.Move(ctx, mover.ID, folder.ID, libraryB.ID); !errors.Is(err, ErrPermissionDenied) {
|
||||||
|
t.Fatalf("sans Create sur la cible : erreur = %v, attendu ErrPermissionDenied", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
authorizer.grant(mover.ID, libraryB.ID, permission.Create)
|
||||||
|
if _, err := service.Move(ctx, mover.ID, folder.ID, libraryB.ID); err != nil {
|
||||||
|
t.Fatalf("Move : %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEnforcementTrashRestore(t *testing.T) {
|
||||||
|
authorizer := newAllowAllAuthorizer()
|
||||||
|
service := newTestServiceWithAuthorizer(t, authorizer)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
deleter := mustUser(t, service, "deleter")
|
||||||
|
library := mustLibrary(t, service, owner, "Photos")
|
||||||
|
folder := mustFolder(t, service, owner, library.ID, "Action")
|
||||||
|
|
||||||
|
authorizer.allowAll = false
|
||||||
|
authorizer.active = map[ident.UUID]bool{deleter.ID: true}
|
||||||
|
|
||||||
|
if _, err := service.Trash(ctx, deleter.ID, folder.ID); !errors.Is(err, ErrPermissionDenied) {
|
||||||
|
t.Fatalf("Trash sans Delete : erreur = %v, attendu ErrPermissionDenied", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
authorizer.grant(deleter.ID, folder.ID, permission.Delete)
|
||||||
|
trashed, err := service.Trash(ctx, deleter.ID, folder.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Trash : %v", err)
|
||||||
|
}
|
||||||
|
if trashed.DeletedBy == nil || *trashed.DeletedBy != deleter.ID {
|
||||||
|
t.Fatalf("deleted_by = %v, attendu %s", trashed.DeletedBy, deleter.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := service.Get(ctx, deleter.ID, folder.ID); !errors.Is(err, ErrPermissionDenied) {
|
||||||
|
t.Fatalf("Get sans Read : erreur = %v, attendu ErrPermissionDenied", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := service.Restore(ctx, deleter.ID, folder.ID); err != nil {
|
||||||
|
t.Fatalf("Restore : %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,165 @@
|
|||||||
|
package document
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/hex"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"mediarr/database"
|
||||||
|
"mediarr/ident"
|
||||||
|
"mediarr/identity"
|
||||||
|
"mediarr/permission"
|
||||||
|
)
|
||||||
|
|
||||||
|
var testPolicyID = ident.MustParse("00000000-0000-7000-8000-000000000101")
|
||||||
|
|
||||||
|
type testAuthorizer struct {
|
||||||
|
allowAll bool
|
||||||
|
active map[ident.UUID]bool
|
||||||
|
grants map[ident.UUID]map[ident.UUID]permission.Permission
|
||||||
|
}
|
||||||
|
|
||||||
|
func newAllowAllAuthorizer() *testAuthorizer {
|
||||||
|
return &testAuthorizer{allowAll: true}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *testAuthorizer) IsActive(_ context.Context, principalID ident.UUID) (bool, error) {
|
||||||
|
if a.active == nil {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
if active, exists := a.active[principalID]; exists {
|
||||||
|
return active, nil
|
||||||
|
}
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *testAuthorizer) Can(_ context.Context, principalID, documentID ident.UUID, required permission.Permission) (bool, error) {
|
||||||
|
if a.allowAll {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
if a.active != nil {
|
||||||
|
if active, exists := a.active[principalID]; exists && !active {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return a.grants[documentID][principalID].Has(required), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *testAuthorizer) grant(principalID, documentID ident.UUID, permissions permission.Permission) {
|
||||||
|
if a.grants == nil {
|
||||||
|
a.grants = make(map[ident.UUID]map[ident.UUID]permission.Permission)
|
||||||
|
}
|
||||||
|
if a.grants[documentID] == nil {
|
||||||
|
a.grants[documentID] = make(map[ident.UUID]permission.Permission)
|
||||||
|
}
|
||||||
|
a.grants[documentID][principalID] |= permissions
|
||||||
|
}
|
||||||
|
|
||||||
|
func newTestService(t *testing.T) *Service {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
return newTestServiceWithAuthorizer(t, newAllowAllAuthorizer())
|
||||||
|
}
|
||||||
|
|
||||||
|
func newTestServiceWithAuthorizer(t *testing.T, authorizer Authorizer) *Service {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
t.Chdir(t.TempDir())
|
||||||
|
|
||||||
|
err, db := database.NewDatabase(database.NewDatabaseParams{})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewDatabase : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Cleanup(func() {
|
||||||
|
if err := db.Close(); err != nil {
|
||||||
|
t.Errorf("Close : %v", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return NewService(*db, authorizer)
|
||||||
|
}
|
||||||
|
|
||||||
|
func mustPrincipal(t *testing.T, service *Service, kind, name string) *identity.Principal {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
principal, err := identity.NewService(service.db).CreatePrincipal(context.Background(), identity.CreatePrincipalParams{
|
||||||
|
Kind: kind,
|
||||||
|
Name: name,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreatePrincipal(%q) : %v", name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return principal
|
||||||
|
}
|
||||||
|
|
||||||
|
func mustUser(t *testing.T, service *Service, name string) *identity.Principal {
|
||||||
|
return mustPrincipal(t, service, identity.KindUser, name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func mustLibrary(t *testing.T, service *Service, owner *identity.Principal, name string) *Document {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
library, err := service.CreateLibrary(context.Background(), owner.ID, CreateLibraryParams{
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
CreatedBy: owner.ID,
|
||||||
|
Name: name,
|
||||||
|
PolicyID: testPolicyID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreateLibrary(%q) : %v", name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return library
|
||||||
|
}
|
||||||
|
|
||||||
|
func mustFolder(t *testing.T, service *Service, owner *identity.Principal, parentID ident.UUID, name string) *Document {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
folder, err := service.CreateFolder(context.Background(), owner.ID, CreateFolderParams{
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
CreatedBy: owner.ID,
|
||||||
|
ParentID: parentID,
|
||||||
|
Name: name,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreateFolder(%q) : %v", name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return folder
|
||||||
|
}
|
||||||
|
|
||||||
|
func mustKindDocument(t *testing.T, service *Service, owner *identity.Principal, parent *Document, kind, name string) *Document {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
id, err := ident.NewV7()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewV7 : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
now := nowMillis()
|
||||||
|
|
||||||
|
created, err := service.db.WriteQueries.CreateDocument(context.Background(), database.CreateDocumentParams{
|
||||||
|
ID: id,
|
||||||
|
Kind: kind,
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
CreatedBy: owner.ID,
|
||||||
|
ParentID: &parent.ID,
|
||||||
|
Name: name,
|
||||||
|
NameKey: strings.ToLower(name),
|
||||||
|
Path: parent.Path + hex.EncodeToString(id[:]) + "/",
|
||||||
|
Depth: parent.Depth + 1,
|
||||||
|
Metadata: "{}",
|
||||||
|
AclInherit: 1,
|
||||||
|
Rev: 1,
|
||||||
|
CreatedAt: now,
|
||||||
|
UpdatedAt: now,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreateDocument(%q) : %v", name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return documentFromDB(created)
|
||||||
|
}
|
||||||
@@ -0,0 +1,250 @@
|
|||||||
|
package document
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"encoding/hex"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"mediarr/database"
|
||||||
|
"mediarr/ident"
|
||||||
|
"mediarr/permission"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s *Service) Rename(ctx context.Context, actorID, documentID ident.UUID, name string) (*Document, error) {
|
||||||
|
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, nameKey, err := normalizeName(name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
updated, err := s.db.WriteQueries.UpdateDocumentName(ctx, database.UpdateDocumentNameParams{
|
||||||
|
Name: normalized,
|
||||||
|
NameKey: nameKey,
|
||||||
|
UpdatedAt: nowMillis(),
|
||||||
|
ID: documentID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return nil, fmt.Errorf("%w: %s", ErrDocumentNotFound, documentID)
|
||||||
|
}
|
||||||
|
if isUniqueViolation(err) {
|
||||||
|
return nil, fmt.Errorf("%w: %s", ErrNameTaken, normalized)
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := s.can(ctx, actorID, documentID, permission.Admin); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if document.ParentID == nil {
|
||||||
|
return nil, ErrCannotMoveRoot
|
||||||
|
}
|
||||||
|
if *document.ParentID == newParentID {
|
||||||
|
return documentFromDB(*document), nil
|
||||||
|
}
|
||||||
|
if document.ID == newParentID {
|
||||||
|
return nil, ErrInvalidMove
|
||||||
|
}
|
||||||
|
|
||||||
|
parent, err := s.getDocument(ctx, newParentID)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, ErrDocumentNotFound) {
|
||||||
|
return nil, fmt.Errorf("%w: %s", ErrParentNotFound, newParentID)
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if parent.DeletedAt.Valid {
|
||||||
|
return nil, fmt.Errorf("%w: %s", ErrParentDeleted, newParentID)
|
||||||
|
}
|
||||||
|
if !isContainerKind(parent.Kind) {
|
||||||
|
return nil, fmt.Errorf("%w: %s", ErrInvalidParent, parent.Kind)
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(parent.Path, document.Path) {
|
||||||
|
return nil, ErrInvalidMove
|
||||||
|
}
|
||||||
|
if err := s.can(ctx, actorID, newParentID, permission.Create); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
newPath := parent.Path + hex.EncodeToString(document.ID[:]) + "/"
|
||||||
|
newDepth := parent.Depth + 1
|
||||||
|
depthDelta := newDepth - document.Depth
|
||||||
|
now := nowMillis()
|
||||||
|
|
||||||
|
var moved database.Document
|
||||||
|
|
||||||
|
err = s.withTx(ctx, func(q *database.Queries) error {
|
||||||
|
updated, err := q.MoveDocument(ctx, database.MoveDocumentParams{
|
||||||
|
ParentID: &newParentID,
|
||||||
|
Path: newPath,
|
||||||
|
Depth: newDepth,
|
||||||
|
UpdatedAt: now,
|
||||||
|
ID: documentID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
moved = updated
|
||||||
|
|
||||||
|
descendants, err := q.ListDocumentDescendants(ctx, database.ListDocumentDescendantsParams{
|
||||||
|
Prefix: document.Path,
|
||||||
|
RootID: documentID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, descendant := range descendants {
|
||||||
|
relativePath := strings.TrimPrefix(descendant.Path, document.Path)
|
||||||
|
if err := q.UpdateDocumentLocation(ctx, database.UpdateDocumentLocationParams{
|
||||||
|
Path: newPath + relativePath,
|
||||||
|
Depth: descendant.Depth + depthDelta,
|
||||||
|
UpdatedAt: now,
|
||||||
|
ID: descendant.ID,
|
||||||
|
}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
if isUniqueViolation(err) {
|
||||||
|
return nil, fmt.Errorf("%w: %s", ErrNameTaken, document.Name)
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return documentFromDB(moved), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) Trash(ctx context.Context, actorID, documentID ident.UUID) (*Document, error) {
|
||||||
|
document, err := s.getActiveDocument(ctx, documentID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := s.can(ctx, actorID, documentID, permission.Delete); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
batch, err := ident.NewV7()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
now := nowMillis()
|
||||||
|
|
||||||
|
deleted, err := s.db.WriteQueries.TrashDocumentSubtree(ctx, database.TrashDocumentSubtreeParams{
|
||||||
|
DeletedAt: sql.NullInt64{
|
||||||
|
Int64: now,
|
||||||
|
Valid: true,
|
||||||
|
},
|
||||||
|
DeletedBy: &actorID,
|
||||||
|
TrashBatch: batch.Bytes(),
|
||||||
|
UpdatedAt: now,
|
||||||
|
Prefix: document.Path,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if deleted == 0 {
|
||||||
|
return nil, fmt.Errorf("%w: %s", ErrDocumentNotFound, documentID)
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.getDocumentModel(ctx, documentID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) Restore(ctx context.Context, actorID, documentID ident.UUID) (*Document, error) {
|
||||||
|
document, err := s.getDocument(ctx, documentID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := s.can(ctx, actorID, documentID, permission.Delete); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if !document.DeletedAt.Valid {
|
||||||
|
return documentFromDB(*document), nil
|
||||||
|
}
|
||||||
|
if len(document.TrashBatch) == 0 {
|
||||||
|
return nil, fmt.Errorf("%w: %s", ErrInvalidTrashBatch, documentID)
|
||||||
|
}
|
||||||
|
|
||||||
|
for parentID := document.ParentID; parentID != nil; {
|
||||||
|
parent, err := s.getDocument(ctx, *parentID)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, ErrDocumentNotFound) {
|
||||||
|
return nil, fmt.Errorf("%w: %s", ErrParentNotFound, *parentID)
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if parent.DeletedAt.Valid && !bytes.Equal(parent.TrashBatch, document.TrashBatch) {
|
||||||
|
return nil, fmt.Errorf("%w: %s", ErrParentDeleted, *parentID)
|
||||||
|
}
|
||||||
|
parentID = parent.ParentID
|
||||||
|
}
|
||||||
|
|
||||||
|
restored, err := s.db.WriteQueries.RestoreDocumentBatch(ctx, database.RestoreDocumentBatchParams{
|
||||||
|
UpdatedAt: nowMillis(),
|
||||||
|
TrashBatch: document.TrashBatch,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
if isUniqueViolation(err) {
|
||||||
|
return nil, fmt.Errorf("%w: %s", ErrNameTaken, document.Name)
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if restored == 0 {
|
||||||
|
return nil, fmt.Errorf("%w: %s", ErrDocumentNotFound, documentID)
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.getDocumentModel(ctx, documentID)
|
||||||
|
}
|
||||||
@@ -0,0 +1,425 @@
|
|||||||
|
package document
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/hex"
|
||||||
|
"errors"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRename(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
library := mustLibrary(t, service, owner, "Photos")
|
||||||
|
folder := mustFolder(t, service, owner, library.ID, "Action")
|
||||||
|
|
||||||
|
renamed, err := service.Rename(ctx, owner.ID, folder.ID, "Aventure")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Rename : %v", err)
|
||||||
|
}
|
||||||
|
if renamed.Name != "Aventure" || renamed.NameKey != "aventure" {
|
||||||
|
t.Fatalf("nom = %q/%q, attendu Aventure/aventure", renamed.Name, renamed.NameKey)
|
||||||
|
}
|
||||||
|
if renamed.Rev != folder.Rev+1 {
|
||||||
|
t.Fatalf("rev = %d, attendu %d", renamed.Rev, folder.Rev+1)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := service.Rename(ctx, owner.ID, folder.ID, "a/b"); !errors.Is(err, ErrInvalidName) {
|
||||||
|
t.Fatalf("erreur = %v, attendu ErrInvalidName", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
other := mustFolder(t, service, owner, library.ID, "Drame")
|
||||||
|
if _, err := service.Rename(ctx, owner.ID, other.ID, "aventure"); !errors.Is(err, ErrNameTaken) {
|
||||||
|
t.Fatalf("erreur = %v, attendu ErrNameTaken", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := service.Trash(ctx, owner.ID, other.ID); err != nil {
|
||||||
|
t.Fatalf("Trash : %v", err)
|
||||||
|
}
|
||||||
|
if _, err := service.Rename(ctx, owner.ID, other.ID, "Autre"); !errors.Is(err, ErrDocumentDeleted) {
|
||||||
|
t.Fatalf("erreur = %v, attendu ErrDocumentDeleted", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMove(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
|
||||||
|
libraryA := mustLibrary(t, service, owner, "Photos")
|
||||||
|
libraryB := mustLibrary(t, service, owner, "Films")
|
||||||
|
folder := mustFolder(t, service, owner, libraryA.ID, "Action")
|
||||||
|
child := mustFolder(t, service, owner, folder.ID, "Extras")
|
||||||
|
|
||||||
|
moved, err := service.Move(ctx, owner.ID, folder.ID, libraryB.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Move : %v", err)
|
||||||
|
}
|
||||||
|
if moved.ParentID == nil || *moved.ParentID != libraryB.ID {
|
||||||
|
t.Fatalf("parent = %v, attendu %s", moved.ParentID, libraryB.ID)
|
||||||
|
}
|
||||||
|
if moved.Path != libraryB.Path+hex.EncodeToString(folder.ID[:])+"/" {
|
||||||
|
t.Fatalf("path = %q", moved.Path)
|
||||||
|
}
|
||||||
|
if moved.Depth != 1 {
|
||||||
|
t.Fatalf("depth = %d, attendu 1", moved.Depth)
|
||||||
|
}
|
||||||
|
if moved.Rev != folder.Rev+1 {
|
||||||
|
t.Fatalf("rev = %d, attendu %d", moved.Rev, folder.Rev+1)
|
||||||
|
}
|
||||||
|
|
||||||
|
movedChild, err := service.Get(ctx, owner.ID, child.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Get(child) : %v", err)
|
||||||
|
}
|
||||||
|
if movedChild.ParentID == nil || *movedChild.ParentID != folder.ID {
|
||||||
|
t.Fatalf("parent enfant = %v, attendu %s", movedChild.ParentID, folder.ID)
|
||||||
|
}
|
||||||
|
if movedChild.Path != moved.Path+hex.EncodeToString(child.ID[:])+"/" {
|
||||||
|
t.Fatalf("path enfant = %q", movedChild.Path)
|
||||||
|
}
|
||||||
|
if movedChild.Depth != 2 {
|
||||||
|
t.Fatalf("depth enfant = %d, attendu 2", movedChild.Depth)
|
||||||
|
}
|
||||||
|
if movedChild.Rev != child.Rev+1 {
|
||||||
|
t.Fatalf("rev enfant = %d, attendu %d", movedChild.Rev, child.Rev+1)
|
||||||
|
}
|
||||||
|
|
||||||
|
children, err := service.ListChildren(ctx, owner.ID, libraryB.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ListChildren : %v", err)
|
||||||
|
}
|
||||||
|
if len(children) != 1 || children[0].ID != folder.ID {
|
||||||
|
t.Fatalf("enfants = %+v", children)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMoveRootRejected(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
library := mustLibrary(t, service, owner, "Photos")
|
||||||
|
other := mustLibrary(t, service, owner, "Films")
|
||||||
|
|
||||||
|
_, err := service.Move(ctx, owner.ID, library.ID, other.ID)
|
||||||
|
if !errors.Is(err, ErrCannotMoveRoot) {
|
||||||
|
t.Fatalf("erreur = %v, attendu ErrCannotMoveRoot", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMoveIntoDescendantRejected(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
library := mustLibrary(t, service, owner, "Photos")
|
||||||
|
folder := mustFolder(t, service, owner, library.ID, "Action")
|
||||||
|
child := mustFolder(t, service, owner, folder.ID, "Extras")
|
||||||
|
|
||||||
|
_, err := service.Move(ctx, owner.ID, folder.ID, child.ID)
|
||||||
|
if !errors.Is(err, ErrInvalidMove) {
|
||||||
|
t.Fatalf("erreur = %v, attendu ErrInvalidMove", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMoveIntoItselfRejected(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
library := mustLibrary(t, service, owner, "Photos")
|
||||||
|
folder := mustFolder(t, service, owner, library.ID, "Action")
|
||||||
|
|
||||||
|
_, err := service.Move(ctx, owner.ID, folder.ID, folder.ID)
|
||||||
|
if !errors.Is(err, ErrInvalidMove) {
|
||||||
|
t.Fatalf("erreur = %v, attendu ErrInvalidMove", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMoveToInvalidParentRejected(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
library := mustLibrary(t, service, owner, "Photos")
|
||||||
|
folder := mustFolder(t, service, owner, library.ID, "Action")
|
||||||
|
movie := mustKindDocument(t, service, owner, library, "movie", "Film")
|
||||||
|
|
||||||
|
_, err := service.Move(ctx, owner.ID, folder.ID, movie.ID)
|
||||||
|
if !errors.Is(err, ErrInvalidParent) {
|
||||||
|
t.Fatalf("erreur = %v, attendu ErrInvalidParent", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMoveToSameParentIsNoop(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
library := mustLibrary(t, service, owner, "Photos")
|
||||||
|
folder := mustFolder(t, service, owner, library.ID, "Action")
|
||||||
|
|
||||||
|
moved, err := service.Move(ctx, owner.ID, folder.ID, library.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Move : %v", err)
|
||||||
|
}
|
||||||
|
if moved.Rev != folder.Rev {
|
||||||
|
t.Fatalf("rev = %d, attendu %d", moved.Rev, folder.Rev)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMoveDuplicateNameRejected(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
libraryA := mustLibrary(t, service, owner, "Photos")
|
||||||
|
libraryB := mustLibrary(t, service, owner, "Films")
|
||||||
|
folder := mustFolder(t, service, owner, libraryA.ID, "Action")
|
||||||
|
mustFolder(t, service, owner, libraryB.ID, "action")
|
||||||
|
|
||||||
|
_, err := service.Move(ctx, owner.ID, folder.ID, libraryB.ID)
|
||||||
|
if !errors.Is(err, ErrNameTaken) {
|
||||||
|
t.Fatalf("erreur = %v, attendu ErrNameTaken", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMoveRejectsDeletedDocument(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
libraryA := mustLibrary(t, service, owner, "Photos")
|
||||||
|
libraryB := mustLibrary(t, service, owner, "Films")
|
||||||
|
folder := mustFolder(t, service, owner, libraryA.ID, "Action")
|
||||||
|
|
||||||
|
if _, err := service.Trash(ctx, owner.ID, folder.ID); err != nil {
|
||||||
|
t.Fatalf("Trash : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := service.Move(ctx, owner.ID, folder.ID, libraryB.ID)
|
||||||
|
if !errors.Is(err, ErrDocumentDeleted) {
|
||||||
|
t.Fatalf("erreur = %v, attendu ErrDocumentDeleted", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMoveRejectsDeletedParent(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
libraryA := mustLibrary(t, service, owner, "Photos")
|
||||||
|
libraryB := mustLibrary(t, service, owner, "Films")
|
||||||
|
folder := mustFolder(t, service, owner, libraryA.ID, "Action")
|
||||||
|
|
||||||
|
if _, err := service.Trash(ctx, owner.ID, libraryB.ID); err != nil {
|
||||||
|
t.Fatalf("Trash : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := service.Move(ctx, owner.ID, folder.ID, libraryB.ID)
|
||||||
|
if !errors.Is(err, ErrParentDeleted) {
|
||||||
|
t.Fatalf("erreur = %v, attendu ErrParentDeleted", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTrashAndRestore(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
library := mustLibrary(t, service, owner, "Photos")
|
||||||
|
folder := mustFolder(t, service, owner, library.ID, "Action")
|
||||||
|
child := mustFolder(t, service, owner, folder.ID, "Extras")
|
||||||
|
|
||||||
|
trashed, err := service.Trash(ctx, owner.ID, folder.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Trash : %v", err)
|
||||||
|
}
|
||||||
|
if trashed.DeletedAt == nil {
|
||||||
|
t.Fatal("deleted_at non renseigné")
|
||||||
|
}
|
||||||
|
if trashed.DeletedBy == nil || *trashed.DeletedBy != owner.ID {
|
||||||
|
t.Fatalf("deleted_by = %v, attendu %s", trashed.DeletedBy, owner.ID)
|
||||||
|
}
|
||||||
|
if len(trashed.TrashBatch) == 0 {
|
||||||
|
t.Fatal("trash_batch non renseigné")
|
||||||
|
}
|
||||||
|
|
||||||
|
trashedChild, err := service.Get(ctx, owner.ID, child.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Get(child) : %v", err)
|
||||||
|
}
|
||||||
|
if trashedChild.DeletedAt == nil {
|
||||||
|
t.Fatal("enfant non supprimé")
|
||||||
|
}
|
||||||
|
if !bytes.Equal(trashedChild.TrashBatch, trashed.TrashBatch) {
|
||||||
|
t.Fatal("l’enfant n’appartient pas au même lot de corbeille")
|
||||||
|
}
|
||||||
|
|
||||||
|
children, err := service.ListChildren(ctx, owner.ID, library.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ListChildren : %v", err)
|
||||||
|
}
|
||||||
|
if len(children) != 0 {
|
||||||
|
t.Fatalf("enfants = %d, attendu 0", len(children))
|
||||||
|
}
|
||||||
|
|
||||||
|
restored, err := service.Restore(ctx, owner.ID, folder.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Restore : %v", err)
|
||||||
|
}
|
||||||
|
if restored.DeletedAt != nil || restored.DeletedBy != nil || len(restored.TrashBatch) != 0 {
|
||||||
|
t.Fatalf("document restauré avec des champs de corbeille : %+v", restored)
|
||||||
|
}
|
||||||
|
|
||||||
|
restoredChild, err := service.Get(ctx, owner.ID, child.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Get(child restauré) : %v", err)
|
||||||
|
}
|
||||||
|
if restoredChild.DeletedAt != nil {
|
||||||
|
t.Fatal("enfant non restauré")
|
||||||
|
}
|
||||||
|
|
||||||
|
children, err = service.ListChildren(ctx, owner.ID, library.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ListChildren après restauration : %v", err)
|
||||||
|
}
|
||||||
|
if len(children) != 1 || children[0].ID != folder.ID {
|
||||||
|
t.Fatalf("enfants après restauration = %+v", children)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRestoreNonDeletedIsNoop(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
library := mustLibrary(t, service, owner, "Photos")
|
||||||
|
|
||||||
|
restored, err := service.Restore(ctx, owner.ID, library.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Restore : %v", err)
|
||||||
|
}
|
||||||
|
if restored.ID != library.ID || restored.DeletedAt != nil {
|
||||||
|
t.Fatalf("document = %+v", restored)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTrashAlreadyDeleted(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
library := mustLibrary(t, service, owner, "Photos")
|
||||||
|
|
||||||
|
if _, err := service.Trash(ctx, owner.ID, library.ID); err != nil {
|
||||||
|
t.Fatalf("Trash : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := service.Trash(ctx, owner.ID, library.ID); !errors.Is(err, ErrDocumentDeleted) {
|
||||||
|
t.Fatalf("erreur = %v, attendu ErrDocumentDeleted", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRestoreConflict(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
library := mustLibrary(t, service, owner, "Photos")
|
||||||
|
folder := mustFolder(t, service, owner, library.ID, "Action")
|
||||||
|
|
||||||
|
if _, err := service.Trash(ctx, owner.ID, folder.ID); err != nil {
|
||||||
|
t.Fatalf("Trash : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
mustFolder(t, service, owner, library.ID, "Action")
|
||||||
|
|
||||||
|
if _, err := service.Restore(ctx, owner.ID, folder.ID); !errors.Is(err, ErrNameTaken) {
|
||||||
|
t.Fatalf("erreur = %v, attendu ErrNameTaken", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRestoreParentDeletedInDifferentBatch(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
library := mustLibrary(t, service, owner, "Photos")
|
||||||
|
folder := mustFolder(t, service, owner, library.ID, "Action")
|
||||||
|
child := mustFolder(t, service, owner, folder.ID, "Extras")
|
||||||
|
|
||||||
|
if _, err := service.Trash(ctx, owner.ID, child.ID); err != nil {
|
||||||
|
t.Fatalf("Trash(child) : %v", err)
|
||||||
|
}
|
||||||
|
if _, err := service.Trash(ctx, owner.ID, folder.ID); err != nil {
|
||||||
|
t.Fatalf("Trash(folder) : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := service.Restore(ctx, owner.ID, child.ID); !errors.Is(err, ErrParentDeleted) {
|
||||||
|
t.Fatalf("erreur = %v, attendu ErrParentDeleted", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRestoreRejectsDeletedAncestor(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
library := mustLibrary(t, service, owner, "Photos")
|
||||||
|
folder := mustFolder(t, service, owner, library.ID, "Action")
|
||||||
|
child := mustFolder(t, service, owner, folder.ID, "Extras")
|
||||||
|
|
||||||
|
if _, err := service.Trash(ctx, owner.ID, folder.ID); err != nil {
|
||||||
|
t.Fatalf("Trash(folder) : %v", err)
|
||||||
|
}
|
||||||
|
if _, err := service.Trash(ctx, owner.ID, library.ID); err != nil {
|
||||||
|
t.Fatalf("Trash(library) : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := service.Restore(ctx, owner.ID, child.ID); !errors.Is(err, ErrParentDeleted) {
|
||||||
|
t.Fatalf("erreur = %v, attendu ErrParentDeleted", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMoveUpdatesTrashedDescendants(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
libraryA := mustLibrary(t, service, owner, "Photos")
|
||||||
|
libraryB := mustLibrary(t, service, owner, "Films")
|
||||||
|
folder := mustFolder(t, service, owner, libraryA.ID, "Action")
|
||||||
|
child := mustFolder(t, service, owner, folder.ID, "Extras")
|
||||||
|
|
||||||
|
if _, err := service.Trash(ctx, owner.ID, child.ID); err != nil {
|
||||||
|
t.Fatalf("Trash(child) : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
moved, err := service.Move(ctx, owner.ID, folder.ID, libraryB.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Move : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
restored, err := service.Restore(ctx, owner.ID, child.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Restore(child) : %v", err)
|
||||||
|
}
|
||||||
|
if restored.Path != moved.Path+hex.EncodeToString(child.ID[:])+"/" {
|
||||||
|
t.Fatalf("path = %q, attendu %q", restored.Path, moved.Path+hex.EncodeToString(child.ID[:])+"/")
|
||||||
|
}
|
||||||
|
if restored.Depth != 2 {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package document
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"mediarr/ident"
|
||||||
|
"mediarr/permission"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s *Service) Get(ctx context.Context, actorID, id ident.UUID) (*Document, error) {
|
||||||
|
document, err := s.getDocument(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := s.can(ctx, actorID, id, permission.Read); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return documentFromDB(*document), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) ListChildren(ctx context.Context, actorID, parentID ident.UUID) ([]Document, error) {
|
||||||
|
if _, err := s.getActiveDocument(ctx, parentID); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := s.can(ctx, actorID, parentID, permission.Read); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := s.db.Queries.ListDocumentChildren(ctx, &parentID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
documents := make([]Document, 0, len(rows))
|
||||||
|
for _, row := range rows {
|
||||||
|
allowed, err := s.allowed(ctx, actorID, row.ID, permission.Read)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if allowed {
|
||||||
|
documents = append(documents, *documentFromDB(row))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return documents, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) ListLibraries(ctx context.Context, actorID ident.UUID) ([]Document, error) {
|
||||||
|
rows, err := s.db.Queries.ListLibraries(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
documents := make([]Document, 0, len(rows))
|
||||||
|
for _, row := range rows {
|
||||||
|
allowed, err := s.allowed(ctx, actorID, row.ID, permission.Read)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if allowed {
|
||||||
|
documents = append(documents, *documentFromDB(row))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return documents, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,114 @@
|
|||||||
|
package document
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"mediarr/ident"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestListLibraries(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
|
||||||
|
mustLibrary(t, service, owner, "Photos")
|
||||||
|
mustLibrary(t, service, owner, "Films")
|
||||||
|
|
||||||
|
libraries, err := service.ListLibraries(ctx, owner.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ListLibraries : %v", err)
|
||||||
|
}
|
||||||
|
if len(libraries) != 2 {
|
||||||
|
t.Fatalf("bibliothèques = %d, attendu 2", len(libraries))
|
||||||
|
}
|
||||||
|
if libraries[0].Name != "Films" || libraries[1].Name != "Photos" {
|
||||||
|
t.Fatalf("ordre = %q, %q", libraries[0].Name, libraries[1].Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestListChildren(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
library := mustLibrary(t, service, owner, "Photos")
|
||||||
|
|
||||||
|
mustFolder(t, service, owner, library.ID, "B")
|
||||||
|
a := mustFolder(t, service, owner, library.ID, "A")
|
||||||
|
|
||||||
|
children, err := service.ListChildren(ctx, owner.ID, library.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ListChildren : %v", err)
|
||||||
|
}
|
||||||
|
if len(children) != 2 || children[0].Name != "A" || children[1].Name != "B" {
|
||||||
|
t.Fatalf("enfants = %+v", children)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := service.Trash(ctx, owner.ID, a.ID); err != nil {
|
||||||
|
t.Fatalf("Trash : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
children, err = service.ListChildren(ctx, owner.ID, library.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ListChildren après trash : %v", err)
|
||||||
|
}
|
||||||
|
if len(children) != 1 || children[0].Name != "B" {
|
||||||
|
t.Fatalf("enfants après trash = %+v", children)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetDeletedDocument(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
library := mustLibrary(t, service, owner, "Photos")
|
||||||
|
|
||||||
|
if _, err := service.Trash(ctx, owner.ID, library.ID); err != nil {
|
||||||
|
t.Fatalf("Trash : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
deleted, err := service.Get(ctx, owner.ID, library.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Get : %v", err)
|
||||||
|
}
|
||||||
|
if deleted.DeletedAt == nil {
|
||||||
|
t.Fatal("deleted_at non renseigné")
|
||||||
|
}
|
||||||
|
if deleted.DeletedBy == nil || *deleted.DeletedBy != owner.ID {
|
||||||
|
t.Fatalf("deleted_by = %v, attendu %s", deleted.DeletedBy, owner.ID)
|
||||||
|
}
|
||||||
|
if len(deleted.TrashBatch) == 0 {
|
||||||
|
t.Fatal("trash_batch non renseigné")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetMissingDocument(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
missing, err := ident.NewV7()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewV7 : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = service.Get(ctx, owner.ID, missing)
|
||||||
|
if !errors.Is(err, ErrDocumentNotFound) {
|
||||||
|
t.Fatalf("erreur = %v, attendu ErrDocumentNotFound", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestListChildrenMissingParent(t *testing.T) {
|
||||||
|
service := newTestService(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
owner := mustUser(t, service, "owner")
|
||||||
|
missing, err := ident.NewV7()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewV7 : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = service.ListChildren(ctx, owner.ID, missing)
|
||||||
|
if !errors.Is(err, ErrDocumentNotFound) {
|
||||||
|
t.Fatalf("erreur = %v, attendu ErrDocumentNotFound", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,208 @@
|
|||||||
|
package document
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"mediarr/database"
|
||||||
|
"mediarr/ident"
|
||||||
|
"mediarr/permission"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Authorizer interface {
|
||||||
|
Can(ctx context.Context, principalID, documentID ident.UUID, required permission.Permission) (bool, error)
|
||||||
|
IsActive(ctx context.Context, principalID ident.UUID) (bool, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Service struct {
|
||||||
|
db database.DatabaseClient
|
||||||
|
authorizer Authorizer
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewService(db database.DatabaseClient, authorizer Authorizer) *Service {
|
||||||
|
return &Service{
|
||||||
|
db: db,
|
||||||
|
authorizer: authorizer,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) withTx(ctx context.Context, fn func(*database.Queries) error) error {
|
||||||
|
tx, err := s.db.WriteDB.BeginTx(ctx, nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
_ = tx.Rollback()
|
||||||
|
}()
|
||||||
|
|
||||||
|
if err := fn(s.db.WriteQueries.WithTx(tx)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return tx.Commit()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) getDocument(ctx context.Context, id ident.UUID) (*database.Document, error) {
|
||||||
|
document, err := s.db.Queries.GetDocument(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return nil, fmt.Errorf("%w: %s", ErrDocumentNotFound, id)
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &document, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) getActiveDocument(ctx context.Context, id ident.UUID) (*database.Document, error) {
|
||||||
|
document, err := s.getDocument(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if document.DeletedAt.Valid {
|
||||||
|
return nil, fmt.Errorf("%w: %s", ErrDocumentDeleted, id)
|
||||||
|
}
|
||||||
|
|
||||||
|
return document, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) getDocumentModel(ctx context.Context, id ident.UUID) (*Document, error) {
|
||||||
|
document, err := s.getDocument(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return documentFromDB(*document), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) requireActive(ctx context.Context, principalID ident.UUID) error {
|
||||||
|
active, err := s.authorizer.IsActive(ctx, principalID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !active {
|
||||||
|
return fmt.Errorf("%w: principal inactif %s", ErrPermissionDenied, principalID)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) allowed(ctx context.Context, principalID, documentID ident.UUID, required permission.Permission) (bool, error) {
|
||||||
|
return s.authorizer.Can(ctx, principalID, documentID, required)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) can(ctx context.Context, principalID, documentID ident.UUID, required permission.Permission) error {
|
||||||
|
allowed, err := s.allowed(ctx, principalID, documentID, required)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !allowed {
|
||||||
|
return fmt.Errorf("%w: %s sur %s", ErrPermissionDenied, principalID, documentID)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func normalizeName(name string) (string, string, error) {
|
||||||
|
normalized := strings.TrimSpace(name)
|
||||||
|
if normalized == "" || len(normalized) > 255 || strings.Contains(normalized, "/") || strings.ContainsRune(normalized, 0) {
|
||||||
|
return "", "", fmt.Errorf("%w: %q", ErrInvalidName, name)
|
||||||
|
}
|
||||||
|
|
||||||
|
return normalized, strings.ToLower(normalized), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func normalizeVisibility(visibility string) (string, error) {
|
||||||
|
if visibility == "" {
|
||||||
|
return VisibilityPrivate, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
switch visibility {
|
||||||
|
case VisibilityPrivate, VisibilityCluster, VisibilityFederated, VisibilityPublic:
|
||||||
|
return visibility, nil
|
||||||
|
default:
|
||||||
|
return "", fmt.Errorf("%w: %q", ErrInvalidVisibility, visibility)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func metadataValue(metadata string) string {
|
||||||
|
if metadata == "" {
|
||||||
|
return "{}"
|
||||||
|
}
|
||||||
|
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
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func isContainerKind(kind string) bool {
|
||||||
|
return kind == KindLibrary || kind == KindFolder
|
||||||
|
}
|
||||||
|
|
||||||
|
func isUniqueViolation(err error) bool {
|
||||||
|
return err != nil && strings.Contains(strings.ToLower(err.Error()), "unique constraint failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
func documentFromDB(document database.Document) *Document {
|
||||||
|
visibility := ""
|
||||||
|
if document.Visibility.Valid {
|
||||||
|
visibility = document.Visibility.String
|
||||||
|
}
|
||||||
|
|
||||||
|
var position *string
|
||||||
|
if document.Position.Valid {
|
||||||
|
value := document.Position.String
|
||||||
|
position = &value
|
||||||
|
}
|
||||||
|
|
||||||
|
var deletedAt *int64
|
||||||
|
if document.DeletedAt.Valid {
|
||||||
|
value := document.DeletedAt.Int64
|
||||||
|
deletedAt = &value
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Document{
|
||||||
|
ID: document.ID,
|
||||||
|
Kind: document.Kind,
|
||||||
|
OwnerID: document.OwnerID,
|
||||||
|
CreatedBy: document.CreatedBy,
|
||||||
|
ParentID: document.ParentID,
|
||||||
|
Name: document.Name,
|
||||||
|
NameKey: document.NameKey,
|
||||||
|
Path: document.Path,
|
||||||
|
Depth: document.Depth,
|
||||||
|
Position: position,
|
||||||
|
HeadVersionID: document.HeadVersionID,
|
||||||
|
Metadata: document.Metadata,
|
||||||
|
PolicyID: document.PolicyID,
|
||||||
|
Visibility: visibility,
|
||||||
|
ACLInherit: document.AclInherit != 0,
|
||||||
|
Rev: document.Rev,
|
||||||
|
CreatedAt: document.CreatedAt,
|
||||||
|
UpdatedAt: document.UpdatedAt,
|
||||||
|
DeletedAt: deletedAt,
|
||||||
|
DeletedBy: document.DeletedBy,
|
||||||
|
TrashBatch: document.TrashBatch,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func nowMillis() int64 {
|
||||||
|
return time.Now().UnixMilli()
|
||||||
|
}
|
||||||
@@ -97,6 +97,7 @@ func (s *Service) ListGroups(ctx context.Context, memberID ident.UUID) ([]Group,
|
|||||||
groups = append(groups, Group{
|
groups = append(groups, Group{
|
||||||
ID: row.ID,
|
ID: row.ID,
|
||||||
Name: row.Name,
|
Name: row.Name,
|
||||||
|
Status: row.Status,
|
||||||
Role: row.Role,
|
Role: row.Role,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,12 @@ const (
|
|||||||
RoleMember = "member"
|
RoleMember = "member"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
StatusActive = "active"
|
||||||
|
StatusDisabled = "disabled"
|
||||||
|
StatusRevoked = "revoked"
|
||||||
|
)
|
||||||
|
|
||||||
type Principal struct {
|
type Principal struct {
|
||||||
ID ident.UUID
|
ID ident.UUID
|
||||||
Kind string
|
Kind string
|
||||||
@@ -31,6 +37,7 @@ type Principal struct {
|
|||||||
type Group struct {
|
type Group struct {
|
||||||
ID ident.UUID
|
ID ident.UUID
|
||||||
Name string
|
Name string
|
||||||
|
Status string
|
||||||
Role string
|
Role string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -31,9 +31,11 @@ func (s *Service) CreatePrincipal(ctx context.Context, params CreatePrincipalPar
|
|||||||
|
|
||||||
status := params.Status
|
status := params.Status
|
||||||
if status == "" {
|
if status == "" {
|
||||||
status = "active"
|
status = StatusActive
|
||||||
}
|
}
|
||||||
if status != "active" && status != "disabled" && status != "revoked" {
|
switch status {
|
||||||
|
case StatusActive, StatusDisabled, StatusRevoked:
|
||||||
|
default:
|
||||||
return nil, fmt.Errorf("%w: %q", ErrInvalidStatus, status)
|
return nil, fmt.Errorf("%w: %q", ErrInvalidStatus, status)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,7 +89,7 @@ func (s *Service) CreateGuest(ctx context.Context, name string) (*Principal, err
|
|||||||
ID: id,
|
ID: id,
|
||||||
Kind: KindGuest,
|
Kind: KindGuest,
|
||||||
Name: name,
|
Name: name,
|
||||||
Status: "active",
|
Status: StatusActive,
|
||||||
CreatedAt: now,
|
CreatedAt: now,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -27,8 +27,8 @@ func TestCreatePrincipal(t *testing.T) {
|
|||||||
if created.Name != "alice" {
|
if created.Name != "alice" {
|
||||||
t.Fatalf("name = %q, attendu alice", created.Name)
|
t.Fatalf("name = %q, attendu alice", created.Name)
|
||||||
}
|
}
|
||||||
if created.Status != "active" {
|
if created.Status != StatusActive {
|
||||||
t.Fatalf("status = %q, attendu active", created.Status)
|
t.Fatalf("status = %q, attendu %q", created.Status, StatusActive)
|
||||||
}
|
}
|
||||||
if len(created.Groups) != 0 {
|
if len(created.Groups) != 0 {
|
||||||
t.Fatalf("groupes = %+v, attendu aucun", created.Groups)
|
t.Fatalf("groupes = %+v, attendu aucun", created.Groups)
|
||||||
|
|||||||
@@ -0,0 +1,128 @@
|
|||||||
|
package ingest
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
|
"os/exec"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type FFprobeProber struct {
|
||||||
|
Binary string
|
||||||
|
}
|
||||||
|
|
||||||
|
type ffprobeOutput struct {
|
||||||
|
Format struct {
|
||||||
|
FormatName string `json:"format_name"`
|
||||||
|
Duration string `json:"duration"`
|
||||||
|
BitRate string `json:"bit_rate"`
|
||||||
|
} `json:"format"`
|
||||||
|
Streams []struct {
|
||||||
|
CodecType string `json:"codec_type"`
|
||||||
|
CodecName string `json:"codec_name"`
|
||||||
|
Width int `json:"width"`
|
||||||
|
Height int `json:"height"`
|
||||||
|
Channels int `json:"channels"`
|
||||||
|
BitRate string `json:"bit_rate"`
|
||||||
|
} `json:"streams"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p FFprobeProber) Probe(ctx context.Context, path string, _ fs.FileInfo) (Probe, error) {
|
||||||
|
binary := strings.TrimSpace(p.Binary)
|
||||||
|
if binary == "" {
|
||||||
|
binary = "ffprobe"
|
||||||
|
}
|
||||||
|
|
||||||
|
command := exec.CommandContext(ctx, binary,
|
||||||
|
"-v", "error",
|
||||||
|
"-print_format", "json",
|
||||||
|
"-show_format",
|
||||||
|
"-show_streams",
|
||||||
|
"-i", path,
|
||||||
|
)
|
||||||
|
|
||||||
|
var stderr bytes.Buffer
|
||||||
|
command.Stderr = &stderr
|
||||||
|
|
||||||
|
output, err := command.Output()
|
||||||
|
if err != nil {
|
||||||
|
message := strings.TrimSpace(stderr.String())
|
||||||
|
if message == "" {
|
||||||
|
return Probe{}, fmt.Errorf("ffprobe %s : %w", path, err)
|
||||||
|
}
|
||||||
|
return Probe{}, fmt.Errorf("ffprobe %s : %w: %s", path, err, message)
|
||||||
|
}
|
||||||
|
|
||||||
|
var parsed ffprobeOutput
|
||||||
|
if err := json.Unmarshal(output, &parsed); err != nil {
|
||||||
|
return Probe{}, fmt.Errorf("ffprobe %s : réponse JSON invalide : %w", path, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
probe := Probe{
|
||||||
|
Mime: mimeForPath(path),
|
||||||
|
Metadata: make(map[string]any),
|
||||||
|
}
|
||||||
|
|
||||||
|
if parsed.Format.FormatName != "" {
|
||||||
|
probe.Metadata["container"] = parsed.Format.FormatName
|
||||||
|
}
|
||||||
|
if duration := durationMillis(parsed.Format.Duration); duration > 0 {
|
||||||
|
probe.Metadata["duration_ms"] = duration
|
||||||
|
}
|
||||||
|
if bitRate := positiveInt(parsed.Format.BitRate); bitRate > 0 {
|
||||||
|
probe.Metadata["bit_rate"] = bitRate
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, stream := range parsed.Streams {
|
||||||
|
switch stream.CodecType {
|
||||||
|
case "video":
|
||||||
|
if _, exists := probe.Metadata["video_codec"]; exists {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if stream.CodecName != "" {
|
||||||
|
probe.Metadata["video_codec"] = stream.CodecName
|
||||||
|
}
|
||||||
|
if stream.Width > 0 {
|
||||||
|
probe.Metadata["width"] = int64(stream.Width)
|
||||||
|
}
|
||||||
|
if stream.Height > 0 {
|
||||||
|
probe.Metadata["height"] = int64(stream.Height)
|
||||||
|
}
|
||||||
|
if bitRate := positiveInt(stream.BitRate); bitRate > 0 {
|
||||||
|
probe.Metadata["video_bit_rate"] = bitRate
|
||||||
|
}
|
||||||
|
case "audio":
|
||||||
|
if _, exists := probe.Metadata["audio_codec"]; exists {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if stream.CodecName != "" {
|
||||||
|
probe.Metadata["audio_codec"] = stream.CodecName
|
||||||
|
}
|
||||||
|
if stream.Channels > 0 {
|
||||||
|
probe.Metadata["audio_channels"] = int64(stream.Channels)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return probe, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func durationMillis(value string) int64 {
|
||||||
|
seconds, err := strconv.ParseFloat(value, 64)
|
||||||
|
if err != nil || seconds <= 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return int64(seconds * 1000)
|
||||||
|
}
|
||||||
|
|
||||||
|
func positiveInt(value string) int64 {
|
||||||
|
parsed, err := strconv.ParseInt(value, 10, 64)
|
||||||
|
if err != nil || parsed <= 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return parsed
|
||||||
|
}
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
package ingest
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFFprobeProber(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
script := filepath.Join(dir, "ffprobe")
|
||||||
|
scriptContent := `#!/bin/sh
|
||||||
|
cat <<'JSON'
|
||||||
|
{"format":{"format_name":"matroska,webm","duration":"12.345","bit_rate":"800000"},"streams":[{"codec_type":"video","codec_name":"h264","width":1920,"height":1080,"bit_rate":"700000"},{"codec_type":"audio","codec_name":"aac","channels":2}]}
|
||||||
|
JSON
|
||||||
|
`
|
||||||
|
if err := os.WriteFile(script, []byte(scriptContent), 0o755); err != nil {
|
||||||
|
t.Fatalf("WriteFile(script) : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
media := filepath.Join(dir, "film.mkv")
|
||||||
|
if err := os.WriteFile(media, []byte("x"), 0o644); err != nil {
|
||||||
|
t.Fatalf("WriteFile(media) : %v", err)
|
||||||
|
}
|
||||||
|
info, err := os.Stat(media)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Stat : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
probe, err := FFprobeProber{Binary: script}.Probe(context.Background(), media, info)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Probe : %v", err)
|
||||||
|
}
|
||||||
|
if probe.Mime == "" {
|
||||||
|
t.Fatal("mime vide")
|
||||||
|
}
|
||||||
|
|
||||||
|
expected := map[string]int64{
|
||||||
|
"duration_ms": 12345,
|
||||||
|
"bit_rate": 800000,
|
||||||
|
"width": 1920,
|
||||||
|
"height": 1080,
|
||||||
|
"video_bit_rate": 700000,
|
||||||
|
"audio_channels": 2,
|
||||||
|
}
|
||||||
|
for key, value := range expected {
|
||||||
|
actual, ok := probe.Metadata[key].(int64)
|
||||||
|
if !ok || actual != value {
|
||||||
|
t.Fatalf("metadata[%q] = %v, attendu %d", key, probe.Metadata[key], value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if probe.Metadata["container"] != "matroska,webm" {
|
||||||
|
t.Fatalf("container = %v", probe.Metadata["container"])
|
||||||
|
}
|
||||||
|
if probe.Metadata["video_codec"] != "h264" {
|
||||||
|
t.Fatalf("video_codec = %v", probe.Metadata["video_codec"])
|
||||||
|
}
|
||||||
|
if probe.Metadata["audio_codec"] != "aac" {
|
||||||
|
t.Fatalf("audio_codec = %v", probe.Metadata["audio_codec"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFFprobeProberError(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
script := filepath.Join(dir, "ffprobe")
|
||||||
|
if err := os.WriteFile(script, []byte("#!/bin/sh\necho erreur >&2\nexit 1\n"), 0o755); err != nil {
|
||||||
|
t.Fatalf("WriteFile(script) : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
media := filepath.Join(dir, "film.mkv")
|
||||||
|
if err := os.WriteFile(media, []byte("x"), 0o644); err != nil {
|
||||||
|
t.Fatalf("WriteFile(media) : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := (FFprobeProber{Binary: script}).Probe(context.Background(), media, nil); err == nil {
|
||||||
|
t.Fatal("Probe avec ffprobe en échec : erreur attendue")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,169 @@
|
|||||||
|
package ingest
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
|
"mime"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"mediarr/content"
|
||||||
|
"mediarr/document"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
KindVideo = document.KindVideo
|
||||||
|
KindSubtitle = document.KindSubtitle
|
||||||
|
KindImage = document.KindImage
|
||||||
|
KindFile = document.KindFile
|
||||||
|
)
|
||||||
|
|
||||||
|
type Probe struct {
|
||||||
|
Mime string
|
||||||
|
Metadata map[string]any
|
||||||
|
}
|
||||||
|
|
||||||
|
type Prober interface {
|
||||||
|
Probe(ctx context.Context, path string, info fs.FileInfo) (Probe, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type BasicProber struct{}
|
||||||
|
|
||||||
|
func (BasicProber) Probe(_ context.Context, path string, _ fs.FileInfo) (Probe, error) {
|
||||||
|
return Probe{Mime: mimeForPath(path)}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func mimeForPath(path string) string {
|
||||||
|
mimeType := mime.TypeByExtension(strings.ToLower(filepath.Ext(path)))
|
||||||
|
if mimeType == "" {
|
||||||
|
mimeType = "application/octet-stream"
|
||||||
|
}
|
||||||
|
return mimeType
|
||||||
|
}
|
||||||
|
|
||||||
|
type Service struct {
|
||||||
|
documents *document.Service
|
||||||
|
contentService *content.Service
|
||||||
|
prober Prober
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewService(documents *document.Service, contentService *content.Service, prober Prober) *Service {
|
||||||
|
if prober == nil {
|
||||||
|
prober = BasicProber{}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Service{
|
||||||
|
documents: documents,
|
||||||
|
contentService: contentService,
|
||||||
|
prober: prober,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type ScanResult struct {
|
||||||
|
Files int
|
||||||
|
Directories int
|
||||||
|
Created int
|
||||||
|
Updated int
|
||||||
|
Unchanged int
|
||||||
|
Pruned int
|
||||||
|
Errors []ScanError
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r ScanResult) HasErrors() bool {
|
||||||
|
return len(r.Errors) > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
type ScanError struct {
|
||||||
|
Path string
|
||||||
|
Err error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e ScanError) Error() string {
|
||||||
|
return fmt.Sprintf("%s: %v", e.Path, e.Err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e ScanError) Unwrap() error {
|
||||||
|
return e.Err
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrServiceNotConfigured = errors.New("service d'ingestion non configuré")
|
||||||
|
ErrInvalidRoot = errors.New("racine de scan invalide")
|
||||||
|
ErrNotFolder = errors.New("document existant n'est pas un dossier")
|
||||||
|
ErrNotFile = errors.New("document existant n'est pas un fichier")
|
||||||
|
ErrNameConflict = errors.New("conflit de nom insensible à la casse")
|
||||||
|
)
|
||||||
|
|
||||||
|
func KindForFile(name string) string {
|
||||||
|
switch strings.ToLower(filepath.Ext(name)) {
|
||||||
|
case ".mkv", ".mp4", ".m4v", ".avi", ".mov", ".webm", ".ts", ".m2ts", ".wmv", ".flv", ".mpg", ".mpeg":
|
||||||
|
return KindVideo
|
||||||
|
case ".srt", ".ass", ".ssa", ".vtt", ".sub":
|
||||||
|
return KindSubtitle
|
||||||
|
case ".jpg", ".jpeg", ".png", ".webp", ".gif", ".bmp", ".tif", ".tiff", ".heic", ".avif":
|
||||||
|
return KindImage
|
||||||
|
default:
|
||||||
|
return KindFile
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func metadataJSON(existing string, updates map[string]any) (string, error) {
|
||||||
|
values := make(map[string]any)
|
||||||
|
if existing != "" {
|
||||||
|
if err := json.Unmarshal([]byte(existing), &values); err != nil {
|
||||||
|
return "", fmt.Errorf("%w: %v", document.ErrInvalidMetadata, err)
|
||||||
|
}
|
||||||
|
if values == nil {
|
||||||
|
values = make(map[string]any)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for key, value := range updates {
|
||||||
|
values[key] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
encoded, err := json.Marshal(values)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(encoded), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func hasSourcePath(metadata, root string) bool {
|
||||||
|
var values map[string]any
|
||||||
|
if err := json.Unmarshal([]byte(metadata), &values); err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
sourceRoot, _ := values["source_root"].(string)
|
||||||
|
sourcePath, ok := values["source_path"].(string)
|
||||||
|
return ok && sourcePath != "" && sourceRoot == root
|
||||||
|
}
|
||||||
|
|
||||||
|
func fileMetadata(existing, root, relPath string, info fs.FileInfo, probe Probe) (string, error) {
|
||||||
|
updates := map[string]any{
|
||||||
|
"source_root": root,
|
||||||
|
"source_path": relPath,
|
||||||
|
"size": info.Size(),
|
||||||
|
"mtime": info.ModTime().UnixMilli(),
|
||||||
|
}
|
||||||
|
if probe.Mime != "" {
|
||||||
|
updates["mime"] = probe.Mime
|
||||||
|
}
|
||||||
|
for key, value := range probe.Metadata {
|
||||||
|
updates[key] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
return metadataJSON(existing, updates)
|
||||||
|
}
|
||||||
|
|
||||||
|
func directoryMetadata(existing, root, relPath string) (string, error) {
|
||||||
|
return metadataJSON(existing, map[string]any{
|
||||||
|
"source_root": root,
|
||||||
|
"source_path": relPath,
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
package ingest
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"mediarr/document"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestKindForFile(t *testing.T) {
|
||||||
|
cases := map[string]string{
|
||||||
|
"film.mkv": document.KindVideo,
|
||||||
|
"film.MP4": document.KindVideo,
|
||||||
|
"episode.srt": document.KindSubtitle,
|
||||||
|
"poster.JPG": document.KindImage,
|
||||||
|
"notes.txt": document.KindFile,
|
||||||
|
"sans-extension": document.KindFile,
|
||||||
|
"archive.tar.gz": document.KindFile,
|
||||||
|
}
|
||||||
|
for name, expected := range cases {
|
||||||
|
if kind := KindForFile(name); kind != expected {
|
||||||
|
t.Fatalf("KindForFile(%q) = %q, attendu %q", name, kind, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBasicProber(t *testing.T) {
|
||||||
|
t.Chdir(t.TempDir())
|
||||||
|
path := filepath.Join("film.mkv")
|
||||||
|
if err := os.WriteFile(path, []byte("x"), 0o644); err != nil {
|
||||||
|
t.Fatalf("WriteFile : %v", err)
|
||||||
|
}
|
||||||
|
info, err := os.Stat(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Stat : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
probe, err := BasicProber{}.Probe(context.Background(), path, info)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Probe : %v", err)
|
||||||
|
}
|
||||||
|
if probe.Mime == "" {
|
||||||
|
t.Fatal("mime vide")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMetadataJSONPreservesExistingFields(t *testing.T) {
|
||||||
|
metadata, err := metadataJSON(`{"genre":"drame","size":1}`, map[string]any{
|
||||||
|
"source_path": "film.mkv",
|
||||||
|
"size": int64(2),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("metadataJSON : %v", err)
|
||||||
|
}
|
||||||
|
if metadata != `{"genre":"drame","size":2,"source_path":"film.mkv"}` {
|
||||||
|
t.Fatalf("metadata = %q", metadata)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := metadataJSON(`{`, map[string]any{"x": 1}); err == nil {
|
||||||
|
t.Fatal("metadataJSON avec JSON invalide : erreur attendue")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestScanRequiresServices(t *testing.T) {
|
||||||
|
var service *Service
|
||||||
|
if _, err := service.Scan(context.Background(), [16]byte{}, [16]byte{}, "."); err != ErrServiceNotConfigured {
|
||||||
|
t.Fatalf("erreur = %v, attendu ErrServiceNotConfigured", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
+517
@@ -0,0 +1,517 @@
|
|||||||
|
package ingest
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"crypto/sha256"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"io/fs"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"mediarr/content"
|
||||||
|
"mediarr/document"
|
||||||
|
"mediarr/ident"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ScanOptions struct {
|
||||||
|
Prune bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) Scan(ctx context.Context, actorID, parentID ident.UUID, rootPath string) (ScanResult, error) {
|
||||||
|
return s.ScanWithOptions(ctx, actorID, parentID, rootPath, ScanOptions{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) ScanWithOptions(ctx context.Context, actorID, parentID ident.UUID, rootPath string, options ScanOptions) (ScanResult, error) {
|
||||||
|
absolute, parent, info, err := s.prepare(ctx, actorID, parentID, rootPath)
|
||||||
|
if err != nil {
|
||||||
|
return ScanResult{}, err
|
||||||
|
}
|
||||||
|
if !info.IsDir() {
|
||||||
|
return ScanResult{}, fmt.Errorf("%w: %s n'est pas un dossier", ErrInvalidRoot, rootPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.scanTree(ctx, actorID, parent, absolute, options)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) Import(ctx context.Context, actorID, parentID ident.UUID, rootPath string) (ScanResult, error) {
|
||||||
|
return s.ImportWithOptions(ctx, actorID, parentID, rootPath, ScanOptions{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) ImportWithOptions(ctx context.Context, actorID, parentID ident.UUID, rootPath string, options ScanOptions) (ScanResult, error) {
|
||||||
|
absolute, parent, info, err := s.prepare(ctx, actorID, parentID, rootPath)
|
||||||
|
if err != nil {
|
||||||
|
return ScanResult{}, err
|
||||||
|
}
|
||||||
|
if info.IsDir() {
|
||||||
|
return s.scanTree(ctx, actorID, parent, absolute, options)
|
||||||
|
}
|
||||||
|
if !info.Mode().IsRegular() {
|
||||||
|
return ScanResult{}, fmt.Errorf("%w: %s n'est ni un fichier ni un dossier", ErrInvalidRoot, rootPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.importFile(ctx, actorID, parent, absolute)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) prepare(ctx context.Context, actorID, parentID ident.UUID, rootPath string) (string, *document.Document, fs.FileInfo, error) {
|
||||||
|
if s == nil || s.documents == nil || s.contentService == nil {
|
||||||
|
return "", nil, nil, ErrServiceNotConfigured
|
||||||
|
}
|
||||||
|
|
||||||
|
rootPath = strings.TrimSpace(rootPath)
|
||||||
|
if rootPath == "" {
|
||||||
|
return "", nil, nil, fmt.Errorf("%w: chemin vide", ErrInvalidRoot)
|
||||||
|
}
|
||||||
|
|
||||||
|
absolute, err := filepath.Abs(rootPath)
|
||||||
|
if err != nil {
|
||||||
|
return "", nil, nil, fmt.Errorf("%w: %v", ErrInvalidRoot, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
parent, err := s.documents.Get(ctx, actorID, parentID)
|
||||||
|
if err != nil {
|
||||||
|
return "", nil, nil, err
|
||||||
|
}
|
||||||
|
if parent.DeletedAt != nil {
|
||||||
|
return "", nil, nil, fmt.Errorf("%w: %s", document.ErrDocumentDeleted, parentID)
|
||||||
|
}
|
||||||
|
if parent.Kind != document.KindLibrary && parent.Kind != document.KindFolder {
|
||||||
|
return "", nil, nil, fmt.Errorf("%w: %s", document.ErrInvalidParent, parent.Kind)
|
||||||
|
}
|
||||||
|
|
||||||
|
info, err := os.Stat(absolute)
|
||||||
|
if err != nil {
|
||||||
|
return "", nil, nil, fmt.Errorf("%w: %s: %v", ErrInvalidRoot, rootPath, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return absolute, parent, info, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) scanTree(ctx context.Context, actorID ident.UUID, parent *document.Document, absolute string, options ScanOptions) (ScanResult, error) {
|
||||||
|
var result ScanResult
|
||||||
|
|
||||||
|
rootChildren, err := s.children(ctx, actorID, parent.ID)
|
||||||
|
if err != nil {
|
||||||
|
return result, err
|
||||||
|
}
|
||||||
|
|
||||||
|
directories := map[string]*document.Document{".": parent}
|
||||||
|
children := map[ident.UUID]map[string]*document.Document{parent.ID: rootChildren}
|
||||||
|
seen := make(map[ident.UUID]struct{})
|
||||||
|
|
||||||
|
walkErr := filepath.WalkDir(absolute, func(fullPath string, entry fs.DirEntry, walkErr error) error {
|
||||||
|
if walkErr != nil {
|
||||||
|
result.Errors = append(result.Errors, ScanError{Path: fullPath, Err: walkErr})
|
||||||
|
if entry != nil && entry.IsDir() {
|
||||||
|
return fs.SkipDir
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if fullPath == absolute {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
relPath, err := filepath.Rel(absolute, fullPath)
|
||||||
|
if err != nil {
|
||||||
|
result.Errors = append(result.Errors, ScanError{Path: fullPath, Err: err})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
relPath = filepath.ToSlash(relPath)
|
||||||
|
|
||||||
|
if entry.Type()&fs.ModeSymlink != 0 {
|
||||||
|
s.markPresent(ctx, actorID, relPath, entry.Name(), directories, children, seen)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if entry.IsDir() {
|
||||||
|
result.Directories++
|
||||||
|
_, created, err := s.scanDirectory(ctx, actorID, absolute, relPath, entry.Name(), directories, children, seen)
|
||||||
|
if err != nil {
|
||||||
|
result.Errors = append(result.Errors, ScanError{Path: fullPath, Err: err})
|
||||||
|
return fs.SkipDir
|
||||||
|
}
|
||||||
|
if created {
|
||||||
|
result.Created++
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if !entry.Type().IsRegular() {
|
||||||
|
s.markPresent(ctx, actorID, relPath, entry.Name(), directories, children, seen)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
result.Files++
|
||||||
|
created, updated, err := s.scanFile(ctx, actorID, absolute, fullPath, relPath, entry.Name(), directories, children, seen)
|
||||||
|
if err != nil {
|
||||||
|
result.Errors = append(result.Errors, ScanError{Path: fullPath, Err: err})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
switch {
|
||||||
|
case created:
|
||||||
|
result.Created++
|
||||||
|
case updated:
|
||||||
|
result.Updated++
|
||||||
|
default:
|
||||||
|
result.Unchanged++
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if walkErr != nil {
|
||||||
|
return result, walkErr
|
||||||
|
}
|
||||||
|
|
||||||
|
if options.Prune && len(result.Errors) == 0 {
|
||||||
|
pruned, errs := s.prune(ctx, actorID, absolute, directories, children, seen)
|
||||||
|
result.Pruned = pruned
|
||||||
|
result.Errors = append(result.Errors, errs...)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) importFile(ctx context.Context, actorID ident.UUID, parent *document.Document, absolute string) (ScanResult, error) {
|
||||||
|
var result ScanResult
|
||||||
|
|
||||||
|
rootChildren, err := s.children(ctx, actorID, parent.ID)
|
||||||
|
if err != nil {
|
||||||
|
return result, err
|
||||||
|
}
|
||||||
|
|
||||||
|
directories := map[string]*document.Document{".": parent}
|
||||||
|
children := map[ident.UUID]map[string]*document.Document{parent.ID: rootChildren}
|
||||||
|
seen := make(map[ident.UUID]struct{})
|
||||||
|
|
||||||
|
name := filepath.Base(absolute)
|
||||||
|
result.Files = 1
|
||||||
|
|
||||||
|
created, updated, err := s.scanFile(ctx, actorID, filepath.Dir(absolute), absolute, name, name, directories, children, seen)
|
||||||
|
if err != nil {
|
||||||
|
return result, err
|
||||||
|
}
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case created:
|
||||||
|
result.Created = 1
|
||||||
|
case updated:
|
||||||
|
result.Updated = 1
|
||||||
|
default:
|
||||||
|
result.Unchanged = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) scanDirectory(
|
||||||
|
ctx context.Context,
|
||||||
|
actorID ident.UUID,
|
||||||
|
root, relPath, name string,
|
||||||
|
directories map[string]*document.Document,
|
||||||
|
children map[ident.UUID]map[string]*document.Document,
|
||||||
|
seen map[ident.UUID]struct{},
|
||||||
|
) (*document.Document, bool, error) {
|
||||||
|
parentRel := path.Dir(relPath)
|
||||||
|
parent := directories[parentRel]
|
||||||
|
if parent == nil {
|
||||||
|
return nil, false, fmt.Errorf("%w: parent introuvable pour %s", ErrInvalidRoot, relPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
siblings, err := s.siblings(ctx, actorID, parent, children)
|
||||||
|
if err != nil {
|
||||||
|
return nil, false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
nameKey := strings.ToLower(name)
|
||||||
|
existing := siblings[nameKey]
|
||||||
|
if existing != nil {
|
||||||
|
seen[existing.ID] = struct{}{}
|
||||||
|
if existing.Name != name {
|
||||||
|
return nil, false, fmt.Errorf("%w: %s", ErrNameConflict, relPath)
|
||||||
|
}
|
||||||
|
if existing.Kind != document.KindFolder {
|
||||||
|
return nil, false, fmt.Errorf("%w: %s", ErrNotFolder, relPath)
|
||||||
|
}
|
||||||
|
directories[relPath] = existing
|
||||||
|
return existing, false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
metadata, err := directoryMetadata("", root, relPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
created, err := s.documents.CreateChild(ctx, actorID, document.CreateChildParams{
|
||||||
|
Kind: document.KindFolder,
|
||||||
|
OwnerID: actorID,
|
||||||
|
CreatedBy: actorID,
|
||||||
|
ParentID: parent.ID,
|
||||||
|
Name: name,
|
||||||
|
Metadata: metadata,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
siblings[nameKey] = created
|
||||||
|
seen[created.ID] = struct{}{}
|
||||||
|
directories[relPath] = created
|
||||||
|
return created, true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) scanFile(
|
||||||
|
ctx context.Context,
|
||||||
|
actorID ident.UUID,
|
||||||
|
root, fullPath, relPath, name string,
|
||||||
|
directories map[string]*document.Document,
|
||||||
|
children map[ident.UUID]map[string]*document.Document,
|
||||||
|
seen map[ident.UUID]struct{},
|
||||||
|
) (bool, bool, error) {
|
||||||
|
parentRel := path.Dir(relPath)
|
||||||
|
parent := directories[parentRel]
|
||||||
|
if parent == nil {
|
||||||
|
return false, false, fmt.Errorf("%w: parent introuvable pour %s", ErrInvalidRoot, relPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
siblings, err := s.siblings(ctx, actorID, parent, children)
|
||||||
|
if err != nil {
|
||||||
|
return false, false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
nameKey := strings.ToLower(name)
|
||||||
|
existing := siblings[nameKey]
|
||||||
|
if existing != nil {
|
||||||
|
seen[existing.ID] = struct{}{}
|
||||||
|
if existing.Name != name {
|
||||||
|
return false, false, fmt.Errorf("%w: %s", ErrNameConflict, relPath)
|
||||||
|
}
|
||||||
|
if existing.Kind == document.KindFolder || existing.Kind == document.KindLibrary {
|
||||||
|
return false, false, fmt.Errorf("%w: %s", ErrNotFile, relPath)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
info, err := os.Stat(fullPath)
|
||||||
|
if err != nil {
|
||||||
|
return false, false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
probe, err := s.prober.Probe(ctx, fullPath, info)
|
||||||
|
if err != nil {
|
||||||
|
return false, false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
existingMetadata := ""
|
||||||
|
if existing != nil {
|
||||||
|
existingMetadata = existing.Metadata
|
||||||
|
}
|
||||||
|
metadata, err := fileMetadata(existingMetadata, root, relPath, info, probe)
|
||||||
|
if err != nil {
|
||||||
|
return false, false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
created := false
|
||||||
|
current := existing
|
||||||
|
if current == nil {
|
||||||
|
current, err = s.documents.CreateChild(ctx, actorID, document.CreateChildParams{
|
||||||
|
Kind: KindForFile(name),
|
||||||
|
OwnerID: actorID,
|
||||||
|
CreatedBy: actorID,
|
||||||
|
ParentID: parent.ID,
|
||||||
|
Name: name,
|
||||||
|
Metadata: metadata,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return false, false, err
|
||||||
|
}
|
||||||
|
created = true
|
||||||
|
siblings[nameKey] = current
|
||||||
|
} else if current.Metadata != metadata {
|
||||||
|
current, err = s.documents.SetMetadata(ctx, actorID, current.ID, metadata)
|
||||||
|
if err != nil {
|
||||||
|
return false, false, err
|
||||||
|
}
|
||||||
|
siblings[nameKey] = current
|
||||||
|
}
|
||||||
|
seen[current.ID] = struct{}{}
|
||||||
|
|
||||||
|
contentRoot, size, err := hashFile(fullPath)
|
||||||
|
if err != nil {
|
||||||
|
return false, false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
head, err := s.contentService.HeadVersion(ctx, actorID, current.ID)
|
||||||
|
if err != nil {
|
||||||
|
return false, false, err
|
||||||
|
}
|
||||||
|
if head != nil && head.Size == size && bytes.Equal(head.ContentRoot, contentRoot) {
|
||||||
|
return created, false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
file, err := os.Open(fullPath)
|
||||||
|
if err != nil {
|
||||||
|
return false, false, err
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
_ = file.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
if _, err := s.contentService.CreateVersion(ctx, actorID, current.ID, content.CreateVersionParams{
|
||||||
|
Source: file,
|
||||||
|
Mime: probe.Mime,
|
||||||
|
Origin: content.OriginImport,
|
||||||
|
Message: "scan",
|
||||||
|
}); err != nil {
|
||||||
|
return false, false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return created, true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) markPresent(
|
||||||
|
ctx context.Context,
|
||||||
|
actorID ident.UUID,
|
||||||
|
relPath, name string,
|
||||||
|
directories map[string]*document.Document,
|
||||||
|
children map[ident.UUID]map[string]*document.Document,
|
||||||
|
seen map[ident.UUID]struct{},
|
||||||
|
) {
|
||||||
|
parent := directories[path.Dir(relPath)]
|
||||||
|
if parent == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
siblings, err := s.siblings(ctx, actorID, parent, children)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if existing := siblings[strings.ToLower(name)]; existing != nil {
|
||||||
|
seen[existing.ID] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) prune(
|
||||||
|
ctx context.Context,
|
||||||
|
actorID ident.UUID,
|
||||||
|
root string,
|
||||||
|
directories map[string]*document.Document,
|
||||||
|
children map[ident.UUID]map[string]*document.Document,
|
||||||
|
seen map[ident.UUID]struct{},
|
||||||
|
) (int, []ScanError) {
|
||||||
|
var pruned int
|
||||||
|
var errs []ScanError
|
||||||
|
visited := make(map[ident.UUID]struct{})
|
||||||
|
prunable := make(map[ident.UUID]bool)
|
||||||
|
|
||||||
|
for relPath, parent := range directories {
|
||||||
|
siblings, err := s.siblings(ctx, actorID, parent, children)
|
||||||
|
if err != nil {
|
||||||
|
errs = append(errs, ScanError{Path: relPath, Err: err})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, child := range siblings {
|
||||||
|
if _, ok := seen[child.ID]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if _, ok := visited[child.ID]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !s.prunable(ctx, actorID, child, root, prunable) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := s.documents.Trash(ctx, actorID, child.ID); err != nil {
|
||||||
|
errs = append(errs, ScanError{Path: relPath, Err: err})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
visited[child.ID] = struct{}{}
|
||||||
|
pruned++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return pruned, errs
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) prunable(ctx context.Context, actorID ident.UUID, doc *document.Document, root string, cache map[ident.UUID]bool) bool {
|
||||||
|
if allowed, exists := cache[doc.ID]; exists {
|
||||||
|
return allowed
|
||||||
|
}
|
||||||
|
|
||||||
|
if !hasSourcePath(doc.Metadata, root) {
|
||||||
|
cache[doc.ID] = false
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if doc.Kind != document.KindFolder {
|
||||||
|
cache[doc.ID] = true
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
children, err := s.documents.ListChildren(ctx, actorID, doc.ID)
|
||||||
|
if err != nil {
|
||||||
|
cache[doc.ID] = false
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for i := range children {
|
||||||
|
if !s.prunable(ctx, actorID, &children[i], root, cache) {
|
||||||
|
cache[doc.ID] = false
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cache[doc.ID] = true
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) siblings(
|
||||||
|
ctx context.Context,
|
||||||
|
actorID ident.UUID,
|
||||||
|
parent *document.Document,
|
||||||
|
children map[ident.UUID]map[string]*document.Document,
|
||||||
|
) (map[string]*document.Document, error) {
|
||||||
|
if siblings, exists := children[parent.ID]; exists {
|
||||||
|
return siblings, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
siblings, err := s.children(ctx, actorID, parent.ID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
children[parent.ID] = siblings
|
||||||
|
|
||||||
|
return siblings, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) children(ctx context.Context, actorID, parentID ident.UUID) (map[string]*document.Document, error) {
|
||||||
|
rows, err := s.documents.ListChildren(ctx, actorID, parentID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
result := make(map[string]*document.Document, len(rows))
|
||||||
|
for i := range rows {
|
||||||
|
result[rows[i].NameKey] = &rows[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func hashFile(filePath string) ([]byte, int64, error) {
|
||||||
|
file, err := os.Open(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
_ = file.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
hash := sha256.New()
|
||||||
|
size, err := io.Copy(hash, file)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return hash.Sum(nil), size, nil
|
||||||
|
}
|
||||||
@@ -1,25 +1,56 @@
|
|||||||
package node
|
package node
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"mediarr/acl"
|
||||||
"mediarr/auth"
|
"mediarr/auth"
|
||||||
"mediarr/cluster"
|
"mediarr/cluster"
|
||||||
"mediarr/database"
|
"mediarr/database"
|
||||||
|
"mediarr/ident"
|
||||||
"mediarr/identity"
|
"mediarr/identity"
|
||||||
|
"mediarr/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const defaultStorageBackendID = "00000000-0000-7000-8000-000000000201"
|
||||||
|
|
||||||
type Node struct {
|
type Node struct {
|
||||||
db database.DatabaseClient
|
db database.DatabaseClient
|
||||||
clusterKeyPath string
|
clusterKeyPath string
|
||||||
cluster cluster.Identity
|
cluster cluster.Identity
|
||||||
|
storagePath string
|
||||||
|
store storage.Store
|
||||||
|
backendID ident.UUID
|
||||||
}
|
}
|
||||||
|
|
||||||
type NewNodeParams struct {
|
type NewNodeParams struct {
|
||||||
DatabaseName string
|
DatabaseName string
|
||||||
ClusterKeyPath string
|
ClusterKeyPath string
|
||||||
|
StoragePath string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNode(params NewNodeParams) (error, Node) {
|
func NewNode(params NewNodeParams) (error, Node) {
|
||||||
|
|
||||||
|
storagePath := params.StoragePath
|
||||||
|
if storagePath == "" {
|
||||||
|
storagePath = "data"
|
||||||
|
}
|
||||||
|
|
||||||
|
absoluteStoragePath, err := filepath.Abs(storagePath)
|
||||||
|
if err != nil {
|
||||||
|
return err, Node{}
|
||||||
|
}
|
||||||
|
|
||||||
|
store, err := storage.NewLocal(absoluteStoragePath)
|
||||||
|
if err != nil {
|
||||||
|
return err, Node{}
|
||||||
|
}
|
||||||
|
|
||||||
|
backendID, err := ident.Parse(defaultStorageBackendID)
|
||||||
|
if err != nil {
|
||||||
|
return err, Node{}
|
||||||
|
}
|
||||||
|
|
||||||
var databaseName *string
|
var databaseName *string
|
||||||
if params.DatabaseName != "" {
|
if params.DatabaseName != "" {
|
||||||
databaseName = ¶ms.DatabaseName
|
databaseName = ¶ms.DatabaseName
|
||||||
@@ -41,6 +72,9 @@ func NewNode(params NewNodeParams) (error, Node) {
|
|||||||
return nil, Node{
|
return nil, Node{
|
||||||
db: *db,
|
db: *db,
|
||||||
clusterKeyPath: clusterKeyPath,
|
clusterKeyPath: clusterKeyPath,
|
||||||
|
storagePath: absoluteStoragePath,
|
||||||
|
store: store,
|
||||||
|
backendID: backendID,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,3 +93,7 @@ func (n *Node) Identity() *identity.Service {
|
|||||||
func (n *Node) Auth() *auth.Service {
|
func (n *Node) Auth() *auth.Service {
|
||||||
return auth.NewService(n.db)
|
return auth.NewService(n.db)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n *Node) ACL() *acl.Service {
|
||||||
|
return acl.NewService(n.db)
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package node
|
||||||
|
|
||||||
|
import "mediarr/content"
|
||||||
|
|
||||||
|
func (n *Node) Content() *content.Service {
|
||||||
|
return content.NewService(n.db, n.store, n.backendID, n.ACL())
|
||||||
|
}
|
||||||
@@ -0,0 +1,177 @@
|
|||||||
|
package node
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/hex"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"mediarr/content"
|
||||||
|
"mediarr/database"
|
||||||
|
"mediarr/document"
|
||||||
|
"mediarr/ident"
|
||||||
|
"mediarr/identity"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNodeContent(t *testing.T) {
|
||||||
|
t.Chdir(t.TempDir())
|
||||||
|
|
||||||
|
err, node := NewNode(NewNodeParams{DatabaseName: "content", StoragePath: "store"})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewNode : %v", err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() {
|
||||||
|
if err := node.Close(); err != nil {
|
||||||
|
t.Errorf("Close : %v", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if err := node.Up(); err != nil {
|
||||||
|
t.Fatalf("Up : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
owner, err := node.Identity().CreatePrincipal(ctx, identity.CreatePrincipalParams{
|
||||||
|
Kind: identity.KindUser,
|
||||||
|
Name: "owner",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreatePrincipal(owner) : %v", err)
|
||||||
|
}
|
||||||
|
other, err := node.Identity().CreatePrincipal(ctx, identity.CreatePrincipalParams{
|
||||||
|
Kind: identity.KindUser,
|
||||||
|
Name: "other",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreatePrincipal(other) : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
library, err := node.Documents().CreateLibrary(ctx, owner.ID, document.CreateLibraryParams{
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
CreatedBy: owner.ID,
|
||||||
|
Name: "Films",
|
||||||
|
PolicyID: ident.MustParse("00000000-0000-7000-8000-000000000101"),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreateLibrary : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
videoID := createNodeContentDocument(t, &node, owner, library, "video", "film.mkv")
|
||||||
|
|
||||||
|
payload := []byte("contenu de test mediarr")
|
||||||
|
version, err := node.Content().CreateVersion(ctx, owner.ID, videoID, content.CreateVersionParams{
|
||||||
|
Source: bytes.NewReader(payload),
|
||||||
|
Mime: "video/x-matroska",
|
||||||
|
Message: "première version",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreateVersion : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedRoot := sha256.Sum256(payload)
|
||||||
|
if version.Seq != 1 {
|
||||||
|
t.Fatalf("seq = %d, attendu 1", version.Seq)
|
||||||
|
}
|
||||||
|
if version.Size != int64(len(payload)) {
|
||||||
|
t.Fatalf("size = %d, attendu %d", version.Size, len(payload))
|
||||||
|
}
|
||||||
|
if !bytes.Equal(version.ContentRoot, expectedRoot[:]) {
|
||||||
|
t.Fatalf("content_root = %x, attendu %x", version.ContentRoot, expectedRoot)
|
||||||
|
}
|
||||||
|
|
||||||
|
documentRow, err := node.Documents().Get(ctx, owner.ID, videoID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Get(video) : %v", err)
|
||||||
|
}
|
||||||
|
if documentRow.HeadVersionID == nil || *documentRow.HeadVersionID != version.ID {
|
||||||
|
t.Fatalf("head_version_id = %v, attendu %s", documentRow.HeadVersionID, version.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
reader, opened, err := node.Content().OpenVersion(ctx, owner.ID, version.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("OpenVersion : %v", err)
|
||||||
|
}
|
||||||
|
data, err := io.ReadAll(reader)
|
||||||
|
if err != nil {
|
||||||
|
_ = reader.Close()
|
||||||
|
t.Fatalf("ReadAll : %v", err)
|
||||||
|
}
|
||||||
|
if err := reader.Close(); err != nil {
|
||||||
|
t.Fatalf("Close(reader) : %v", err)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(data, payload) {
|
||||||
|
t.Fatalf("contenu = %q, attendu %q", data, payload)
|
||||||
|
}
|
||||||
|
if opened.ID != version.ID {
|
||||||
|
t.Fatalf("version ouverte = %s, attendu %s", opened.ID, version.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
second, err := node.Content().CreateVersion(ctx, owner.ID, videoID, content.CreateVersionParams{
|
||||||
|
Source: bytes.NewReader(payload),
|
||||||
|
Mime: "video/x-matroska",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreateVersion(doublon) : %v", err)
|
||||||
|
}
|
||||||
|
if second.Seq != 2 {
|
||||||
|
t.Fatalf("seq = %d, attendu 2", second.Seq)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(second.ContentRoot, version.ContentRoot) {
|
||||||
|
t.Fatalf("doublon : content_root = %x, attendu %x", second.ContentRoot, version.ContentRoot)
|
||||||
|
}
|
||||||
|
|
||||||
|
versions, err := node.Content().ListVersions(ctx, owner.ID, videoID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ListVersions : %v", err)
|
||||||
|
}
|
||||||
|
if len(versions) != 2 {
|
||||||
|
t.Fatalf("versions = %d, attendu 2", len(versions))
|
||||||
|
}
|
||||||
|
if versions[0].ID != second.ID || versions[1].ID != version.ID {
|
||||||
|
t.Fatalf("ordre des versions = %s, %s", versions[0].ID, versions[1].ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, _, err := node.Content().OpenVersion(ctx, other.ID, version.ID); !errors.Is(err, document.ErrPermissionDenied) {
|
||||||
|
t.Fatalf("OpenVersion sans Read : erreur = %v, attendu ErrPermissionDenied", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := node.Content().CreateVersion(ctx, owner.ID, videoID, content.CreateVersionParams{}); !errors.Is(err, content.ErrInvalidSource) {
|
||||||
|
t.Fatalf("CreateVersion sans source : erreur = %v, attendu ErrInvalidSource", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func createNodeContentDocument(t *testing.T, node *Node, owner *identity.Principal, parent *document.Document, kind, name string) ident.UUID {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
id, err := ident.NewV7()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewV7 : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
now := time.Now().UnixMilli()
|
||||||
|
if _, err := node.db.WriteQueries.CreateDocument(context.Background(), database.CreateDocumentParams{
|
||||||
|
ID: id,
|
||||||
|
Kind: kind,
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
CreatedBy: owner.ID,
|
||||||
|
ParentID: &parent.ID,
|
||||||
|
Name: name,
|
||||||
|
NameKey: strings.ToLower(name),
|
||||||
|
Path: parent.Path + hex.EncodeToString(id[:]) + "/",
|
||||||
|
Depth: parent.Depth + 1,
|
||||||
|
Metadata: "{}",
|
||||||
|
AclInherit: 1,
|
||||||
|
Rev: 1,
|
||||||
|
CreatedAt: now,
|
||||||
|
UpdatedAt: now,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("CreateDocument(%q) : %v", name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return id
|
||||||
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
package node
|
package node
|
||||||
|
|
||||||
func (n *Node) AddDocument() {
|
import "mediarr/document"
|
||||||
|
|
||||||
|
func (n *Node) Documents() *document.Service {
|
||||||
|
return document.NewService(n.db, n.ACL())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,165 @@
|
|||||||
|
package node
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"mediarr/acl"
|
||||||
|
"mediarr/document"
|
||||||
|
"mediarr/ident"
|
||||||
|
"mediarr/identity"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNodeDocuments(t *testing.T) {
|
||||||
|
t.Chdir(t.TempDir())
|
||||||
|
|
||||||
|
err, node := NewNode(NewNodeParams{DatabaseName: "documents"})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewNode : %v", err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() {
|
||||||
|
if err := node.Close(); err != nil {
|
||||||
|
t.Errorf("Close : %v", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if err := node.Up(); err != nil {
|
||||||
|
t.Fatalf("Up : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
owner, err := node.Identity().CreatePrincipal(ctx, identity.CreatePrincipalParams{
|
||||||
|
Kind: identity.KindUser,
|
||||||
|
Name: "alice",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreatePrincipal : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
policyID := ident.MustParse("00000000-0000-7000-8000-000000000101")
|
||||||
|
|
||||||
|
library, err := node.Documents().CreateLibrary(ctx, owner.ID, document.CreateLibraryParams{
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
CreatedBy: owner.ID,
|
||||||
|
Name: "Photos",
|
||||||
|
PolicyID: policyID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreateLibrary : %v", err)
|
||||||
|
}
|
||||||
|
if library.Kind != document.KindLibrary {
|
||||||
|
t.Fatalf("kind = %q, attendu %q", library.Kind, document.KindLibrary)
|
||||||
|
}
|
||||||
|
|
||||||
|
folder, err := node.Documents().CreateFolder(ctx, owner.ID, document.CreateFolderParams{
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
CreatedBy: owner.ID,
|
||||||
|
ParentID: library.ID,
|
||||||
|
Name: "Vacances",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreateFolder : %v", err)
|
||||||
|
}
|
||||||
|
if folder.ParentID == nil || *folder.ParentID != library.ID {
|
||||||
|
t.Fatalf("parent = %v, attendu %s", folder.ParentID, library.ID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNodeDocumentsEnforcesACL(t *testing.T) {
|
||||||
|
t.Chdir(t.TempDir())
|
||||||
|
|
||||||
|
err, node := NewNode(NewNodeParams{DatabaseName: "documents_acl"})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewNode : %v", err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() {
|
||||||
|
if err := node.Close(); err != nil {
|
||||||
|
t.Errorf("Close : %v", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if err := node.Up(); err != nil {
|
||||||
|
t.Fatalf("Up : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
owner, err := node.Identity().CreatePrincipal(ctx, identity.CreatePrincipalParams{
|
||||||
|
Kind: identity.KindUser,
|
||||||
|
Name: "owner",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreatePrincipal(owner) : %v", err)
|
||||||
|
}
|
||||||
|
other, err := node.Identity().CreatePrincipal(ctx, identity.CreatePrincipalParams{
|
||||||
|
Kind: identity.KindUser,
|
||||||
|
Name: "other",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreatePrincipal(other) : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
policyID := ident.MustParse("00000000-0000-7000-8000-000000000101")
|
||||||
|
|
||||||
|
library, err := node.Documents().CreateLibrary(ctx, owner.ID, document.CreateLibraryParams{
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
CreatedBy: owner.ID,
|
||||||
|
Name: "Photos",
|
||||||
|
PolicyID: policyID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreateLibrary : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := node.Documents().Get(ctx, other.ID, library.ID); !errors.Is(err, document.ErrPermissionDenied) {
|
||||||
|
t.Fatalf("Get sans ACL : erreur = %v, attendu ErrPermissionDenied", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := node.ACL().Grant(ctx, acl.GrantParams{
|
||||||
|
DocumentID: library.ID,
|
||||||
|
SubjectID: other.ID,
|
||||||
|
Permissions: acl.PermissionRead,
|
||||||
|
GrantedBy: owner.ID,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("Grant(Read) : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := node.Documents().Get(ctx, other.ID, library.ID); err != nil {
|
||||||
|
t.Fatalf("Get avec Read : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
libraries, err := node.Documents().ListLibraries(ctx, other.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ListLibraries : %v", err)
|
||||||
|
}
|
||||||
|
if len(libraries) != 1 || libraries[0].ID != library.ID {
|
||||||
|
t.Fatalf("bibliothèques = %+v", libraries)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := node.Documents().CreateFolder(ctx, other.ID, document.CreateFolderParams{
|
||||||
|
OwnerID: other.ID,
|
||||||
|
CreatedBy: other.ID,
|
||||||
|
ParentID: library.ID,
|
||||||
|
Name: "Vacances",
|
||||||
|
}); !errors.Is(err, document.ErrPermissionDenied) {
|
||||||
|
t.Fatalf("CreateFolder sans Create : erreur = %v, attendu ErrPermissionDenied", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := node.ACL().Grant(ctx, acl.GrantParams{
|
||||||
|
DocumentID: library.ID,
|
||||||
|
SubjectID: other.ID,
|
||||||
|
Permissions: acl.PermissionCreate,
|
||||||
|
GrantedBy: owner.ID,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("Grant(Create) : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := node.Documents().CreateFolder(ctx, other.ID, document.CreateFolderParams{
|
||||||
|
OwnerID: other.ID,
|
||||||
|
CreatedBy: other.ID,
|
||||||
|
ParentID: library.ID,
|
||||||
|
Name: "Vacances",
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("CreateFolder avec Create : %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package node
|
||||||
|
|
||||||
|
import "mediarr/ingest"
|
||||||
|
|
||||||
|
func (n *Node) Ingest() *ingest.Service {
|
||||||
|
return n.IngestWithProber(nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *Node) IngestWithProber(prober ingest.Prober) *ingest.Service {
|
||||||
|
return ingest.NewService(n.Documents(), n.Content(), prober)
|
||||||
|
}
|
||||||
@@ -0,0 +1,317 @@
|
|||||||
|
package node
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"mediarr/content"
|
||||||
|
"mediarr/document"
|
||||||
|
"mediarr/ident"
|
||||||
|
"mediarr/identity"
|
||||||
|
"mediarr/ingest"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNodeIngestScan(t *testing.T) {
|
||||||
|
t.Chdir(t.TempDir())
|
||||||
|
|
||||||
|
err, node := NewNode(NewNodeParams{DatabaseName: "ingest", StoragePath: "store"})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewNode : %v", err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() {
|
||||||
|
if err := node.Close(); err != nil {
|
||||||
|
t.Errorf("Close : %v", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if err := node.Up(); err != nil {
|
||||||
|
t.Fatalf("Up : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
owner, err := node.Identity().CreatePrincipal(ctx, identity.CreatePrincipalParams{
|
||||||
|
Kind: identity.KindUser,
|
||||||
|
Name: "owner",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreatePrincipal(owner) : %v", err)
|
||||||
|
}
|
||||||
|
other, err := node.Identity().CreatePrincipal(ctx, identity.CreatePrincipalParams{
|
||||||
|
Kind: identity.KindUser,
|
||||||
|
Name: "other",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreatePrincipal(other) : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
library, err := node.Documents().CreateLibrary(ctx, owner.ID, document.CreateLibraryParams{
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
CreatedBy: owner.ID,
|
||||||
|
Name: "Films",
|
||||||
|
PolicyID: ident.MustParse("00000000-0000-7000-8000-000000000101"),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreateLibrary : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
source := filepath.Join(t.TempDir(), "library")
|
||||||
|
if err := os.MkdirAll(filepath.Join(source, "Season 1"), 0o755); err != nil {
|
||||||
|
t.Fatalf("MkdirAll : %v", err)
|
||||||
|
}
|
||||||
|
writeNodeTestFile(t, filepath.Join(source, "film.mkv"), "film")
|
||||||
|
writeNodeTestFile(t, filepath.Join(source, "notes.txt"), "notes")
|
||||||
|
writeNodeTestFile(t, filepath.Join(source, "Season 1", "episode.srt"), "subs")
|
||||||
|
|
||||||
|
result, err := node.Ingest().Scan(ctx, owner.ID, library.ID, source)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Scan : %v", err)
|
||||||
|
}
|
||||||
|
if result.HasErrors() {
|
||||||
|
t.Fatalf("Scan erreurs : %v", result.Errors)
|
||||||
|
}
|
||||||
|
if result.Files != 3 || result.Directories != 1 || result.Created != 4 || result.Updated != 0 || result.Unchanged != 0 {
|
||||||
|
t.Fatalf("résultat scan = %+v", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
children, err := node.Documents().ListChildren(ctx, owner.ID, library.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ListChildren(library) : %v", err)
|
||||||
|
}
|
||||||
|
film := findNodeTestChild(t, children, "film.mkv")
|
||||||
|
notes := findNodeTestChild(t, children, "notes.txt")
|
||||||
|
season := findNodeTestChild(t, children, "Season 1")
|
||||||
|
if film.Kind != document.KindVideo {
|
||||||
|
t.Fatalf("kind film = %q, attendu %q", film.Kind, document.KindVideo)
|
||||||
|
}
|
||||||
|
if notes.Kind != document.KindFile {
|
||||||
|
t.Fatalf("kind notes = %q, attendu %q", notes.Kind, document.KindFile)
|
||||||
|
}
|
||||||
|
if season.Kind != document.KindFolder {
|
||||||
|
t.Fatalf("kind saison = %q, attendu %q", season.Kind, document.KindFolder)
|
||||||
|
}
|
||||||
|
if !strings.Contains(film.Metadata, `"source_path":"film.mkv"`) {
|
||||||
|
t.Fatalf("metadata film = %q", film.Metadata)
|
||||||
|
}
|
||||||
|
|
||||||
|
seasonChildren, err := node.Documents().ListChildren(ctx, owner.ID, season.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ListChildren(saison) : %v", err)
|
||||||
|
}
|
||||||
|
episode := findNodeTestChild(t, seasonChildren, "episode.srt")
|
||||||
|
if episode.Kind != document.KindSubtitle {
|
||||||
|
t.Fatalf("kind épisode = %q, attendu %q", episode.Kind, document.KindSubtitle)
|
||||||
|
}
|
||||||
|
|
||||||
|
versions, err := node.Content().ListVersions(ctx, owner.ID, film.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ListVersions(film) : %v", err)
|
||||||
|
}
|
||||||
|
if len(versions) != 1 {
|
||||||
|
t.Fatalf("versions film = %d, attendu 1", len(versions))
|
||||||
|
}
|
||||||
|
if versions[0].Seq != 1 || versions[0].Origin != content.OriginImport {
|
||||||
|
t.Fatalf("version film = %+v", versions[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
head, err := node.Content().HeadVersion(ctx, owner.ID, film.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("HeadVersion(film) : %v", err)
|
||||||
|
}
|
||||||
|
if head == nil || head.ID != versions[0].ID {
|
||||||
|
t.Fatalf("head film = %+v, attendu %s", head, versions[0].ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err = node.Ingest().Scan(ctx, owner.ID, library.ID, source)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Scan(rescan) : %v", err)
|
||||||
|
}
|
||||||
|
if result.HasErrors() {
|
||||||
|
t.Fatalf("Scan(rescan) erreurs : %v", result.Errors)
|
||||||
|
}
|
||||||
|
if result.Created != 0 || result.Updated != 0 || result.Unchanged != 3 {
|
||||||
|
t.Fatalf("résultat rescan = %+v", result)
|
||||||
|
}
|
||||||
|
versions, err = node.Content().ListVersions(ctx, owner.ID, film.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ListVersions(film après rescan) : %v", err)
|
||||||
|
}
|
||||||
|
if len(versions) != 1 {
|
||||||
|
t.Fatalf("versions film après rescan = %d, attendu 1", len(versions))
|
||||||
|
}
|
||||||
|
|
||||||
|
writeNodeTestFile(t, filepath.Join(source, "film.mkv"), "film v2")
|
||||||
|
result, err = node.Ingest().Scan(ctx, owner.ID, library.ID, source)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Scan(modifié) : %v", err)
|
||||||
|
}
|
||||||
|
if result.HasErrors() {
|
||||||
|
t.Fatalf("Scan(modifié) erreurs : %v", result.Errors)
|
||||||
|
}
|
||||||
|
if result.Created != 0 || result.Updated != 1 || result.Unchanged != 2 {
|
||||||
|
t.Fatalf("résultat scan modifié = %+v", result)
|
||||||
|
}
|
||||||
|
versions, err = node.Content().ListVersions(ctx, owner.ID, film.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ListVersions(film modifié) : %v", err)
|
||||||
|
}
|
||||||
|
if len(versions) != 2 {
|
||||||
|
t.Fatalf("versions film modifié = %d, attendu 2", len(versions))
|
||||||
|
}
|
||||||
|
if versions[0].Seq != 2 || versions[0].Origin != content.OriginImport {
|
||||||
|
t.Fatalf("nouvelle version = %+v", versions[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := node.Ingest().Scan(ctx, other.ID, library.ID, source); !errors.Is(err, document.ErrPermissionDenied) {
|
||||||
|
t.Fatalf("Scan sans Read : erreur = %v, attendu ErrPermissionDenied", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeNodeTestFile(t *testing.T, path, content string) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
|
||||||
|
t.Fatalf("WriteFile(%s) : %v", path, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func findNodeTestChild(t *testing.T, children []document.Document, name string) *document.Document {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
for i := range children {
|
||||||
|
if children[i].Name == name {
|
||||||
|
return &children[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Fatalf("enfant %q introuvable dans %+v", name, children)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNodeIngestImportAndPrune(t *testing.T) {
|
||||||
|
t.Chdir(t.TempDir())
|
||||||
|
|
||||||
|
err, node := NewNode(NewNodeParams{DatabaseName: "ingest_prune", StoragePath: "store"})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewNode : %v", err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() {
|
||||||
|
if err := node.Close(); err != nil {
|
||||||
|
t.Errorf("Close : %v", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if err := node.Up(); err != nil {
|
||||||
|
t.Fatalf("Up : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
owner, err := node.Identity().CreatePrincipal(ctx, identity.CreatePrincipalParams{
|
||||||
|
Kind: identity.KindUser,
|
||||||
|
Name: "owner",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreatePrincipal(owner) : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
library, err := node.Documents().CreateLibrary(ctx, owner.ID, document.CreateLibraryParams{
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
CreatedBy: owner.ID,
|
||||||
|
Name: "Films",
|
||||||
|
PolicyID: ident.MustParse("00000000-0000-7000-8000-000000000101"),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreateLibrary : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
source := filepath.Join(t.TempDir(), "library")
|
||||||
|
if err := os.MkdirAll(source, 0o755); err != nil {
|
||||||
|
t.Fatalf("MkdirAll : %v", err)
|
||||||
|
}
|
||||||
|
writeNodeTestFile(t, filepath.Join(source, "keep.mkv"), "keep")
|
||||||
|
writeNodeTestFile(t, filepath.Join(source, "remove.txt"), "remove")
|
||||||
|
|
||||||
|
if _, err := node.Ingest().Scan(ctx, owner.ID, library.ID, source); err != nil {
|
||||||
|
t.Fatalf("Scan : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := node.Documents().CreateChild(ctx, owner.ID, document.CreateChildParams{
|
||||||
|
Kind: document.KindFile,
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
CreatedBy: owner.ID,
|
||||||
|
ParentID: library.ID,
|
||||||
|
Name: "manual.txt",
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("CreateChild(manual) : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
extra := filepath.Join(t.TempDir(), "extra.jpg")
|
||||||
|
writeNodeTestFile(t, extra, "extra")
|
||||||
|
imported, err := node.Ingest().Import(ctx, owner.ID, library.ID, extra)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Import : %v", err)
|
||||||
|
}
|
||||||
|
if imported.Files != 1 || imported.Created != 1 || imported.Updated != 0 || imported.Unchanged != 0 {
|
||||||
|
t.Fatalf("résultat import = %+v", imported)
|
||||||
|
}
|
||||||
|
|
||||||
|
imported, err = node.Ingest().Import(ctx, owner.ID, library.ID, extra)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Import(doublon) : %v", err)
|
||||||
|
}
|
||||||
|
if imported.Files != 1 || imported.Created != 0 || imported.Updated != 0 || imported.Unchanged != 1 {
|
||||||
|
t.Fatalf("résultat import doublon = %+v", imported)
|
||||||
|
}
|
||||||
|
|
||||||
|
imported, err = node.Ingest().Import(ctx, owner.ID, library.ID, source)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Import(dossier) : %v", err)
|
||||||
|
}
|
||||||
|
if imported.Files != 2 || imported.Created != 0 || imported.Updated != 0 || imported.Unchanged != 2 {
|
||||||
|
t.Fatalf("résultat import dossier = %+v", imported)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.Remove(filepath.Join(source, "remove.txt")); err != nil {
|
||||||
|
t.Fatalf("Remove : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := node.Ingest().ScanWithOptions(ctx, owner.ID, library.ID, source, ingest.ScanOptions{Prune: true})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ScanWithOptions : %v", err)
|
||||||
|
}
|
||||||
|
if result.HasErrors() {
|
||||||
|
t.Fatalf("ScanWithOptions erreurs : %v", result.Errors)
|
||||||
|
}
|
||||||
|
if result.Pruned != 1 {
|
||||||
|
t.Fatalf("pruned = %d, attendu 1", result.Pruned)
|
||||||
|
}
|
||||||
|
|
||||||
|
children, err := node.Documents().ListChildren(ctx, owner.ID, library.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ListChildren : %v", err)
|
||||||
|
}
|
||||||
|
if hasNodeTestChild(children, "remove.txt") {
|
||||||
|
t.Fatal("remove.txt toujours présent après prune")
|
||||||
|
}
|
||||||
|
if !hasNodeTestChild(children, "keep.mkv") {
|
||||||
|
t.Fatal("keep.mkv absent après prune")
|
||||||
|
}
|
||||||
|
if !hasNodeTestChild(children, "manual.txt") {
|
||||||
|
t.Fatal("manual.txt supprimé alors qu'il n'est pas issu du scan")
|
||||||
|
}
|
||||||
|
if !hasNodeTestChild(children, "extra.jpg") {
|
||||||
|
t.Fatal("extra.jpg supprimé alors qu'il vient d'une autre racine")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func hasNodeTestChild(children []document.Document, name string) bool {
|
||||||
|
for i := range children {
|
||||||
|
if children[i].Name == name {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
+48
-13
@@ -4,11 +4,13 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"mediarr/cluster"
|
"mediarr/cluster"
|
||||||
"mediarr/database"
|
"mediarr/database"
|
||||||
"mediarr/ident"
|
"mediarr/ident"
|
||||||
|
"mediarr/identity"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -32,30 +34,63 @@ func (n *Node) Up() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := n.ensureStorageBackend(ctx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return n.ensureClusterIdentity(ctx)
|
return n.ensureClusterIdentity(ctx)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n *Node) ensureStorageBackend(ctx context.Context) error {
|
||||||
|
_, err := n.db.Queries.GetStorageBackend(ctx, n.backendID)
|
||||||
|
switch {
|
||||||
|
case err == nil:
|
||||||
|
return nil
|
||||||
|
case !errors.Is(err, sql.ErrNoRows):
|
||||||
|
return fmt.Errorf("stockage : lecture du backend : %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
config, err := json.Marshal(map[string]string{"root": n.storagePath})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := n.db.WriteQueries.CreateStorageBackend(ctx, database.CreateStorageBackendParams{
|
||||||
|
ID: n.backendID,
|
||||||
|
Kind: "hub_fs",
|
||||||
|
Name: "local",
|
||||||
|
Config: string(config),
|
||||||
|
Enabled: 1,
|
||||||
|
State: "ok",
|
||||||
|
CreatedAt: time.Now().UnixMilli(),
|
||||||
|
}); err != nil {
|
||||||
|
return fmt.Errorf("stockage : création du backend : %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (n *Node) ensureClusterIdentity(ctx context.Context) error {
|
func (n *Node) ensureClusterIdentity(ctx context.Context) error {
|
||||||
|
|
||||||
principal, err := n.db.Queries.GetClusterPrincipal(ctx)
|
principal, err := n.db.Queries.GetClusterPrincipal(ctx)
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case err == nil:
|
case err == nil:
|
||||||
identity, loadErr := cluster.LoadOrCreate(n.clusterKeyPath, false)
|
clusterIdentity, loadErr := cluster.LoadOrCreate(n.clusterKeyPath, false)
|
||||||
if loadErr != nil {
|
if loadErr != nil {
|
||||||
return fmt.Errorf("cluster : principal présent en base mais clé indisponible : %w", loadErr)
|
return fmt.Errorf("cluster : principal présent en base mais clé indisponible : %w", loadErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !bytes.Equal(principal.PublicKey, identity.PublicKey) {
|
if !bytes.Equal(principal.PublicKey, clusterIdentity.PublicKey) {
|
||||||
return errors.New("cluster : la clé publique de cluster.key ne correspond pas au principal en base")
|
return errors.New("cluster : la clé publique de cluster.key ne correspond pas au principal en base")
|
||||||
}
|
}
|
||||||
|
|
||||||
n.cluster = identity
|
n.cluster = clusterIdentity
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
case errors.Is(err, sql.ErrNoRows):
|
case errors.Is(err, sql.ErrNoRows):
|
||||||
identity, createErr := cluster.LoadOrCreate(n.clusterKeyPath, true)
|
clusterIdentity, createErr := cluster.LoadOrCreate(n.clusterKeyPath, true)
|
||||||
if createErr != nil {
|
if createErr != nil {
|
||||||
return createErr
|
return createErr
|
||||||
}
|
}
|
||||||
@@ -69,21 +104,21 @@ func (n *Node) ensureClusterIdentity(ctx context.Context) error {
|
|||||||
ID: clusterID,
|
ID: clusterID,
|
||||||
Kind: "cluster",
|
Kind: "cluster",
|
||||||
Name: "cluster",
|
Name: "cluster",
|
||||||
PublicKey: identity.PublicKeyBytes(),
|
PublicKey: clusterIdentity.PublicKeyBytes(),
|
||||||
Status: "active",
|
Status: identity.StatusActive,
|
||||||
CreatedAt: time.Now().UnixMilli(),
|
CreatedAt: time.Now().UnixMilli(),
|
||||||
})
|
})
|
||||||
if createErr != nil {
|
if createErr != nil {
|
||||||
existing, getErr := n.db.Queries.GetClusterPrincipal(ctx)
|
existing, getErr := n.db.Queries.GetClusterPrincipal(ctx)
|
||||||
if getErr == nil && bytes.Equal(existing.PublicKey, identity.PublicKey) {
|
if getErr == nil && bytes.Equal(existing.PublicKey, clusterIdentity.PublicKey) {
|
||||||
n.cluster = identity
|
n.cluster = clusterIdentity
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Errorf("cluster : création du principal : %w", createErr)
|
return fmt.Errorf("cluster : création du principal : %w", createErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
n.cluster = identity
|
n.cluster = clusterIdentity
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@@ -99,28 +134,28 @@ func requiredNodePrincipals(now int64) []database.CreatePrincipalParams {
|
|||||||
ID: ident.MustParse("00000000-0000-7000-8000-000000000001"),
|
ID: ident.MustParse("00000000-0000-7000-8000-000000000001"),
|
||||||
Kind: "system",
|
Kind: "system",
|
||||||
Name: "system",
|
Name: "system",
|
||||||
Status: "active",
|
Status: identity.StatusActive,
|
||||||
CreatedAt: now,
|
CreatedAt: now,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
ID: ident.MustParse("00000000-0000-7000-8000-000000000002"),
|
ID: ident.MustParse("00000000-0000-7000-8000-000000000002"),
|
||||||
Kind: "group",
|
Kind: "group",
|
||||||
Name: "admins",
|
Name: "admins",
|
||||||
Status: "active",
|
Status: identity.StatusActive,
|
||||||
CreatedAt: now,
|
CreatedAt: now,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
ID: ident.MustParse("00000000-0000-7000-8000-000000000003"),
|
ID: ident.MustParse("00000000-0000-7000-8000-000000000003"),
|
||||||
Kind: "group",
|
Kind: "group",
|
||||||
Name: "members",
|
Name: "members",
|
||||||
Status: "active",
|
Status: identity.StatusActive,
|
||||||
CreatedAt: now,
|
CreatedAt: now,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
ID: ident.MustParse("00000000-0000-7000-8000-000000000004"),
|
ID: ident.MustParse("00000000-0000-7000-8000-000000000004"),
|
||||||
Kind: "group",
|
Kind: "group",
|
||||||
Name: "guests",
|
Name: "guests",
|
||||||
Status: "active",
|
Status: identity.StatusActive,
|
||||||
CreatedAt: now,
|
CreatedAt: now,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"mediarr/cluster"
|
"mediarr/cluster"
|
||||||
|
"mediarr/identity"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestUpCreatesAndKeepsClusterIdentity(t *testing.T) {
|
func TestUpCreatesAndKeepsClusterIdentity(t *testing.T) {
|
||||||
@@ -25,9 +26,9 @@ func TestUpCreatesAndKeepsClusterIdentity(t *testing.T) {
|
|||||||
t.Fatalf("Up : %v", err)
|
t.Fatalf("Up : %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
identity := node.ClusterIdentity()
|
clusterIdentity := node.ClusterIdentity()
|
||||||
if len(identity.PublicKey) != 32 {
|
if len(clusterIdentity.PublicKey) != 32 {
|
||||||
t.Fatalf("clé publique de %d octets, attendu 32", len(identity.PublicKey))
|
t.Fatalf("clé publique de %d octets, attendu 32", len(clusterIdentity.PublicKey))
|
||||||
}
|
}
|
||||||
|
|
||||||
principal, err := node.db.Queries.GetClusterPrincipal(context.Background())
|
principal, err := node.db.Queries.GetClusterPrincipal(context.Background())
|
||||||
@@ -40,10 +41,10 @@ func TestUpCreatesAndKeepsClusterIdentity(t *testing.T) {
|
|||||||
if principal.Name != "cluster" {
|
if principal.Name != "cluster" {
|
||||||
t.Fatalf("name = %q, attendu cluster", principal.Name)
|
t.Fatalf("name = %q, attendu cluster", principal.Name)
|
||||||
}
|
}
|
||||||
if principal.Status != "active" {
|
if principal.Status != identity.StatusActive {
|
||||||
t.Fatalf("status = %q, attendu active", principal.Status)
|
t.Fatalf("status = %q, attendu active", principal.Status)
|
||||||
}
|
}
|
||||||
if !bytes.Equal(principal.PublicKey, identity.PublicKey) {
|
if !bytes.Equal(principal.PublicKey, clusterIdentity.PublicKey) {
|
||||||
t.Fatal("la clé publique en base ne correspond pas à la clé chargée")
|
t.Fatal("la clé publique en base ne correspond pas à la clé chargée")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package permission
|
||||||
|
|
||||||
|
type Permission int64
|
||||||
|
|
||||||
|
const (
|
||||||
|
Read Permission = 1 << iota
|
||||||
|
Write
|
||||||
|
Create
|
||||||
|
Delete
|
||||||
|
Share
|
||||||
|
Admin
|
||||||
|
Request
|
||||||
|
)
|
||||||
|
|
||||||
|
const All = Read |
|
||||||
|
Write |
|
||||||
|
Create |
|
||||||
|
Delete |
|
||||||
|
Share |
|
||||||
|
Admin |
|
||||||
|
Request
|
||||||
|
|
||||||
|
func (p Permission) Valid() bool {
|
||||||
|
return p > 0 && p <= All
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p Permission) Has(required Permission) bool {
|
||||||
|
if required == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if !required.Valid() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return p&Admin != 0 || p&required == required
|
||||||
|
}
|
||||||
+220
-3
@@ -1,8 +1,225 @@
|
|||||||
package storage
|
package storage
|
||||||
|
|
||||||
type Storage struct {
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/hex"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"mediarr/ident"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrInvalidLocator = errors.New("locator de stockage invalide")
|
||||||
|
ErrNotFound = errors.New("objet de stockage introuvable")
|
||||||
|
)
|
||||||
|
|
||||||
|
type Staged struct {
|
||||||
|
Locator string
|
||||||
|
Size int64
|
||||||
|
Root []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStorage() Storage {
|
type Store interface {
|
||||||
return Storage{}
|
Stage(ctx context.Context, id ident.UUID, src io.Reader) (Staged, error)
|
||||||
|
Commit(ctx context.Context, staged Staged) (string, error)
|
||||||
|
Replace(ctx context.Context, staged Staged) (string, error)
|
||||||
|
Open(ctx context.Context, locator string) (io.ReadCloser, error)
|
||||||
|
Remove(ctx context.Context, locator string) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type Local struct {
|
||||||
|
root string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewLocal(root string) (*Local, error) {
|
||||||
|
if strings.TrimSpace(root) == "" {
|
||||||
|
return nil, errors.New("racine de stockage vide")
|
||||||
|
}
|
||||||
|
|
||||||
|
absolute, err := filepath.Abs(root)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, directory := range []string{
|
||||||
|
filepath.Join(absolute, "staging"),
|
||||||
|
filepath.Join(absolute, "blobs"),
|
||||||
|
} {
|
||||||
|
if err := os.MkdirAll(directory, 0o750); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Local{root: absolute}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Local) Stage(ctx context.Context, id ident.UUID, src io.Reader) (Staged, error) {
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
return Staged{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
locator := filepath.ToSlash(filepath.Join("staging", hex.EncodeToString(id[:])))
|
||||||
|
path, err := l.resolve(locator)
|
||||||
|
if err != nil {
|
||||||
|
return Staged{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
file, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o640)
|
||||||
|
if err != nil {
|
||||||
|
return Staged{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
hasher := sha256.New()
|
||||||
|
size, copyErr := io.Copy(io.MultiWriter(file, hasher), src)
|
||||||
|
closeErr := file.Close()
|
||||||
|
if copyErr != nil {
|
||||||
|
_ = os.Remove(path)
|
||||||
|
return Staged{}, copyErr
|
||||||
|
}
|
||||||
|
if closeErr != nil {
|
||||||
|
_ = os.Remove(path)
|
||||||
|
return Staged{}, closeErr
|
||||||
|
}
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
_ = os.Remove(path)
|
||||||
|
return Staged{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return Staged{
|
||||||
|
Locator: locator,
|
||||||
|
Size: size,
|
||||||
|
Root: hasher.Sum(nil),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Local) Commit(ctx context.Context, staged Staged) (string, error) {
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if len(staged.Root) != sha256.Size {
|
||||||
|
return "", fmt.Errorf("%w: empreinte de %d octets", ErrInvalidLocator, len(staged.Root))
|
||||||
|
}
|
||||||
|
|
||||||
|
source, err := l.resolve(staged.Locator)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
root := hex.EncodeToString(staged.Root)
|
||||||
|
locator := filepath.ToSlash(filepath.Join("blobs", root[:2], root[2:4], root))
|
||||||
|
target, err := l.resolve(locator)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if err := os.MkdirAll(filepath.Dir(target), 0o750); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := os.Stat(target); err == nil {
|
||||||
|
if err := os.Remove(source); err != nil && !os.IsNotExist(err) {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return locator, nil
|
||||||
|
} else if !os.IsNotExist(err) {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.Rename(source, target); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return locator, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Local) Replace(ctx context.Context, staged Staged) (string, error) {
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if len(staged.Root) != sha256.Size {
|
||||||
|
return "", fmt.Errorf("%w: empreinte de %d octets", ErrInvalidLocator, len(staged.Root))
|
||||||
|
}
|
||||||
|
|
||||||
|
source, err := l.resolve(staged.Locator)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
root := hex.EncodeToString(staged.Root)
|
||||||
|
locator := filepath.ToSlash(filepath.Join("blobs", root[:2], root[2:4], root))
|
||||||
|
target, err := l.resolve(locator)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if err := os.MkdirAll(filepath.Dir(target), 0o750); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.Rename(source, target); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return locator, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Local) Open(ctx context.Context, locator string) (io.ReadCloser, error) {
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
path, err := l.resolve(locator)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
file, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
return nil, fmt.Errorf("%w: %s", ErrNotFound, locator)
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return file, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Local) Remove(ctx context.Context, locator string) error {
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
path, err := l.resolve(locator)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.Remove(path); err != nil && !os.IsNotExist(err) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Local) resolve(locator string) (string, error) {
|
||||||
|
if locator == "" || filepath.IsAbs(locator) {
|
||||||
|
return "", fmt.Errorf("%w: %q", ErrInvalidLocator, locator)
|
||||||
|
}
|
||||||
|
|
||||||
|
cleaned := filepath.Clean(filepath.FromSlash(locator))
|
||||||
|
if cleaned == "." || cleaned == ".." || strings.HasPrefix(cleaned, ".."+string(filepath.Separator)) {
|
||||||
|
return "", fmt.Errorf("%w: %q", ErrInvalidLocator, locator)
|
||||||
|
}
|
||||||
|
|
||||||
|
path := filepath.Join(l.root, cleaned)
|
||||||
|
relative, err := filepath.Rel(l.root, path)
|
||||||
|
if err != nil || relative == ".." || strings.HasPrefix(relative, ".."+string(filepath.Separator)) {
|
||||||
|
return "", fmt.Errorf("%w: %q", ErrInvalidLocator, locator)
|
||||||
|
}
|
||||||
|
|
||||||
|
return path, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,190 @@
|
|||||||
|
package storage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/hex"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"mediarr/ident"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestLocalStageCommitDedupOpenRemove(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
root := t.TempDir()
|
||||||
|
|
||||||
|
store, err := NewLocal(root)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewLocal : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
payload := []byte("contenu local")
|
||||||
|
expectedRoot := sha256.Sum256(payload)
|
||||||
|
|
||||||
|
staged, err := store.Stage(ctx, mustUUID(t), bytes.NewReader(payload))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Stage : %v", err)
|
||||||
|
}
|
||||||
|
if staged.Size != int64(len(payload)) {
|
||||||
|
t.Fatalf("size = %d, attendu %d", staged.Size, len(payload))
|
||||||
|
}
|
||||||
|
if !bytes.Equal(staged.Root, expectedRoot[:]) {
|
||||||
|
t.Fatalf("root = %x, attendu %x", staged.Root, expectedRoot)
|
||||||
|
}
|
||||||
|
if !strings.HasPrefix(staged.Locator, "staging/") {
|
||||||
|
t.Fatalf("locator = %q, attendu staging/...", staged.Locator)
|
||||||
|
}
|
||||||
|
|
||||||
|
locator, err := store.Commit(ctx, staged)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Commit : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
rootHex := hex.EncodeToString(expectedRoot[:])
|
||||||
|
expectedLocator := filepath.ToSlash(filepath.Join("blobs", rootHex[:2], rootHex[2:4], rootHex))
|
||||||
|
if locator != expectedLocator {
|
||||||
|
t.Fatalf("locator = %q, attendu %q", locator, expectedLocator)
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(filepath.Join(root, filepath.FromSlash(staged.Locator))); !os.IsNotExist(err) {
|
||||||
|
t.Fatalf("fichier de staging encore présent : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
reader, err := store.Open(ctx, locator)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Open : %v", err)
|
||||||
|
}
|
||||||
|
data, err := io.ReadAll(reader)
|
||||||
|
if err != nil {
|
||||||
|
_ = reader.Close()
|
||||||
|
t.Fatalf("ReadAll : %v", err)
|
||||||
|
}
|
||||||
|
if err := reader.Close(); err != nil {
|
||||||
|
t.Fatalf("Close : %v", err)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(data, payload) {
|
||||||
|
t.Fatalf("contenu = %q, attendu %q", data, payload)
|
||||||
|
}
|
||||||
|
|
||||||
|
duplicate, err := store.Stage(ctx, mustUUID(t), bytes.NewReader(payload))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Stage(doublon) : %v", err)
|
||||||
|
}
|
||||||
|
duplicateLocator, err := store.Commit(ctx, duplicate)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Commit(doublon) : %v", err)
|
||||||
|
}
|
||||||
|
if duplicateLocator != locator {
|
||||||
|
t.Fatalf("locator doublon = %q, attendu %q", duplicateLocator, locator)
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(filepath.Join(root, filepath.FromSlash(duplicate.Locator))); !os.IsNotExist(err) {
|
||||||
|
t.Fatalf("staging doublon encore présent : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := store.Remove(ctx, locator); err != nil {
|
||||||
|
t.Fatalf("Remove : %v", err)
|
||||||
|
}
|
||||||
|
if _, err := store.Open(ctx, locator); !errors.Is(err, ErrNotFound) {
|
||||||
|
t.Fatalf("Open après Remove : erreur = %v, attendu ErrNotFound", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLocalReplace(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
root := t.TempDir()
|
||||||
|
|
||||||
|
store, err := NewLocal(root)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewLocal : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
payload := []byte("contenu à réparer")
|
||||||
|
staged, err := store.Stage(ctx, mustUUID(t), bytes.NewReader(payload))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Stage : %v", err)
|
||||||
|
}
|
||||||
|
locator, err := store.Commit(ctx, staged)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Commit : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.WriteFile(filepath.Join(root, filepath.FromSlash(locator)), []byte("corrompu"), 0o640); err != nil {
|
||||||
|
t.Fatalf("WriteFile : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
replacement, err := store.Stage(ctx, mustUUID(t), bytes.NewReader(payload))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Stage(remplacement) : %v", err)
|
||||||
|
}
|
||||||
|
replacedLocator, err := store.Replace(ctx, replacement)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Replace : %v", err)
|
||||||
|
}
|
||||||
|
if replacedLocator != locator {
|
||||||
|
t.Fatalf("locator = %q, attendu %q", replacedLocator, locator)
|
||||||
|
}
|
||||||
|
|
||||||
|
reader, err := store.Open(ctx, locator)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Open : %v", err)
|
||||||
|
}
|
||||||
|
data, err := io.ReadAll(reader)
|
||||||
|
if err != nil {
|
||||||
|
_ = reader.Close()
|
||||||
|
t.Fatalf("ReadAll : %v", err)
|
||||||
|
}
|
||||||
|
if err := reader.Close(); err != nil {
|
||||||
|
t.Fatalf("Close : %v", err)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(data, payload) {
|
||||||
|
t.Fatalf("contenu = %q, attendu %q", data, payload)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLocalRejectsInvalidLocators(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
store, err := NewLocal(t.TempDir())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewLocal : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, locator := range []string{"", "../secret", "/etc/passwd", "staging/../../secret"} {
|
||||||
|
if _, err := store.Open(ctx, locator); !errors.Is(err, ErrInvalidLocator) {
|
||||||
|
t.Fatalf("Open(%q) : erreur = %v, attendu ErrInvalidLocator", locator, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := store.Commit(ctx, Staged{Locator: "staging/x", Root: []byte{1}}); !errors.Is(err, ErrInvalidLocator) {
|
||||||
|
t.Fatalf("Commit avec racine invalide : erreur = %v, attendu ErrInvalidLocator", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLocalStageHonorsCanceledContext(t *testing.T) {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
cancel()
|
||||||
|
|
||||||
|
store, err := NewLocal(t.TempDir())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewLocal : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := store.Stage(ctx, mustUUID(t), strings.NewReader("contenu")); !errors.Is(err, context.Canceled) {
|
||||||
|
t.Fatalf("Stage : erreur = %v, attendu context.Canceled", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func mustUUID(t *testing.T) ident.UUID {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
id, err := ident.NewV7()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewV7 : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return id
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user