Skip to content

Commit

Permalink
feat: remove 3rd-party app startup check if app path is not set
Browse files Browse the repository at this point in the history
  • Loading branch information
5rahim committed Jul 6, 2024
1 parent d5beb6e commit 1812f24
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 98 deletions.
27 changes: 14 additions & 13 deletions internal/database/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,6 @@ type AnilistSettings struct {
BlurAdultContent bool `gorm:"column:blur_adult_content" json:"blurAdultContent"`
}

type MediaPlayerSettings struct {
Default string `gorm:"column:default_player" json:"defaultPlayer"` // "vlc" or "mpc-hc"
Host string `gorm:"column:player_host" json:"host"`
VlcUsername string `gorm:"column:vlc_username" json:"vlcUsername"`
VlcPassword string `gorm:"column:vlc_password" json:"vlcPassword"`
VlcPort int `gorm:"column:vlc_port" json:"vlcPort"`
VlcPath string `gorm:"column:vlc_path" json:"vlcPath"`
MpcPort int `gorm:"column:mpc_port" json:"mpcPort"`
MpcPath string `gorm:"column:mpc_path" json:"mpcPath"`
MpvSocket string `gorm:"column:mpv_socket" json:"mpvSocket"`
MpvPath string `gorm:"column:mpv_path" json:"mpvPath"`
}

