2021-09-21 08:33:24 +09:30
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
|
|
|
"encoding/json"
|
2022-05-14 16:38:48 +09:30
|
|
|
"flag"
|
2021-09-21 08:33:24 +09:30
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2021-09-28 21:17:54 +09:30
|
|
|
"github.com/tardisx/gropple/config"
|
2021-09-30 23:48:56 +09:30
|
|
|
"github.com/tardisx/gropple/download"
|
2021-09-26 21:13:33 +09:30
|
|
|
"github.com/tardisx/gropple/version"
|
2021-09-21 08:33:24 +09:30
|
|
|
)
|
|
|
|
|
2022-04-18 19:53:07 +09:30
|
|
|
var dm *download.Manager
|
2022-04-07 20:39:14 +09:30
|
|
|
var configService *config.ConfigService
|
2021-09-24 15:35:54 +09:30
|
|
|
|
2022-04-18 13:01:07 +09:30
|
|
|
var versionInfo = version.Manager{
|
2023-03-13 10:50:25 +10:30
|
|
|
VersionInfo: version.Info{CurrentVersion: "v0.6.0-alpha.3"},
|
2022-04-18 13:01:07 +09:30
|
|
|
}
|
2021-09-22 11:42:53 +09:30
|
|
|
|
2021-09-21 08:33:24 +09:30
|
|
|
//go:embed web
|
|
|
|
var webFS embed.FS
|
|
|
|
|
2021-09-30 23:48:56 +09:30
|
|
|
type successResponse struct {
|
|
|
|
Success bool `json:"success"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type errorResponse struct {
|
|
|
|
Success bool `json:"success"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
2021-09-24 15:35:54 +09:30
|
|
|
|
2021-09-21 08:33:24 +09:30
|
|
|
func main() {
|
2022-04-18 13:01:07 +09:30
|
|
|
log.Printf("Starting gropple %s - https://github.com/tardisx/gropple", versionInfo.GetInfo().CurrentVersion)
|
2022-04-07 21:46:39 +09:30
|
|
|
|
2022-05-14 16:38:48 +09:30
|
|
|
var configPath string
|
|
|
|
flag.StringVar(&configPath, "config-path", "", "path to config file")
|
|
|
|
flag.Parse()
|
|
|
|
|
2022-04-07 21:46:39 +09:30
|
|
|
configService = &config.ConfigService{}
|
2022-05-14 16:38:48 +09:30
|
|
|
if configPath != "" {
|
|
|
|
configService.ConfigPath = configPath
|
|
|
|
} else {
|
|
|
|
configService.DetermineConfigDir()
|
|
|
|
}
|
|
|
|
|
2022-04-07 21:46:39 +09:30
|
|
|
exists, err := configService.ConfigFileExists()
|
2022-04-07 20:39:14 +09:30
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
if !exists {
|
2021-09-30 12:27:59 +09:30
|
|
|
log.Print("No config file - creating default config")
|
2022-04-07 21:46:39 +09:30
|
|
|
configService.LoadDefaultConfig()
|
|
|
|
configService.WriteConfig()
|
|
|
|
log.Printf("Configuration written to %s", configService.ConfigPath)
|
2021-09-30 12:27:59 +09:30
|
|
|
} else {
|
2022-04-07 21:46:39 +09:30
|
|
|
err := configService.LoadConfig()
|
2021-09-30 12:27:59 +09:30
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2022-04-07 21:46:39 +09:30
|
|
|
log.Printf("Configuration loaded from %s", configService.ConfigPath)
|
2021-09-30 12:27:59 +09:30
|
|
|
}
|
2021-09-24 15:35:54 +09:30
|
|
|
|
2022-04-18 19:53:07 +09:30
|
|
|
// create the download manager
|
|
|
|
dm = &download.Manager{MaxPerDomain: configService.Config.Server.MaximumActiveDownloads}
|
|
|
|
|
2021-09-21 08:33:24 +09:30
|
|
|
r := mux.NewRouter()
|
2021-09-30 23:48:56 +09:30
|
|
|
r.HandleFunc("/", homeHandler)
|
2022-01-05 23:56:12 +10:30
|
|
|
r.HandleFunc("/static/{filename}", staticHandler)
|
2021-09-30 23:48:56 +09:30
|
|
|
r.HandleFunc("/config", configHandler)
|
|
|
|
r.HandleFunc("/fetch", fetchHandler)
|
2021-10-25 22:45:56 +10:30
|
|
|
r.HandleFunc("/fetch/{id}", fetchHandler)
|
2021-09-28 22:09:12 +09:30
|
|
|
|
2021-09-30 23:48:56 +09:30
|
|
|
// info for the list
|
|
|
|
r.HandleFunc("/rest/fetch", fetchInfoRESTHandler)
|
|
|
|
// info for one, including update
|
|
|
|
r.HandleFunc("/rest/fetch/{id}", fetchInfoOneRESTHandler)
|
|
|
|
r.HandleFunc("/rest/version", versionRESTHandler)
|
|
|
|
r.HandleFunc("/rest/config", configRESTHandler)
|
2021-09-21 08:33:24 +09:30
|
|
|
|
|
|
|
http.Handle("/", r)
|
|
|
|
|
|
|
|
srv := &http.Server{
|
|
|
|
Handler: r,
|
2022-04-07 20:39:14 +09:30
|
|
|
Addr: fmt.Sprintf(":%d", configService.Config.Server.Port),
|
2021-09-21 08:33:24 +09:30
|
|
|
// Good practice: enforce timeouts for servers you create!
|
2021-09-21 17:59:30 +09:30
|
|
|
WriteTimeout: 5 * time.Second,
|
|
|
|
ReadTimeout: 5 * time.Second,
|
2021-09-21 08:33:24 +09:30
|
|
|
}
|
|
|
|
|
2021-09-26 21:13:33 +09:30
|
|
|
// check for a new version every 4 hours
|
|
|
|
go func() {
|
|
|
|
for {
|
2023-03-15 04:45:10 +10:30
|
|
|
err := versionInfo.UpdateGitHubVersion()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("could not get version info: %s", err)
|
|
|
|
}
|
2021-09-26 21:13:33 +09:30
|
|
|
time.Sleep(time.Hour * 4)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-11-21 16:19:49 +10:30
|
|
|
// start downloading queued downloads when slots available, and clean up
|
|
|
|
// old entries
|
2022-04-18 19:53:07 +09:30
|
|
|
go dm.ManageQueue()
|
2023-03-13 10:25:59 +10:30
|
|
|
dm.AddStressTestData(configService)
|
2022-07-05 20:43:32 +09:30
|
|
|
|
2022-04-07 21:46:39 +09:30
|
|
|
log.Printf("Visit %s for details on installing the bookmarklet and to check status", configService.Config.Server.Address)
|
2021-09-21 08:33:24 +09:30
|
|
|
log.Fatal(srv.ListenAndServe())
|
2022-04-07 20:39:14 +09:30
|
|
|
|
2021-09-26 21:13:33 +09:30
|
|
|
}
|
2021-09-21 08:33:24 +09:30
|
|
|
|
2021-09-30 23:48:56 +09:30
|
|
|
// versionRESTHandler returns the version information, if we have up-to-date info from github
|
|
|
|
func versionRESTHandler(w http.ResponseWriter, r *http.Request) {
|
2022-04-18 13:01:07 +09:30
|
|
|
if versionInfo.GetInfo().GithubVersionFetched {
|
|
|
|
b, _ := json.Marshal(versionInfo.GetInfo())
|
2023-03-15 04:45:10 +10:30
|
|
|
_, err := w.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("could not write to client: %s", err)
|
|
|
|
}
|
2021-09-26 21:13:33 +09:30
|
|
|
} else {
|
|
|
|
w.WriteHeader(400)
|
|
|
|
}
|
2021-09-21 08:33:24 +09:30
|
|
|
}
|
|
|
|
|
2021-09-30 23:48:56 +09:30
|
|
|
// homeHandler returns the main index page
|
|
|
|
func homeHandler(w http.ResponseWriter, r *http.Request) {
|
2021-09-21 08:33:24 +09:30
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
2022-04-07 20:39:14 +09:30
|
|
|
bookmarkletURL := fmt.Sprintf("javascript:(function(f,s,n,o){window.open(f+encodeURIComponent(s),n,o)}('%s/fetch?url=',window.location,'yourform','width=%d,height=%d'));", configService.Config.Server.Address, configService.Config.UI.PopupWidth, configService.Config.UI.PopupHeight)
|
2021-09-21 08:33:24 +09:30
|
|
|
|
2021-09-30 17:46:01 +09:30
|
|
|
t, err := template.ParseFS(webFS, "web/layout.tmpl", "web/menu.tmpl", "web/index.html")
|
2021-09-21 08:33:24 +09:30
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Info struct {
|
2022-07-05 20:43:32 +09:30
|
|
|
Manager *download.Manager
|
2021-09-21 08:33:24 +09:30
|
|
|
BookmarkletURL template.URL
|
2021-10-26 22:48:16 +10:30
|
|
|
Config *config.Config
|
2022-04-09 15:13:22 +09:30
|
|
|
Version version.Info
|
2021-09-21 08:33:24 +09:30
|
|
|
}
|
|
|
|
|
|
|
|
info := Info{
|
2022-07-05 20:43:32 +09:30
|
|
|
Manager: dm,
|
2021-09-21 08:33:24 +09:30
|
|
|
BookmarkletURL: template.URL(bookmarkletURL),
|
2022-04-07 20:39:14 +09:30
|
|
|
Config: configService.Config,
|
2022-04-18 13:01:07 +09:30
|
|
|
Version: versionInfo.GetInfo(),
|
2021-09-21 08:33:24 +09:30
|
|
|
}
|
|
|
|
|
2022-07-05 20:43:32 +09:30
|
|
|
dm.Lock.Lock()
|
|
|
|
defer dm.Lock.Unlock()
|
2021-09-21 17:13:30 +09:30
|
|
|
err = t.ExecuteTemplate(w, "layout", info)
|
2021-09-21 08:33:24 +09:30
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 23:56:12 +10:30
|
|
|
// staticHandler handles requests for static files
|
|
|
|
func staticHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
|
|
|
|
filename := vars["filename"]
|
|
|
|
if strings.Index(filename, ".js") == len(filename)-3 {
|
|
|
|
f, err := webFS.Open("web/" + filename)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error accessing %s - %v", filename, err)
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2023-03-15 04:45:10 +10:30
|
|
|
_, err = io.Copy(w, f)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("could not write to client: %s", err)
|
|
|
|
}
|
2022-01-05 23:56:12 +10:30
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
}
|
|
|
|
|
2021-09-30 23:48:56 +09:30
|
|
|
// configHandler returns the configuration page
|
|
|
|
func configHandler(w http.ResponseWriter, r *http.Request) {
|
2021-09-28 22:09:12 +09:30
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
2021-09-30 17:46:01 +09:30
|
|
|
t, err := template.ParseFS(webFS, "web/layout.tmpl", "web/menu.tmpl", "web/config.html")
|
2021-09-28 22:09:12 +09:30
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = t.ExecuteTemplate(w, "layout", nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-30 23:48:56 +09:30
|
|
|
// configRESTHandler handles both reading and writing of the configuration
|
|
|
|
func configRESTHandler(w http.ResponseWriter, r *http.Request) {
|
2021-09-29 23:15:44 +09:30
|
|
|
|
|
|
|
if r.Method == "POST" {
|
|
|
|
log.Printf("Updating config")
|
|
|
|
b, err := io.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-04-07 20:39:14 +09:30
|
|
|
err = configService.Config.UpdateFromJSON(b)
|
2021-09-29 23:15:44 +09:30
|
|
|
|
|
|
|
if err != nil {
|
2021-09-30 23:48:56 +09:30
|
|
|
errorRes := errorResponse{Success: false, Error: err.Error()}
|
2021-09-29 23:15:44 +09:30
|
|
|
errorResB, _ := json.Marshal(errorRes)
|
|
|
|
w.WriteHeader(400)
|
2023-03-15 04:45:10 +10:30
|
|
|
_, err = w.Write(errorResB)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("could not write to client: %s", err)
|
|
|
|
}
|
2021-09-29 23:15:44 +09:30
|
|
|
return
|
|
|
|
}
|
2022-04-07 20:39:14 +09:30
|
|
|
configService.WriteConfig()
|
2021-09-29 23:15:44 +09:30
|
|
|
}
|
2022-04-07 20:39:14 +09:30
|
|
|
b, _ := json.Marshal(configService.Config)
|
2023-03-15 04:45:10 +10:30
|
|
|
_, err := w.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("could not write config to client: %s", err)
|
|
|
|
}
|
2021-09-28 22:09:12 +09:30
|
|
|
}
|
|
|
|
|
2021-09-30 23:48:56 +09:30
|
|
|
func fetchInfoOneRESTHandler(w http.ResponseWriter, r *http.Request) {
|
2021-09-21 08:33:24 +09:30
|
|
|
vars := mux.Vars(r)
|
|
|
|
idString := vars["id"]
|
|
|
|
if idString != "" {
|
|
|
|
id, err := strconv.Atoi(idString)
|
|
|
|
if err != nil {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-05 20:43:32 +09:30
|
|
|
thisDownload, err := dm.GetDlById(id)
|
|
|
|
if err != nil {
|
2021-09-30 23:48:56 +09:30
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2022-07-05 20:43:32 +09:30
|
|
|
if thisDownload == nil {
|
|
|
|
panic("should not happen")
|
|
|
|
}
|
2021-09-30 23:48:56 +09:30
|
|
|
|
|
|
|
if r.Method == "POST" {
|
|
|
|
|
|
|
|
type updateRequest struct {
|
2022-07-05 20:43:32 +09:30
|
|
|
Action string `json:"action"`
|
|
|
|
Profile string `json:"profile"`
|
|
|
|
Destination string `json:"destination"`
|
2021-09-30 23:48:56 +09:30
|
|
|
}
|
|
|
|
|
|
|
|
thisReq := updateRequest{}
|
|
|
|
|
|
|
|
b, err := io.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(b, &thisReq)
|
|
|
|
if err != nil {
|
|
|
|
errorRes := errorResponse{Success: false, Error: err.Error()}
|
|
|
|
errorResB, _ := json.Marshal(errorRes)
|
|
|
|
w.WriteHeader(400)
|
2023-03-15 04:45:10 +10:30
|
|
|
_, err = w.Write(errorResB)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("could not write to client: %s", err)
|
|
|
|
}
|
2021-09-30 23:48:56 +09:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if thisReq.Action == "start" {
|
|
|
|
// find the profile they asked for
|
2022-04-07 20:39:14 +09:30
|
|
|
profile := configService.Config.ProfileCalled(thisReq.Profile)
|
2021-09-30 23:48:56 +09:30
|
|
|
if profile == nil {
|
|
|
|
panic("bad profile name?")
|
|
|
|
}
|
|
|
|
// set the profile
|
2022-07-05 20:43:32 +09:30
|
|
|
thisDownload.Lock.Lock()
|
2021-09-30 23:48:56 +09:30
|
|
|
thisDownload.DownloadProfile = *profile
|
2022-07-05 20:43:32 +09:30
|
|
|
thisDownload.Lock.Unlock()
|
|
|
|
|
|
|
|
dm.Queue(thisDownload)
|
2021-09-30 23:48:56 +09:30
|
|
|
|
|
|
|
succRes := successResponse{Success: true, Message: "download started"}
|
|
|
|
succResB, _ := json.Marshal(succRes)
|
2023-03-15 04:45:10 +10:30
|
|
|
_, err = w.Write(succResB)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("could not write to client: %s", err)
|
|
|
|
}
|
2021-09-21 17:26:16 +09:30
|
|
|
return
|
|
|
|
}
|
2022-01-05 23:56:12 +10:30
|
|
|
|
2022-07-05 20:43:32 +09:30
|
|
|
if thisReq.Action == "change_destination" {
|
|
|
|
|
|
|
|
// nil means (probably) that they chose "don't move" - which is fine,
|
|
|
|
// and maps to nil on the Download (the default state).
|
|
|
|
destination := configService.Config.DestinationCalled(thisReq.Destination)
|
2023-03-13 10:32:20 +10:30
|
|
|
dm.ChangeDestination(thisDownload, destination)
|
2022-07-05 20:43:32 +09:30
|
|
|
|
2023-03-13 10:32:20 +10:30
|
|
|
// log.Printf("%#v", thisDownload)
|
2022-07-05 20:43:32 +09:30
|
|
|
|
|
|
|
succRes := successResponse{Success: true, Message: "destination changed"}
|
|
|
|
succResB, _ := json.Marshal(succRes)
|
2023-03-15 04:45:10 +10:30
|
|
|
_, err = w.Write(succResB)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("could not write to client: %s", err)
|
|
|
|
}
|
2022-07-05 20:43:32 +09:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-05 23:56:12 +10:30
|
|
|
if thisReq.Action == "stop" {
|
2022-07-05 20:43:32 +09:30
|
|
|
|
|
|
|
thisDownload.Stop()
|
2022-01-05 23:56:12 +10:30
|
|
|
succRes := successResponse{Success: true, Message: "download stopped"}
|
|
|
|
succResB, _ := json.Marshal(succRes)
|
2023-03-15 04:45:10 +10:30
|
|
|
_, err = w.Write(succResB)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("could not write to client: %s", err)
|
|
|
|
}
|
2022-01-05 23:56:12 +10:30
|
|
|
return
|
|
|
|
}
|
2021-09-21 17:26:16 +09:30
|
|
|
}
|
2021-09-30 23:48:56 +09:30
|
|
|
|
|
|
|
// just a get, return the object
|
2023-03-10 00:07:29 +10:30
|
|
|
thisDownload.Lock.Lock()
|
|
|
|
defer thisDownload.Lock.Unlock()
|
|
|
|
|
2021-09-30 23:48:56 +09:30
|
|
|
b, _ := json.Marshal(thisDownload)
|
2023-03-10 00:07:29 +10:30
|
|
|
|
2023-03-15 04:45:10 +10:30
|
|
|
_, err = w.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("could not write to client: %s", err)
|
|
|
|
}
|
2021-09-30 23:48:56 +09:30
|
|
|
return
|
2021-09-21 08:33:24 +09:30
|
|
|
} else {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-30 23:48:56 +09:30
|
|
|
func fetchInfoRESTHandler(w http.ResponseWriter, r *http.Request) {
|
2022-04-18 19:53:07 +09:30
|
|
|
|
2023-03-10 00:07:29 +10:30
|
|
|
b, err := dm.DownloadsAsJSON()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-03-15 04:45:10 +10:30
|
|
|
_, err = w.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("could not write to client: %s", err)
|
|
|
|
}
|
2021-09-26 12:33:31 +09:30
|
|
|
}
|
|
|
|
|
2021-09-30 23:48:56 +09:30
|
|
|
func fetchHandler(w http.ResponseWriter, r *http.Request) {
|
2021-09-21 08:33:24 +09:30
|
|
|
|
2021-10-25 22:45:56 +10:30
|
|
|
// if they refreshed the popup, just load the existing object, don't
|
|
|
|
// create a new one
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
idString := vars["id"]
|
|
|
|
|
|
|
|
idInt, err := strconv.ParseInt(idString, 10, 32)
|
2022-04-18 19:53:07 +09:30
|
|
|
|
|
|
|
// existing, load it up
|
2021-10-25 22:45:56 +10:30
|
|
|
if err == nil && idInt > 0 {
|
2022-07-05 20:43:32 +09:30
|
|
|
|
|
|
|
dl, err := dm.GetDlById(int(idInt))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("not found")
|
|
|
|
w.WriteHeader(404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-18 19:53:07 +09:30
|
|
|
t, err := template.ParseFS(webFS, "web/layout.tmpl", "web/popup.html")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-10-25 22:45:56 +10:30
|
|
|
|
2022-04-18 19:53:07 +09:30
|
|
|
templateData := map[string]interface{}{"dl": dl, "config": configService.Config, "canStop": download.CanStopDownload}
|
2021-10-25 22:45:56 +10:30
|
|
|
|
2022-04-18 19:53:07 +09:30
|
|
|
err = t.ExecuteTemplate(w, "layout", templateData)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2021-10-25 22:45:56 +10:30
|
|
|
}
|
2022-04-18 19:53:07 +09:30
|
|
|
return
|
2021-10-25 22:45:56 +10:30
|
|
|
}
|
|
|
|
|
2021-09-21 08:33:24 +09:30
|
|
|
query := r.URL.Query()
|
2021-09-26 12:48:42 +09:30
|
|
|
url, present := query["url"]
|
2021-09-21 08:33:24 +09:30
|
|
|
|
|
|
|
if !present {
|
2021-09-26 12:48:42 +09:30
|
|
|
w.WriteHeader(400)
|
|
|
|
fmt.Fprint(w, "No url supplied")
|
|
|
|
return
|
2021-09-21 08:33:24 +09:30
|
|
|
} else {
|
|
|
|
|
2022-07-05 20:43:32 +09:30
|
|
|
log.Printf("popup for %s", url)
|
2021-09-26 12:48:42 +09:30
|
|
|
// check the URL for a sudden but inevitable betrayal
|
2022-04-07 20:39:14 +09:30
|
|
|
if strings.Contains(url[0], configService.Config.Server.Address) {
|
2021-09-26 12:48:42 +09:30
|
|
|
w.WriteHeader(400)
|
2022-04-06 20:35:28 +09:30
|
|
|
fmt.Fprint(w, "you mustn't gropple your gropple :-)")
|
2021-09-26 12:48:42 +09:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-05 20:43:32 +09:30
|
|
|
// create the new download
|
|
|
|
log.Print("creating")
|
|
|
|
newDL := download.NewDownload(url[0], configService.Config)
|
|
|
|
log.Print("adding")
|
|
|
|
dm.AddDownload(newDL)
|
|
|
|
log.Print("done")
|
2021-09-26 12:48:42 +09:30
|
|
|
|
2021-09-21 17:13:30 +09:30
|
|
|
t, err := template.ParseFS(webFS, "web/layout.tmpl", "web/popup.html")
|
2021-09-21 08:33:24 +09:30
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-07-05 20:43:32 +09:30
|
|
|
log.Print("lock dl")
|
|
|
|
newDL.Lock.Lock()
|
|
|
|
defer newDL.Lock.Unlock()
|
|
|
|
|
|
|
|
templateData := map[string]interface{}{"Version": versionInfo.GetInfo(), "dl": newDL, "config": configService.Config, "canStop": download.CanStopDownload}
|
2021-09-21 17:46:18 +09:30
|
|
|
|
2021-09-30 23:48:56 +09:30
|
|
|
err = t.ExecuteTemplate(w, "layout", templateData)
|
2021-09-21 08:33:24 +09:30
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-07-05 20:43:32 +09:30
|
|
|
log.Print("unlock dl because rendered")
|
2021-09-21 08:33:24 +09:30
|
|
|
}
|
|
|
|
}
|