Added pushover notification when a file gets uploaded, could use improvement

This commit is contained in:
Tuxx 2024-08-17 00:16:22 +02:00
parent c8f7c32649
commit 493bf00baa
2 changed files with 53 additions and 0 deletions

View file

@ -81,6 +81,8 @@ var Config struct {
maxDurationSize int64
disableAccessKey bool
defaultRandomFilename bool
pushOverToken string
pushOverKey string
}
var Templates = make(map[string]*pongo2.Template)
@ -328,6 +330,10 @@ func main() {
flag.Int64Var(&Config.maxDurationSize, "max-duration-size", 4*1024*1024*1024, "Size of file before max-duration-time is used to determine expiry max time. (Default is 4GB)")
flag.BoolVar(&Config.disableAccessKey, "disable-access-key", false, "Disables access key usage. (Default is false.)")
flag.BoolVar(&Config.defaultRandomFilename, "default-random-filename", true, "Makes it so the random filename is not default if set false. (Default is true.)")
flag.StringVar(&Config.pushOverToken, "pushover-token", "",
"Pushover API Token.")
flag.StringVar(&Config.pushOverKey, "pushover-key", "",
"Pushover API Key.")
iniflags.Parse()
mux := setup()

View file

@ -51,6 +51,14 @@ type Upload struct {
Metadata backends.Metadata
}
// PushoverMessage represents the structure of the message to send
type PushoverMessage struct {
Token string `json:"token"`
User string `json:"user"`
Message string `json:"message"`
Title string `json:"title,omitempty"`
}
func uploadPostHandler(c web.C, w http.ResponseWriter, r *http.Request) {
if !strictReferrerCheck(r, getSiteURL(r), []string{"Linx-Delete-Key", "Linx-Expiry", "Linx-Randomize", "X-Requested-With"}) {
badRequestHandler(c, w, r, RespAUTO, "")
@ -364,6 +372,13 @@ func processUpload(upReq UploadRequest) (upload Upload, err error) {
return upload, err
}
title := "New linx-server file."
message := fmt.Sprintf("File %s was just uploaded to linx-server. %s%s", upload.Filename, Config.siteURL, upload.Filename)
err = SendPushoverMessage(Config.pushOverToken, Config.pushOverKey, message, title)
if err != nil {
return upload, err
}
return
}
@ -421,6 +436,38 @@ func barePlusExt(filename string) (barename, extension string) {
return
}
// SendPushoverMessage sends a push notification using the Pushover API
func SendPushoverMessage(appToken, userKey, message, title string) error {
// Create the message struct
msg := PushoverMessage{
Token: appToken,
User: userKey,
Message: message,
Title: title,
}
// Encode the message into JSON
jsonData, err := json.Marshal(msg)
if err != nil {
return fmt.Errorf("failed to marshal JSON: %w", err)
}
// Send the request
resp, err := http.Post("https://api.pushover.net/1/messages.json", "application/json", bytes.NewBuffer(jsonData))
if err != nil {
return fmt.Errorf("failed to send request: %w", err)
}
defer resp.Body.Close()
// Check the response status
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("received non-OK response: %d", resp.StatusCode)
}
fmt.Println("Message sent successfully")
return nil
}
func parseExpiry(expStr string) time.Duration {
if expStr == "" {
return time.Duration(Config.defaultExpiry) * time.Second