ezshare/server/grpc.go
2021-12-04 11:30:42 +01:00

56 lines
1.4 KiB
Go

package server
import (
"context"
"fmt"
"gitea.benny.dog/torjus/ezshare/pb"
"gitea.benny.dog/torjus/ezshare/store"
)
type GRPCFileServiceServer struct {
Hostname string
store store.FileStore
pb.UnimplementedFileServiceServer
}
func NewGRPCFileServiceServer(store store.FileStore) *GRPCFileServiceServer {
return &GRPCFileServiceServer{Hostname: "localhost:8051", store: store}
}
func (s *GRPCFileServiceServer) UploadFile(ctx context.Context, req *pb.UploadFileRequest) (*pb.UploadFileResponse, error) {
var f pb.File
f.Data = req.GetData()
id, err := s.store.StoreFile(&f)
if err != nil {
return nil, err
}
return &pb.UploadFileResponse{Id: id, FileUrl: fmt.Sprintf("%s/files/%s", s.Hostname, id)}, nil
}
func (s *GRPCFileServiceServer) GetFile(ctx context.Context, req *pb.GetFileRequest) (*pb.GetFileResponse, error) {
f, err := s.store.GetFile(req.Id)
if err != nil {
return nil, err
}
return &pb.GetFileResponse{File: f}, nil
}
func (s *GRPCFileServiceServer) DeleteFile(ctx context.Context, req *pb.DeleteFileRequest) (*pb.DeleteFileResponse, error) {
err := s.store.DeleteFile(req.Id)
return &pb.DeleteFileResponse{}, err
}
func (s *GRPCFileServiceServer) ListFiles(ctx context.Context, req *pb.ListFilesRequest) (*pb.ListFilesResponse, error) {
infos, err := s.store.ListFiles()
if err != nil {
return nil, err
}
return &pb.ListFilesResponse{
Files: infos,
}, nil
}