Send download link on completion

This commit is contained in:
Ken-Håvard Lieng 2020-05-17 03:55:46 +02:00
parent fa99a96733
commit b92f5cfb43
6 changed files with 67 additions and 6 deletions

View file

@ -63,8 +63,23 @@ func (i *ircHandler) run() {
} else if state.Connected {
i.log("Connected")
}
case progress := <-i.client.Progress:
i.state.sendJSON("pm", Message{Server: i.client.Host, From: "@dcc", Content: progress.ToJSON()})
if progress.PercCompletion == 100 {
i.state.sendJSON("pm", Message{
Server: i.client.Host,
From: "@dcc",
Content: fmt.Sprintf("%s://%s/downloads/%s/%s", i.state.String("scheme"),
i.state.String("host"), i.state.user.Username, progress.File),
})
} else {
i.state.sendJSON("pm", Message{
Server: i.client.Host,
From: "@dcc",
Content: fmt.Sprintf("%s: %.2f%% %s remaining, %.2fs left", progress.File,
progress.PercCompletion, progress.BytesRemaining, progress.SecondsToGo),
})
}
}
}
}

View file

@ -3,6 +3,7 @@ package server
import (
"log"
"net/http"
"strconv"
"strings"
"sync"
@ -180,6 +181,33 @@ func (d *Dispatch) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
d.upgradeWS(w, r, state)
} else if strings.HasPrefix(r.URL.Path, "/downloads") {
state := d.handleAuth(w, r, false, false)
if state == nil {
log.Println("[Auth] No state")
fail(w, http.StatusInternalServerError)
return
}
params := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
if len(params) == 3 {
userID, err := strconv.ParseUint(params[1], 10, 64)
if err != nil {
fail(w, http.StatusBadRequest)
}
if userID != state.user.ID {
fail(w, http.StatusUnauthorized)
}
filename := params[2]
w.Header().Set("Content-Disposition", "attachment; filename="+filename)
http.ServeFile(w, r, storage.Path.DownloadedFile(state.user.Username, filename))
} else {
fail(w, http.StatusNotFound)
}
} else {
d.serveFiles(w, r)
}

View file

@ -63,6 +63,12 @@ func (h *wsHandler) dispatchRequest(req WSRequest) {
func (h *wsHandler) init(r *http.Request) {
h.state.setWS(h.addr.String(), h.ws)
h.state.user.SetLastIP(addrToIPBytes(h.addr))
if r.TLS != nil {
h.state.Set("scheme", "https")
} else {
h.state.Set("scheme", "http")
}
h.state.Set("host", r.Host)
log.Println(h.addr, "[State] User ID:", h.state.user.ID, "|",
h.state.numIRC(), "IRC connections |",