Compare commits
2 commits
12f82e106e
...
3defe55599
Author | SHA1 | Date | |
---|---|---|---|
|
3defe55599 | ||
|
3403deb463 |
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
7
LICENSE.txt
Normal file
7
LICENSE.txt
Normal file
|
@ -0,0 +1,7 @@
|
|||
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.
|
27
README.md
Normal file
27
README.md
Normal file
|
@ -0,0 +1,27 @@
|
|||
# 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
|
||||
```
|
20
chunker.go
20
chunker.go
|
@ -17,13 +17,17 @@ type chunker struct {
|
|||
gc *gokapiClient
|
||||
parallelChunks int
|
||||
chunkSize int
|
||||
maxDownloads int
|
||||
maxDays int
|
||||
}
|
||||
|
||||
func newChunker(gc *gokapiClient, parallelChunks, chunkSize int) *chunker {
|
||||
func newChunker(gc *gokapiClient, config Config) *chunker {
|
||||
return &chunker{
|
||||
gc: gc,
|
||||
parallelChunks: parallelChunks,
|
||||
chunkSize: chunkSize,
|
||||
parallelChunks: config.ParallelChunks,
|
||||
chunkSize: config.ChunkSize,
|
||||
maxDownloads: config.MaxDownloads,
|
||||
maxDays: config.MaxDays,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -45,8 +49,8 @@ func (c *chunker) UploadFile(ctx context.Context, filename string, progress func
|
|||
filename: fname,
|
||||
totalSize: fstat.Size(),
|
||||
contentType: mime.TypeByExtension(filepath.Ext(fname)),
|
||||
allowedDownloads: 5,
|
||||
expiryDays: 7,
|
||||
allowedDownloads: c.maxDownloads,
|
||||
expiryDays: c.maxDays,
|
||||
password: "",
|
||||
}
|
||||
|
||||
|
@ -61,6 +65,10 @@ 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{})
|
||||
|
@ -91,7 +99,7 @@ func (c *chunker) upload(ctx context.Context, fi uploadInfo, r io.ReaderAt, prog
|
|||
}()
|
||||
|
||||
errGroup, egctx := errgroup.WithContext(ctx)
|
||||
errGroup.SetLimit(c.parallelChunks)
|
||||
errGroup.SetLimit(uploaders)
|
||||
|
||||
for i := 0; i < chunks; i++ {
|
||||
errGroup.Go(func() error {
|
||||
|
|
|
@ -5,4 +5,6 @@ type Config struct {
|
|||
APIKey string
|
||||
ParallelChunks int
|
||||
ChunkSize int
|
||||
MaxDownloads int
|
||||
MaxDays int
|
||||
}
|
||||
|
|
64
display.go
Normal file
64
display.go
Normal file
|
@ -0,0 +1,64 @@
|
|||
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
|
||||
}
|
57
main.go
57
main.go
|
@ -3,21 +3,43 @@ package main
|
|||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"github.com/schollz/progressbar/v3"
|
||||
"fmt"
|
||||
"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("GOKAPI_HOSTNAME"),
|
||||
APIKey: os.Getenv("GOKAPI_API_KEY"),
|
||||
Hostname: os.Getenv(envVarURL),
|
||||
APIKey: os.Getenv(envVarAPIKey),
|
||||
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 {
|
||||
|
@ -27,24 +49,19 @@ func main() {
|
|||
ctx := context.Background()
|
||||
|
||||
client := newGokapiClient(hostUrl, config.APIKey)
|
||||
cnkr := newChunker(client, config.ParallelChunks, config.ChunkSize)
|
||||
cnkr := newChunker(client, config)
|
||||
|
||||
maxFileLen := 0
|
||||
for _, file := range flag.Args() {
|
||||
maxFileLen = max(maxFileLen, len(filepath.Base(file)))
|
||||
}
|
||||
|
||||
for _, file := range flag.Args() {
|
||||
_, err := cnkr.UploadFile(ctx, file, progressBarReport(file))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
ur, err := cnkr.UploadFile(ctx, file, progressBarReport(file, maxFileLen))
|
||||
if err == nil {
|
||||
displayCompletion(file, maxFileLen, ur)
|
||||
} else {
|
||||
displayFailedCompletion(file, maxFileLen, 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))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue