Skip to content

Commit

Permalink
feat(putio): make the maximum number of active tasks configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyXiang committed Aug 23, 2023
1 parent 1d1a696 commit 541b55c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ env CGO_ENABLED=0 go install -trimpath -ldflags="-s -w" github.com/RoyXiang/putc
* `REMOTE_SRC` and `REMOTE_DEST` (default: `src:` and `dest:`)
* If something like `rclone move putio: drive:Downloads/` is wanted,
set `REMOTE_SRC` to `putio:` and `REMOTE_DEST` to `drive:Downloads/`
* `MAX_TRANSFERS` (default: the maximum number of simultaneous tasks allowed by your Put.io subscription)
* `DELAY_BEFORE_TRANSFER` (default: `0s`)
* Delay before moving files to the destination so that you can manipulate files on Put.io first
* `EXCLUDE_FILETYPES` (e.g. `exe,log`)
Expand Down
7 changes: 5 additions & 2 deletions putio/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"golang.org/x/oauth2"
)

func New(token string) *Put {
func New(token string, maxTransfers int) *Put {
ctx := context.Background()
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
oauthClient := oauth2.NewClient(ctx, tokenSource)
Expand All @@ -19,10 +19,13 @@ func New(token string) *Put {
if err != nil || !info.AccountActive {
log.Fatal("You must have an active Put.io subscription")
}
if maxTransfers <= 0 || maxTransfers > info.SimultaneousDownloadLimit {
maxTransfers = info.SimultaneousDownloadLimit
}

result := &Put{
Client: client,
MaxTransfers: info.SimultaneousDownloadLimit,
MaxTransfers: maxTransfers,
DefaultDownloadFolder: "",
}
if settings, err := client.Account.Settings(ctx); err == nil && settings.DefaultDownloadFolder != RootFolderId {
Expand Down
12 changes: 9 additions & 3 deletions rclone/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"os"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -43,9 +44,6 @@ var (
)

func init() {
accessToken := parseRCloneConfig()
Put = putio.New(accessToken)

rcGlobalConfig := fs.GetConfig(nil)
argMultiThreadCutoff = int64(rcGlobalConfig.MultiThreadCutoff)
argLargeFileTransfers = rcGlobalConfig.Transfers
Expand All @@ -69,9 +67,14 @@ func init() {
}

osEnv := os.Environ()
maxTransfers := 0
for _, env := range osEnv {
pair := strings.SplitN(env, "=", 2)
switch pair[0] {
case "MAX_TRANSFERS":
if maxTransfersInEnv, err := strconv.Atoi(pair[1]); err == nil {
maxTransfers = maxTransfersInEnv
}
case "RENAMING_STYLE":
styleInEnv := strings.ToLower(pair[1])
switch styleInEnv {
Expand Down Expand Up @@ -101,6 +104,9 @@ func init() {
}
}

accessToken := parseRCloneConfig()
Put = putio.New(accessToken, maxTransfers)

taskChan = make(chan *putio.FileInfo, 1)
transferQueue = make(chan struct{}, argMaxTransfers)
}
Expand Down

0 comments on commit 541b55c

Please sign in to comment.