134 lines
3.3 KiB
Go
134 lines
3.3 KiB
Go
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
|
|
}
|