arns-lt-tree-push-service/server.go
Arnas Udovic 9b60784dbb
All checks were successful
continuous-integration/drone/push Build is passing
DownloadAttachment
2023-06-27 16:57:14 +03:00

157 lines
3 KiB
Go

package main
import (
"context"
"fmt"
"google.golang.org/grpc"
"io"
"log"
"net"
"os"
"path"
"path/filepath"
zordTree "g.arns.lt/zordsdavini/zord-tree"
)
type server struct {
UnimplementedTreeManagerServer
}
func (TreeManagerServer *server) GetSummery(_ context.Context, in *TreeRequest) (*Tree, error) {
log.Println(in)
tree, err := zordTree.BuildTree(dest, meta)
if err != nil {
log.Fatal(err)
return nil, err
}
if *in.Path != "" {
ntree, err := tree.Slice(dest + *in.Path)
if err != nil {
log.Fatal(err)
return &Tree{}, err
}
tree = ntree
}
if len(in.Filter) != 0 {
ntree, found := tree.Filter(simplifyFilter(in.Filter))
if !found {
return &Tree{}, err
}
tree = ntree
}
formattedTree := FormatTreeSummery(tree)
return &formattedTree, nil
}
func (TreeManagerServer *server) GetFile(_ context.Context, in *FileRequest) (*FileContent, error) {
log.Println(in)
tree, err := zordTree.BuildTree(dest, meta)
if err != nil {
log.Fatal(err)
return nil, err
}
file, err := tree.FileById(in.Id)
if err != nil {
return nil, err
}
fileContent := GetContent(file)
return &fileContent, nil
}
func (TreeManagerServer *server) RebuildTree(ctx context.Context, in *TreeSecret) (*RebuildResponse, error) {
success := false
if in.Key == key {
err := removeContents(dest)
if err == nil {
Collect()
success = true
}
}
response := RebuildResponse{Success: success}
return &response, nil
}
func (TreeManagerServer *server) DownloadAttachment(in *AttachmentRequest, responseStream TreeManager_DownloadAttachmentServer) error {
file, err := os.Open(path.Clean(fmt.Sprintf("%s/%s/%s", dest, "__a", in.Path)))
if err != nil {
return err
}
defer file.Close()
bufferSize := 64 * 1024
buff := make([]byte, bufferSize)
for {
bytesRead, err := file.Read(buff)
if err != nil {
if err != io.EOF {
log.Println("Error during downloadingAttachment: ", err)
}
break
}
resp := &AttachmentResponse{Chunk: buff[:bytesRead]}
err = responseStream.Send(resp)
if err != nil {
return err
}
}
return nil
}
func removeContents(dir string) error {
d, err := os.Open(dir)
if err != nil {
return err
}
defer d.Close()
names, err := d.Readdirnames(-1)
if err != nil {
return err
}
for _, name := range names {
err = os.RemoveAll(filepath.Join(dir, name))
if err != nil {
return err
}
}
return nil
}
func simplifyFilter(filter []*TreeRequest_Filter) map[string][]string {
simpleFilter := make(map[string][]string)
for _, f := range filter {
simpleFilter[f.Key] = append(simpleFilter[f.Key], f.Value)
}
return simpleFilter
}
func Run() {
if _, err := os.Stat(dest); os.IsNotExist(err) {
log.Fatalf("Destination directory does not exist.")
}
ln, err := net.Listen("tcp", ":8301")
if err != nil {
log.Fatalf("Failed to listen: " + err.Error())
}
var opts []grpc.ServerOption
grpcServer := grpc.NewServer(opts...)
RegisterTreeManagerServer(grpcServer, &server{})
err = grpcServer.Serve(ln)
if err != nil {
log.Fatalf("failed to serve: %v", err)
}
}