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

@ -10,4 +10,8 @@ precacheAndRoute(self.__WB_MANIFEST, {
}); });
const handler = createHandlerBoundToURL('/'); const handler = createHandlerBoundToURL('/');
registerRoute(new NavigationRoute(handler)); registerRoute(
new NavigationRoute(handler, {
denylist: [new RegExp('/downloads/')]
})
);

View File

@ -73,7 +73,6 @@ func (c *Client) Download(pack *DCCSend) {
defer file.Close() defer file.Close()
con, err := net.Dial("tcp", fmt.Sprintf("%s:%d", pack.IP, pack.Port)) con, err := net.Dial("tcp", fmt.Sprintf("%s:%d", pack.IP, pack.Port))
if err != nil { if err != nil {
c.Progress <- DownloadProgress{ c.Progress <- DownloadProgress{
PercCompletion: -1, PercCompletion: -1,
@ -86,7 +85,7 @@ func (c *Client) Download(pack *DCCSend) {
defer con.Close() defer con.Close()
var speed float64 var speed float64
var prevPerc float64 var prevUpdate time.Time
secondsElapsed := int64(0) secondsElapsed := int64(0)
totalBytes := uint64(0) totalBytes := uint64(0)
buf := make([]byte, 0, 4*1024) buf := make([]byte, 0, 4*1024)
@ -120,9 +119,12 @@ func (c *Client) Download(pack *DCCSend) {
secondsElapsed = (now - start) / 1e9 secondsElapsed = (now - start) / 1e9
speed = round2(float64(totalBytes) / (float64(secondsElapsed))) speed = round2(float64(totalBytes) / (float64(secondsElapsed)))
secondsToGo := round2((float64(pack.Length) - float64(totalBytes)) / speed) secondsToGo := round2((float64(pack.Length) - float64(totalBytes)) / speed)
con.Write(byteRead(totalBytes)) con.Write(byteRead(totalBytes))
if percentage-prevPerc >= 0.1 {
prevPerc = percentage if time.Since(prevUpdate) >= time.Second {
prevUpdate = time.Now()
c.Progress <- DownloadProgress{ c.Progress <- DownloadProgress{
Speed: humanReadableByteCount(speed, true), Speed: humanReadableByteCount(speed, true),
PercCompletion: percentage, PercCompletion: percentage,
@ -134,7 +136,9 @@ func (c *Client) Download(pack *DCCSend) {
} }
} }
} }
con.Write(byteRead(totalBytes)) con.Write(byteRead(totalBytes))
c.Progress <- DownloadProgress{ c.Progress <- DownloadProgress{
Speed: humanReadableByteCount(speed, true), Speed: humanReadableByteCount(speed, true),
PercCompletion: 100, PercCompletion: 100,

View File

@ -63,8 +63,23 @@ func (i *ircHandler) run() {
} else if state.Connected { } else if state.Connected {
i.log("Connected") i.log("Connected")
} }
case progress := <-i.client.Progress: 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 ( import (
"log" "log"
"net/http" "net/http"
"strconv"
"strings" "strings"
"sync" "sync"
@ -180,6 +181,33 @@ func (d *Dispatch) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
d.upgradeWS(w, r, state) 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 { } else {
d.serveFiles(w, r) d.serveFiles(w, r)
} }

View File

@ -63,6 +63,12 @@ func (h *wsHandler) dispatchRequest(req WSRequest) {
func (h *wsHandler) init(r *http.Request) { func (h *wsHandler) init(r *http.Request) {
h.state.setWS(h.addr.String(), h.ws) h.state.setWS(h.addr.String(), h.ws)
h.state.user.SetLastIP(addrToIPBytes(h.addr)) 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, "|", log.Println(h.addr, "[State] User ID:", h.state.user.ID, "|",
h.state.numIRC(), "IRC connections |", h.state.numIRC(), "IRC connections |",

View File

@ -56,6 +56,10 @@ func (d directory) Downloads(username string) string {
return filepath.Join(d.User(username), "downloads") return filepath.Join(d.User(username), "downloads")
} }
func (d directory) DownloadedFile(username string, file string) string {
return filepath.Join(d.Downloads(username), file)
}
func (d directory) Config() string { func (d directory) Config() string {
return filepath.Join(d.ConfigRoot(), "config.toml") return filepath.Join(d.ConfigRoot(), "config.toml")
} }