(directoryPath string, mode string, scope string)
| 16 | } |
| 17 | |
| 18 | func GetDirectoryItems(directoryPath string, mode string, scope string) ([]types.DirectoryItem, int64) { |
| 19 | var directoryItems []types.DirectoryItem |
| 20 | var directoryIDs []directoryID |
| 21 | |
| 22 | // Open the directory |
| 23 | dir, err := os.Open(directoryPath) |
| 24 | if err != nil { |
| 25 | log.Printf("Error opening directory %s: %v", directoryPath, err) |
| 26 | return nil, 0 |
| 27 | } |
| 28 | defer dir.Close() |
| 29 | |
| 30 | // Read only the immediate directory contents (non-recursive) |
| 31 | files, err := dir.Readdir(-1) // -1 means return all entries |
| 32 | if err != nil { |
| 33 | log.Printf("Error reading directory %s: %v", directoryPath, err) |
| 34 | return nil, 0 |
| 35 | } |
| 36 | |
| 37 | for i, file := range files { |
| 38 | fullPath := filepath.Join(directoryPath, file.Name()) |
| 39 | parentPath := GetParentPath(fullPath) |
| 40 | parentPath = ReplaceHostPrefix(parentPath, scope) |
| 41 | if parentPath[len(parentPath)-1] != '/' { |
| 42 | parentPath += "/" |
| 43 | } |
| 44 | path := fmt.Sprintf("%s%s", parentPath, file.Name()) |
| 45 | isDir := file.IsDir() |
| 46 | size := ConvertBytesToString(file.Size()) |
| 47 | var sizeBytes int64 = file.Size() |
| 48 | |
| 49 | if isDir && mode == "Quality mode" { |
| 50 | directoryIDs = append(directoryIDs, directoryID{id: i, fullPath: fullPath}) |
| 51 | } |
| 52 | |
| 53 | directoryItem := types.DirectoryItem{ |
| 54 | Id: i, |
| 55 | Name: file.Name(), |
| 56 | ParentPath: parentPath, |
| 57 | IsDirectory: isDir, |
| 58 | Path: path, |
| 59 | DateModified: file.ModTime(), |
| 60 | Size: size, |
| 61 | SizeBytes: sizeBytes, |
| 62 | } |
| 63 | |
| 64 | directoryItems = append(directoryItems, directoryItem) |
| 65 | } |
| 66 | |
| 67 | if mode == "Quality mode" { |
| 68 | ch := make(chan types.DirectorySizeOutput, len(directoryIDs)) |
| 69 | var wg sync.WaitGroup |
| 70 | for _, dirID := range directoryIDs { |
| 71 | wg.Add(1) |
| 72 | go GetDirectorySizeAsync(dirID.fullPath, dirID.id, ch, &wg) |
| 73 | } |
| 74 | |
| 75 | go func() { |
no test coverage detected