system_service.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package v2
  2. import (
  3. "context"
  4. "os"
  5. apiv2pb "github.com/usememos/memos/proto/gen/api/v2"
  6. "github.com/usememos/memos/server/profile"
  7. "github.com/usememos/memos/store"
  8. "google.golang.org/grpc/codes"
  9. "google.golang.org/grpc/status"
  10. )
  11. type SystemService struct {
  12. apiv2pb.UnimplementedSystemServiceServer
  13. Profile *profile.Profile
  14. Store *store.Store
  15. }
  16. // NewSystemService creates a new SystemService.
  17. func NewSystemService(profile *profile.Profile, store *store.Store) *SystemService {
  18. return &SystemService{
  19. Profile: profile,
  20. Store: store,
  21. }
  22. }
  23. func (s *SystemService) GetSystemInfo(ctx context.Context, _ *apiv2pb.GetSystemInfoRequest) (*apiv2pb.GetSystemInfoResponse, error) {
  24. defaultSystemInfo := &apiv2pb.SystemInfo{}
  25. // Get the database size if the user is a host.
  26. userIDPtr := ctx.Value(UserIDContextKey)
  27. if userIDPtr != nil {
  28. userID := userIDPtr.(int32)
  29. user, err := s.Store.GetUser(ctx, &store.FindUser{
  30. ID: &userID,
  31. })
  32. if err != nil {
  33. return nil, status.Errorf(codes.Internal, "failed to get user: %v", err)
  34. }
  35. if user != nil && user.Role == store.RoleHost {
  36. fi, err := os.Stat(s.Profile.DSN)
  37. if err != nil {
  38. return nil, status.Errorf(codes.Internal, "failed to get file info: %v", err)
  39. }
  40. defaultSystemInfo.DbSize = fi.Size()
  41. }
  42. }
  43. response := &apiv2pb.GetSystemInfoResponse{
  44. SystemInfo: defaultSystemInfo,
  45. }
  46. return response, nil
  47. }