send2gokapi/main.go

68 lines
1.3 KiB
Go
Raw Normal View History

2025-01-03 04:16:27 +00:00
package main
import (
"context"
"flag"
2025-01-04 03:21:10 +00:00
"fmt"
2025-01-03 04:16:27 +00:00
"log"
"net/url"
"os"
"path/filepath"
)
2025-01-04 03:21:10 +00:00
const (
envVarURL = "SEND2GOKAPI_URL"
envVarAPIKey = "SEND2GOKAPI_API_KEY"
keyEscape = 27
)
2025-01-03 04:16:27 +00:00
func main() {
2025-01-04 03:21:10 +00:00
flagDownloads := flag.Int("d", 5, "Max number of downloads")
flagDays := flag.Int("t", 7, "Amount of time to keep file, in days")
flag.Parse()
if flag.NArg() < 1 {
flag.Usage()
os.Exit(2)
}
2025-01-03 04:16:27 +00:00
config := Config{
2025-01-04 03:21:10 +00:00
Hostname: os.Getenv(envVarURL),
APIKey: os.Getenv(envVarAPIKey),
2025-01-03 04:16:27 +00:00
ParallelChunks: 4,
ChunkSize: 1024 * 100,
2025-01-04 03:21:10 +00:00
MaxDownloads: max(*flagDownloads, 0),
MaxDays: max(*flagDays, 0),
}
if config.Hostname == "" || config.APIKey == "" {
fmt.Fprintf(flag.CommandLine.Output(), "Please set the following environment variables:\n")
envVarUsage()
os.Exit(2)
2025-01-03 04:16:27 +00:00
}
hostUrl, err := url.Parse(config.Hostname)
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
client := newGokapiClient(hostUrl, config.APIKey)
2025-01-04 03:21:10 +00:00
cnkr := newChunker(client, config)
2025-01-03 04:16:27 +00:00
2025-01-04 03:21:10 +00:00
maxFileLen := 0
2025-01-03 04:16:27 +00:00
for _, file := range flag.Args() {
2025-01-04 03:21:10 +00:00
maxFileLen = max(maxFileLen, len(filepath.Base(file)))
2025-01-03 04:16:27 +00:00
}
2025-01-04 03:21:10 +00:00
for _, file := range flag.Args() {
ur, err := cnkr.UploadFile(ctx, file, progressBarReport(file, maxFileLen))
if err == nil {
displayCompletion(file, maxFileLen, ur)
} else {
displayFailedCompletion(file, maxFileLen, err)
2025-01-03 04:16:27 +00:00
}
}
}