Browse Source

Configurable shared link TTL

master
Tyler Sommer 1 year ago
parent
commit
44ccf897b4
Signed by: tyler-sommer GPG Key ID: C09C010500DBD008
3 changed files with 27 additions and 12 deletions
  1. +18
    -4
      cmd/spaced/main.go
  2. +6
    -8
      cmd/spaced/urls.go
  3. +3
    -0
      config.toml.dist

+ 18
- 4
cmd/spaced/main.go View File

@ -17,12 +17,24 @@ import (
"github.com/fsnotify/fsnotify"
)
type duration time.Duration
func (d *duration) UnmarshalText(text []byte) error {
dur, err := time.ParseDuration(string(text))
if err != nil {
return err
}
*d = duration(dur)
return nil
}
var config struct {
S3 s3Config `toml:"s3"`
URL urlShortenerConfig `toml:"url"`
FS fswatchConfig `toml:"fswatch"`
ShareTTL duration `toml:"share_ttl"`
LogFile string
}
@ -34,7 +46,7 @@ func init() {
fmt.Println()
fmt.Println("Command spaced automatically uploads screenshots and puts a shareable URL in")
fmt.Println("the system clipboard. The screenshots are uploaded to an S3-compatible bucket,")
fmt.Println("and eokvin as a URL shortening service.")
fmt.Println("and eokvin is used as a URL shortening service.")
fmt.Println()
fmt.Println("spaced works with an S3-compatible storage, such as DigitalOcean Spaces.")
fmt.Println()
@ -142,18 +154,20 @@ func worker(stop chan struct{}) {
log.Println("error writing to storage:", err.Error())
continue
}
u, err := s3.PresignedGetObject(f, 30*time.Minute, url.Values{})
u, err := s3.PresignedGetObject(f, time.Duration(config.ShareTTL), url.Values{})
if err != nil {
log.Println("error getting public aws url:", err.Error())
continue
}
log.Println("AWS URL:", u.String())
su, err := c.NewShortURL(u)
su, err := c.NewShortURL(u, time.Duration(config.ShareTTL))
if err != nil {
log.Println("error getting short share url:", err.Error())
continue
}
log.Println("Share URL:", su.String())
log.Printf("Share URL: %s\t(valid until %s)\n",
su.String(),
time.Now().Add(time.Duration(config.ShareTTL)).Format("Jan 02 15:02 MST"))
cmd := exec.Command("pbcopy")
p, err := cmd.StdinPipe()
if err != nil {

+ 6
- 8
cmd/spaced/urls.go View File

@ -2,6 +2,7 @@ package main
import (
"net/url"
"time"
"github.com/pkg/errors"
"github.com/veonik/eokvin"
@ -18,15 +19,15 @@ type urlShortenerConfig struct {
}
type urlShortener interface {
NewShortURL(u *url.URL) (*url.URL, error)
NewShortURL(u *url.URL, shareTTL time.Duration) (*url.URL, error)
}
type eokvinShortener struct {
*eokvin.Client
}
func (c *eokvinShortener) NewShortURL(u *url.URL) (*url.URL, error) {
su, err := c.Client.NewShortURL(u)
func (c *eokvinShortener) NewShortURL(u *url.URL, shareTTL time.Duration) (*url.URL, error) {
su, err := c.Client.NewShortURL(u, shareTTL)
if err != nil {
return nil, err
}
@ -41,11 +42,8 @@ func (c urlShortenerConfig) GetService() (urlShortener, error) {
if token == "" || endpoint == "" {
return nil, errors.New("options 'token' and 'endpoint' are required")
}
c := eokvin.NewClient(c.Options["token"])
c.Endpoint = endpoint
return &eokvinShortener{
Client: c,
}, nil
c := eokvin.NewClient(endpoint, token)
return &eokvinShortener{Client: c}, nil
default:
return nil, errors.Errorf("unsupported URL shortener kind %s", c.Kind)
}

+ 3
- 0
config.toml.dist View File

@ -1,3 +1,6 @@
# Shared items expire after this duration
share_ttl = "16h"
# Filesystem options
[fs]
# Path to monitor for new files

Loading…
Cancel
Save