Refactor web #26

Merged
tardisx merged 8 commits from refactor-web into main 2023-11-20 07:38:17 +10:30
2 changed files with 108 additions and 26 deletions
Showing only changes of commit 1be673d28b - Show all commits

View File

@ -0,0 +1,60 @@
{{ define "content" }}
<div id="layout" class="pure-g pure-u-1" x-data="popup_create()" x-init="">
<h2>Download create</h2>
<p>URL: <tt>{{ .url }}</tt></p>
<table class="pure-table" >
<tr>
<th>profile</th>
<td>
<select x-bind:disabled="profile_chosen" x-on:change="update_profile()" class="pure-input-1-2" x-model="profile_chosen">
<option value="">choose a profile</option>
{{ range $i := .config.DownloadProfiles }}
<option>{{ $i.Name }}</option>
{{ end }}
</select>
</td>
</tr>
<tr>
<th>destination</th>
<td>
<select x-on:change="update_destination()" class="pure-input-1-2" x-model="destination_chosen">
<option value="-">leave in {{ .config.Server.DownloadPath }}</option>
{{ range $i := .config.Destinations }}
<option>{{ $i.Name }}</option>
{{ end }}
</select>
</td>
</tr>
<tr>
<th>&nbsp;</th>
<td>
<button class="pure-button" @click="start()">start download</button>
</td>
</tr>
</table>
</div>
{{ end }}
{{ define "js" }}
<script>
function popup_create() {
return {
profile_chosen: null,
destination_chosen: null,
start() {
let op = {
method: 'POST',
body: JSON.stringify({action: 'start'}),
headers: { 'Content-Type': 'application/json' }
};
fetch('/rest/fetch', op)
.then(response => response.json())
.then(info => {
console.log(info)
})
}
}
}
</script>
{{ end }}

View File

@ -317,15 +317,17 @@ func fetchInfoRESTHandler(dm *download.Manager) func(w http.ResponseWriter, r *h
func fetchHandler(cs *config.ConfigService, vm *version.Manager, dm *download.Manager) func(w http.ResponseWriter, r *http.Request) { func fetchHandler(cs *config.ConfigService, vm *version.Manager, dm *download.Manager) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
method := r.Method
// if they refreshed the popup, just load the existing object, don't // if they refreshed the popup, just load the existing object, don't
// create a new one // create a new one
vars := mux.Vars(r) vars := mux.Vars(r)
idString := vars["id"] idString := vars["id"]
idInt, err := strconv.ParseInt(idString, 10, 32) idInt, idOK := strconv.ParseInt(idString, 10, 32)
// existing, load it up if method == "GET" && idOK == nil && idInt > 0 {
if err == nil && idInt > 0 { // existing, load it up
dl, err := dm.GetDlById(int(idInt)) dl, err := dm.GetDlById(int(idInt))
if err != nil { if err != nil {
@ -346,43 +348,63 @@ func fetchHandler(cs *config.ConfigService, vm *version.Manager, dm *download.Ma
panic(err) panic(err)
} }
return return
} } else if method == "POST" {
// creating a new one
panic("should not get here")
query := r.URL.Query() query := r.URL.Query()
url, present := query["url"] url, present := query["url"]
if !present { if !present {
w.WriteHeader(400)
fmt.Fprint(w, "No url supplied")
return
} else {
log.Printf("popup for %s", url)
// check the URL for a sudden but inevitable betrayal
if strings.Contains(url[0], cs.Config.Server.Address) {
w.WriteHeader(400) w.WriteHeader(400)
fmt.Fprint(w, "you mustn't gropple your gropple :-)") fmt.Fprint(w, "No url supplied")
return
} else {
log.Printf("popup for %s (POST)", url)
// create the new download
newDL := download.NewDownload(url[0], cs.Config)
dm.AddDownload(newDL)
t, err := template.ParseFS(webFS, "web/layout.tmpl", "web/popup.html")
if err != nil {
panic(err)
}
newDL.Lock.Lock()
defer newDL.Lock.Unlock()
templateData := map[string]interface{}{"Version": vm.GetInfo(), "dl": newDL, "config": cs.Config, "canStop": download.CanStopDownload}
err = t.ExecuteTemplate(w, "layout", templateData)
if err != nil {
panic(err)
}
}
} else {
// a GET, show the popup so they can start the download (or just close
// the popup if they didn't mean it)
query := r.URL.Query()
url, present := query["url"]
if !present {
w.WriteHeader(400)
fmt.Fprint(w, "No url supplied")
return return
} }
// create the new download t, err := template.ParseFS(webFS, "data/templates/layout.tmpl", "data/templates/popup_create.tmpl")
newDL := download.NewDownload(url[0], cs.Config)
dm.AddDownload(newDL)
t, err := template.ParseFS(webFS, "web/layout.tmpl", "web/popup.html")
if err != nil { if err != nil {
panic(err) panic(err)
} }
templateData := map[string]interface{}{"config": cs.Config, "url": url[0]}
newDL.Lock.Lock()
defer newDL.Lock.Unlock()
templateData := map[string]interface{}{"Version": vm.GetInfo(), "dl": newDL, "config": cs.Config, "canStop": download.CanStopDownload}
err = t.ExecuteTemplate(w, "layout", templateData) err = t.ExecuteTemplate(w, "layout", templateData)
if err != nil { if err != nil {
panic(err) panic(err)
} }
} }
} }
} }