ezshare/server/grpc.go
2021-12-03 23:05:29 +01:00

40 lines
966 B
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
}