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
|
2023-03-14 19:04:52 +03:00
|
|
|
Filename string
|
|
|
|
Blob []byte
|
2023-03-19 14:37:57 +03:00
|
|
|
InternalPath string
|
2023-03-14 19:04:52 +03:00
|
|
|
ExternalLink string
|
|
|
|
Type string
|
|
|
|
Size int64
|
2023-04-03 08:41:27 +03:00
|
|
|
PublicID string
|
2023-03-14 19:04:52 +03:00
|
|
|
LinkedMemoAmount int
|
2022-05-19 13:32:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2023-03-14 19:04:52 +03:00
|
|
|
Filename: raw.Filename,
|
|
|
|
Blob: raw.Blob,
|
2023-03-19 14:37:57 +03:00
|
|
|
InternalPath: raw.InternalPath,
|
2023-03-14 19:04:52 +03:00
|
|
|
ExternalLink: raw.ExternalLink,
|
|
|
|
Type: raw.Type,
|
|
|
|
Size: raw.Size,
|
2023-04-03 08:41:27 +03:00
|
|
|
PublicID: raw.PublicID,
|
2023-03-14 19:04:52 +03:00
|
|
|
LinkedMemoAmount: raw.LinkedMemoAmount,
|
2022-05-19 13:32:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
2023-04-03 09:13:22 +03:00
|
|
|
resourceRaw, err := createResourceImpl(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-10-01 05:37:02 +03:00
|
|
|
resource := resourceRaw.toResource()
|
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
return resource, nil
|
|
|
|
}
|
2021-12-14 15:08:12 +03:00
|
|
|
|
2023-05-25 19:38:27 +03:00
|
|
|
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 := patchResourceImpl(ctx, tx, patch)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.resourceCache.Store(resourceRaw.ID, resourceRaw)
|
|
|
|
resource := resourceRaw.toResource()
|
|
|
|
|
|
|
|
return resource, nil
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
2023-04-03 09:13:22 +03:00
|
|
|
resourceRawList, err := findResourceListImpl(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 {
|
2023-05-25 19:38:27 +03:00
|
|
|
if !find.GetBlob {
|
|
|
|
s.resourceCache.Store(raw.ID, raw)
|
|
|
|
}
|
2022-05-19 13:32:04 +03:00
|
|
|
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) {
|
2023-05-25 19:38:27 +03:00
|
|
|
if !find.GetBlob && find.ID != nil {
|
|
|
|
if raw, ok := s.resourceCache.Load(find.ID); ok {
|
|
|
|
return raw.(*resourceRaw).toResource(), 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()
|
|
|
|
|
2023-04-03 09:13:22 +03:00
|
|
|
list, err := findResourceListImpl(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-10-01 05:37:02 +03:00
|
|
|
resourceRaw := list[0]
|
2023-05-25 19:38:27 +03:00
|
|
|
if !find.GetBlob {
|
|
|
|
s.resourceCache.Store(resourceRaw.ID, resourceRaw)
|
|
|
|
}
|
2022-10-01 05:37:02 +03:00
|
|
|
resource := resourceRaw.toResource()
|
|
|
|
|
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()
|
|
|
|
|
2022-11-06 07:21:58 +03:00
|
|
|
if err := deleteResource(ctx, tx, delete); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-09 04:02:59 +03:00
|
|
|
if err := s.vacuumImpl(ctx, tx); err != nil {
|
2022-02-03 10:32:03 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-07 05:17:12 +03:00
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return FormatError(err)
|
|
|
|
}
|
2023-05-25 19:38:27 +03:00
|
|
|
s.resourceCache.Delete(delete.ID)
|
2022-08-07 05:17:12 +03:00
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-03 09:13:22 +03:00
|
|
|
func createResourceImpl(ctx context.Context, tx *sql.Tx, create *api.ResourceCreate) (*resourceRaw, error) {
|
|
|
|
fields := []string{"filename", "blob", "external_link", "type", "size", "creator_id", "internal_path", "public_id"}
|
|
|
|
values := []any{create.Filename, create.Blob, create.ExternalLink, create.Type, create.Size, create.CreatorID, create.InternalPath, create.PublicID}
|
|
|
|
placeholders := []string{"?", "?", "?", "?", "?", "?", "?", "?"}
|
2022-08-07 05:17:12 +03:00
|
|
|
query := `
|
2022-02-03 10:32:03 +03:00
|
|
|
INSERT INTO resource (
|
2023-02-27 16:26:50 +03:00
|
|
|
` + strings.Join(fields, ",") + `
|
2022-02-03 10:32:03 +03:00
|
|
|
)
|
2023-02-27 16:26:50 +03:00
|
|
|
VALUES (` + strings.Join(placeholders, ",") + `)
|
|
|
|
RETURNING id, ` + strings.Join(fields, ",") + `, created_ts, updated_ts
|
2022-08-07 05:17:12 +03:00
|
|
|
`
|
2022-05-19 13:32:04 +03:00
|
|
|
var resourceRaw resourceRaw
|
2023-03-18 17:34:22 +03:00
|
|
|
dests := []any{
|
2022-05-19 13:32:04 +03:00
|
|
|
&resourceRaw.ID,
|
|
|
|
&resourceRaw.Filename,
|
|
|
|
&resourceRaw.Blob,
|
2023-01-21 03:46:49 +03:00
|
|
|
&resourceRaw.ExternalLink,
|
2022-05-19 13:32:04 +03:00
|
|
|
&resourceRaw.Type,
|
|
|
|
&resourceRaw.Size,
|
2022-10-29 10:40:09 +03:00
|
|
|
&resourceRaw.CreatorID,
|
2023-04-03 09:13:22 +03:00
|
|
|
&resourceRaw.InternalPath,
|
|
|
|
&resourceRaw.PublicID,
|
2023-02-27 16:26:50 +03:00
|
|
|
}
|
2023-03-18 17:34:22 +03:00
|
|
|
dests = append(dests, []any{&resourceRaw.CreatedTs, &resourceRaw.UpdatedTs}...)
|
2023-02-27 16:26:50 +03:00
|
|
|
if err := tx.QueryRowContext(ctx, query, values...).Scan(dests...); err != nil {
|
2022-10-29 10:40:09 +03:00
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &resourceRaw, nil
|
|
|
|
}
|
|
|
|
|
2023-04-03 09:13:22 +03:00
|
|
|
func patchResourceImpl(ctx context.Context, tx *sql.Tx, patch *api.ResourcePatch) (*resourceRaw, error) {
|
2023-03-18 17:34:22 +03:00
|
|
|
set, args := []string{}, []any{}
|
2022-10-29 10:40:09 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2023-04-03 08:41:27 +03:00
|
|
|
if v := patch.PublicID; v != nil {
|
|
|
|
set, args = append(set, "public_id = ?"), append(args, *v)
|
|
|
|
}
|
2022-10-29 10:40:09 +03:00
|
|
|
|
|
|
|
args = append(args, patch.ID)
|
2023-04-03 09:13:22 +03:00
|
|
|
fields := []string{"id", "filename", "external_link", "type", "size", "creator_id", "created_ts", "updated_ts", "internal_path", "public_id"}
|
2022-10-29 10:40:09 +03:00
|
|
|
query := `
|
|
|
|
UPDATE resource
|
|
|
|
SET ` + strings.Join(set, ", ") + `
|
|
|
|
WHERE id = ?
|
2023-02-27 16:26:50 +03:00
|
|
|
RETURNING ` + strings.Join(fields, ", ")
|
2022-10-29 10:40:09 +03:00
|
|
|
var resourceRaw resourceRaw
|
2023-03-18 17:34:22 +03:00
|
|
|
dests := []any{
|
2022-10-29 10:40:09 +03:00
|
|
|
&resourceRaw.ID,
|
|
|
|
&resourceRaw.Filename,
|
2023-01-21 03:46:49 +03:00
|
|
|
&resourceRaw.ExternalLink,
|
2022-10-29 10:40:09 +03:00
|
|
|
&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,
|
2023-04-03 09:13:22 +03:00
|
|
|
&resourceRaw.InternalPath,
|
|
|
|
&resourceRaw.PublicID,
|
2023-02-27 16:26:50 +03:00
|
|
|
}
|
|
|
|
if err := tx.QueryRowContext(ctx, query, args...).Scan(dests...); err != nil {
|
2022-02-03 10:32:03 +03:00
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
2022-05-19 13:32:04 +03:00
|
|
|
return &resourceRaw, nil
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
|
2023-04-03 09:13:22 +03:00
|
|
|
func findResourceListImpl(ctx context.Context, tx *sql.Tx, find *api.ResourceFind) ([]*resourceRaw, error) {
|
2023-03-18 17:34:22 +03:00
|
|
|
where, args := []string{"1 = 1"}, []any{}
|
2022-02-06 06:26:53 +03:00
|
|
|
|
2022-05-02 21:05:43 +03:00
|
|
|
if v := find.ID; v != nil {
|
2023-03-14 19:04:52 +03:00
|
|
|
where, args = append(where, "resource.id = ?"), append(args, *v)
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
2022-05-02 21:05:43 +03:00
|
|
|
if v := find.CreatorID; v != nil {
|
2023-03-14 19:04:52 +03:00
|
|
|
where, args = append(where, "resource.creator_id = ?"), append(args, *v)
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
if v := find.Filename; v != nil {
|
2023-03-14 19:04:52 +03:00
|
|
|
where, args = append(where, "resource.filename = ?"), append(args, *v)
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
2022-09-30 17:58:59 +03:00
|
|
|
if v := find.MemoID; v != nil {
|
2023-03-14 19:04:52 +03:00
|
|
|
where, args = append(where, "resource.id in (SELECT resource_id FROM memo_resource WHERE memo_id = ?)"), append(args, *v)
|
2022-09-30 17:58:59 +03:00
|
|
|
}
|
2023-04-03 08:41:27 +03:00
|
|
|
if v := find.PublicID; v != nil {
|
|
|
|
where, args = append(where, "resource.public_id = ?"), append(args, *v)
|
|
|
|
}
|
2021-12-14 15:08:12 +03:00
|
|
|
|
2023-04-03 09:13:22 +03:00
|
|
|
fields := []string{"resource.id", "resource.filename", "resource.external_link", "resource.type", "resource.size", "resource.creator_id", "resource.created_ts", "resource.updated_ts", "internal_path", "public_id"}
|
2023-01-25 11:11:02 +03:00
|
|
|
if find.GetBlob {
|
2023-03-14 19:04:52 +03:00
|
|
|
fields = append(fields, "resource.blob")
|
2023-01-25 11:11:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
query := fmt.Sprintf(`
|
2022-02-03 10:32:03 +03:00
|
|
|
SELECT
|
2023-03-14 19:04:52 +03:00
|
|
|
COUNT(DISTINCT memo_resource.memo_id) AS linked_memo_amount,
|
2023-01-25 11:11:02 +03:00
|
|
|
%s
|
2022-02-03 10:32:03 +03:00
|
|
|
FROM resource
|
2023-03-14 19:04:52 +03:00
|
|
|
LEFT JOIN memo_resource ON resource.id = memo_resource.resource_id
|
2023-01-25 11:11:02 +03:00
|
|
|
WHERE %s
|
2023-03-14 19:04:52 +03:00
|
|
|
GROUP BY resource.id
|
|
|
|
ORDER BY resource.id DESC
|
2023-01-25 11:11:02 +03:00
|
|
|
`, strings.Join(fields, ", "), strings.Join(where, " AND "))
|
2023-04-01 11:51:20 +03:00
|
|
|
if find.Limit != nil {
|
|
|
|
query = fmt.Sprintf("%s LIMIT %d", query, *find.Limit)
|
|
|
|
if find.Offset != nil {
|
|
|
|
query = fmt.Sprintf("%s OFFSET %d", query, *find.Offset)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 05:17:12 +03:00
|
|
|
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
|
2023-03-18 17:34:22 +03:00
|
|
|
dests := []any{
|
2023-03-14 19:04:52 +03:00
|
|
|
&resourceRaw.LinkedMemoAmount,
|
2022-05-19 13:32:04 +03:00
|
|
|
&resourceRaw.ID,
|
|
|
|
&resourceRaw.Filename,
|
2023-01-21 03:46:49 +03:00
|
|
|
&resourceRaw.ExternalLink,
|
2022-05-19 13:32:04 +03:00
|
|
|
&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,
|
2023-04-03 09:13:22 +03:00
|
|
|
&resourceRaw.InternalPath,
|
|
|
|
&resourceRaw.PublicID,
|
2023-01-25 11:11:02 +03:00
|
|
|
}
|
|
|
|
if find.GetBlob {
|
2023-02-27 16:26:50 +03:00
|
|
|
dests = append(dests, &resourceRaw.Blob)
|
|
|
|
}
|
|
|
|
if err := rows.Scan(dests...); err != nil {
|
2022-02-03 10:32:03 +03:00
|
|
|
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 {
|
2023-03-18 17:34:22 +03:00
|
|
|
where, args := []string{"id = ?"}, []any{delete.ID}
|
2022-11-06 07:21:58 +03:00
|
|
|
|
|
|
|
stmt := `DELETE FROM resource WHERE ` + strings.Join(where, " AND ")
|
|
|
|
result, err := tx.ExecContext(ctx, stmt, args...)
|
2022-02-03 10:32:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return FormatError(err)
|
|
|
|
}
|
|
|
|
|
2022-09-03 13:54:22 +03:00
|
|
|
rows, _ := result.RowsAffected()
|
|
|
|
if rows == 0 {
|
2022-11-06 07:21:58 +03:00
|
|
|
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)
|
2022-09-03 13:54:22 +03:00
|
|
|
}
|
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
return nil
|
2021-12-14 15:08:12 +03:00
|
|
|
}
|