Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes bensadeh/circumflex#140 #142

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -949,6 +965,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