diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/LICENSE.txt b/LICENSE.txt deleted file mode 100644 index dd5cb13..0000000 --- a/LICENSE.txt +++ /dev/null @@ -1,7 +0,0 @@ -Copyright (c) 2025 Leon Mika - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md deleted file mode 100644 index 9ddd4c2..0000000 --- a/README.md +++ /dev/null @@ -1,27 +0,0 @@ -# send2gokapi - -Small CLI tool to upload files to a [Gokapi](https://github.com/Forceu/Gokapi) instance. - -## Usage - -In order to use this tool, the following environment variables must be set: - -- `SEND2GOKAPI_URL`: Base URL of a Gokapi instance. For example: `https://your.gokapi.url/`. -- `SEND2GOKAPI_API_KEY`: API Key - -```shell -send2gokapi [OPTIONS] FILES -``` - -Valid options: - -- `-d`: Max number of downloads allowed for each uploaded file (0 = unlimited). Default is 5 -- `-t`: Number of days to keep files (0 = unlimited). Default is 7. - -## Installation - -If you have Go: - -```shell -$ go install lmika.dev/cmd/send2gokapi@latest -``` diff --git a/chunker.go b/chunker.go index 1115391..d72d8ba 100644 --- a/chunker.go +++ b/chunker.go @@ -17,17 +17,13 @@ type chunker struct { gc *gokapiClient parallelChunks int chunkSize int - maxDownloads int - maxDays int } -func newChunker(gc *gokapiClient, config Config) *chunker { +func newChunker(gc *gokapiClient, parallelChunks, chunkSize int) *chunker { return &chunker{ gc: gc, - parallelChunks: config.ParallelChunks, - chunkSize: config.ChunkSize, - maxDownloads: config.MaxDownloads, - maxDays: config.MaxDays, + parallelChunks: parallelChunks, + chunkSize: chunkSize, } } @@ -49,8 +45,8 @@ func (c *chunker) UploadFile(ctx context.Context, filename string, progress func filename: fname, totalSize: fstat.Size(), contentType: mime.TypeByExtension(filepath.Ext(fname)), - allowedDownloads: c.maxDownloads, - expiryDays: c.maxDays, + allowedDownloads: 5, + expiryDays: 7, password: "", } @@ -65,10 +61,6 @@ func (c *chunker) upload(ctx context.Context, fi uploadInfo, r io.ReaderAt, prog } chunks := int(fi.totalSize/int64(c.chunkSize) + 1) - uploaders := c.parallelChunks - if chunks < uploaders { - uploaders = 1 - } chunkUploaded := make(chan uploadedChunk) doneChunkReport := make(chan struct{}) @@ -99,7 +91,7 @@ func (c *chunker) upload(ctx context.Context, fi uploadInfo, r io.ReaderAt, prog }() errGroup, egctx := errgroup.WithContext(ctx) - errGroup.SetLimit(uploaders) + errGroup.SetLimit(c.parallelChunks) for i := 0; i < chunks; i++ { errGroup.Go(func() error { diff --git a/config.go b/config.go index f77d5b5..a345d75 100644 --- a/config.go +++ b/config.go @@ -5,6 +5,4 @@ type Config struct { APIKey string ParallelChunks int ChunkSize int - MaxDownloads int - MaxDays int } diff --git a/display.go b/display.go deleted file mode 100644 index 9e3ba01..0000000 --- a/display.go +++ /dev/null @@ -1,64 +0,0 @@ -package main - -import ( - "flag" - "fmt" - "github.com/schollz/progressbar/v3" - "os" - "path/filepath" - "strings" -) - -var ( - ansiMoveUp = []byte{keyEscape, '[', '1', 'A'} - ansiClearLine = []byte{keyEscape, '[', '2', 'K'} - ansiLineFeed = []byte{keyEscape, '[', '1', 'G'} -) - -func envVarUsage() { - fmt.Fprintf(flag.CommandLine.Output(), " %-20s Gokapi URL (e.g. https://gokapi.example.com/)\n", envVarURL) - fmt.Fprintf(flag.CommandLine.Output(), " %-20s Gokapi API key\n", envVarAPIKey) -} - -func init() { - flag.Usage = func() { - fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [FLAGS] FILES...\n", os.Args[0]) - flag.PrintDefaults() - fmt.Fprintf(flag.CommandLine.Output(), "Environment variables:\n") - envVarUsage() - } -} - -func progressBarReport(filename string, maxFileLen int) func(cr ChunkReport) { - var pr *progressbar.ProgressBar - - displayedFileName := formatFilename(filename, maxFileLen) - return func(cr ChunkReport) { - if cr.UploadedChunks == 0 && pr == nil { - pr = progressbar.DefaultBytes(cr.TotalSize, displayedFileName) - } - pr.Set(int(cr.UploadedBytes)) - } -} - -func displayCompletion(filename string, maxFileLen int, ur UploadResponse) { - os.Stdout.Write(ansiMoveUp) - os.Stdout.Write(ansiClearLine) - os.Stdout.Write(ansiLineFeed) - - displayedFileName := formatFilename(filename, maxFileLen) - fmt.Printf("%v: done - %v\n", displayedFileName, ur.FileInfo.UrlHotlink) -} - -func displayFailedCompletion(filename string, maxFileLen int, err error) { - os.Stdout.Write(ansiClearLine) - os.Stdout.Write(ansiLineFeed) - - displayedFileName := formatFilename(filename, maxFileLen) - fmt.Printf("%v: error - %v\n", displayedFileName, err) -} - -func formatFilename(filename string, maxFileLen int) string { - baseFileName := filepath.Base(filename) - return strings.Repeat(" ", maxFileLen-len(baseFileName)) + baseFileName -} diff --git a/main.go b/main.go index 46cc06c..3a147c9 100644 --- a/main.go +++ b/main.go @@ -3,43 +3,21 @@ package main import ( "context" "flag" - "fmt" + "github.com/schollz/progressbar/v3" "log" "net/url" "os" "path/filepath" ) -const ( - envVarURL = "SEND2GOKAPI_URL" - envVarAPIKey = "SEND2GOKAPI_API_KEY" - - keyEscape = 27 -) - func main() { - 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) - } - config := Config{ - Hostname: os.Getenv(envVarURL), - APIKey: os.Getenv(envVarAPIKey), + Hostname: os.Getenv("GOKAPI_HOSTNAME"), + APIKey: os.Getenv("GOKAPI_API_KEY"), ParallelChunks: 4, ChunkSize: 1024 * 100, - 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) } + flag.Parse() hostUrl, err := url.Parse(config.Hostname) if err != nil { @@ -49,19 +27,24 @@ func main() { ctx := context.Background() client := newGokapiClient(hostUrl, config.APIKey) - cnkr := newChunker(client, config) - - maxFileLen := 0 - for _, file := range flag.Args() { - maxFileLen = max(maxFileLen, len(filepath.Base(file))) - } + cnkr := newChunker(client, config.ParallelChunks, config.ChunkSize) 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) + _, err := cnkr.UploadFile(ctx, file, progressBarReport(file)) + if err != nil { + log.Fatal(err) } } + +} + +func progressBarReport(filename string) func(cr ChunkReport) { + var pr *progressbar.ProgressBar + + return func(cr ChunkReport) { + if cr.UploadedChunks == 0 && pr == nil { + pr = progressbar.DefaultBytes(cr.TotalSize, filepath.Base(filename)) + } + pr.Set(int(cr.UploadedBytes)) + } }