-
Notifications
You must be signed in to change notification settings - Fork 0
/
servecmd.go
63 lines (57 loc) · 1.65 KB
/
servecmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package commands
import (
"fmt"
"github.com/codegangsta/cli"
pb "github.com/cpssd/paranoid/proto/fileserver"
"golang.org/x/net/context"
"google.golang.org/grpc"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"time"
)
//Adds files from Paranoid File Server
func Serve(c *cli.Context) {
args := c.Args()
if len(args) < 2 {
cli.ShowCommandHelp(c, "serve")
os.Exit(1)
}
requestTimeout := c.Int("timeout")
requestLimit := c.Int("access")
file := args[1]
usr, err := user.Current()
if err != nil {
fmt.Println("Unable to get information on current user:", err)
Log.Fatal("Could not get user information:", err)
}
ip, port, uuid, pool := getFsMeta(usr, args[0])
address := ip + ":" + port
serveFilePath, err := filepath.Abs(file)
serveData, err := ioutil.ReadFile(serveFilePath)
var opts []grpc.DialOption
opts = append(opts, grpc.WithTimeout(2*time.Second))
opts = append(opts, grpc.WithInsecure())
connection, err := grpc.Dial(address, opts...)
if err != nil {
fmt.Println("Failed to Connect to Discovery Share Server")
Log.Fatal("Unable to Connect to Discovery Share Server", err)
}
defer connection.Close()
serverClient := pb.NewFileserverClient(connection)
response, err := serverClient.ServeFile(context.Background(),
&pb.ServeRequest{
Uuid: uuid,
FilePath: serveFilePath,
FileData: serveData,
Pool: pool,
Timeout: int32(requestTimeout),
Limit: int32(requestLimit),
})
if err != nil {
fmt.Println("Unable to send File to Discovery Share Server")
Log.Fatal("Couldn't message Discovery Share Server", err)
}
fmt.Println("File now avaliable at:", "http://"+ip+response.ServerPort+"/"+response.ServeResponse)
}