123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- package store
- import (
- "context"
- "database/sql"
- "fmt"
- "sort"
- "strings"
- "github.com/usememos/memos/api"
- "github.com/usememos/memos/common"
- )
- // resourceRaw is the store model for an Resource.
- // Fields have exactly the same meanings as Resource.
- type resourceRaw struct {
- ID int
- // Standard fields
- CreatorID int
- CreatedTs int64
- UpdatedTs int64
- // Domain specific fields
- Filename string
- Blob []byte
- Type string
- Size int64
- }
- func (raw *resourceRaw) toResource() *api.Resource {
- return &api.Resource{
- ID: raw.ID,
- // Standard fields
- CreatorID: raw.CreatorID,
- CreatedTs: raw.CreatedTs,
- UpdatedTs: raw.UpdatedTs,
- // Domain specific fields
- Filename: raw.Filename,
- Blob: raw.Blob,
- Type: raw.Type,
- Size: raw.Size,
- }
- }
- func (s *Store) ComposeMemoResourceList(ctx context.Context, memo *api.Memo) error {
- resourceList, err := s.FindResourceList(ctx, &api.ResourceFind{
- MemoID: &memo.ID,
- })
- if err != nil {
- return err
- }
- for _, resource := range resourceList {
- memoResource, err := s.FindMemoResource(ctx, &api.MemoResourceFind{
- MemoID: &memo.ID,
- ResourceID: &resource.ID,
- })
- if err != nil {
- return err
- }
- resource.CreatedTs = memoResource.CreatedTs
- resource.UpdatedTs = memoResource.UpdatedTs
- }
- sort.Slice(resourceList, func(i, j int) bool {
- if resourceList[i].CreatedTs != resourceList[j].CreatedTs {
- return resourceList[i].CreatedTs < resourceList[j].CreatedTs
- }
- return resourceList[i].ID < resourceList[j].ID
- })
- memo.ResourceList = resourceList
- return nil
- }
- func (s *Store) CreateResource(ctx context.Context, create *api.ResourceCreate) (*api.Resource, error) {
- tx, err := s.db.BeginTx(ctx, nil)
- if err != nil {
- return nil, FormatError(err)
- }
- defer tx.Rollback()
- resourceRaw, err := createResource(ctx, tx, create)
- if err != nil {
- return nil, err
- }
- if err := tx.Commit(); err != nil {
- return nil, FormatError(err)
- }
- if err := s.cache.UpsertCache(api.ResourceCache, resourceRaw.ID, resourceRaw); err != nil {
- return nil, err
- }
- resource := resourceRaw.toResource()
- return resource, nil
- }
- func (s *Store) FindResourceList(ctx context.Context, find *api.ResourceFind) ([]*api.Resource, error) {
- tx, err := s.db.BeginTx(ctx, nil)
- if err != nil {
- return nil, FormatError(err)
- }
- defer tx.Rollback()
- resourceRawList, err := findResourceList(ctx, tx, find)
- if err != nil {
- return nil, err
- }
- resourceList := []*api.Resource{}
- for _, raw := range resourceRawList {
- resourceList = append(resourceList, raw.toResource())
- }
- return resourceList, nil
- }
- func (s *Store) FindResource(ctx context.Context, find *api.ResourceFind) (*api.Resource, error) {
- if find.ID != nil {
- resourceRaw := &resourceRaw{}
- has, err := s.cache.FindCache(api.ResourceCache, *find.ID, resourceRaw)
- if err != nil {
- return nil, err
- }
- if has {
- return resourceRaw.toResource(), nil
- }
- }
- tx, err := s.db.BeginTx(ctx, nil)
- if err != nil {
- return nil, FormatError(err)
- }
- defer tx.Rollback()
- list, err := findResourceList(ctx, tx, find)
- if err != nil {
- return nil, err
- }
- if len(list) == 0 {
- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("not found")}
- }
- resourceRaw := list[0]
- if err := s.cache.UpsertCache(api.ResourceCache, resourceRaw.ID, resourceRaw); err != nil {
- return nil, err
- }
- resource := resourceRaw.toResource()
- return resource, nil
- }
- func (s *Store) DeleteResource(ctx context.Context, delete *api.ResourceDelete) error {
- tx, err := s.db.BeginTx(ctx, nil)
- if err != nil {
- return FormatError(err)
- }
- defer tx.Rollback()
- if err := deleteResource(ctx, tx, delete); err != nil {
- return err
- }
- if err := vacuum(ctx, tx); err != nil {
- return err
- }
- if err := tx.Commit(); err != nil {
- return FormatError(err)
- }
- s.cache.DeleteCache(api.ResourceCache, delete.ID)
- return nil
- }
- func (s *Store) PatchResource(ctx context.Context, patch *api.ResourcePatch) (*api.Resource, error) {
- tx, err := s.db.BeginTx(ctx, nil)
- if err != nil {
- return nil, FormatError(err)
- }
- defer tx.Rollback()
- resourceRaw, err := patchResource(ctx, tx, patch)
- if err != nil {
- return nil, err
- }
- if err := tx.Commit(); err != nil {
- return nil, FormatError(err)
- }
- if err := s.cache.UpsertCache(api.ResourceCache, resourceRaw.ID, resourceRaw); err != nil {
- return nil, err
- }
- resource := resourceRaw.toResource()
- return resource, nil
- }
- func createResource(ctx context.Context, tx *sql.Tx, create *api.ResourceCreate) (*resourceRaw, error) {
- query := `
- INSERT INTO resource (
- filename,
- blob,
- type,
- size,
- creator_id
- )
- VALUES (?, ?, ?, ?, ?)
- RETURNING id, filename, blob, type, size, creator_id, created_ts, updated_ts
- `
- var resourceRaw resourceRaw
- if err := tx.QueryRowContext(ctx, query, create.Filename, create.Blob, create.Type, create.Size, create.CreatorID).Scan(
- &resourceRaw.ID,
- &resourceRaw.Filename,
- &resourceRaw.Blob,
- &resourceRaw.Type,
- &resourceRaw.Size,
- &resourceRaw.CreatorID,
- &resourceRaw.CreatedTs,
- &resourceRaw.UpdatedTs,
- ); err != nil {
- return nil, FormatError(err)
- }
- return &resourceRaw, nil
- }
- func patchResource(ctx context.Context, tx *sql.Tx, patch *api.ResourcePatch) (*resourceRaw, error) {
- set, args := []string{}, []interface{}{}
- if v := patch.UpdatedTs; v != nil {
- set, args = append(set, "updated_ts = ?"), append(args, *v)
- }
- if v := patch.Filename; v != nil {
- set, args = append(set, "filename = ?"), append(args, *v)
- }
- args = append(args, patch.ID)
- query := `
- UPDATE resource
- SET ` + strings.Join(set, ", ") + `
- WHERE id = ?
- RETURNING id, filename, blob, type, size, creator_id, created_ts, updated_ts
- `
- var resourceRaw resourceRaw
- if err := tx.QueryRowContext(ctx, query, args...).Scan(
- &resourceRaw.ID,
- &resourceRaw.Filename,
- &resourceRaw.Blob,
- &resourceRaw.Type,
- &resourceRaw.Size,
- &resourceRaw.CreatorID,
- &resourceRaw.CreatedTs,
- &resourceRaw.UpdatedTs,
- ); err != nil {
- return nil, FormatError(err)
- }
- return &resourceRaw, nil
- }
- func findResourceList(ctx context.Context, tx *sql.Tx, find *api.ResourceFind) ([]*resourceRaw, error) {
- where, args := []string{"1 = 1"}, []interface{}{}
- if v := find.ID; v != nil {
- where, args = append(where, "id = ?"), append(args, *v)
- }
- if v := find.CreatorID; v != nil {
- where, args = append(where, "creator_id = ?"), append(args, *v)
- }
- if v := find.Filename; v != nil {
- where, args = append(where, "filename = ?"), append(args, *v)
- }
- if v := find.MemoID; v != nil {
- where, args = append(where, "id in (SELECT resource_id FROM memo_resource WHERE memo_id = ?)"), append(args, *v)
- }
- query := `
- SELECT
- id,
- filename,
- blob,
- type,
- size,
- creator_id,
- created_ts,
- updated_ts
- FROM resource
- WHERE ` + strings.Join(where, " AND ") + `
- ORDER BY id DESC
- `
- rows, err := tx.QueryContext(ctx, query, args...)
- if err != nil {
- return nil, FormatError(err)
- }
- defer rows.Close()
- resourceRawList := make([]*resourceRaw, 0)
- for rows.Next() {
- var resourceRaw resourceRaw
- if err := rows.Scan(
- &resourceRaw.ID,
- &resourceRaw.Filename,
- &resourceRaw.Blob,
- &resourceRaw.Type,
- &resourceRaw.Size,
- &resourceRaw.CreatorID,
- &resourceRaw.CreatedTs,
- &resourceRaw.UpdatedTs,
- ); err != nil {
- return nil, FormatError(err)
- }
- resourceRawList = append(resourceRawList, &resourceRaw)
- }
- if err := rows.Err(); err != nil {
- return nil, FormatError(err)
- }
- return resourceRawList, nil
- }
- func deleteResource(ctx context.Context, tx *sql.Tx, delete *api.ResourceDelete) error {
- where, args := []string{"id = ?"}, []interface{}{delete.ID}
- stmt := `DELETE FROM resource WHERE ` + strings.Join(where, " AND ")
- result, err := tx.ExecContext(ctx, stmt, args...)
- if err != nil {
- return FormatError(err)
- }
- rows, _ := result.RowsAffected()
- if rows == 0 {
- return &common.Error{Code: common.NotFound, Err: fmt.Errorf("resource not found")}
- }
- return nil
- }
- func vacuumResource(ctx context.Context, tx *sql.Tx) error {
- stmt := `
- DELETE FROM
- resource
- WHERE
- creator_id NOT IN (
- SELECT
- id
- FROM
- user
- )`
- _, err := tx.ExecContext(ctx, stmt)
- if err != nil {
- return FormatError(err)
- }
- return nil
- }
|