sortfile.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package sortfile
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "net/http"
  6. "os"
  7. "path/filepath"
  8. "sort"
  9. "strconv"
  10. "imuslab.com/arozos/mod/filesystem"
  11. "imuslab.com/arozos/mod/filesystem/arozfs"
  12. user "imuslab.com/arozos/mod/user"
  13. "imuslab.com/arozos/mod/utils"
  14. )
  15. type LargeFileScanner struct {
  16. userHandler *user.UserHandler
  17. }
  18. func NewLargeFileScanner(u *user.UserHandler) *LargeFileScanner {
  19. return &LargeFileScanner{
  20. userHandler: u,
  21. }
  22. }
  23. func (s *LargeFileScanner) HandleLargeFileList(w http.ResponseWriter, r *http.Request) {
  24. userinfo, err := s.userHandler.GetUserInfoFromRequest(w, r)
  25. if err != nil {
  26. utils.SendErrorResponse(w, err.Error())
  27. return
  28. }
  29. //Check if limit is set. If yes, use the limit in return
  30. limit, err := utils.GetPara(r, "number")
  31. if err != nil {
  32. limit = "20"
  33. }
  34. //Try convert the limit to integer
  35. limitInt, err := strconv.Atoi(limit)
  36. if err != nil {
  37. limitInt = 20
  38. }
  39. //Get all the fshandler for this user
  40. fsHandlers := userinfo.GetAllFileSystemHandler()
  41. type FileObject struct {
  42. Filename string
  43. Filepath string
  44. Size int64
  45. realpath string
  46. thisfsh *filesystem.FileSystemHandler
  47. }
  48. //Walk all filesystem handlers and buffer all files and their sizes
  49. fileList := []*FileObject{}
  50. for _, fsh := range fsHandlers {
  51. if fsh.IsNetworkDrive() {
  52. //Skip network drive
  53. continue
  54. }
  55. walkRoot := fsh.Path
  56. if fsh.Hierarchy == "user" {
  57. walkRoot = arozfs.ToSlash(filepath.Join(fsh.Path, "users", userinfo.Username))
  58. }
  59. fsh.FileSystemAbstraction.Walk(walkRoot, func(path string, info os.FileInfo, err error) error {
  60. if info == nil || err != nil {
  61. //Disk IO Error
  62. return errors.New("Disk IO Error: " + err.Error())
  63. }
  64. if info.IsDir() {
  65. return nil
  66. }
  67. //Push the current file into the filelist
  68. if info.Size() > 0 {
  69. vpath, _ := fsh.FileSystemAbstraction.RealPathToVirtualPath(path, userinfo.Username)
  70. fileList = append(fileList, &FileObject{
  71. Filename: filepath.Base(path),
  72. Filepath: vpath,
  73. realpath: path,
  74. thisfsh: fsh,
  75. Size: info.Size(),
  76. })
  77. }
  78. return nil
  79. })
  80. }
  81. //Sort the fileList
  82. sort.Slice(fileList, func(i, j int) bool {
  83. return fileList[i].Size > fileList[j].Size
  84. })
  85. //Set the max filecount to prevent slice bounds out of range
  86. if len(fileList) < limitInt {
  87. limitInt = len(fileList)
  88. }
  89. //Format the results and return
  90. jsonString, _ := json.Marshal(fileList[:limitInt])
  91. utils.SendJSONResponse(w, string(jsonString))
  92. }