type LibrarySettings struct {
LibraryPath string `gorm:"column:library_path" json:"libraryPath"`
AutoUpdateProgress bool `gorm:"column:auto_update_progress" json:"autoUpdateProgress"`
Expand All @@ -80,6 +67,20 @@ type LibrarySettings struct {
OpenWebURLOnStart bool `gorm:"column:open_web_url_on_start" json:"openWebURLOnStart"`
RefreshLibraryOnStart bool `gorm:"column:refresh_library_on_start" json:"refreshLibraryOnStart"`
}

type MediaPlayerSettings struct {
Default string `gorm:"column:default_player" json:"defaultPlayer"` // "vlc" or "mpc-hc"
Host string `gorm:"column:player_host" json:"host"`
VlcUsername string `gorm:"column:vlc_username" json:"vlcUsername"`
VlcPassword string `gorm:"column:vlc_password" json:"vlcPassword"`
VlcPort int `gorm:"column:vlc_port" json:"vlcPort"`
VlcPath string `gorm:"column:vlc_path" json:"vlcPath"`
MpcPort int `gorm:"column:mpc_port" json:"mpcPort"`
MpcPath string `gorm:"column:mpc_path" json:"mpcPath"`
MpvSocket string `gorm:"column:mpv_socket" json:"mpvSocket"`
MpvPath string `gorm:"column:mpv_path" json:"mpvPath"`
}

type TorrentSettings struct {
Default string `gorm:"column:default_torrent_client" json:"defaultTorrentClient"`
QBittorrentPath string `gorm:"column:qbittorrent_path" json:"qbittorrentPath"`
Expand Down
2 changes: 1 addition & 1 deletion internal/handlers/torrent_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func HandleTorrentClientDownload(c *RouteCtx) error {
// try to start torrent client if it's not running
ok := c.App.TorrentClientRepository.Start()
if !ok {
return c.RespondWithError(errors.New("could not start torrent client, verify your settings"))
return c.RespondWithError(errors.New("could not contact torrent client, verify your settings or make sure it's running"))
}

// get magnets
Expand Down
6 changes: 5 additions & 1 deletion internal/mediaplayers/mediaplayer/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ func (m *Repository) Play(path string) error {
}
err = m.VLC.AddAndPlay(path)
if err != nil {
return errors.New("could not open and play video, verify your settings")
if m.VLC.Path != "" {
return errors.New("could not open and play video, verify your settings")
} else {
return errors.New("could not open and play video, make sure VLC is running or specify the application path in your settings")
}
}
return nil
case "mpc-hc":
Expand Down
35 changes: 10 additions & 25 deletions internal/mediaplayers/vlc/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package vlc

import (
"errors"
"github.com/seanime-app/seanime/internal/util"
"os/exec"
"runtime"
"strings"
"time"
)

Expand Down Expand Up @@ -39,36 +39,21 @@ func (vlc *VLC) getExecutablePath() string {
}
}

func (vlc *VLC) isRunning(executable string) bool {
cmd := exec.Command("tasklist")
output, err := cmd.Output()
if err != nil {
return false
}

// TODO
//var cmd *exec.Cmd
//switch runtime.GOOS {
//case "windows":
// cmd = exec.Command("tasklist")
//case "linux":
// cmd = exec.Command("pgrep", executable)
//case "darwin":
// cmd = exec.Command("pgrep", executable)
//default:
// return false
//}
func (vlc *VLC) Start() error {

return strings.Contains(string(output), executable)
}
// If the path is empty, do not check if VLC is running
if vlc.Path == "" {
return nil
}

func (vlc *VLC) Start() error {
// Check if VLC is already running
name := vlc.getExecutableName()
exe := vlc.getExecutablePath()
if vlc.isRunning(name) {
if util.ProgramIsRunning(name) {
return nil
}

// Start VLC
exe := vlc.getExecutablePath()
cmd := exec.Command(exe)
err := cmd.Start()
if err != nil {
Expand Down
62 changes: 32 additions & 30 deletions internal/torrents/qbittorrent/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,45 +19,47 @@ import (
)

type Client struct {
baseURL string
logger *zerolog.Logger
client *http.Client
Username string
Password string
Port int
Host string
Path string

Application qbittorrent_application.Client
Log qbittorrent_log.Client
RSS qbittorrent_rss.Client
Search qbittorrent_search.Client
Sync qbittorrent_sync.Client
Torrent qbittorrent_torrent.Client
Transfer qbittorrent_transfer.Client
baseURL string
logger *zerolog.Logger
client *http.Client
Username string
Password string
Port int
Host string
Path string
DisableBinaryUse bool
Application qbittorrent_application.Client
Log qbittorrent_log.Client
RSS qbittorrent_rss.Client
Search qbittorrent_search.Client
Sync qbittorrent_sync.Client
Torrent qbittorrent_torrent.Client
Transfer qbittorrent_transfer.Client
}

type NewClientOptions struct {
Logger *zerolog.Logger
Username string
Password string
Port int
Host string
Path string
Logger *zerolog.Logger
Username string
Password string
Port int
Host string
Path string
DisableBinaryUse bool
}

func NewClient(opts *NewClientOptions) *Client {
baseURL := fmt.Sprintf("http://%s:%d/api/v2", opts.Host, opts.Port)
client := &http.Client{}
return &Client{
baseURL: baseURL,
logger: opts.Logger,
client: client,
Username: opts.Username,
Password: opts.Password,
Port: opts.Port,
Path: opts.Path,
Host: opts.Host,
baseURL: baseURL,
logger: opts.Logger,
client: client,
Username: opts.Username,
Password: opts.Password,
Port: opts.Port,
Path: opts.Path,
DisableBinaryUse: opts.DisableBinaryUse,
Host: opts.Host,
Application: qbittorrent_application.Client{
BaseUrl: baseURL + "/app",
Client: client,
Expand Down
25 changes: 13 additions & 12 deletions internal/torrents/qbittorrent/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package qbittorrent

import (
"errors"
"github.com/seanime-app/seanime/internal/util"
"os/exec"
"runtime"
"strings"
"time"
)

Expand Down Expand Up @@ -35,23 +35,19 @@ func (c *Client) getExecutablePath() string {
}
}

func (c *Client) isRunning(executable string) bool {
cmd := exec.Command("tasklist")
output, err := cmd.Output()
if err != nil {
return false
}
func (c *Client) Start() error {

return strings.Contains(string(output), executable)
}
// If the path is empty, do not check if qBittorrent is running
if c.Path == "" {
return nil
}

func (c *Client) Start() error {
name := c.getExecutableName()
exe := c.getExecutablePath()
if c.isRunning(name) {
if util.ProgramIsRunning(name) {
return nil
}

exe := c.getExecutablePath()
cmd := exec.Command(exe)
err := cmd.Start()
if err != nil {
Expand All @@ -68,6 +64,11 @@ func (c *Client) CheckStart() bool {
return false
}

// If the path is empty, assume it's running
if c.Path == "" {
return true
}

_, err := c.Application.GetAppVersion()
if err == nil {
return true
Expand Down
25 changes: 13 additions & 12 deletions internal/torrents/transmission/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package transmission
import (
"context"
"errors"
"github.com/seanime-app/seanime/internal/util"
"os/exec"
"runtime"
"strings"
"time"
)

Expand Down Expand Up @@ -37,23 +37,19 @@ func (c *Transmission) getExecutablePath() string {
}
}

func (c *Transmission) isRunning(executable string) bool {
cmd := exec.Command("tasklist")
output, err := cmd.Output()
if err != nil {
return false
}
func (c *Transmission) Start() error {

return strings.Contains(string(output), executable)
}
// If the path is empty, do not check if Transmission is running
if c.Path == "" {
return nil
}

func (c *Transmission) Start() error {
name := c.getExecutableName()
exe := c.getExecutablePath()
if c.isRunning(name) {
if util.ProgramIsRunning(name) {
return nil
}

exe := c.getExecutablePath()
cmd := exec.Command(exe)
err := cmd.Start()
if err != nil {
Expand All @@ -70,6 +66,11 @@ func (c *Transmission) CheckStart() bool {
return false
}

// If the path is empty, assume it's running
if c.Path == "" {
return true
}

_, _, _, err := c.Client.RPCVersion(context.Background())
if err == nil {
return true
Expand Down
8 changes: 5 additions & 3 deletions internal/updater/selfupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (su *SelfUpdater) recover(assetUrl string) {
_, _ = su.updater.DownloadLatestRelease(assetUrl, su.fallbackDest)
}

su.logger.Error().Msg("selfupdate: Failed to update, please restore the backup")
su.logger.Error().Msg("selfupdate: Failed to install update. Update downloaded to 'seanime_new_release'")
}

func getExePath() string {
Expand Down Expand Up @@ -174,6 +174,7 @@ func (su *SelfUpdater) Run() error {
// seanime.exe + /backup_restore_if_failed/seanime.exe
// LICENSE + /backup_restore_if_failed/LICENSE
for _, file := range files {
// We don't check for errors here because we don't want to stop the update process if LICENSE is not found for example
_ = copyFile(filepath.Join(exeDir, file), filepath.Join(backupDir, file))
}

Expand All @@ -188,7 +189,7 @@ func (su *SelfUpdater) Run() error {
// LICENSE -> LICENSE.old
for _, file := range files {
err = os.Rename(filepath.Join(exeDir, file), filepath.Join(exeDir, file+".old"))
// If the renaming failed, attempt to recover ONLY if the file is the binary
// We care about the error ONLY if the file is the executable
if err != nil && (file == "seanime" || file == "seanime.exe") {
renamingFailed = true
failedEntryNames = append(failedEntryNames, file)
Expand All @@ -200,9 +201,10 @@ func (su *SelfUpdater) Run() error {

if renamingFailed {
fmt.Println("---------------------------------")
fmt.Println("Please close your browser and the file explorer, a second attempt will be made in 30 seconds")
fmt.Println("A second attempt will be made in 30 seconds")
fmt.Println("---------------------------------")
time.Sleep(30 * time.Second)
// Here `failedEntryNames` should only contain NECESSARY files that failed to rename
for _, entry := range failedEntryNames {
err = os.Rename(filepath.Join(exeDir, entry), filepath.Join(exeDir, entry+".old"))
if err != nil {
Expand Down
25 changes: 24 additions & 1 deletion internal/util/mem.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
package util

import "fmt"
import (
"fmt"
"os/exec"
"runtime"
"strings"
)

func GetMemAddrStr(v interface{}) string {
return fmt.Sprintf("%p", v)
}

func ProgramIsRunning(name string) bool {
var cmd *exec.Cmd
switch runtime.GOOS {
case "windows":
cmd = exec.Command("tasklist")
case "linux":
cmd = exec.Command("pgrep", name)
case "darwin":
cmd = exec.Command("pgrep", name)
default:
return false
}

output, _ := cmd.Output()

return strings.Contains(string(output), name)
}
1 change: 1 addition & 0 deletions seanime-web/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts",
"out/types/**/*.ts"
],
"exclude": [
"node_modules"
Expand Down

0 comments on commit 1812f24

Please sign in to comment.