2023-02-11 20:31:39 +08:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-04-11 17:53:00 +08:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"google.golang.org/protobuf/proto"
|
2024-04-12 08:32:54 +08:00
|
|
|
|
|
|
|
storepb "github.com/usememos/memos/proto/gen/store"
|
2023-02-11 20:31:39 +08:00
|
|
|
)
|
|
|
|
|
2023-07-04 10:05:57 +08:00
|
|
|
type Storage struct {
|
2023-08-04 21:55:07 +08:00
|
|
|
ID int32
|
2023-02-24 00:02:51 +08:00
|
|
|
Name string
|
2023-07-04 10:05:57 +08:00
|
|
|
Type string
|
|
|
|
Config string
|
2023-02-11 20:31:39 +08:00
|
|
|
}
|
|
|
|
|
2023-07-04 10:05:57 +08:00
|
|
|
type FindStorage struct {
|
2023-08-04 21:55:07 +08:00
|
|
|
ID *int32
|
2023-02-11 20:31:39 +08:00
|
|
|
}
|
|
|
|
|
2023-07-04 10:05:57 +08:00
|
|
|
type UpdateStorage struct {
|
2023-08-04 21:55:07 +08:00
|
|
|
ID int32
|
2023-07-04 10:05:57 +08:00
|
|
|
Name *string
|
|
|
|
Config *string
|
|
|
|
}
|
2023-02-11 20:31:39 +08:00
|
|
|
|
2023-07-04 10:05:57 +08:00
|
|
|
type DeleteStorage struct {
|
2023-08-04 21:55:07 +08:00
|
|
|
ID int32
|
2023-02-11 20:31:39 +08:00
|
|
|
}
|
|
|
|
|
2023-07-04 10:05:57 +08:00
|
|
|
func (s *Store) CreateStorage(ctx context.Context, create *Storage) (*Storage, error) {
|
2023-09-26 19:43:55 +08:00
|
|
|
return s.driver.CreateStorage(ctx, create)
|
2023-02-11 20:31:39 +08:00
|
|
|
}
|
|
|
|
|
2023-07-04 10:05:57 +08:00
|
|
|
func (s *Store) ListStorages(ctx context.Context, find *FindStorage) ([]*Storage, error) {
|
2023-09-26 19:43:55 +08:00
|
|
|
return s.driver.ListStorages(ctx, find)
|
2023-02-11 20:31:39 +08:00
|
|
|
}
|
|
|
|
|
2023-07-04 10:05:57 +08:00
|
|
|
func (s *Store) GetStorage(ctx context.Context, find *FindStorage) (*Storage, error) {
|
2023-07-20 23:15:56 +08:00
|
|
|
list, err := s.ListStorages(ctx, find)
|
2023-02-13 19:36:48 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(list) == 0 {
|
2023-07-04 10:05:57 +08:00
|
|
|
return nil, nil
|
2023-02-13 19:36:48 +08:00
|
|
|
}
|
|
|
|
|
2023-07-04 10:05:57 +08:00
|
|
|
return list[0], nil
|
2023-02-13 19:36:48 +08:00
|
|
|
}
|
|
|
|
|
2023-07-04 10:05:57 +08:00
|
|
|
func (s *Store) UpdateStorage(ctx context.Context, update *UpdateStorage) (*Storage, error) {
|
2023-09-26 19:43:55 +08:00
|
|
|
return s.driver.UpdateStorage(ctx, update)
|
2023-02-11 20:31:39 +08:00
|
|
|
}
|
|
|
|
|
2023-07-04 10:05:57 +08:00
|
|
|
func (s *Store) DeleteStorage(ctx context.Context, delete *DeleteStorage) error {
|
2023-09-26 19:43:55 +08:00
|
|
|
return s.driver.DeleteStorage(ctx, delete)
|
2023-02-11 20:31:39 +08:00
|
|
|
}
|
2024-04-11 17:53:00 +08:00
|
|
|
|
|
|
|
func (s *Store) CreateStorageV1(ctx context.Context, create *storepb.Storage) (*storepb.Storage, error) {
|
|
|
|
storageRaw := &Storage{
|
|
|
|
Name: create.Name,
|
|
|
|
Type: create.Type.String(),
|
|
|
|
}
|
|
|
|
|
2024-04-11 17:58:49 +08:00
|
|
|
if create.Type == storepb.Storage_S3 {
|
2024-04-11 17:53:00 +08:00
|
|
|
configBytes, err := proto.Marshal(create.Config.GetS3Config())
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to marshal s3 config")
|
|
|
|
}
|
|
|
|
storageRaw.Config = string(configBytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
storageRaw, err := s.driver.CreateStorage(ctx, storageRaw)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
storage, err := convertStorageFromRaw(storageRaw)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return storage, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) ListStoragesV1(ctx context.Context, find *FindStorage) ([]*storepb.Storage, error) {
|
|
|
|
list, err := s.driver.ListStorages(ctx, find)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
storages := []*storepb.Storage{}
|
|
|
|
for _, storageRaw := range list {
|
|
|
|
storage, err := convertStorageFromRaw(storageRaw)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
storages = append(storages, storage)
|
|
|
|
}
|
|
|
|
return storages, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) GetStorageV1(ctx context.Context, find *FindStorage) (*storepb.Storage, error) {
|
|
|
|
list, err := s.ListStoragesV1(ctx, find)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(list) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return list[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type UpdateStorageV1 struct {
|
|
|
|
ID int32
|
|
|
|
Type storepb.Storage_Type
|
|
|
|
Name *string
|
|
|
|
Config *storepb.StorageConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) UpdateStorageV1(ctx context.Context, update *UpdateStorageV1) (*storepb.Storage, error) {
|
|
|
|
updateRaw := &UpdateStorage{
|
|
|
|
ID: update.ID,
|
|
|
|
}
|
|
|
|
if update.Name != nil {
|
|
|
|
updateRaw.Name = update.Name
|
|
|
|
}
|
|
|
|
if update.Config != nil {
|
|
|
|
configRaw, err := convertStorageConfigToRaw(update.Type, update.Config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
updateRaw.Config = &configRaw
|
|
|
|
}
|
|
|
|
storageRaw, err := s.driver.UpdateStorage(ctx, updateRaw)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
storage, err := convertStorageFromRaw(storageRaw)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return storage, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertStorageFromRaw(storageRaw *Storage) (*storepb.Storage, error) {
|
|
|
|
storage := &storepb.Storage{
|
|
|
|
Id: storageRaw.ID,
|
|
|
|
Name: storageRaw.Name,
|
|
|
|
Type: storepb.Storage_Type(storepb.Storage_Type_value[storageRaw.Type]),
|
|
|
|
}
|
|
|
|
storageConfig, err := convertStorageConfigFromRaw(storage.Type, storageRaw.Config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
storage.Config = storageConfig
|
|
|
|
return storage, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertStorageConfigFromRaw(storageType storepb.Storage_Type, configRaw string) (*storepb.StorageConfig, error) {
|
|
|
|
storageConfig := &storepb.StorageConfig{}
|
2024-04-11 17:58:49 +08:00
|
|
|
if storageType == storepb.Storage_S3 {
|
2024-04-11 17:53:00 +08:00
|
|
|
s3Config := &storepb.S3Config{}
|
|
|
|
err := proto.Unmarshal([]byte(configRaw), s3Config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
storageConfig.StorageConfig = &storepb.StorageConfig_S3Config{S3Config: s3Config}
|
|
|
|
}
|
|
|
|
return storageConfig, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertStorageConfigToRaw(storageType storepb.Storage_Type, config *storepb.StorageConfig) (string, error) {
|
|
|
|
raw := ""
|
2024-04-11 17:58:49 +08:00
|
|
|
if storageType == storepb.Storage_S3 {
|
2024-04-11 17:53:00 +08:00
|
|
|
bytes, err := proto.Marshal(config.GetS3Config())
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
raw = string(bytes)
|
|
|
|
}
|
|
|
|
return raw, nil
|
|
|
|
}
|