mirror of
https://github.com/chenasraf/stimvisor.git
synced 2026-05-17 17:38:11 +00:00
refactor: split out dirs to other modules
This commit is contained in:
8
app.go
8
app.go
@@ -68,7 +68,7 @@ func (a *App) GetLibraryInfo() LibraryInfo {
|
||||
}
|
||||
// fmt.Printf("User Dir: %s\n", userDir)
|
||||
userId := filepath.Base(userDir)
|
||||
gd, err := dirs.GetGameDirectories(userId)
|
||||
gd, err := steam.GetAllDirs(userId)
|
||||
if err != nil {
|
||||
return LibraryMetaInfo(err)
|
||||
}
|
||||
@@ -99,7 +99,7 @@ func ScreenshotCollectionError(err error) ScreenshotCollectionResponse {
|
||||
const SHORT_SCREENSHOTS_LIMIT = 5
|
||||
|
||||
func (a *App) GetScreenshots() ScreenshotCollectionResponse {
|
||||
screenshotsDirPaths, err := dirs.GetScreenshotsDirs()
|
||||
screenshotsDirPaths, err := screenshots.GetAllDirs()
|
||||
if err != nil {
|
||||
logger.Error("Returning error: %s", err)
|
||||
return ScreenshotCollectionError(err)
|
||||
@@ -112,7 +112,7 @@ func (a *App) GetScreenshots() ScreenshotCollectionResponse {
|
||||
}
|
||||
|
||||
func (a *App) GetScreenshotsForGame(gameId string) ScreenshotCollectionResponse {
|
||||
screenshotsDirPath, err := dirs.GetScreenshotsDir(gameId)
|
||||
screenshotsDirPath, err := screenshots.GetDirForGame(gameId)
|
||||
if err != nil {
|
||||
logger.Error("Returning error: %s", err)
|
||||
return ScreenshotCollectionError(err)
|
||||
@@ -137,7 +137,7 @@ func (a *App) GetGames() GamesResponse {
|
||||
logger.Error("Returning error: %s", err)
|
||||
return GamesError(err)
|
||||
}
|
||||
gameDirPaths, err := dirs.GetGameDirectories(userId)
|
||||
gameDirPaths, err := steam.GetAllDirs(userId)
|
||||
if err != nil {
|
||||
logger.Error("Returning error: %s", err)
|
||||
return GamesError(err)
|
||||
|
||||
@@ -6,6 +6,18 @@ import (
|
||||
"runtime"
|
||||
)
|
||||
|
||||
const (
|
||||
DARWIN_STEAM_DIR = "%s/Library/Application Support/Steam"
|
||||
WINDOWS_STEAM_DIR = "%s/Steam"
|
||||
LINUX_STEAM_DIR = "%s/.steam/steam"
|
||||
)
|
||||
|
||||
var STEAM_INTERNAL_IDS = []string{
|
||||
"7", // Steam Config
|
||||
"760", // Steam Cloud - Screenshots
|
||||
"241100", // Steam Controller Config
|
||||
}
|
||||
|
||||
func GetConfigDir() string {
|
||||
if os.Getenv("XDG_CONFIG_HOME") != "" {
|
||||
return filepath.Join(os.Getenv("XDG_CONFIG_HOME"), "stimvisor")
|
||||
|
||||
99
dirs/dirs.go
99
dirs/dirs.go
@@ -5,24 +5,11 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"slices"
|
||||
"strconv"
|
||||
|
||||
"github.com/chenasraf/stimvisor/common"
|
||||
"github.com/chenasraf/stimvisor/logger"
|
||||
)
|
||||
|
||||
const (
|
||||
DARWIN_STEAM_DIR = "%s/Library/Application Support/Steam"
|
||||
WINDOWS_STEAM_DIR = "%s/Steam"
|
||||
LINUX_STEAM_DIR = "%s/.steam/steam"
|
||||
)
|
||||
|
||||
var STEAM_INTERNAL_IDS = []string{
|
||||
"7", // Steam Config
|
||||
"760", // Steam Cloud - Screenshots
|
||||
"241100", // Steam Controller Config
|
||||
}
|
||||
|
||||
// GetSteamDirectory returns the Steam directory path based on the operating system.
|
||||
func GetSteamDirectory() (string, error) {
|
||||
osname := runtime.GOOS
|
||||
@@ -36,16 +23,16 @@ func GetSteamDirectory() (string, error) {
|
||||
}
|
||||
localAppData = fmt.Sprintf("%s/AppData/Local", homedir)
|
||||
}
|
||||
return fmt.Sprintf(WINDOWS_STEAM_DIR, localAppData), nil
|
||||
return fmt.Sprintf(common.WINDOWS_STEAM_DIR, localAppData), nil
|
||||
}
|
||||
|
||||
homedir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Could not determine the user's home directory")
|
||||
}
|
||||
format = LINUX_STEAM_DIR
|
||||
format = common.LINUX_STEAM_DIR
|
||||
if osname == "darwin" {
|
||||
format = DARWIN_STEAM_DIR
|
||||
format = common.DARWIN_STEAM_DIR
|
||||
}
|
||||
return fmt.Sprintf(format, homedir), nil
|
||||
}
|
||||
@@ -109,81 +96,3 @@ func GetUserDirectory(userId string) (string, error) {
|
||||
logger.Info("Get User Dir: %s/userdata/%s", steamDir, userId)
|
||||
return fmt.Sprintf("%s/userdata/%s", steamDir, userId), nil
|
||||
}
|
||||
|
||||
// GetGameDirectories returns a list of directories for all games of a specific Steam user by user ID.
|
||||
func GetGameDirectories(userId string) ([]string, error) {
|
||||
userDir, err := GetUserDirectory(userId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
entries, err := os.ReadDir(userDir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
gameDirs := []string{}
|
||||
for _, entry := range entries {
|
||||
if !entry.IsDir() {
|
||||
continue
|
||||
}
|
||||
if slices.Contains(STEAM_INTERNAL_IDS, entry.Name()) {
|
||||
continue
|
||||
}
|
||||
if _, err := strconv.Atoi(entry.Name()); err != nil {
|
||||
continue
|
||||
}
|
||||
gameDir := fmt.Sprintf("%s/%s", userDir, entry.Name())
|
||||
gameDirs = append(gameDirs, gameDir)
|
||||
}
|
||||
return gameDirs, nil
|
||||
}
|
||||
|
||||
// GetGameDirectory returns the directory path for a specific game by game ID.
|
||||
func GetGameDirectory(gameId string) (string, error) {
|
||||
userDir, err := GetSteamUserDirectory()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s/%s", userDir, gameId), nil
|
||||
}
|
||||
|
||||
// GetScreenshotsDirs returns a list of directories for all screenshots of all games.
|
||||
func GetScreenshotsDirs() ([]string, error) {
|
||||
syncDir, err := GetSyncDirectory()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var dirs []string
|
||||
remoteDir := fmt.Sprintf("%s/remote", syncDir)
|
||||
entries, err := os.ReadDir(remoteDir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, entry := range entries {
|
||||
// logger.Debug("Entry: %s", entry.Name())
|
||||
if !entry.IsDir() {
|
||||
continue
|
||||
}
|
||||
if slices.Contains(STEAM_INTERNAL_IDS, entry.Name()) {
|
||||
continue
|
||||
}
|
||||
scrDir := fmt.Sprintf("%s/%s/screenshots", remoteDir, entry.Name())
|
||||
logger.Debug("Checking: %s", scrDir)
|
||||
if _, err := os.Stat(scrDir); os.IsNotExist(err) {
|
||||
continue
|
||||
}
|
||||
dirs = append(dirs, scrDir)
|
||||
}
|
||||
return dirs, nil
|
||||
}
|
||||
|
||||
// GetScreenshotsDir returns the directory path for screenshots of a specific game by game ID.
|
||||
func GetScreenshotsDir(gameId string) (string, error) {
|
||||
syncDir, err := GetSyncDirectory()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return fmt.Sprintf("%s/remote/%s/screenshots", syncDir, gameId), nil
|
||||
}
|
||||
|
||||
@@ -5,8 +5,10 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/chenasraf/stimvisor/common"
|
||||
"github.com/chenasraf/stimvisor/dirs"
|
||||
"github.com/chenasraf/stimvisor/logger"
|
||||
"github.com/chenasraf/stimvisor/steam"
|
||||
@@ -42,7 +44,7 @@ func NewScreenshotsDirFromPath(path string, limit int) ScreenshotCollection {
|
||||
defer dir.Close()
|
||||
s := ScreenshotCollection{}
|
||||
s.Dir = path
|
||||
s.GameId = filepath.Base(getDir(path, 1))
|
||||
s.GameId = filepath.Base(baseDirCount(path, 1))
|
||||
steamdir, err := dirs.GetSteamUserDirectory()
|
||||
if err != nil {
|
||||
return s
|
||||
@@ -53,7 +55,7 @@ func NewScreenshotsDirFromPath(path string, limit int) ScreenshotCollection {
|
||||
return s
|
||||
}
|
||||
s.GameName = info.Name
|
||||
s.UserId = filepath.Base(getDir(path, 4))
|
||||
s.UserId = filepath.Base(baseDirCount(path, 4))
|
||||
|
||||
files, err := dir.Readdir(0)
|
||||
if err != nil {
|
||||
@@ -100,7 +102,46 @@ func NewScreenshotsDirFromPath(path string, limit int) ScreenshotCollection {
|
||||
return s
|
||||
}
|
||||
|
||||
func getDir(path string, depth int) string {
|
||||
// GetAllDirs returns a list of directories for all screenshots of all games.
|
||||
func GetAllDirs() ([]string, error) {
|
||||
syncDir, err := dirs.GetSyncDirectory()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var dirs []string
|
||||
remoteDir := fmt.Sprintf("%s/remote", syncDir)
|
||||
entries, err := os.ReadDir(remoteDir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, entry := range entries {
|
||||
// logger.Debug("Entry: %s", entry.Name())
|
||||
if !entry.IsDir() {
|
||||
continue
|
||||
}
|
||||
if slices.Contains(common.STEAM_INTERNAL_IDS, entry.Name()) {
|
||||
continue
|
||||
}
|
||||
scrDir := fmt.Sprintf("%s/%s/screenshots", remoteDir, entry.Name())
|
||||
logger.Debug("Checking: %s", scrDir)
|
||||
if _, err := os.Stat(scrDir); os.IsNotExist(err) {
|
||||
continue
|
||||
}
|
||||
dirs = append(dirs, scrDir)
|
||||
}
|
||||
return dirs, nil
|
||||
}
|
||||
|
||||
// GetDirForGame returns the directory path for screenshots of a specific game by game ID.
|
||||
func GetDirForGame(gameId string) (string, error) {
|
||||
syncDir, err := dirs.GetSyncDirectory()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return fmt.Sprintf("%s/remote/%s/screenshots", syncDir, gameId), nil
|
||||
}
|
||||
|
||||
func baseDirCount(path string, depth int) string {
|
||||
for i := 0; i < depth; i++ {
|
||||
path = filepath.Dir(path)
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strconv"
|
||||
|
||||
"github.com/chenasraf/stimvisor/common"
|
||||
"github.com/chenasraf/stimvisor/dirs"
|
||||
@@ -30,7 +32,7 @@ type GameInfo struct {
|
||||
|
||||
// GetGameInfo retrieves the information of a game given its ID.
|
||||
func GetGameInfo(gameId string) (GameInfo, error) {
|
||||
gameDir, err := dirs.GetGameDirectory(gameId)
|
||||
gameDir, err := GetGameDir(gameId)
|
||||
if err != nil {
|
||||
return GameInfo{}, err
|
||||
}
|
||||
@@ -144,3 +146,42 @@ func fetchGameInfo(gameId string) (map[string]interface{}, error) {
|
||||
|
||||
return respGameData, nil
|
||||
}
|
||||
|
||||
// GetAllDirs returns a list of directories for all games of a specific Steam user by user ID.
|
||||
func GetAllDirs(userId string) ([]string, error) {
|
||||
userDir, err := dirs.GetUserDirectory(userId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
entries, err := os.ReadDir(userDir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
gameDirs := []string{}
|
||||
for _, entry := range entries {
|
||||
if !entry.IsDir() {
|
||||
continue
|
||||
}
|
||||
if slices.Contains(common.STEAM_INTERNAL_IDS, entry.Name()) {
|
||||
continue
|
||||
}
|
||||
if _, err := strconv.Atoi(entry.Name()); err != nil {
|
||||
continue
|
||||
}
|
||||
gameDir := fmt.Sprintf("%s/%s", userDir, entry.Name())
|
||||
gameDirs = append(gameDirs, gameDir)
|
||||
}
|
||||
return gameDirs, nil
|
||||
}
|
||||
|
||||
// GetGameDir returns the directory path for a specific game by game ID.
|
||||
func GetGameDir(gameId string) (string, error) {
|
||||
userDir, err := dirs.GetSteamUserDirectory()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s/%s", userDir, gameId), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user