2021-12-14 15:08:12 +03:00
|
|
|
package store
|
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
import (
|
2022-08-07 05:17:12 +03:00
|
|
|
"context"
|
2022-05-21 19:59:22 +03:00
|
|
|
"database/sql"
|
2022-02-03 10:32:03 +03:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2022-06-27 17:09:06 +03:00
|
|
|
|
|
|
|
"github.com/usememos/memos/api"
|
|
|
|
"github.com/usememos/memos/common"
|
2022-02-03 10:32:03 +03:00
|
|
|
)
|
2021-12-14 15:08:12 +03:00
|
|
|
|
2022-05-19 13:32:04 +03:00
|
|
|
// 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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 05:17:12 +03:00
|
|
|
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)
|
2022-02-03 10:32:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-12-14 15:08:12 +03:00
|
|
|
}
|
|
|
|
|
2022-08-07 05:17:12 +03:00
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
2022-05-19 13:32:04 +03:00
|
|
|
resource := resourceRaw.toResource()
|
|
|
|
|
2022-08-07 03:09:43 +03:00
|
|
|
if err := s.cache.UpsertCache(api.ResourceCache, resource.ID, resource); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
return resource, nil
|
|
|
|
}
|
2021-12-14 15:08:12 +03:00
|
|
|
|
2022-08-07 05:17:12 +03:00
|
|
|
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)
|
2022-02-03 10:32:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-19 13:32:04 +03:00
|
|
|
resourceList := []*api.Resource{}
|
|
|
|
for _, raw := range resourceRawList {
|
|
|
|
resourceList = append(resourceList, raw.toResource())
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceList, nil
|
2021-12-14 15:08:12 +03:00
|
|
|
}
|
|
|
|
|
2022-08-07 05:17:12 +03:00
|
|
|
func (s *Store) FindResource(ctx context.Context, find *api.ResourceFind) (*api.Resource, error) {
|
2022-08-07 03:09:43 +03:00
|
|
|
if find.ID != nil {
|
|
|
|
resource := &api.Resource{}
|
|
|
|
has, err := s.cache.FindCache(api.ResourceCache, *find.ID, resource)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if has {
|
|
|
|
return resource, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 05:17:12 +03:00
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
list, err := findResourceList(ctx, tx, find)
|
2022-02-04 13:54:24 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(list) == 0 {
|
|
|
|
return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("not found")}
|
|
|
|
}
|
|
|
|
|
2022-05-19 13:32:04 +03:00
|
|
|
resource := list[0].toResource()
|
|
|
|
|
2022-08-07 03:09:43 +03:00
|
|
|
if err := s.cache.UpsertCache(api.ResourceCache, resource.ID, resource); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-19 13:32:04 +03:00
|
|
|
return resource, nil
|
2022-02-04 13:54:24 +03:00
|
|
|
}
|
|
|
|
|
2022-08-07 05:17:12 +03:00
|
|
|
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()
|
|
|
|
|
|
|
|
err = deleteResource(ctx, tx, delete)
|
2022-02-03 10:32:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-07 05:17:12 +03:00
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return FormatError(err)
|
|
|
|
}
|
|
|
|
|
2022-08-07 03:09:43 +03:00
|
|
|
s.cache.DeleteCache(api.ResourceCache, delete.ID)
|
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-07 05:17:12 +03:00
|
|
|
func createResource(ctx context.Context, tx *sql.Tx, create *api.ResourceCreate) (*resourceRaw, error) {
|
|
|
|
query := `
|
2022-02-03 10:32:03 +03:00
|
|
|
INSERT INTO resource (
|
|
|
|
filename,
|
|
|
|
blob,
|
|
|
|
type,
|
|
|
|
size,
|
|
|
|
creator_id
|
|
|
|
)
|
|
|
|
VALUES (?, ?, ?, ?, ?)
|
2022-08-06 20:30:48 +03:00
|
|
|
RETURNING id, filename, blob, type, size, creator_id, created_ts, updated_ts
|
2022-08-07 05:17:12 +03:00
|
|
|
`
|
2022-05-19 13:32:04 +03:00
|
|
|
var resourceRaw resourceRaw
|
2022-08-07 05:17:12 +03:00
|
|
|
if err := tx.QueryRowContext(ctx, query, create.Filename, create.Blob, create.Type, create.Size, create.CreatorID).Scan(
|
2022-05-19 13:32:04 +03:00
|
|
|
&resourceRaw.ID,
|
|
|
|
&resourceRaw.Filename,
|
|
|
|
&resourceRaw.Blob,
|
|
|
|
&resourceRaw.Type,
|
|
|
|
&resourceRaw.Size,
|
2022-08-06 20:30:48 +03:00
|
|
|
&resourceRaw.CreatorID,
|
2022-05-19 13:32:04 +03:00
|
|
|
&resourceRaw.CreatedTs,
|
|
|
|
&resourceRaw.UpdatedTs,
|
2022-02-03 10:32:03 +03:00
|
|
|
); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
2022-05-19 13:32:04 +03:00
|
|
|
return &resourceRaw, nil
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
|
2022-08-07 05:17:12 +03:00
|
|
|
func findResourceList(ctx context.Context, tx *sql.Tx, find *api.ResourceFind) ([]*resourceRaw, error) {
|
2022-02-03 10:32:03 +03:00
|
|
|
where, args := []string{"1 = 1"}, []interface{}{}
|
2022-02-06 06:26:53 +03:00
|
|
|
|
2022-05-02 21:05:43 +03:00
|
|
|
if v := find.ID; v != nil {
|
2022-02-03 10:32:03 +03:00
|
|
|
where, args = append(where, "id = ?"), append(args, *v)
|
|
|
|
}
|
2022-05-02 21:05:43 +03:00
|
|
|
if v := find.CreatorID; v != nil {
|
2022-02-03 10:32:03 +03:00
|
|
|
where, args = append(where, "creator_id = ?"), append(args, *v)
|
|
|
|
}
|
|
|
|
if v := find.Filename; v != nil {
|
|
|
|
where, args = append(where, "filename = ?"), append(args, *v)
|
|
|
|
}
|
2021-12-14 15:08:12 +03:00
|
|
|
|
2022-08-07 05:17:12 +03:00
|
|
|
query := `
|
2022-02-03 10:32:03 +03:00
|
|
|
SELECT
|
|
|
|
id,
|
|
|
|
filename,
|
|
|
|
blob,
|
|
|
|
type,
|
|
|
|
size,
|
2022-08-06 20:30:48 +03:00
|
|
|
creator_id,
|
2022-02-03 10:32:03 +03:00
|
|
|
created_ts,
|
|
|
|
updated_ts
|
|
|
|
FROM resource
|
2022-08-07 05:17:12 +03:00
|
|
|
WHERE ` + strings.Join(where, " AND ") + `
|
|
|
|
ORDER BY created_ts DESC
|
|
|
|
`
|
|
|
|
rows, err := tx.QueryContext(ctx, query, args...)
|
2022-02-03 10:32:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
2021-12-14 15:08:12 +03:00
|
|
|
|
2022-05-19 13:32:04 +03:00
|
|
|
resourceRawList := make([]*resourceRaw, 0)
|
2021-12-14 15:08:12 +03:00
|
|
|
for rows.Next() {
|
2022-05-19 13:32:04 +03:00
|
|
|
var resourceRaw resourceRaw
|
2022-02-03 10:32:03 +03:00
|
|
|
if err := rows.Scan(
|
2022-05-19 13:32:04 +03:00
|
|
|
&resourceRaw.ID,
|
|
|
|
&resourceRaw.Filename,
|
|
|
|
&resourceRaw.Blob,
|
|
|
|
&resourceRaw.Type,
|
|
|
|
&resourceRaw.Size,
|
2022-08-06 20:30:48 +03:00
|
|
|
&resourceRaw.CreatorID,
|
2022-05-19 13:32:04 +03:00
|
|
|
&resourceRaw.CreatedTs,
|
|
|
|
&resourceRaw.UpdatedTs,
|
2022-02-03 10:32:03 +03:00
|
|
|
); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
2022-05-19 13:32:04 +03:00
|
|
|
resourceRawList = append(resourceRawList, &resourceRaw)
|
2021-12-14 15:08:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Err(); err != nil {
|
2022-02-03 10:32:03 +03:00
|
|
|
return nil, FormatError(err)
|
2021-12-14 15:08:12 +03:00
|
|
|
}
|
|
|
|
|
2022-05-19 13:32:04 +03:00
|
|
|
return resourceRawList, nil
|
2021-12-14 15:08:12 +03:00
|
|
|
}
|
|
|
|
|
2022-08-07 05:17:12 +03:00
|
|
|
func deleteResource(ctx context.Context, tx *sql.Tx, delete *api.ResourceDelete) error {
|
|
|
|
_, err := tx.ExecContext(ctx, `
|
2022-07-26 17:32:26 +03:00
|
|
|
PRAGMA foreign_keys = ON;
|
2022-08-06 20:30:48 +03:00
|
|
|
DELETE FROM resource WHERE id = ? AND creator_id = ?
|
|
|
|
`, delete.ID, delete.CreatorID)
|
2022-02-03 10:32:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return FormatError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-12-14 15:08:12 +03:00
|
|
|
}
|