From 493bf00baae82c0f53689d217755551ffddb90cb Mon Sep 17 00:00:00 2001 From: Tuxx Date: Sat, 17 Aug 2024 00:16:22 +0200 Subject: [PATCH] Added pushover notification when a file gets uploaded, could use improvement --- server.go | 6 ++++++ upload.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/server.go b/server.go index ec66f75..cfe55f8 100644 --- a/server.go +++ b/server.go @@ -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() diff --git a/upload.go b/upload.go index 0485c22..10464da 100644 --- a/upload.go +++ b/upload.go @@ -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