memos/store/resource.go

345 lines
8.6 KiB
Go
Raw Normal View History

package store
2022-02-03 15:32:03 +08:00
import (
2022-08-07 10:17:12 +08:00
"context"
2022-05-22 00:59:22 +08:00
"database/sql"
2022-02-03 15:32:03 +08:00
"fmt"
"strings"
2022-06-27 22:09:06 +08:00
"github.com/usememos/memos/api"
"github.com/usememos/memos/common"
2022-02-03 15:32:03 +08:00
)
2022-05-19 18:32:04 +08: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
InternalPath string
ExternalLink string
Type string
Size int64
PublicID string
LinkedMemoAmount int
2022-05-19 18:32:04 +08: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
Filename: raw.Filename,
Blob: raw.Blob,
InternalPath: raw.InternalPath,
ExternalLink: raw.ExternalLink,
Type: raw.Type,
Size: raw.Size,
PublicID: raw.PublicID,
LinkedMemoAmount: raw.LinkedMemoAmount,
2022-05-19 18:32:04 +08:00
}
}
2022-08-07 10:17:12 +08: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 := createResourceImpl(ctx, tx, create)
2022-02-03 15:32:03 +08:00
if err != nil {
return nil, err
}
2022-08-07 10:17:12 +08:00
if err := tx.Commit(); err != nil {
return nil, FormatError(err)
}
2022-10-01 10:37:02 +08:00
resource := resourceRaw.toResource()
2022-02-03 15:32:03 +08:00
return resource, nil
}
2022-08-07 10:17:12 +08: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 := findResourceListImpl(ctx, tx, find)
2022-02-03 15:32:03 +08:00
if err != nil {
return nil, err
}
2022-05-19 18:32:04 +08:00
resourceList := []*api.Resource{}
for _, raw := range resourceRawList {
resourceList = append(resourceList, raw.toResource())
}
return resourceList, nil
}
2022-08-07 10:17:12 +08:00
func (s *Store) FindResource(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()
list, err := findResourceListImpl(ctx, tx, find)
2022-02-04 18:54:24 +08: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 10:37:02 +08:00
resourceRaw := list[0]
resource := resourceRaw.toResource()
2022-05-19 18:32:04 +08:00
return resource, nil
2022-02-04 18:54:24 +08:00
}
2022-08-07 10:17:12 +08: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 12:21:58 +08:00
if err := deleteResource(ctx, tx, delete); err != nil {
return err
}
if err := s.vacuumImpl(ctx, tx); err != nil {
2022-02-03 15:32:03 +08:00
return err
}
2022-08-07 10:17:12 +08:00
if err := tx.Commit(); err != nil {
return FormatError(err)
}
2022-02-03 15:32:03 +08:00
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 := patchResourceImpl(ctx, tx, patch)
if err != nil {
return nil, err
}
if err := tx.Commit(); err != nil {
return nil, FormatError(err)
}
resource := resourceRaw.toResource()
return resource, nil
}
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 10:17:12 +08:00
query := `
2022-02-03 15:32:03 +08:00
INSERT INTO resource (
` + strings.Join(fields, ",") + `
2022-02-03 15:32:03 +08:00
)
VALUES (` + strings.Join(placeholders, ",") + `)
RETURNING id, ` + strings.Join(fields, ",") + `, created_ts, updated_ts
2022-08-07 10:17:12 +08:00
`
2022-05-19 18:32:04 +08:00
var resourceRaw resourceRaw
dests := []any{
2022-05-19 18:32:04 +08:00
&resourceRaw.ID,
&resourceRaw.Filename,
&resourceRaw.Blob,
&resourceRaw.ExternalLink,
2022-05-19 18:32:04 +08:00
&resourceRaw.Type,
&resourceRaw.Size,
&resourceRaw.CreatorID,
&resourceRaw.InternalPath,
&resourceRaw.PublicID,
}
dests = append(dests, []any{&resourceRaw.CreatedTs, &resourceRaw.UpdatedTs}...)
if err := tx.QueryRowContext(ctx, query, values...).Scan(dests...); err != nil {
return nil, FormatError(err)
}
return &resourceRaw, nil
}
func patchResourceImpl(ctx context.Context, tx *sql.Tx, patch *api.ResourcePatch) (*resourceRaw, error) {
set, args := []string{}, []any{}
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)
}
if v := patch.PublicID; v != nil {
set, args = append(set, "public_id = ?"), append(args, *v)
}
args = append(args, patch.ID)
fields := []string{"id", "filename", "external_link", "type", "size", "creator_id", "created_ts", "updated_ts", "internal_path", "public_id"}
query := `
UPDATE resource
SET ` + strings.Join(set, ", ") + `
WHERE id = ?
RETURNING ` + strings.Join(fields, ", ")
var resourceRaw resourceRaw
dests := []any{
&resourceRaw.ID,
&resourceRaw.Filename,
&resourceRaw.ExternalLink,
&resourceRaw.Type,
&resourceRaw.Size,
2022-08-07 01:30:48 +08:00
&resourceRaw.CreatorID,
2022-05-19 18:32:04 +08:00
&resourceRaw.CreatedTs,
&resourceRaw.UpdatedTs,
&resourceRaw.InternalPath,
&resourceRaw.PublicID,
}
if err := tx.QueryRowContext(ctx, query, args...).Scan(dests...); err != nil {
2022-02-03 15:32:03 +08:00
return nil, FormatError(err)
}
2022-05-19 18:32:04 +08:00
return &resourceRaw, nil
2022-02-03 15:32:03 +08:00
}
func findResourceListImpl(ctx context.Context, tx *sql.Tx, find *api.ResourceFind) ([]*resourceRaw, error) {
where, args := []string{"1 = 1"}, []any{}
2022-02-06 11:26:53 +08:00
2022-05-03 02:05:43 +08:00
if v := find.ID; v != nil {
where, args = append(where, "resource.id = ?"), append(args, *v)
2022-02-03 15:32:03 +08:00
}
2022-05-03 02:05:43 +08:00
if v := find.CreatorID; v != nil {
where, args = append(where, "resource.creator_id = ?"), append(args, *v)
2022-02-03 15:32:03 +08:00
}
if v := find.Filename; v != nil {
where, args = append(where, "resource.filename = ?"), append(args, *v)
2022-02-03 15:32:03 +08:00
}
2022-09-30 22:58:59 +08:00
if v := find.MemoID; v != nil {
where, args = append(where, "resource.id in (SELECT resource_id FROM memo_resource WHERE memo_id = ?)"), append(args, *v)
2022-09-30 22:58:59 +08:00
}
if v := find.PublicID; v != nil {
where, args = append(where, "resource.public_id = ?"), append(args, *v)
}
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"}
if find.GetBlob {
fields = append(fields, "resource.blob")
}
query := fmt.Sprintf(`
2022-02-03 15:32:03 +08:00
SELECT
COUNT(DISTINCT memo_resource.memo_id) AS linked_memo_amount,
%s
2022-02-03 15:32:03 +08:00
FROM resource
LEFT JOIN memo_resource ON resource.id = memo_resource.resource_id
WHERE %s
GROUP BY resource.id
ORDER BY resource.id DESC
`, strings.Join(fields, ", "), strings.Join(where, " AND "))
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 10:17:12 +08:00
rows, err := tx.QueryContext(ctx, query, args...)
2022-02-03 15:32:03 +08:00
if err != nil {
return nil, FormatError(err)
}
defer rows.Close()
2022-05-19 18:32:04 +08:00
resourceRawList := make([]*resourceRaw, 0)
for rows.Next() {
2022-05-19 18:32:04 +08:00
var resourceRaw resourceRaw
dests := []any{
&resourceRaw.LinkedMemoAmount,
2022-05-19 18:32:04 +08:00
&resourceRaw.ID,
&resourceRaw.Filename,
&resourceRaw.ExternalLink,
2022-05-19 18:32:04 +08:00
&resourceRaw.Type,
&resourceRaw.Size,
2022-08-07 01:30:48 +08:00
&resourceRaw.CreatorID,
2022-05-19 18:32:04 +08:00
&resourceRaw.CreatedTs,
&resourceRaw.UpdatedTs,
&resourceRaw.InternalPath,
&resourceRaw.PublicID,
}
if find.GetBlob {
dests = append(dests, &resourceRaw.Blob)
}
if err := rows.Scan(dests...); err != nil {
2022-02-03 15:32:03 +08:00
return nil, FormatError(err)
}
2022-05-19 18:32:04 +08:00
resourceRawList = append(resourceRawList, &resourceRaw)
}
if err := rows.Err(); err != nil {
2022-02-03 15:32:03 +08:00
return nil, FormatError(err)
}
2022-05-19 18:32:04 +08:00
return resourceRawList, nil
}
2022-08-07 10:17:12 +08:00
func deleteResource(ctx context.Context, tx *sql.Tx, delete *api.ResourceDelete) error {
where, args := []string{"id = ?"}, []any{delete.ID}
2022-11-06 12:21:58 +08:00
stmt := `DELETE FROM resource WHERE ` + strings.Join(where, " AND ")
result, err := tx.ExecContext(ctx, stmt, args...)
2022-02-03 15:32:03 +08:00
if err != nil {
return FormatError(err)
}
rows, _ := result.RowsAffected()
if rows == 0 {
2022-11-06 12:21:58 +08: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-02-03 15:32:03 +08:00
return nil
}