Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ var Config struct {
maxDurationSize int64
disableAccessKey bool
defaultRandomFilename bool
pushOverToken string
pushOverKey string
}

var Templates = make(map[string]*pongo2.Template)
Expand Down Expand Up @@ -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()
Expand Down
47 changes: 47 additions & 0 deletions upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down