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"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-07-10 14:37:18 -04:00
|
|
|
"time"
|
2020-07-09 19:28:39 -04:00
|
|
|
|
2020-07-10 14:37:18 -04:00
|
|
|
"github.com/gdamore/tcell"
|
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-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"
|
|
|
|
"gitlab.com/tslocum/cview"
|
2020-07-09 19:28:39 -04:00
|
|
|
)
|
|
|
|
|
2020-07-10 14:37:18 -04:00
|
|
|
// For choosing between download and the portal - copy of YesNo basically
|
|
|
|
var dlChoiceModal = cview.NewModal().
|
|
|
|
AddButtons([]string{"Download", "Open in portal", "Cancel"})
|
|
|
|
|
|
|
|
// 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() {
|
|
|
|
if viper.GetBool("a-general.color") {
|
2020-07-28 16:58:32 -04:00
|
|
|
dlChoiceModal.SetButtonBackgroundColor(config.GetColor("btn_bg")).
|
|
|
|
SetButtonTextColor(config.GetColor("btn_text")).
|
|
|
|
SetBackgroundColor(config.GetColor("dl_choice_modal_bg")).
|
|
|
|
SetTextColor(config.GetColor("dl_choice_modal_text"))
|
|
|
|
dlChoiceModal.GetFrame().
|
|
|
|
SetBorderColor(config.GetColor("dl_choice_modal_text")).
|
|
|
|
SetTitleColor(config.GetColor("dl_choice_modal_text"))
|
|
|
|
|
|
|
|
dlModal.SetButtonBackgroundColor(config.GetColor("btn_bg")).
|
|
|
|
SetButtonTextColor(config.GetColor("btn_text")).
|
|
|
|
SetBackgroundColor(config.GetColor("dl_modal_bg")).
|
|
|
|
SetTextColor(config.GetColor("dl_modal_text"))
|
|
|
|
dlModal.GetFrame().
|
|
|
|
SetBorderColor(config.GetColor("dl_modal_text")).
|
|
|
|
SetTitleColor(config.GetColor("dl_modal_text"))
|
2020-07-10 14:37:18 -04:00
|
|
|
} else {
|
|
|
|
dlChoiceModal.SetButtonBackgroundColor(tcell.ColorWhite).
|
|
|
|
SetButtonTextColor(tcell.ColorBlack).
|
2020-07-28 16:58:32 -04:00
|
|
|
SetBackgroundColor(tcell.ColorBlack).
|
|
|
|
SetTextColor(tcell.ColorWhite)
|
|
|
|
dlChoiceModal.SetBorderColor(tcell.ColorWhite)
|
|
|
|
dlChoiceModal.GetFrame().SetTitleColor(tcell.ColorWhite)
|
|
|
|
|
2020-07-10 14:37:18 -04:00
|
|
|
dlModal.SetButtonBackgroundColor(tcell.ColorWhite).
|
|
|
|
SetButtonTextColor(tcell.ColorBlack).
|
2020-07-28 16:58:32 -04:00
|
|
|
SetBackgroundColor(tcell.ColorBlack).
|
|
|
|
SetTextColor(tcell.ColorWhite)
|
|
|
|
dlModal.GetFrame().
|
|
|
|
SetBorderColor(tcell.ColorWhite).
|
|
|
|
SetTitleColor(tcell.ColorWhite)
|
2020-07-10 14:37:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
dlChoiceModal.SetBorder(true)
|
2020-07-28 16:58:32 -04:00
|
|
|
dlChoiceModal.GetFrame().SetTitleAlign(cview.AlignCenter)
|
2020-07-10 14:37:18 -04:00
|
|
|
dlChoiceModal.SetDoneFunc(func(buttonIndex int, buttonLabel string) {
|
|
|
|
dlChoiceCh <- buttonLabel
|
|
|
|
})
|
|
|
|
|
|
|
|
dlModal.SetBorder(true)
|
2020-07-28 16:58:32 -04:00
|
|
|
dlModal.GetFrame().
|
|
|
|
SetTitleAlign(cview.AlignCenter).
|
|
|
|
SetTitle(" Download ")
|
2020-07-10 14:37:18 -04:00
|
|
|
dlModal.SetDoneFunc(func(buttonIndex int, buttonLabel string) {
|
|
|
|
if buttonLabel == "Ok" {
|
|
|
|
tabPages.SwitchToPage(strconv.Itoa(curTab))
|
2020-07-28 18:09:36 -04:00
|
|
|
App.SetFocus(tabs[curTab].view)
|
|
|
|
App.Draw()
|
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-07-10 14:37:18 -04:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
parsed, err := url.Parse(u)
|
|
|
|
if err != nil {
|
|
|
|
Error("URL Error", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-10 15:33:39 -04:00
|
|
|
dlChoiceModal.SetText(text)
|
2020-07-10 14:37:18 -04:00
|
|
|
tabPages.ShowPage("dlChoice")
|
|
|
|
tabPages.SendToFront("dlChoice")
|
|
|
|
App.SetFocus(dlChoiceModal)
|
|
|
|
App.Draw()
|
|
|
|
|
|
|
|
choice := <-dlChoiceCh
|
|
|
|
if choice == "Download" {
|
|
|
|
tabPages.HidePage("dlChoice")
|
|
|
|
App.Draw()
|
|
|
|
downloadURL(u, resp)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if choice == "Open in portal" {
|
|
|
|
// Open in mozz's proxy
|
|
|
|
portalURL := u
|
|
|
|
if parsed.RawQuery != "" {
|
|
|
|
// Remove query and add encoded version on the end
|
|
|
|
query := parsed.RawQuery
|
|
|
|
parsed.RawQuery = ""
|
|
|
|
portalURL = parsed.String() + "%3F" + query
|
|
|
|
}
|
|
|
|
portalURL = strings.TrimPrefix(portalURL, "gemini://") + "?raw=1"
|
2020-11-04 20:01:11 -05:00
|
|
|
ok := handleHTTP("https://portal.mozz.us/gemini/"+portalURL, false)
|
|
|
|
if ok {
|
|
|
|
tabPages.SwitchToPage(strconv.Itoa(curTab))
|
|
|
|
App.SetFocus(tabs[curTab].view)
|
|
|
|
App.Draw()
|
|
|
|
}
|
2020-07-10 14:37:18 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
tabPages.SwitchToPage(strconv.Itoa(curTab))
|
2020-07-28 18:09:36 -04:00
|
|
|
App.SetFocus(tabs[curTab].view)
|
2020-07-10 14:37:18 -04:00
|
|
|
App.Draw()
|
|
|
|
}
|
|
|
|
|
|
|
|
// downloadURL pulls up a modal to show download progress and saves the URL content.
|
|
|
|
// downloadPage should be used for Page content.
|
|
|
|
func downloadURL(u string, resp *gemini.Response) {
|
|
|
|
_, _, 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
|
|
|
|
|
|
|
savePath, err := downloadNameFromURL(u, "")
|
|
|
|
if err != nil {
|
|
|
|
Error("Download Error", "Error deciding on file name: "+err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
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())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
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..."})
|
|
|
|
tabPages.ShowPage("dl")
|
|
|
|
tabPages.SendToFront("dl")
|
|
|
|
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 {
|
|
|
|
tabPages.HidePage("dl")
|
|
|
|
Error("Download Error", err.Error())
|
|
|
|
f.Close()
|
|
|
|
os.Remove(savePath) // Remove partial file
|
|
|
|
return
|
|
|
|
}
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-08-25 21:03:21 -04:00
|
|
|
savePath, err = downloadNameFromURL(p.URL, ".gmi")
|
2020-07-10 14:37:18 -04:00
|
|
|
} else {
|
2020-08-25 21:03:21 -04:00
|
|
|
savePath, err = downloadNameFromURL(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.
|
|
|
|
func downloadNameFromURL(u string, ext string) (string, error) {
|
|
|
|
var name string
|
|
|
|
var err error
|
|
|
|
parsed, _ := url.Parse(u)
|
|
|
|
if parsed.Path == "" || path.Base(parsed.Path) == "/" {
|
|
|
|
// No file, just the root domain
|
|
|
|
name, err = getSafeDownloadName(parsed.Hostname()+ext, true, 0)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// There's a specific file
|
|
|
|
name = path.Base(parsed.Path)
|
|
|
|
if !strings.Contains(name, ".") {
|
|
|
|
// No extension
|
|
|
|
name += ext
|
|
|
|
}
|
|
|
|
name, err = getSafeDownloadName(name, false, 0)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filepath.Join(config.DownloadsDir, name), nil
|
|
|
|
}
|
|
|
|
|
2020-07-09 19:28:39 -04:00
|
|
|
// getSafeDownloadName is used by downloads.go only.
|
|
|
|
// It returns a modified name that is unique for the downloads folder.
|
|
|
|
// 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.
|
|
|
|
func getSafeDownloadName(name string, lastDot bool, n int) (string, error) {
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
d, err := os.Open(config.DownloadsDir)
|
|
|
|
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()
|
|
|
|
return getSafeDownloadName(name, lastDot, n+1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
d.Close()
|
|
|
|
return nn, nil // Name doesn't exist already
|
|
|
|
}
|