Skip to content

Commit

Permalink
Fixes bensadeh#140
Browse files Browse the repository at this point in the history
  • Loading branch information
christiangoeschel committed Oct 8, 2024
1 parent a8f6d7b commit d3a8b86
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
33 changes: 33 additions & 0 deletions bubble/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"clx/bubble/ranking"
"clx/cli"
"clx/constants/category"
"clx/download"
"clx/favorites"
"clx/header"
"clx/help"
Expand Down Expand Up @@ -615,6 +616,21 @@ func (m *Model) Update(msg tea.Msg) (*Model, tea.Cmd) {
m.NewStatusMessage(msg.Message)

m.updatePagination()

case message.DownloadFile:
statusMessage := "Document downloaded successfully"
m.hideStatusMessage()
m.disableInput = false

filename, _ := download.GuessDownloadFileName(msg.Url)
downloadDir, _ := download.GetDownloadDir()
absoluteFilepath := fmt.Sprintf("%s/%s", downloadDir, filename)
err := download.DownloadFile(msg.Url, absoluteFilepath)
if err != nil {
statusMessage = "Could not download document"
}

return m, m.NewStatusMessageWithDuration(statusMessage, time.Second*3)
}

if m.isOnHelpScreen {
Expand Down Expand Up @@ -963,6 +979,23 @@ func (m *Model) handleBrowsing(msg tea.Msg) tea.Cmd {
CommentCount: m.SelectedItem().CommentsCount,
}
}

case msg.String() == "d":
if !strings.Contains(m.SelectedItem().Title, "[pdf]") {
return m.NewStatusMessage("Only PDFs are currently supported ...")
}
m.SetDisabledInput(true)

downloadCmd := func() tea.Msg {
return message.DownloadFile{
Url: m.SelectedItem().URL,
}
}

cmds = append(cmds, downloadCmd)
cmds = append(cmds, m.NewStatusMessage("Downloading ..."))

return tea.Batch(cmds...)
}
}

Expand Down
4 changes: 4 additions & 0 deletions bubble/list/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,7 @@ type CategoryFetchingFinished struct {
type AddToFavorites struct {
Item *item.Item
}

type DownloadFile struct {
Url string
}
65 changes: 65 additions & 0 deletions download/download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package download

import (
"fmt"
"io"
"net/http"
"os"
"path"
"time"
)

const DownloadDir string = "circumflex/downloads"

// Guess the PDF filename by extracting the last part of the download url
func GuessDownloadFileName(url string) (string, error) {
absoluteFilepath := path.Base(url)

if absoluteFilepath == "." || absoluteFilepath == "/" {
return "", fmt.Errorf("Could not guess filename, potential invalid URL!")
}

return absoluteFilepath, nil
}

func GetDownloadDir() (string, error) {
dir, err := os.UserHomeDir()
if err != nil {
// If getting the current working directory fails as well,
// the user might have greater issues than simply downloading a file
dir, err = os.Getwd()
if err != nil {
return "", err
}
} else {
dir = fmt.Sprintf("%s/%s", dir, DownloadDir)
}

return dir, nil
}

// Writes to the destination file as it downloads it, without
// loading the entire file into memory.
func DownloadFile(url string, absoluteFilepath string) error {
out, err := os.Create(absoluteFilepath)
if err != nil {
return fmt.Errorf("Could not create file")
}
defer out.Close()

// Timeout download attempt after 10 seconds
client := http.Client{Timeout: 10 * time.Second}

resp, err := client.Get(url)
if err != nil {
return fmt.Errorf("Could not download file")
}
defer resp.Body.Close()

_, err = io.Copy(out, resp.Body)
if err != nil {
return fmt.Errorf("Could not write download file data to destination")
}

return nil
}
3 changes: 3 additions & 0 deletions info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"strings"

"clx/constants/nerdfonts"

. "github.com/logrusorgru/aurora/v3"

"clx/constants/margins"
"clx/keymaps"

text "github.com/MichaelMure/go-term-text"
)

Expand All @@ -26,6 +28,7 @@ func GetText(screenWidth int, enableNerdFonts bool) string {
keys.AddKeymap("Open story link in browser", "o")
keys.AddKeymap("Open comments in browser", "c")
keys.AddSeparator()
keys.AddKeymap("Download article's document (only PDFs)", "d")
keys.AddKeymap("Add to favorites", "f")
keys.AddKeymap("Remove from favorites", "x")
keys.AddSeparator()
Expand Down

0 comments on commit d3a8b86

Please sign in to comment.