From 8284ea7392fe91b1bf32722b9359e1cd25e177af Mon Sep 17 00:00:00 2001 From: Denis Kuzmenok Date: Mon, 6 Aug 2018 08:06:06 +0300 Subject: [PATCH] splitted download path to have separate torrents path. for memory storage download path is not mandatory now. default paths go to kodi temporary directory. --- api/cmd.go | 7 +++++ api/routes.go | 1 + api/torrents.go | 6 ----- bittorrent/service.go | 2 +- config/config.go | 59 ++++++++++++++++++++++++++++++------------- 5 files changed, 50 insertions(+), 25 deletions(-) diff --git a/api/cmd.go b/api/cmd.go index 4943fe6c..58d97480 100644 --- a/api/cmd.go +++ b/api/cmd.go @@ -55,6 +55,13 @@ func ResetClearances(ctx *gin.Context) { xbmc.Notify("Elementum", "LOCALIZE[30264]", config.AddonIcon()) } +// ResetPath ... +func ResetPath(ctx *gin.Context) { + xbmc.SetSetting("download_path", "") + xbmc.SetSetting("library_path", "special://temp/elementum_library/") + xbmc.SetSetting("torrents_path", "special://temp/elementum_torrents/") +} + // SetViewMode ... func SetViewMode(ctx *gin.Context) { contentType := ctx.Params.ByName("content_type") diff --git a/api/routes.go b/api/routes.go index bc75c7c0..18bb934c 100644 --- a/api/routes.go +++ b/api/routes.go @@ -280,6 +280,7 @@ func Routes(btService *bittorrent.BTService) *gin.Engine { cmd.GET("/clear_trakt_cache", ClearTraktCache) cmd.GET("/clear_tmdb_cache", ClearTmdbCache) cmd.GET("/reset_clearances", ResetClearances) + cmd.GET("/reset_path", ResetPath) } return r diff --git a/api/torrents.go b/api/torrents.go index afd76013..5735f3fd 100644 --- a/api/torrents.go +++ b/api/torrents.go @@ -329,12 +329,6 @@ func AddTorrent(btService *bittorrent.BTService) gin.HandlerFunc { } torrentsLog.Infof("Adding torrent from %s", uri) - if config.Get().DownloadPath == "." { - xbmc.Notify("Elementum", "LOCALIZE[30113]", config.AddonIcon()) - ctx.String(404, "Download path empty") - return - } - _, err := btService.AddTorrent(uri) if err != nil { ctx.String(404, err.Error()) diff --git a/bittorrent/service.go b/bittorrent/service.go index b1256376..187ab3e4 100644 --- a/bittorrent/service.go +++ b/bittorrent/service.go @@ -355,7 +355,7 @@ func (s *BTService) CheckAvailableSpace(torrent *Torrent) bool { func (s *BTService) AddTorrent(uri string) (*Torrent, error) { log.Infof("Adding torrent from %s", uri) - if s.config.DownloadPath == "." { + if s.config.DownloadStorage != estorage.StorageMemory && s.config.DownloadPath == "." { xbmc.Notify("Elementum", "LOCALIZE[30113]", config.AddonIcon()) return nil, fmt.Errorf("Download path empty") } diff --git a/config/config.go b/config/config.go index d91415b5..8b664521 100644 --- a/config/config.go +++ b/config/config.go @@ -296,32 +296,55 @@ func Reload() *Configuration { } downloadPath := TranslatePath(xbmc.GetSettingString("download_path")) - if downloadPath == "." { - // xbmc.AddonSettings("plugin.video.elementum") - // xbmc.Dialog("Elementum", "LOCALIZE[30113]") - settingsWarning = "LOCALIZE[30113]" - panic(settingsWarning) - } else if err := IsWritablePath(downloadPath); err != nil { - log.Errorf("Cannot write to location '%s': %#v", downloadPath, err) - // xbmc.AddonSettings("plugin.video.elementum") - // xbmc.Dialog("Elementum", err.Error()) - settingsWarning = err.Error() - panic(settingsWarning) + libraryPath := TranslatePath(xbmc.GetSettingString("library_path")) + torrentsPath := TranslatePath(xbmc.GetSettingString("torrents_path")) + downloadStorage := xbmc.GetSettingInt("download_storage") + + if downloadStorage != 1 { + if downloadPath == "." { + settingsWarning = "LOCALIZE[30113]" + panic(settingsWarning) + } else if err := IsWritablePath(downloadPath); err != nil { + log.Errorf("Cannot write to download location '%s': %#v", downloadPath, err) + settingsWarning = err.Error() + panic(settingsWarning) + } } log.Infof("Using download path: %s", downloadPath) - libraryPath := TranslatePath(xbmc.GetSettingString("library_path")) if libraryPath == "." { - libraryPath = downloadPath - } else if err := IsWritablePath(libraryPath); err != nil { - log.Error(err) - // xbmc.Dialog("Elementum", err.Error()) - // xbmc.AddonSettings("plugin.video.elementum") + settingsWarning = "LOCALIZE[30220]" + panic(settingsWarning) + } else if strings.Contains(libraryPath, "elementum_library") { + if err := os.MkdirAll(libraryPath, 0777); err != nil { + log.Errorf("Could not create temporary library directory: %#v", err) + settingsWarning = err.Error() + panic(settingsWarning) + } + } + if err := IsWritablePath(libraryPath); err != nil { + log.Errorf("Cannot write to library location '%s': %#v", libraryPath, err) settingsWarning = err.Error() panic(settingsWarning) } log.Infof("Using library path: %s", libraryPath) + if torrentsPath == "." { + torrentsPath = filepath.Join(downloadPath, "Torrents") + } else if strings.Contains(torrentsPath, "elementum_torrents") { + if err := os.MkdirAll(torrentsPath, 0777); err != nil { + log.Errorf("Could not create temporary torrents directory: %#v", err) + settingsWarning = err.Error() + panic(settingsWarning) + } + } + if err := IsWritablePath(torrentsPath); err != nil { + log.Errorf("Cannot write to location '%s': %#v", torrentsPath, err) + settingsWarning = err.Error() + panic(settingsWarning) + } + log.Infof("Using torrents path: %s", torrentsPath) + xbmcSettings := xbmc.GetAllSettings() settings := make(map[string]interface{}) for _, setting := range xbmcSettings { @@ -359,7 +382,7 @@ func Reload() *Configuration { newConfig := Configuration{ DownloadPath: downloadPath, LibraryPath: libraryPath, - TorrentsPath: filepath.Join(downloadPath, "Torrents"), + TorrentsPath: torrentsPath, Info: info, Platform: platform, Language: xbmc.GetLanguageISO639_1(),