2020-07-09 19:28:39 -04:00
|
|
|
package display
|
|
|
|
|
|
|
|
import (
|
2020-07-10 14:37:18 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2020-07-09 19:28:39 -04:00
|
|
|
"io/ioutil"
|
2020-12-14 14:28:07 -05:00
|
|
|
"mime"
|
2020-07-09 19:28:39 -04:00
|
|
|
"net/url"
|
|
|
|
"os"
|
2020-12-14 14:28:07 -05:00
|
|
|
"os/exec"
|
2020-07-09 19:28:39 -04:00
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-07-10 14:37:18 -04:00
|
|
|
"time"
|
2020-07-09 19:28:39 -04:00
|
|
|
|
2021-04-07 12:56:32 -04:00
|
|
|
"code.rocketnine.space/tslocum/cview"
|
2021-02-17 14:17:13 -05:00
|
|
|
"github.com/gdamore/tcell/v2"
|
2020-07-09 19:28:39 -04:00
|
|
|
"github.com/makeworld-the-better-one/amfora/config"
|
|
|
|
"github.com/makeworld-the-better-one/amfora/structs"
|
2020-12-14 14:28:07 -05:00
|
|
|
"github.com/makeworld-the-better-one/amfora/sysopen"
|
2020-07-10 14:37:18 -04:00
|
|
|
"github.com/makeworld-the-better-one/go-gemini"
|
2020-12-19 20:09:25 -05:00
|
|
|
"github.com/schollz/progressbar/v3"
|
2020-07-10 14:37:18 -04:00
|
|
|
"github.com/spf13/viper"
|
2020-07-09 19:28:39 -04:00
|
|
|
)
|
|
|
|
|
2021-02-17 14:17:13 -05:00
|
|
|
// For choosing between download and the portal - copy of YesNo basically
|
|
|
|
var dlChoiceModal = cview.NewModal()
|
2020-07-10 14:37:18 -04:00
|
|
|
|
|
|
|
// Channel to indicate what choice they made using the button text
|
|
|
|
var dlChoiceCh = make(chan string)
|
|
|
|
|
2020-07-28 16:58:32 -04:00
|
|
|
var dlModal = cview.NewModal()
|
2020-07-10 14:37:18 -04:00
|
|
|
|
|
|
|
func dlInit() {
|
2021-02-17 14:17:13 -05:00
|
|
|
panels.AddPanel("dl", dlModal, false, false)
|
|
|
|
panels.AddPanel("dlChoice", dlChoiceModal, false, false)
|
|
|
|
|
|
|
|
dlm := dlModal
|
|
|
|
chm := dlChoiceModal
|
2020-07-10 14:37:18 -04:00
|
|
|
if viper.GetBool("a-general.color") {
|
2021-02-17 14:17:13 -05:00
|
|
|
chm.SetButtonBackgroundColor(config.GetColor("btn_bg"))
|
|
|
|
chm.SetButtonTextColor(config.GetColor("btn_text"))
|
|
|
|
chm.SetBackgroundColor(config.GetColor("dl_choice_modal_bg"))
|
|
|
|
chm.SetTextColor(config.GetColor("dl_choice_modal_text"))
|
|
|
|
form := chm.GetForm()
|
|
|
|
form.SetButtonBackgroundColorFocused(config.GetColor("btn_text"))
|
|
|
|
form.SetButtonTextColorFocused(config.GetColor("btn_bg"))
|
|
|
|
frame := chm.GetFrame()
|
|
|
|
frame.SetBorderColor(config.GetColor("dl_choice_modal_text"))
|
|
|
|
frame.SetTitleColor(config.GetColor("dl_choice_modal_text"))
|
|
|
|
|
|
|
|
dlm.SetButtonBackgroundColor(config.GetColor("btn_bg"))
|
|
|
|
dlm.SetButtonTextColor(config.GetColor("btn_text"))
|
|
|
|
dlm.SetBackgroundColor(config.GetColor("dl_modal_bg"))
|
|
|
|
dlm.SetTextColor(config.GetColor("dl_modal_text"))
|
|
|
|
form = dlm.GetForm()
|
|
|
|
form.SetButtonBackgroundColorFocused(config.GetColor("btn_text"))
|
|
|
|
form.SetButtonTextColorFocused(config.GetColor("btn_bg"))
|
|
|
|
frame = dlm.GetFrame()
|
|
|
|
frame.SetBorderColor(config.GetColor("dl_modal_text"))
|
|
|
|
frame.SetTitleColor(config.GetColor("dl_modal_text"))
|
2020-07-10 14:37:18 -04:00
|
|
|
} else {
|
2021-02-17 14:17:13 -05:00
|
|
|
chm.SetButtonBackgroundColor(tcell.ColorWhite)
|
|
|
|
chm.SetButtonTextColor(tcell.ColorBlack)
|
|
|
|
chm.SetBackgroundColor(tcell.ColorBlack)
|
|
|
|
chm.SetTextColor(tcell.ColorWhite)
|
|
|
|
chm.SetBorderColor(tcell.ColorWhite)
|
|
|
|
chm.GetFrame().SetTitleColor(tcell.ColorWhite)
|
|
|
|
form := chm.GetForm()
|
|
|
|
form.SetButtonBackgroundColorFocused(tcell.ColorBlack)
|
|
|
|
form.SetButtonTextColorFocused(tcell.ColorWhite)
|
|
|
|
|
|
|
|
dlm.SetButtonBackgroundColor(tcell.ColorWhite)
|
|
|
|
dlm.SetButtonTextColor(tcell.ColorBlack)
|
|
|
|
dlm.SetBackgroundColor(tcell.ColorBlack)
|
|
|
|
dlm.SetTextColor(tcell.ColorWhite)
|
|
|
|
form = dlm.GetForm()
|
|
|
|
form.SetButtonBackgroundColorFocused(tcell.ColorBlack)
|
|
|
|
form.SetButtonTextColorFocused(tcell.ColorWhite)
|
|
|
|
frame := dlm.GetFrame()
|
|
|
|
frame.SetBorderColor(tcell.ColorWhite)
|
|
|
|
frame.SetTitleColor(tcell.ColorWhite)
|
2020-07-10 14:37:18 -04:00
|
|
|
}
|
|
|
|
|
2021-02-17 14:17:13 -05:00
|
|
|
chm.AddButtons([]string{"Open", "Download", "Cancel"})
|
|
|
|
chm.SetBorder(true)
|
|
|
|
chm.GetFrame().SetTitleAlign(cview.AlignCenter)
|
|
|
|
chm.SetDoneFunc(func(buttonIndex int, buttonLabel string) {
|
2020-07-10 14:37:18 -04:00
|
|
|
dlChoiceCh <- buttonLabel
|
|
|
|
})
|
|
|
|
|
2021-02-17 14:17:13 -05:00
|
|
|
dlm.SetBorder(true)
|
|
|
|
frame := dlm.GetFrame()
|
|
|
|
frame.SetTitleAlign(cview.AlignCenter)
|
|
|
|
frame.SetTitle(" Download ")
|
|
|
|
dlm.SetDoneFunc(func(buttonIndex int, buttonLabel string) {
|
2020-07-10 14:37:18 -04:00
|
|
|
if buttonLabel == "Ok" {
|
2021-02-17 14:17:13 -05:00
|
|
|
panels.HidePanel("dl")
|
2020-07-28 18:09:36 -04:00
|
|
|
App.SetFocus(tabs[curTab].view)
|
|
|
|
App.Draw()
|
2020-07-10 14:37:18 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-14 14:28:07 -05:00
|
|
|
func getMediaHandler(resp *gemini.Response) config.MediaHandler {
|
|
|
|
def := config.MediaHandler{
|
|
|
|
Cmd: nil,
|
|
|
|
NoPrompt: false,
|
2020-12-24 23:38:12 -05:00
|
|
|
Stream: false,
|
2020-12-14 14:28:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
mediatype, _, err := mime.ParseMediaType(resp.Meta)
|
|
|
|
if err != nil {
|
|
|
|
return def
|
|
|
|
}
|
|
|
|
|
|
|
|
if ret, ok := config.MediaHandlers[mediatype]; ok {
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
splitType := strings.Split(mediatype, "/")[0]
|
|
|
|
if ret, ok := config.MediaHandlers[splitType]; ok {
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
if ret, ok := config.MediaHandlers["*"]; ok {
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
return def
|
|
|
|
}
|
|
|
|
|
2020-07-10 14:37:18 -04:00
|
|
|
// dlChoice displays the download choice modal and acts on the user's choice.
|
|
|
|
// It should run in a goroutine.
|
2020-07-10 15:33:39 -04:00
|
|
|
func dlChoice(text, u string, resp *gemini.Response) {
|
2020-12-14 14:28:07 -05:00
|
|
|
mediaHandler := getMediaHandler(resp)
|
|
|
|
var choice string
|
2020-07-10 14:37:18 -04:00
|
|
|
|
2020-12-14 14:28:07 -05:00
|
|
|
if mediaHandler.NoPrompt {
|
|
|
|
choice = "Open"
|
|
|
|
} else {
|
|
|
|
dlChoiceModal.SetText(text)
|
2021-02-17 14:17:13 -05:00
|
|
|
panels.ShowPanel("dlChoice")
|
|
|
|
panels.SendToFront("dlChoice")
|
2020-12-14 14:28:07 -05:00
|
|
|
App.SetFocus(dlChoiceModal)
|
|
|
|
App.Draw()
|
|
|
|
choice = <-dlChoiceCh
|
|
|
|
}
|
2020-07-10 14:37:18 -04:00
|
|
|
|
|
|
|
if choice == "Download" {
|
2021-02-17 14:17:13 -05:00
|
|
|
panels.HidePanel("dlChoice")
|
2020-07-10 14:37:18 -04:00
|
|
|
App.Draw()
|
2020-12-14 14:28:07 -05:00
|
|
|
downloadURL(config.DownloadsDir, u, resp)
|
2020-12-24 23:38:12 -05:00
|
|
|
resp.Body.Close() // Only close when the file is downloaded
|
2020-07-10 14:37:18 -04:00
|
|
|
return
|
|
|
|
}
|
2020-12-14 14:28:07 -05:00
|
|
|
if choice == "Open" {
|
2021-02-17 14:17:13 -05:00
|
|
|
panels.HidePanel("dlChoice")
|
2020-12-14 14:28:07 -05:00
|
|
|
App.Draw()
|
|
|
|
open(u, resp)
|
2020-07-10 14:37:18 -04:00
|
|
|
return
|
|
|
|
}
|
2021-02-17 14:17:13 -05:00
|
|
|
|
|
|
|
// They chose the "Cancel" button
|
|
|
|
panels.HidePanel("dlChoice")
|
2020-07-28 18:09:36 -04:00
|
|
|
App.SetFocus(tabs[curTab].view)
|
2020-07-10 14:37:18 -04:00
|
|
|
App.Draw()
|
|
|
|
}
|
|
|
|
|
2020-12-14 14:28:07 -05:00
|
|
|
// open performs the same actions as downloadURL except it also opens the file.
|
|
|
|
// If there is no system viewer configured for the particular mediatype, it opens it
|
|
|
|
// with the default system viewer.
|
|
|
|
func open(u string, resp *gemini.Response) {
|
|
|
|
mediaHandler := getMediaHandler(resp)
|
2020-12-24 23:38:12 -05:00
|
|
|
|
|
|
|
if mediaHandler.Stream {
|
|
|
|
// Run command with downloaded data from stdin
|
|
|
|
|
|
|
|
cmd := mediaHandler.Cmd
|
|
|
|
var proc *exec.Cmd
|
|
|
|
if len(cmd) == 1 {
|
|
|
|
proc = exec.Command(cmd[0])
|
|
|
|
} else {
|
|
|
|
proc = exec.Command(cmd[0], cmd[1:]...)
|
|
|
|
}
|
|
|
|
proc.Stdin = resp.Body
|
|
|
|
|
|
|
|
err := proc.Start()
|
|
|
|
if err != nil {
|
|
|
|
Error("File Opening Error", "Error executing custom command: "+err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
Info("Opened with " + cmd[0])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-14 14:28:07 -05:00
|
|
|
path := downloadURL(config.TempDownloadsDir, u, resp)
|
|
|
|
if path == "" {
|
|
|
|
return
|
|
|
|
}
|
2021-02-17 14:17:13 -05:00
|
|
|
|
|
|
|
panels.HidePanel("dl")
|
2020-12-14 14:28:07 -05:00
|
|
|
App.SetFocus(tabs[curTab].view)
|
|
|
|
App.Draw()
|
2021-02-17 14:17:13 -05:00
|
|
|
|
2020-12-14 14:28:07 -05:00
|
|
|
if mediaHandler.Cmd == nil {
|
|
|
|
// Open with system default viewer
|
|
|
|
_, err := sysopen.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
Error("System Viewer Error", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
Info("Opened in default system viewer")
|
|
|
|
} else {
|
|
|
|
cmd := mediaHandler.Cmd
|
|
|
|
err := exec.Command(cmd[0], append(cmd[1:], path)...).Start()
|
|
|
|
if err != nil {
|
|
|
|
Error("File Opening Error", "Error executing custom command: "+err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
Info("Opened with " + cmd[0])
|
|
|
|
}
|
|
|
|
App.Draw()
|
|
|
|
}
|
|
|
|
|
2020-07-10 14:37:18 -04:00
|
|
|
// downloadURL pulls up a modal to show download progress and saves the URL content.
|
|
|
|
// downloadPage should be used for Page content.
|
2020-12-14 14:28:07 -05:00
|
|
|
// Returns location downloaded to or an empty string on error.
|
|
|
|
func downloadURL(dir, u string, resp *gemini.Response) string {
|
2020-07-10 14:37:18 -04:00
|
|
|
_, _, width, _ := dlModal.GetInnerRect()
|
|
|
|
// Copy of progressbar.DefaultBytesSilent with custom width
|
|
|
|
bar := progressbar.NewOptions64(
|
|
|
|
-1,
|
|
|
|
progressbar.OptionSetWidth(width),
|
|
|
|
progressbar.OptionSetWriter(ioutil.Discard),
|
|
|
|
progressbar.OptionShowBytes(true),
|
|
|
|
progressbar.OptionThrottle(65*time.Millisecond),
|
|
|
|
progressbar.OptionShowCount(),
|
|
|
|
progressbar.OptionSpinnerType(14),
|
|
|
|
)
|
2020-08-25 19:17:06 -04:00
|
|
|
bar.RenderBlank() //nolint:errcheck
|
2020-07-10 14:37:18 -04:00
|
|
|
|
2020-12-14 14:28:07 -05:00
|
|
|
savePath, err := downloadNameFromURL(dir, u, "")
|
2020-07-10 14:37:18 -04:00
|
|
|
if err != nil {
|
|
|
|
Error("Download Error", "Error deciding on file name: "+err.Error())
|
2020-12-14 14:28:07 -05:00
|
|
|
return ""
|
2020-07-10 14:37:18 -04:00
|
|
|
}
|
|
|
|
f, err := os.OpenFile(savePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
|
|
|
if err != nil {
|
|
|
|
Error("Download Error", "Error creating download file: "+err.Error())
|
2020-12-14 14:28:07 -05:00
|
|
|
return ""
|
2020-07-10 14:37:18 -04:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
done := false
|
|
|
|
|
|
|
|
go func(isDone *bool) {
|
|
|
|
// Update the bar display
|
|
|
|
for !*isDone {
|
|
|
|
dlModal.SetText(bar.String())
|
|
|
|
App.Draw()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}(&done)
|
|
|
|
|
|
|
|
// Display
|
|
|
|
dlModal.ClearButtons()
|
|
|
|
dlModal.AddButtons([]string{"Downloading..."})
|
2021-02-17 14:17:13 -05:00
|
|
|
panels.ShowPanel("dl")
|
|
|
|
panels.SendToFront("dl")
|
2020-07-10 14:37:18 -04:00
|
|
|
App.SetFocus(dlModal)
|
|
|
|
App.Draw()
|
|
|
|
|
2020-07-26 12:12:54 -04:00
|
|
|
_, err = io.Copy(io.MultiWriter(f, bar), resp.Body)
|
2020-07-10 14:37:18 -04:00
|
|
|
done = true
|
2020-07-26 12:12:54 -04:00
|
|
|
if err != nil {
|
2021-02-17 14:17:13 -05:00
|
|
|
panels.HidePanel("dl")
|
2020-07-26 12:12:54 -04:00
|
|
|
Error("Download Error", err.Error())
|
|
|
|
f.Close()
|
|
|
|
os.Remove(savePath) // Remove partial file
|
2020-12-14 14:28:07 -05:00
|
|
|
return ""
|
2020-07-26 12:12:54 -04:00
|
|
|
}
|
2020-07-10 14:37:18 -04:00
|
|
|
dlModal.SetText(fmt.Sprintf("Download complete! File saved to %s.", savePath))
|
|
|
|
dlModal.ClearButtons()
|
|
|
|
dlModal.AddButtons([]string{"Ok"})
|
|
|
|
dlModal.GetForm().SetFocus(100)
|
|
|
|
App.SetFocus(dlModal)
|
|
|
|
App.Draw()
|
2020-12-14 14:28:07 -05:00
|
|
|
|
|
|
|
return savePath
|
2020-07-10 14:37:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// downloadPage saves the passed Page to a file.
|
|
|
|
// It returns the saved path and an error.
|
|
|
|
// It always cleans up, so if an error is returned there is no file saved
|
|
|
|
func downloadPage(p *structs.Page) (string, error) {
|
|
|
|
var savePath string
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if p.Mediatype == structs.TextGemini {
|
2020-12-14 14:28:07 -05:00
|
|
|
savePath, err = downloadNameFromURL(config.DownloadsDir, p.URL, ".gmi")
|
2020-07-10 14:37:18 -04:00
|
|
|
} else {
|
2020-12-14 14:28:07 -05:00
|
|
|
savePath, err = downloadNameFromURL(config.DownloadsDir, p.URL, ".txt")
|
2020-07-10 14:37:18 -04:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile(savePath, []byte(p.Raw), 0644)
|
|
|
|
if err != nil {
|
|
|
|
// Just in case
|
|
|
|
os.Remove(savePath)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return savePath, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// downloadNameFromURL takes a URl and returns a safe download path that will not overwrite any existing file.
|
|
|
|
// ext is an extension that will be added if the file has no extension, and for domain only URLs.
|
|
|
|
// It should include the dot.
|
2020-12-14 14:28:07 -05:00
|
|
|
func downloadNameFromURL(dir, u, ext string) (string, error) {
|
2020-07-10 14:37:18 -04:00
|
|
|
var name string
|
|
|
|
var err error
|
2021-05-13 16:38:53 -04:00
|
|
|
|
2020-07-10 14:37:18 -04:00
|
|
|
parsed, _ := url.Parse(u)
|
2021-05-13 16:38:53 -04:00
|
|
|
if strings.HasPrefix(u, "about:") {
|
|
|
|
name, err = getSafeDownloadName(dir, parsed.Opaque+ext, true, 0)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
} else if parsed.Path == "" || path.Base(parsed.Path) == "/" {
|
2020-07-10 14:37:18 -04:00
|
|
|
// No file, just the root domain
|
2020-12-14 14:28:07 -05:00
|
|
|
name, err = getSafeDownloadName(dir, parsed.Hostname()+ext, true, 0)
|
2020-07-10 14:37:18 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// There's a specific file
|
|
|
|
name = path.Base(parsed.Path)
|
|
|
|
if !strings.Contains(name, ".") {
|
|
|
|
// No extension
|
|
|
|
name += ext
|
|
|
|
}
|
2020-12-14 14:28:07 -05:00
|
|
|
name, err = getSafeDownloadName(dir, name, false, 0)
|
2020-07-10 14:37:18 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
2021-05-13 16:38:53 -04:00
|
|
|
|
2020-12-14 14:28:07 -05:00
|
|
|
return filepath.Join(dir, name), nil
|
2020-07-10 14:37:18 -04:00
|
|
|
}
|
|
|
|
|
2020-07-09 19:28:39 -04:00
|
|
|
// getSafeDownloadName is used by downloads.go only.
|
2020-12-14 14:28:07 -05:00
|
|
|
// It returns a modified name that is unique for the specified folder.
|
2020-07-09 19:28:39 -04:00
|
|
|
// This way duplicate saved files will not overwrite each other.
|
|
|
|
//
|
|
|
|
// lastDot should be set to true if the number added to the name should come before
|
|
|
|
// the last dot in the filename instead of the first.
|
|
|
|
//
|
|
|
|
// n should be set to 0, it is used for recursiveness.
|
2020-12-14 14:28:07 -05:00
|
|
|
func getSafeDownloadName(dir, name string, lastDot bool, n int) (string, error) {
|
2020-07-09 19:28:39 -04:00
|
|
|
// newName("test.txt", 3) -> "test(3).txt"
|
|
|
|
newName := func() string {
|
|
|
|
if n <= 0 {
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
if lastDot {
|
|
|
|
ext := filepath.Ext(name)
|
|
|
|
return strings.TrimSuffix(name, ext) + "(" + strconv.Itoa(n) + ")" + ext
|
|
|
|
}
|
2020-08-27 11:47:57 -04:00
|
|
|
idx := strings.Index(name, ".")
|
|
|
|
if idx == -1 {
|
|
|
|
return name + "(" + strconv.Itoa(n) + ")"
|
|
|
|
}
|
|
|
|
return name[:idx] + "(" + strconv.Itoa(n) + ")" + name[idx:]
|
2020-07-09 19:28:39 -04:00
|
|
|
}
|
|
|
|
|
2020-12-14 14:28:07 -05:00
|
|
|
d, err := os.Open(dir)
|
2020-07-09 19:28:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
files, err := d.Readdirnames(-1)
|
|
|
|
if err != nil {
|
|
|
|
d.Close()
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
nn := newName()
|
|
|
|
for i := range files {
|
|
|
|
if nn == files[i] {
|
|
|
|
d.Close()
|
2020-12-14 14:28:07 -05:00
|
|
|
return getSafeDownloadName(dir, name, lastDot, n+1)
|
2020-07-09 19:28:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
d.Close()
|
|
|
|
return nn, nil // Name doesn't exist already
|
|
|
|
}
|