Skip to content

Commit

Permalink
fix: timeout for decrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
cody committed Aug 28, 2024
1 parent 83b45f2 commit 6d3ece2
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions core/commands/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ import (
"encoding/base64"
"errors"
"fmt"
"io"
"net/http"
"os"
"path"
"strings"
"time"

shell "github.com/bittorrent/go-btfs-api"
cmds "github.com/bittorrent/go-btfs-cmds"
cp "github.com/bittorrent/go-btfs-common/crypto"
Expand All @@ -19,18 +26,13 @@ import (
"github.com/ethereum/go-ethereum/crypto/ecies"
peer "github.com/libp2p/go-libp2p/core/peer"
"github.com/mitchellh/go-homedir"
"io"
"net/http"
"os"
"path"
"strings"
"time"
)

const toOption = "to"
const passOption = "pass"
const fromOption = "from"
const decryptTimeoutOption = "time"
const dirOption = "dir"

const (
defaultTimeout = 30 * time.Second
Expand All @@ -42,12 +44,12 @@ var encryptCmd = &cmds.Command{
Tagline: "encrypt file with the public key of the peer",
},
Arguments: []cmds.Argument{
cmds.StringArg("folder", true, false, "The folder the file to be uploaded."),
cmds.FileArg("path", true, true, "The path to a file to be added to btfs.").EnableRecursive().EnableStdin(),
},
Options: []cmds.Option{
cmds.StringOption(toOption, "the peerID of the node which you want to share with"),
cmds.StringOption(passOption, "p", "the password that you want to encrypt the file by AES"),
cmds.StringOption(dirOption, "d", "the dir to upload"),
},
Run: func(r *cmds.Request, re cmds.ResponseEmitter, e cmds.Environment) error {
n, err := cmdenv.GetNode(e)
Expand Down Expand Up @@ -110,15 +112,14 @@ var encryptCmd = &cmds.Command{
if err != nil {
return err
}
if len(r.Arguments) > 0 {
dst := r.Arguments[0]
if dst == "" {
dst = "/"
}
err = btfsClient.FilesCp(r.Context, "/btfs/"+cid, dst+it.Name()+encryptedFileSuffix)
if err != nil {
return err
}

dir, ok := r.Options[dirOption].(string)
if !ok {
dir = "/"
}
err = btfsClient.FilesCp(r.Context, "/btfs/"+cid, dir+it.Name()+encryptedFileSuffix)
if err != nil {
return err
}
return re.Emit(cid)
},
Expand Down Expand Up @@ -153,9 +154,12 @@ var decryptCmd = &cmds.Command{
var readClose io.ReadCloser
cid := r.Arguments[0]

timeout, ok := r.Options[decryptTimeoutOption].(time.Duration)
var timeout time.Duration
t, ok := r.Options[decryptTimeoutOption].(int64)
if !ok {
timeout = defaultTimeout
} else {
timeout = time.Duration(t) * time.Second
}

from, ok := r.Options[fromOption].(string)
Expand Down

0 comments on commit 6d3ece2

Please sign in to comment.