2023-09-16 00:11:07 +08:00
|
|
|
package v2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-01-15 08:13:06 +08:00
|
|
|
"net/url"
|
2023-09-16 11:48:53 +08:00
|
|
|
"time"
|
2023-09-16 00:11:07 +08:00
|
|
|
|
2024-01-21 10:49:30 +08:00
|
|
|
"github.com/lithammer/shortuuid/v4"
|
2023-09-16 00:11:07 +08:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2023-09-16 11:48:53 +08:00
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
2023-09-17 22:55:13 +08:00
|
|
|
|
|
|
|
apiv2pb "github.com/usememos/memos/proto/gen/api/v2"
|
2024-01-27 05:26:32 +08:00
|
|
|
"github.com/usememos/memos/server/service/metric"
|
2023-09-17 22:55:13 +08:00
|
|
|
"github.com/usememos/memos/store"
|
2023-09-16 00:11:07 +08:00
|
|
|
)
|
|
|
|
|
2024-01-15 08:13:06 +08:00
|
|
|
func (s *APIV2Service) CreateResource(ctx context.Context, request *apiv2pb.CreateResourceRequest) (*apiv2pb.CreateResourceResponse, error) {
|
|
|
|
user, err := getCurrentUser(ctx, s.Store)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed to get current user: %v", err)
|
|
|
|
}
|
|
|
|
if request.ExternalLink != "" {
|
|
|
|
// Only allow those external links scheme with http/https
|
|
|
|
linkURL, err := url.Parse(request.ExternalLink)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "invalid external link: %v", err)
|
|
|
|
}
|
|
|
|
if linkURL.Scheme != "http" && linkURL.Scheme != "https" {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "invalid external link scheme: %v", linkURL.Scheme)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
create := &store.Resource{
|
2024-01-21 10:49:30 +08:00
|
|
|
ResourceName: shortuuid.New(),
|
2024-01-15 08:13:06 +08:00
|
|
|
CreatorID: user.ID,
|
|
|
|
Filename: request.Filename,
|
|
|
|
ExternalLink: request.ExternalLink,
|
|
|
|
Type: request.Type,
|
|
|
|
}
|
|
|
|
if request.MemoId != nil {
|
|
|
|
create.MemoID = request.MemoId
|
|
|
|
}
|
|
|
|
resource, err := s.Store.CreateResource(ctx, create)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed to create resource: %v", err)
|
|
|
|
}
|
2024-01-27 05:26:32 +08:00
|
|
|
|
|
|
|
metric.Enqueue("resource create")
|
2024-01-15 08:13:06 +08:00
|
|
|
return &apiv2pb.CreateResourceResponse{
|
|
|
|
Resource: s.convertResourceFromStore(ctx, resource),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-10-27 09:07:35 +08:00
|
|
|
func (s *APIV2Service) ListResources(ctx context.Context, _ *apiv2pb.ListResourcesRequest) (*apiv2pb.ListResourcesResponse, error) {
|
2023-09-16 00:11:07 +08:00
|
|
|
user, err := getCurrentUser(ctx, s.Store)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed to get current user: %v", err)
|
|
|
|
}
|
|
|
|
resources, err := s.Store.ListResources(ctx, &store.FindResource{
|
2023-09-27 08:09:30 +08:00
|
|
|
CreatorID: &user.ID,
|
2023-09-16 00:11:07 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
2023-09-19 08:24:24 +08:00
|
|
|
return nil, status.Errorf(codes.Internal, "failed to list resources: %v", err)
|
2023-09-16 00:11:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
response := &apiv2pb.ListResourcesResponse{}
|
|
|
|
for _, resource := range resources {
|
2023-10-05 13:36:33 +08:00
|
|
|
response.Resources = append(response.Resources, s.convertResourceFromStore(ctx, resource))
|
2023-09-16 00:11:07 +08:00
|
|
|
}
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
2024-01-20 09:17:31 +08:00
|
|
|
func (s *APIV2Service) GetResource(ctx context.Context, request *apiv2pb.GetResourceRequest) (*apiv2pb.GetResourceResponse, error) {
|
|
|
|
resource, err := s.Store.GetResource(ctx, &store.FindResource{
|
|
|
|
ID: &request.Id,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-01-21 21:27:04 +08:00
|
|
|
return nil, status.Errorf(codes.Internal, "failed to get resource: %v", err)
|
2024-01-20 09:17:31 +08:00
|
|
|
}
|
2024-01-20 12:47:43 +08:00
|
|
|
if resource == nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "resource not found")
|
|
|
|
}
|
2024-01-20 09:17:31 +08:00
|
|
|
|
|
|
|
return &apiv2pb.GetResourceResponse{
|
|
|
|
Resource: s.convertResourceFromStore(ctx, resource),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-01-21 21:27:04 +08:00
|
|
|
func (s *APIV2Service) GetResourceByName(ctx context.Context, request *apiv2pb.GetResourceByNameRequest) (*apiv2pb.GetResourceByNameResponse, error) {
|
|
|
|
resource, err := s.Store.GetResource(ctx, &store.FindResource{
|
|
|
|
ResourceName: &request.Name,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed to get resource: %v", err)
|
|
|
|
}
|
|
|
|
if resource == nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "resource not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &apiv2pb.GetResourceByNameResponse{
|
|
|
|
Resource: s.convertResourceFromStore(ctx, resource),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-10-27 09:07:35 +08:00
|
|
|
func (s *APIV2Service) UpdateResource(ctx context.Context, request *apiv2pb.UpdateResourceRequest) (*apiv2pb.UpdateResourceResponse, error) {
|
2023-10-21 12:41:55 +08:00
|
|
|
if request.UpdateMask == nil || len(request.UpdateMask.Paths) == 0 {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "update mask is required")
|
|
|
|
}
|
|
|
|
|
2023-10-03 23:44:14 +08:00
|
|
|
currentTs := time.Now().Unix()
|
|
|
|
update := &store.UpdateResource{
|
2023-10-21 12:41:55 +08:00
|
|
|
ID: request.Resource.Id,
|
2023-10-03 23:44:14 +08:00
|
|
|
UpdatedTs: ¤tTs,
|
|
|
|
}
|
2023-10-21 12:41:55 +08:00
|
|
|
for _, field := range request.UpdateMask.Paths {
|
2023-10-03 23:44:14 +08:00
|
|
|
if field == "filename" {
|
|
|
|
update.Filename = &request.Resource.Filename
|
|
|
|
} else if field == "memo_id" {
|
|
|
|
update.MemoID = request.Resource.MemoId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource, err := s.Store.UpdateResource(ctx, update)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed to update resource: %v", err)
|
|
|
|
}
|
|
|
|
return &apiv2pb.UpdateResourceResponse{
|
2023-10-05 13:36:33 +08:00
|
|
|
Resource: s.convertResourceFromStore(ctx, resource),
|
2023-10-03 23:44:14 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-10-27 09:07:35 +08:00
|
|
|
func (s *APIV2Service) DeleteResource(ctx context.Context, request *apiv2pb.DeleteResourceRequest) (*apiv2pb.DeleteResourceResponse, error) {
|
2023-09-27 08:09:30 +08:00
|
|
|
user, err := getCurrentUser(ctx, s.Store)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed to get current user: %v", err)
|
|
|
|
}
|
|
|
|
resource, err := s.Store.GetResource(ctx, &store.FindResource{
|
|
|
|
ID: &request.Id,
|
|
|
|
CreatorID: &user.ID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed to find resource: %v", err)
|
|
|
|
}
|
|
|
|
if resource == nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "resource not found")
|
|
|
|
}
|
2023-10-06 19:02:40 +08:00
|
|
|
// Delete the resource from the database.
|
2023-09-27 08:09:30 +08:00
|
|
|
if err := s.Store.DeleteResource(ctx, &store.DeleteResource{
|
|
|
|
ID: resource.ID,
|
|
|
|
}); err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed to delete resource: %v", err)
|
|
|
|
}
|
|
|
|
return &apiv2pb.DeleteResourceResponse{}, nil
|
|
|
|
}
|
|
|
|
|
2023-10-27 09:07:35 +08:00
|
|
|
func (s *APIV2Service) convertResourceFromStore(ctx context.Context, resource *store.Resource) *apiv2pb.Resource {
|
2023-10-05 13:36:33 +08:00
|
|
|
var memoID *int32
|
|
|
|
if resource.MemoID != nil {
|
|
|
|
memo, _ := s.Store.GetMemo(ctx, &store.FindMemo{
|
|
|
|
ID: resource.MemoID,
|
|
|
|
})
|
|
|
|
if memo != nil {
|
|
|
|
memoID = &memo.ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-16 00:11:07 +08:00
|
|
|
return &apiv2pb.Resource{
|
2023-09-27 00:40:16 +08:00
|
|
|
Id: resource.ID,
|
2024-01-21 10:55:49 +08:00
|
|
|
Name: resource.ResourceName,
|
2023-12-19 23:49:24 +08:00
|
|
|
CreateTime: timestamppb.New(time.Unix(resource.CreatedTs, 0)),
|
2023-09-27 00:40:16 +08:00
|
|
|
Filename: resource.Filename,
|
|
|
|
ExternalLink: resource.ExternalLink,
|
|
|
|
Type: resource.Type,
|
|
|
|
Size: resource.Size,
|
2023-10-05 13:36:33 +08:00
|
|
|
MemoId: memoID,
|
2023-09-16 00:11:07 +08:00
|
|
|
}
|
|
|
|
}
|