file.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package dati
  2. /*
  3. Copyright (C) 2023 gearsix <gearsix@tuta.io>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. import (
  16. "fmt"
  17. "os"
  18. "path/filepath"
  19. "sort"
  20. "time"
  21. )
  22. // SortFileList sorts `filepath` (a list of filepaths) in `order`. `order`
  23. // can be any of the following values: "filename", "filename-asc",
  24. // "filename-desc", "modified", "modified-asc", "modified-desc"; by default
  25. // "filename" and "modified" are in ascending direction, if specified an
  26. // "-asc" suffix will set the direction to ascending and "-desc" will set the
  27. // direction to descending.
  28. // This was originally intended to be used before calling LoadDataFiles on a
  29. // set of "data" files.
  30. func SortFileList(paths []string, order string) (sorted []string, err error) {
  31. if order == "filename-desc" {
  32. sorted = sortFileListByName("desc", paths)
  33. } else if order == "filename-asc" || order == "filename" {
  34. sorted = sortFileListByName("asc", paths)
  35. } else if order == "modified-desc" {
  36. sorted, err = sortFileListByMod("desc", paths)
  37. } else if order == "modified-asc" || order == "modified" {
  38. sorted, err = sortFileListByMod("asc", paths)
  39. } else {
  40. err = fmt.Errorf("invalid order '%s'", order)
  41. sorted = paths
  42. }
  43. return
  44. }
  45. func sortFileListByName(direction string, paths []string) []string {
  46. if direction == "desc" {
  47. sort.Slice(paths, func(i, j int) bool {
  48. return filepath.Base(paths[i]) > filepath.Base(paths[j])
  49. })
  50. } else {
  51. sort.Slice(paths, func(i, j int) bool {
  52. return filepath.Base(paths[i]) < filepath.Base(paths[j])
  53. })
  54. }
  55. return paths
  56. }
  57. func sortFileListByMod(direction string, paths []string) ([]string, error) {
  58. stats := make(map[string]os.FileInfo)
  59. for _, p := range paths {
  60. stat, err := os.Stat(p)
  61. if err != nil {
  62. return paths, err
  63. }
  64. stats[p] = stat
  65. }
  66. modtimes := make([]time.Time, 0, len(paths))
  67. for _, stat := range stats {
  68. modtimes = append(modtimes, stat.ModTime())
  69. }
  70. if direction == "desc" {
  71. sort.Slice(modtimes, func(i, j int) bool {
  72. return modtimes[i].After(modtimes[j])
  73. })
  74. } else {
  75. sort.Slice(modtimes, func(i, j int) bool {
  76. return modtimes[i].Before(modtimes[j])
  77. })
  78. }
  79. sorted := make([]string, 0)
  80. for _, t := range modtimes {
  81. for path, stat := range stats {
  82. if t == stat.ModTime() {
  83. sorted = append(sorted, path)
  84. delete(stats, path)
  85. break
  86. }
  87. }
  88. }
  89. if len(sorted) != len(paths) {
  90. return nil, fmt.Errorf("sorted length invalid")
  91. }
  92. return sorted, nil
  93. }