2022-09-12 22:47:27 +00:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-09-27 23:52:13 +00:00
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"path/filepath"
|
2022-09-12 22:47:27 +00:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2024-02-04 00:09:07 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/core/blobstore"
|
2022-10-30 04:05:38 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent/document"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent/group"
|
2022-09-27 23:52:13 +00:00
|
|
|
)
|
|
|
|
|
2023-02-18 06:41:01 +00:00
|
|
|
var ErrInvalidDocExtension = errors.New("invalid document extension")
|
2022-09-12 22:47:27 +00:00
|
|
|
|
|
|
|
type DocumentRepository struct {
|
2024-02-04 00:09:07 +00:00
|
|
|
db *ent.Client
|
|
|
|
bs blobstore.BlobStore
|
2022-09-12 22:47:27 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
type (
|
|
|
|
DocumentCreate struct {
|
|
|
|
Title string `json:"title"`
|
|
|
|
Content io.Reader `json:"content"`
|
|
|
|
}
|
|
|
|
|
|
|
|
DocumentOut struct {
|
|
|
|
ID uuid.UUID `json:"id"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
Path string `json:"path"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func mapDocumentOut(doc *ent.Document) DocumentOut {
|
|
|
|
return DocumentOut{
|
|
|
|
ID: doc.ID,
|
|
|
|
Title: doc.Title,
|
|
|
|
Path: doc.Path,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
mapDocumentOutErr = mapTErrFunc(mapDocumentOut)
|
|
|
|
mapDocumentOutEachErr = mapTEachErrFunc(mapDocumentOut)
|
|
|
|
)
|
|
|
|
|
2024-02-04 00:09:07 +00:00
|
|
|
func (r *DocumentRepository) blobKey(gid uuid.UUID, ext string) string {
|
|
|
|
return filepath.Join(gid.String(), "documents", uuid.NewString()+ext)
|
2022-09-12 22:47:27 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
func (r *DocumentRepository) GetAll(ctx context.Context, gid uuid.UUID) ([]DocumentOut, error) {
|
|
|
|
return mapDocumentOutEachErr(r.db.Document.
|
|
|
|
Query().
|
2022-09-12 22:47:27 +00:00
|
|
|
Where(document.HasGroupWith(group.ID(gid))).
|
2022-09-27 23:52:13 +00:00
|
|
|
All(ctx),
|
|
|
|
)
|
2022-09-12 22:47:27 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
func (r *DocumentRepository) Get(ctx context.Context, id uuid.UUID) (DocumentOut, error) {
|
|
|
|
return mapDocumentOutErr(r.db.Document.Get(ctx, id))
|
2022-09-12 22:47:27 +00:00
|
|
|
}
|
|
|
|
|
2024-02-04 00:09:07 +00:00
|
|
|
func (r *DocumentRepository) Read(ctx context.Context, id uuid.UUID) (io.ReadCloser, error) {
|
|
|
|
doc, err := r.db.Document.Get(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-09-27 23:52:13 +00:00
|
|
|
}
|
|
|
|
|
2024-02-04 00:09:07 +00:00
|
|
|
content, err := r.bs.Get(doc.Path)
|
2022-09-27 23:52:13 +00:00
|
|
|
if err != nil {
|
2024-02-04 00:09:07 +00:00
|
|
|
return nil, err
|
2022-09-27 23:52:13 +00:00
|
|
|
}
|
|
|
|
|
2024-02-04 00:09:07 +00:00
|
|
|
return content, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *DocumentRepository) Create(ctx context.Context, gid uuid.UUID, doc DocumentCreate) (DocumentOut, error) {
|
|
|
|
ext := filepath.Ext(doc.Title)
|
|
|
|
if ext == "" {
|
|
|
|
return DocumentOut{}, ErrInvalidDocExtension
|
2022-09-27 23:52:13 +00:00
|
|
|
}
|
|
|
|
|
2024-02-04 00:09:07 +00:00
|
|
|
key := r.blobKey(gid, ext)
|
|
|
|
|
|
|
|
path, err := r.bs.Put(key, doc.Content)
|
2022-09-27 23:52:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return DocumentOut{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return mapDocumentOutErr(r.db.Document.Create().
|
|
|
|
SetGroupID(gid).
|
2022-09-12 22:47:27 +00:00
|
|
|
SetTitle(doc.Title).
|
2022-09-27 23:52:13 +00:00
|
|
|
SetPath(path).
|
|
|
|
Save(ctx),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *DocumentRepository) Rename(ctx context.Context, id uuid.UUID, title string) (DocumentOut, error) {
|
|
|
|
return mapDocumentOutErr(r.db.Document.UpdateOneID(id).
|
|
|
|
SetTitle(title).
|
|
|
|
Save(ctx))
|
2022-09-12 22:47:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *DocumentRepository) Delete(ctx context.Context, id uuid.UUID) error {
|
2022-09-27 23:52:13 +00:00
|
|
|
doc, err := r.db.Document.Get(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-04 00:09:07 +00:00
|
|
|
err = r.bs.Delete(doc.Path)
|
2022-09-27 23:52:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-12 22:47:27 +00:00
|
|
|
return r.db.Document.DeleteOneID(id).Exec(ctx)
|
|
|
|
}
|