|
1 | 1 | package display
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
| 5 | + "io" |
4 | 6 | "io/ioutil"
|
5 | 7 | "net/url"
|
6 | 8 | "os"
|
7 | 9 | "path"
|
8 | 10 | "path/filepath"
|
9 | 11 | "strconv"
|
10 | 12 | "strings"
|
| 13 | + "time" |
11 | 14 |
|
| 15 | + "github.com/gdamore/tcell" |
12 | 16 | "github.com/makeworld-the-better-one/amfora/config"
|
13 | 17 | "github.com/makeworld-the-better-one/amfora/structs"
|
| 18 | + "github.com/makeworld-the-better-one/go-gemini" |
| 19 | + "github.com/makeworld-the-better-one/progressbar/v3" |
| 20 | + "github.com/spf13/viper" |
| 21 | + "gitlab.com/tslocum/cview" |
14 | 22 | )
|
15 | 23 |
|
| 24 | +// For choosing between download and the portal - copy of YesNo basically |
| 25 | +var dlChoiceModal = cview.NewModal(). |
| 26 | + SetTextColor(tcell.ColorWhite). |
| 27 | + SetText("That file could not be displayed. What would you like to do?"). |
| 28 | + AddButtons([]string{"Download", "Open in portal", "Cancel"}) |
| 29 | + |
| 30 | +// Channel to indicate what choice they made using the button text |
| 31 | +var dlChoiceCh = make(chan string) |
| 32 | + |
| 33 | +var dlModal = cview.NewModal(). |
| 34 | + SetTextColor(tcell.ColorWhite) |
| 35 | + |
| 36 | +func dlInit() { |
| 37 | + if viper.GetBool("a-general.color") { |
| 38 | + dlChoiceModal.SetButtonBackgroundColor(tcell.ColorNavy). |
| 39 | + SetButtonTextColor(tcell.ColorWhite). |
| 40 | + SetBackgroundColor(tcell.ColorPurple) |
| 41 | + dlModal.SetButtonBackgroundColor(tcell.ColorNavy). |
| 42 | + SetButtonTextColor(tcell.ColorWhite). |
| 43 | + SetBackgroundColor(tcell.Color130) // DarkOrange3, #af5f00 |
| 44 | + } else { |
| 45 | + dlChoiceModal.SetButtonBackgroundColor(tcell.ColorWhite). |
| 46 | + SetButtonTextColor(tcell.ColorBlack). |
| 47 | + SetBackgroundColor(tcell.ColorBlack) |
| 48 | + dlModal.SetButtonBackgroundColor(tcell.ColorWhite). |
| 49 | + SetButtonTextColor(tcell.ColorBlack). |
| 50 | + SetBackgroundColor(tcell.ColorBlack) |
| 51 | + } |
| 52 | + |
| 53 | + dlChoiceModal.SetBorder(true) |
| 54 | + dlChoiceModal.SetBorderColor(tcell.ColorWhite) |
| 55 | + dlChoiceModal.SetDoneFunc(func(buttonIndex int, buttonLabel string) { |
| 56 | + dlChoiceCh <- buttonLabel |
| 57 | + }) |
| 58 | + dlChoiceModal.GetFrame().SetTitleColor(tcell.ColorWhite) |
| 59 | + dlChoiceModal.GetFrame().SetTitleAlign(cview.AlignCenter) |
| 60 | + |
| 61 | + dlModal.SetBorder(true) |
| 62 | + dlModal.SetBorderColor(tcell.ColorWhite) |
| 63 | + dlModal.SetDoneFunc(func(buttonIndex int, buttonLabel string) { |
| 64 | + if buttonLabel == "Ok" { |
| 65 | + tabPages.SwitchToPage(strconv.Itoa(curTab)) |
| 66 | + } |
| 67 | + }) |
| 68 | + dlModal.GetFrame().SetTitleColor(tcell.ColorWhite) |
| 69 | + dlModal.GetFrame().SetTitleAlign(cview.AlignCenter) |
| 70 | + dlModal.GetFrame().SetTitle(" Download ") |
| 71 | +} |
| 72 | + |
| 73 | +// dlChoice displays the download choice modal and acts on the user's choice. |
| 74 | +// It should run in a goroutine. |
| 75 | +func dlChoice(u string, resp *gemini.Response) { |
| 76 | + defer resp.Body.Close() |
| 77 | + |
| 78 | + parsed, err := url.Parse(u) |
| 79 | + if err != nil { |
| 80 | + Error("URL Error", err.Error()) |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + tabPages.ShowPage("dlChoice") |
| 85 | + tabPages.SendToFront("dlChoice") |
| 86 | + App.SetFocus(dlChoiceModal) |
| 87 | + App.Draw() |
| 88 | + |
| 89 | + choice := <-dlChoiceCh |
| 90 | + if choice == "Download" { |
| 91 | + tabPages.HidePage("dlChoice") |
| 92 | + App.Draw() |
| 93 | + downloadURL(u, resp) |
| 94 | + return |
| 95 | + } |
| 96 | + if choice == "Open in portal" { |
| 97 | + // Open in mozz's proxy |
| 98 | + portalURL := u |
| 99 | + if parsed.RawQuery != "" { |
| 100 | + // Remove query and add encoded version on the end |
| 101 | + query := parsed.RawQuery |
| 102 | + parsed.RawQuery = "" |
| 103 | + portalURL = parsed.String() + "%3F" + query |
| 104 | + } |
| 105 | + portalURL = strings.TrimPrefix(portalURL, "gemini://") + "?raw=1" |
| 106 | + handleHTTP("https://portal.mozz.us/gemini/"+portalURL, false) |
| 107 | + tabPages.SwitchToPage(strconv.Itoa(curTab)) |
| 108 | + App.Draw() |
| 109 | + return |
| 110 | + } |
| 111 | + tabPages.SwitchToPage(strconv.Itoa(curTab)) |
| 112 | + App.Draw() |
| 113 | +} |
| 114 | + |
| 115 | +// downloadURL pulls up a modal to show download progress and saves the URL content. |
| 116 | +// downloadPage should be used for Page content. |
| 117 | +func downloadURL(u string, resp *gemini.Response) { |
| 118 | + _, _, width, _ := dlModal.GetInnerRect() |
| 119 | + // Copy of progressbar.DefaultBytesSilent with custom width |
| 120 | + bar := progressbar.NewOptions64( |
| 121 | + -1, |
| 122 | + progressbar.OptionSetWidth(width), |
| 123 | + progressbar.OptionSetWriter(ioutil.Discard), |
| 124 | + progressbar.OptionShowBytes(true), |
| 125 | + progressbar.OptionThrottle(65*time.Millisecond), |
| 126 | + progressbar.OptionShowCount(), |
| 127 | + progressbar.OptionSpinnerType(14), |
| 128 | + ) |
| 129 | + bar.RenderBlank() |
| 130 | + |
| 131 | + savePath, err := downloadNameFromURL(u, "") |
| 132 | + if err != nil { |
| 133 | + Error("Download Error", "Error deciding on file name: "+err.Error()) |
| 134 | + return |
| 135 | + } |
| 136 | + f, err := os.OpenFile(savePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) |
| 137 | + if err != nil { |
| 138 | + Error("Download Error", "Error creating download file: "+err.Error()) |
| 139 | + return |
| 140 | + } |
| 141 | + defer f.Close() |
| 142 | + |
| 143 | + done := false |
| 144 | + |
| 145 | + go func(isDone *bool) { |
| 146 | + // Update the bar display |
| 147 | + for !*isDone { |
| 148 | + dlModal.SetText(bar.String()) |
| 149 | + App.Draw() |
| 150 | + time.Sleep(100 * time.Millisecond) |
| 151 | + } |
| 152 | + }(&done) |
| 153 | + |
| 154 | + // Display |
| 155 | + dlModal.ClearButtons() |
| 156 | + dlModal.AddButtons([]string{"Downloading..."}) |
| 157 | + tabPages.ShowPage("dl") |
| 158 | + tabPages.SendToFront("dl") |
| 159 | + App.SetFocus(dlModal) |
| 160 | + App.Draw() |
| 161 | + |
| 162 | + io.Copy(io.MultiWriter(f, bar), resp.Body) |
| 163 | + done = true |
| 164 | + dlModal.SetText(fmt.Sprintf("Download complete! File saved to %s.", savePath)) |
| 165 | + dlModal.ClearButtons() |
| 166 | + dlModal.AddButtons([]string{"Ok"}) |
| 167 | + dlModal.GetForm().SetFocus(100) |
| 168 | + App.SetFocus(dlModal) |
| 169 | + App.Draw() |
| 170 | +} |
| 171 | + |
| 172 | +// downloadPage saves the passed Page to a file. |
| 173 | +// It returns the saved path and an error. |
| 174 | +// It always cleans up, so if an error is returned there is no file saved |
| 175 | +func downloadPage(p *structs.Page) (string, error) { |
| 176 | + var savePath string |
| 177 | + var err error |
| 178 | + |
| 179 | + if p.Mediatype == structs.TextGemini { |
| 180 | + savePath, err = downloadNameFromURL(p.Url, ".gmi") |
| 181 | + } else { |
| 182 | + savePath, err = downloadNameFromURL(p.Url, ".txt") |
| 183 | + } |
| 184 | + if err != nil { |
| 185 | + return "", err |
| 186 | + } |
| 187 | + err = ioutil.WriteFile(savePath, []byte(p.Raw), 0644) |
| 188 | + if err != nil { |
| 189 | + // Just in case |
| 190 | + os.Remove(savePath) |
| 191 | + return "", err |
| 192 | + } |
| 193 | + return savePath, err |
| 194 | +} |
| 195 | + |
| 196 | +// downloadNameFromURL takes a URl and returns a safe download path that will not overwrite any existing file. |
| 197 | +// ext is an extension that will be added if the file has no extension, and for domain only URLs. |
| 198 | +// It should include the dot. |
| 199 | +func downloadNameFromURL(u string, ext string) (string, error) { |
| 200 | + var name string |
| 201 | + var err error |
| 202 | + parsed, _ := url.Parse(u) |
| 203 | + if parsed.Path == "" || path.Base(parsed.Path) == "/" { |
| 204 | + // No file, just the root domain |
| 205 | + name, err = getSafeDownloadName(parsed.Hostname()+ext, true, 0) |
| 206 | + if err != nil { |
| 207 | + return "", err |
| 208 | + } |
| 209 | + } else { |
| 210 | + // There's a specific file |
| 211 | + name = path.Base(parsed.Path) |
| 212 | + if !strings.Contains(name, ".") { |
| 213 | + // No extension |
| 214 | + name += ext |
| 215 | + } |
| 216 | + name, err = getSafeDownloadName(name, false, 0) |
| 217 | + if err != nil { |
| 218 | + return "", err |
| 219 | + } |
| 220 | + } |
| 221 | + return filepath.Join(config.DownloadsDir, name), nil |
| 222 | +} |
| 223 | + |
16 | 224 | // getSafeDownloadName is used by downloads.go only.
|
17 | 225 | // It returns a modified name that is unique for the downloads folder.
|
18 | 226 | // This way duplicate saved files will not overwrite each other.
|
@@ -59,45 +267,3 @@ func getSafeDownloadName(name string, lastDot bool, n int) (string, error) {
|
59 | 267 | d.Close()
|
60 | 268 | return nn, nil // Name doesn't exist already
|
61 | 269 | }
|
62 |
| - |
63 |
| -// downloadPage saves the passed Page to a file. |
64 |
| -// It returns the saved path and an error. |
65 |
| -// It always cleans up, so if an error is returned there is no file saved |
66 |
| -func downloadPage(p *structs.Page) (string, error) { |
67 |
| - // Figure out file name |
68 |
| - var name string |
69 |
| - var err error |
70 |
| - parsed, _ := url.Parse(p.Url) |
71 |
| - if parsed.Path == "" || path.Base(parsed.Path) == "/" { |
72 |
| - // No file, just the root domain |
73 |
| - if p.Mediatype == structs.TextGemini { |
74 |
| - name, err = getSafeDownloadName(parsed.Hostname()+".gmi", true, 0) |
75 |
| - if err != nil { |
76 |
| - return "", err |
77 |
| - } |
78 |
| - } else { |
79 |
| - name, err = getSafeDownloadName(parsed.Hostname()+".txt", true, 0) |
80 |
| - if err != nil { |
81 |
| - return "", err |
82 |
| - } |
83 |
| - } |
84 |
| - } else { |
85 |
| - // There's a specific file |
86 |
| - name = path.Base(parsed.Path) |
87 |
| - if p.Mediatype == structs.TextGemini && !strings.HasSuffix(name, ".gmi") && !strings.HasSuffix(name, ".gemini") { |
88 |
| - name += ".gmi" |
89 |
| - } |
90 |
| - name, err = getSafeDownloadName(name, false, 0) |
91 |
| - if err != nil { |
92 |
| - return "", err |
93 |
| - } |
94 |
| - } |
95 |
| - savePath := filepath.Join(config.DownloadsDir, name) |
96 |
| - err = ioutil.WriteFile(savePath, []byte(p.Raw), 0644) |
97 |
| - if err != nil { |
98 |
| - // Just in case |
99 |
| - os.Remove(savePath) |
100 |
| - return "", err |
101 |
| - } |
102 |
| - return savePath, err |
103 |
| -} |
|
0 commit comments