Skip to content

Commit

Permalink
fix handling errors while streaming or downloading
Browse files Browse the repository at this point in the history
  • Loading branch information
OmarElKhatibCS committed Dec 17, 2021
1 parent 42ea71d commit ba39975
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
16 changes: 10 additions & 6 deletions app/ui/anime_page_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (p *AnimePage) saveEpisode(episode *tohru.Episode, errChan chan error) {
return
}
filePath := p.getDownloadPath(episode)
filename := fmt.Sprintf("%s%s.%s", filePath, episode.EpisodeName, "mp4")
filename := fmt.Sprintf("%s%s.%s", filePath, removeRestrictedChars(episode.EpisodeName), "mp4")

err = os.MkdirAll(filePath, 0777)
if err != nil {
Expand Down Expand Up @@ -84,15 +84,19 @@ func getDwnLink(episode *tohru.Episode) (string, error) {

// getDownloadFolder : Get the download folder for an episode.
func (p *AnimePage) getDownloadPath(episode *tohru.Episode) string {
animeName := p.Anime.AnimeName
animeName := removeRestrictedChars(p.Anime.AnimeName)
episodeNumber := episode.EpisodeNumber

// Remove invalid characters from the folder name
restricted := []string{"<", ">", ":", "/", "|", "?", "*", "\"", "\\", ".", ",", " "}
for _, c := range restricted {
animeName = strings.ReplaceAll(animeName, c, "_")
}
fullPath := path.Join(core.App.Config.DownloadDir, animeName, episodeNumber) + "/"

return fullPath
}

func removeRestrictedChars(s string) string {
restricted := []string{"<", ">", ":", "/", "|", "?", "*", "\"", "\\", ".", ",", " "}
for _, c := range restricted {
s = strings.ReplaceAll(s, c, "_")
}
return s
}
6 changes: 3 additions & 3 deletions app/ui/modals.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func confirmModal(id, text, confirmButton string, f func()) *tview.Modal {
// confirmModal : Creates a new modal for confirmation.
// The user specifies the function to do when confirming.
// If the user cancels, then the modal is removed from the view.
func watchOrDownloadModal(id, text string, stream func(), download func()) *tview.Modal {
func watchOrDownloadModal(id, text string, stream func(chan error), download func(chan error), errChan chan error) *tview.Modal {
// Create new modal
modal := tview.NewModal()

Expand All @@ -65,9 +65,9 @@ func watchOrDownloadModal(id, text string, stream func(), download func()) *tvie
SetFocus(0).
SetDoneFunc(func(buttonIndex int, _ string) {
if buttonIndex == 0 {
stream()
stream(errChan)
} else if buttonIndex == 1 {
download()
download(errChan)
}
log.Printf("Removing %s modal\n", id)
core.App.PageHolder.RemovePage(id)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.17
require (
github.com/cavaliercoder/grab v2.0.0+incompatible
github.com/gdamore/tcell/v2 v2.4.1-0.20210905002822-f057f0a857a1
github.com/khatibomar/tohru v0.0.0-20211216150552-55f82355135e
github.com/khatibomar/tohru v0.0.0-20211217085241-6c7bb681db24
github.com/rivo/tview v0.0.0-20211202162923-2a6de950f73b
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ github.com/gdamore/tcell/v2 v2.4.1-0.20210905002822-f057f0a857a1 h1:QqwPZCwh/k1u
github.com/gdamore/tcell/v2 v2.4.1-0.20210905002822-f057f0a857a1/go.mod h1:Az6Jt+M5idSED2YPGtwnfJV0kXohgdCBPmHGSYc1r04=
github.com/khatibomar/kobayashi v0.0.0-20211216065057-5e57f7c1613e h1:G6TIADzRciDnkdLERDnPGaquhLQJkkLf8B/CcJNA/c8=
github.com/khatibomar/kobayashi v0.0.0-20211216065057-5e57f7c1613e/go.mod h1:psne+LyxlXje2KHHiNsuiPADcKqhztsy+cuGWN+Y8yU=
github.com/khatibomar/tohru v0.0.0-20211216150552-55f82355135e h1:DUWShEGqJjEo7fOqROKxC96bWFqLEw9w53+1mCcXjbQ=
github.com/khatibomar/tohru v0.0.0-20211216150552-55f82355135e/go.mod h1:YtA+t2qCDQkkOL0F+QBkaiy5bLd1P7ah/yVs6kZlETo=
github.com/khatibomar/tohru v0.0.0-20211217085241-6c7bb681db24 h1:innFxC3UQBWxgdZX9/2FxgVVCnNRiGS6NBWEUqPEA8U=
github.com/khatibomar/tohru v0.0.0-20211217085241-6c7bb681db24/go.mod h1:YtA+t2qCDQkkOL0F+QBkaiy5bLd1P7ah/yVs6kZlETo=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
Expand Down

0 comments on commit ba39975

Please sign in to comment.