add ntfy integration
This commit is contained in:
@@ -2,8 +2,10 @@ package file
|
||||
|
||||
import (
|
||||
"ResendIt/internal/config"
|
||||
"ResendIt/internal/notify"
|
||||
"ResendIt/internal/util"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
@@ -22,6 +24,7 @@ type Handler struct {
|
||||
type ConfigService interface {
|
||||
GetIntDefault(key string, def int) int
|
||||
GetInt64Default(key string, def int64) int64
|
||||
GetStringDefault(key string, def string) string
|
||||
}
|
||||
|
||||
func NewHandler(s *Service, cfg ConfigService) *Handler {
|
||||
@@ -79,6 +82,20 @@ func (h *Handler) Upload(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
enabled := h.configService.GetIntDefault(config.KeyUseNtfy, config.DefaultUseNtfy)
|
||||
if enabled == 1 {
|
||||
ntfyURL := h.configService.GetStringDefault(config.KeyNtfyUrl, "")
|
||||
topic := h.configService.GetStringDefault(config.KeyNtfyTopic, config.DefaultNtfyTopic)
|
||||
|
||||
go func() {
|
||||
title := "ReSendit: new upload"
|
||||
msg := fmt.Sprintf("%s (%s)\nID: %s", record.Filename, util.HumanSize(record.Size), record.ID)
|
||||
if err := notify.Publish(ntfyURL, topic, title, msg); err != nil {
|
||||
log.Printf("ntfy publish failed: %v", err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"id": record.ID,
|
||||
"deletion_id": record.DeletionID,
|
||||
|
||||
36
internal/notify/ntfy.go
Normal file
36
internal/notify/ntfy.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package notify
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Publish(ntfyURL, topic, title, message string) error {
|
||||
ntfyURL = strings.TrimRight(ntfyURL, "/")
|
||||
if ntfyURL == "" || topic == "" {
|
||||
return nil // nothing configured
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", ntfyURL+"/"+topic, strings.NewReader(message))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if title != "" {
|
||||
req.Header.Set("Title", title)
|
||||
}
|
||||
req.Header.Set("Content-Type", "text/plain; charset=utf-8")
|
||||
|
||||
client := &http.Client{Timeout: 3 * time.Second}
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode >= 300 {
|
||||
return fmt.Errorf("ntfy returned %s", res.Status)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user