Skip to content

Commit fd1ac2d

Browse files
authored
Fix media dirs (#3659)
1 parent 5bb8ba2 commit fd1ac2d

File tree

3 files changed

+73
-13
lines changed

3 files changed

+73
-13
lines changed

backend/http/media.go

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import (
1010
"runtime"
1111
"strings"
1212

13+
"github.com/timmo001/system-bridge/settings"
1314
"github.com/timmo001/system-bridge/utils"
15+
"github.com/timmo001/system-bridge/utils/handlers/filesystem"
1416
)
1517

1618
// ServeMediaFileDataHandler handles requests to serve media files from predefined base directories
@@ -167,28 +169,65 @@ func getBaseDirectoryPath(base string) (string, error) {
167169
var basePath string
168170
var err error
169171

172+
// First check hardcoded standard directories (for backward compatibility)
170173
switch base {
171174
case "documents":
172175
basePath, err = getDocumentsPath()
176+
if err == nil {
177+
return basePath, nil
178+
}
173179
case "downloads":
174180
basePath, err = getDownloadsPath()
181+
if err == nil {
182+
return basePath, nil
183+
}
175184
case "home":
176185
basePath, err = getHomePath()
186+
if err == nil {
187+
return basePath, nil
188+
}
177189
case "music":
178190
basePath, err = getMusicPath()
191+
if err == nil {
192+
return basePath, nil
193+
}
179194
case "pictures":
180195
basePath, err = getPicturesPath()
196+
if err == nil {
197+
return basePath, nil
198+
}
181199
case "videos":
182200
basePath, err = getVideosPath()
183-
default:
184-
return "", fmt.Errorf("invalid base directory: %s", base)
201+
if err == nil {
202+
return basePath, nil
203+
}
185204
}
186205

187-
if err != nil {
188-
return "", fmt.Errorf("failed to get %s path: %w", base, err)
206+
// If not found in hardcoded list, check user directories (XDG + custom)
207+
directories := filesystem.GetUserDirectories()
208+
for _, dir := range directories {
209+
if dir.Key == base {
210+
if dir.Path == "" {
211+
return "", fmt.Errorf("directory %s has empty path", base)
212+
}
213+
return dir.Path, nil
214+
}
215+
}
216+
217+
// Also check custom media directories from settings
218+
s, err := settings.Load()
219+
if err == nil {
220+
for _, directory := range s.Media.Directories {
221+
if directory.Name == base {
222+
if directory.Path == "" {
223+
return "", fmt.Errorf("directory %s has empty path", base)
224+
}
225+
return directory.Path, nil
226+
}
227+
}
189228
}
190229

191-
return basePath, nil
230+
return "", fmt.Errorf("invalid base directory: %s", base)
192231
}
193232

194233
// getContentType returns the MIME content type for a file based on its extension

event/handler/get-files.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ func RegisterGetFilesHandler(router *event.MessageRouter) {
5555
}
5656
}
5757

58+
// Validate base directory path is not empty
59+
if baseDirectory.Path == "" {
60+
slog.Error("Base directory path is empty", "base", data.BaseDirectory)
61+
return event.MessageResponse{
62+
ID: message.ID,
63+
Type: event.ResponseTypeError,
64+
Subtype: event.ResponseSubtypeBadDirectory,
65+
Message: "Base directory path is empty",
66+
}
67+
}
68+
5869
// Get files
5970
var files []GetFileResponseData
6071
if data.Path != "" {

utils/handlers/filesystem/filesystem_linux.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,32 @@ func getUserDirectories() []DirectoryInfo {
3232
parts := strings.SplitN(line, "=", 2)
3333
if len(parts) == 2 {
3434
key := strings.ToLower(strings.TrimPrefix(parts[0], "XDG_"))
35-
key = strings.TrimSuffix(key, "_DIR")
35+
key = strings.TrimSuffix(key, "_dir")
36+
// Normalize XDG key names to match expected convention across platforms
37+
// XDG uses "DOWNLOAD_DIR" (singular) but codebase expects "downloads" (plural)
38+
if key == "download" {
39+
key = "downloads"
40+
}
3641
path := strings.Trim(parts[1], "\"")
3742
path = strings.ReplaceAll(path, "$HOME", homeDir)
3843
dirs[key] = path
3944
}
4045
}
4146
}
4247

43-
return []DirectoryInfo{
44-
{Key: "desktop", Path: dirs["desktop"]},
45-
{Key: "documents", Path: dirs["documents"]},
46-
{Key: "downloads", Path: dirs["download"]},
47-
{Key: "music", Path: dirs["music"]},
48-
{Key: "pictures", Path: dirs["pictures"]},
49-
{Key: "videos", Path: dirs["videos"]},
48+
// Build DirectoryInfo list from all parsed XDG directories
49+
userDirs := make([]DirectoryInfo, 0, len(dirs))
50+
for key, path := range dirs {
51+
// Only include directories with non-empty paths
52+
if path != "" {
53+
userDirs = append(userDirs, DirectoryInfo{
54+
Key: key,
55+
Path: path,
56+
})
57+
}
5058
}
59+
60+
return userDirs
5161
}
5262
}
5363

0 commit comments

Comments
 (0)