2023-02-11 15:31:39 +03:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2023-02-23 19:02:51 +03:00
|
|
|
"encoding/json"
|
2023-02-11 15:31:39 +03:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/usememos/memos/api"
|
|
|
|
"github.com/usememos/memos/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
type storageRaw struct {
|
2023-02-23 19:02:51 +03:00
|
|
|
ID int
|
|
|
|
Name string
|
|
|
|
Type api.StorageType
|
|
|
|
Config *api.StorageConfig
|
2023-02-11 15:31:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (raw *storageRaw) toStorage() *api.Storage {
|
|
|
|
return &api.Storage{
|
2023-02-23 19:02:51 +03:00
|
|
|
ID: raw.ID,
|
|
|
|
Name: raw.Name,
|
|
|
|
Type: raw.Type,
|
|
|
|
Config: raw.Config,
|
2023-02-11 15:31:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) CreateStorage(ctx context.Context, create *api.StorageCreate) (*api.Storage, error) {
|
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
storageRaw, err := createStorageRaw(ctx, tx, create)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return storageRaw.toStorage(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) PatchStorage(ctx context.Context, patch *api.StoragePatch) (*api.Storage, error) {
|
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
storageRaw, err := patchStorageRaw(ctx, tx, patch)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return storageRaw.toStorage(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) FindStorageList(ctx context.Context, find *api.StorageFind) ([]*api.Storage, error) {
|
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
storageRawList, err := findStorageRawList(ctx, tx, find)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
list := []*api.Storage{}
|
|
|
|
for _, raw := range storageRawList {
|
|
|
|
list = append(list, raw.toStorage())
|
|
|
|
}
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
2023-02-13 14:36:48 +03:00
|
|
|
func (s *Store) FindStorage(ctx context.Context, find *api.StorageFind) (*api.Storage, error) {
|
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
list, err := findStorageRawList(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")}
|
|
|
|
}
|
|
|
|
|
|
|
|
storageRaw := list[0]
|
|
|
|
return storageRaw.toStorage(), nil
|
|
|
|
}
|
|
|
|
|
2023-02-11 15:31:39 +03:00
|
|
|
func (s *Store) DeleteStorage(ctx context.Context, delete *api.StorageDelete) error {
|
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return FormatError(err)
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
if err := deleteStorage(ctx, tx, delete); err != nil {
|
|
|
|
return FormatError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return FormatError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createStorageRaw(ctx context.Context, tx *sql.Tx, create *api.StorageCreate) (*storageRaw, error) {
|
2023-02-23 19:02:51 +03:00
|
|
|
set := []string{"name", "type", "config"}
|
2023-03-18 17:34:22 +03:00
|
|
|
args := []any{create.Name, create.Type}
|
2023-02-23 19:02:51 +03:00
|
|
|
placeholder := []string{"?", "?", "?"}
|
|
|
|
|
|
|
|
var configBytes []byte
|
|
|
|
var err error
|
|
|
|
if create.Type == api.StorageS3 {
|
|
|
|
configBytes, err = json.Marshal(create.Config.S3Config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("unsupported storage type %s", string(create.Type))
|
|
|
|
}
|
|
|
|
args = append(args, string(configBytes))
|
2023-02-11 15:31:39 +03:00
|
|
|
|
|
|
|
query := `
|
|
|
|
INSERT INTO storage (
|
|
|
|
` + strings.Join(set, ", ") + `
|
|
|
|
)
|
|
|
|
VALUES (` + strings.Join(placeholder, ",") + `)
|
2023-02-23 19:02:51 +03:00
|
|
|
RETURNING id
|
2023-02-11 15:31:39 +03:00
|
|
|
`
|
2023-02-23 19:02:51 +03:00
|
|
|
storageRaw := storageRaw{
|
|
|
|
Name: create.Name,
|
|
|
|
Type: create.Type,
|
|
|
|
Config: create.Config,
|
|
|
|
}
|
2023-02-11 15:31:39 +03:00
|
|
|
if err := tx.QueryRowContext(ctx, query, args...).Scan(
|
|
|
|
&storageRaw.ID,
|
|
|
|
); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &storageRaw, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func patchStorageRaw(ctx context.Context, tx *sql.Tx, patch *api.StoragePatch) (*storageRaw, error) {
|
2023-03-18 17:34:22 +03:00
|
|
|
set, args := []string{}, []any{}
|
2023-02-11 15:31:39 +03:00
|
|
|
if v := patch.Name; v != nil {
|
|
|
|
set, args = append(set, "name = ?"), append(args, *v)
|
|
|
|
}
|
2023-02-23 19:02:51 +03:00
|
|
|
if v := patch.Config; v != nil {
|
|
|
|
var configBytes []byte
|
|
|
|
var err error
|
|
|
|
if patch.Type == api.StorageS3 {
|
|
|
|
configBytes, err = json.Marshal(patch.Config.S3Config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("unsupported storage type %s", string(patch.Type))
|
|
|
|
}
|
|
|
|
set, args = append(set, "config = ?"), append(args, string(configBytes))
|
2023-02-13 14:36:48 +03:00
|
|
|
}
|
2023-02-11 15:31:39 +03:00
|
|
|
args = append(args, patch.ID)
|
|
|
|
|
|
|
|
query := `
|
|
|
|
UPDATE storage
|
|
|
|
SET ` + strings.Join(set, ", ") + `
|
|
|
|
WHERE id = ?
|
2023-02-23 19:02:51 +03:00
|
|
|
RETURNING id, name, type, config
|
2023-02-11 15:31:39 +03:00
|
|
|
`
|
|
|
|
var storageRaw storageRaw
|
2023-02-23 19:02:51 +03:00
|
|
|
var storageConfig string
|
2023-02-11 15:31:39 +03:00
|
|
|
if err := tx.QueryRowContext(ctx, query, args...).Scan(
|
|
|
|
&storageRaw.ID,
|
|
|
|
&storageRaw.Name,
|
2023-02-23 19:02:51 +03:00
|
|
|
&storageRaw.Type,
|
|
|
|
&storageConfig,
|
2023-02-11 15:31:39 +03:00
|
|
|
); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
2023-02-23 19:02:51 +03:00
|
|
|
if storageRaw.Type == api.StorageS3 {
|
|
|
|
s3Config := &api.StorageS3Config{}
|
|
|
|
if err := json.Unmarshal([]byte(storageConfig), s3Config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
storageRaw.Config = &api.StorageConfig{
|
|
|
|
S3Config: s3Config,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("unsupported storage type %s", string(storageRaw.Type))
|
|
|
|
}
|
2023-02-11 15:31:39 +03:00
|
|
|
|
|
|
|
return &storageRaw, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func findStorageRawList(ctx context.Context, tx *sql.Tx, find *api.StorageFind) ([]*storageRaw, error) {
|
2023-03-18 17:34:22 +03:00
|
|
|
where, args := []string{"1 = 1"}, []any{}
|
2023-02-11 15:31:39 +03:00
|
|
|
|
2023-02-14 04:56:04 +03:00
|
|
|
if v := find.ID; v != nil {
|
|
|
|
where, args = append(where, "id = ?"), append(args, *v)
|
|
|
|
}
|
2023-02-11 15:31:39 +03:00
|
|
|
|
|
|
|
query := `
|
|
|
|
SELECT
|
|
|
|
id,
|
|
|
|
name,
|
2023-02-23 19:02:51 +03:00
|
|
|
type,
|
|
|
|
config
|
2023-02-11 15:31:39 +03:00
|
|
|
FROM storage
|
|
|
|
WHERE ` + strings.Join(where, " AND ") + `
|
2023-02-17 15:12:08 +03:00
|
|
|
ORDER BY id DESC
|
2023-02-11 15:31:39 +03:00
|
|
|
`
|
|
|
|
rows, err := tx.QueryContext(ctx, query, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
storageRawList := make([]*storageRaw, 0)
|
|
|
|
for rows.Next() {
|
|
|
|
var storageRaw storageRaw
|
2023-02-23 19:02:51 +03:00
|
|
|
var storageConfig string
|
2023-02-11 15:31:39 +03:00
|
|
|
if err := rows.Scan(
|
|
|
|
&storageRaw.ID,
|
|
|
|
&storageRaw.Name,
|
2023-02-23 19:02:51 +03:00
|
|
|
&storageRaw.Type,
|
|
|
|
&storageConfig,
|
2023-02-11 15:31:39 +03:00
|
|
|
); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
2023-02-23 19:02:51 +03:00
|
|
|
if storageRaw.Type == api.StorageS3 {
|
|
|
|
s3Config := &api.StorageS3Config{}
|
|
|
|
if err := json.Unmarshal([]byte(storageConfig), s3Config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
storageRaw.Config = &api.StorageConfig{
|
|
|
|
S3Config: s3Config,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("unsupported storage type %s", string(storageRaw.Type))
|
|
|
|
}
|
2023-02-11 15:31:39 +03:00
|
|
|
storageRawList = append(storageRawList, &storageRaw)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return storageRawList, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteStorage(ctx context.Context, tx *sql.Tx, delete *api.StorageDelete) error {
|
2023-03-18 17:34:22 +03:00
|
|
|
where, args := []string{"id = ?"}, []any{delete.ID}
|
2023-02-11 15:31:39 +03:00
|
|
|
|
|
|
|
stmt := `DELETE FROM storage 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("storage not found")}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|