sortfile.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. user "imuslab.com/arozos/mod/user"
  12. "imuslab.com/arozos/mod/utils"
  13. )
  14. type LargeFileScanner struct {
  15. userHandler *user.UserHandler
  16. }
  17. func NewLargeFileScanner(u *user.UserHandler) *LargeFileScanner {
  18. return &LargeFileScanner{
  19. userHandler: u,
  20. }
  21. }
  22. func (s *LargeFileScanner) HandleLargeFileList(w http.ResponseWriter, r *http.Request) {
  23. userinfo, err := s.userHandler.GetUserInfoFromRequest(w, r)
  24. if err != nil {
  25. utils.SendErrorResponse(w, err.Error())
  26. return
  27. }
  28. //Check if limit is set. If yes, use the limit in return
  29. limit, err := utils.Mv(r, "number", false)
  30. if err != nil {
  31. limit = "20"
  32. }
  33. //Try convert the limit to integer
  34. limitInt, err := strconv.Atoi(limit)
  35. if err != nil {
  36. limitInt = 20
  37. }
  38. //Get all the fshandler for this user
  39. fsHandlers := userinfo.GetAllFileSystemHandler()
  40. type FileObject struct {
  41. Filename string
  42. Filepath string
  43. Size int64
  44. IsOwner bool
  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. fsh.FileSystemAbstraction.Walk(fsh.Path, func(path string, info os.FileInfo, err error) error {
  52. if info == nil || err != nil {
  53. //Disk IO Error
  54. return errors.New("Disk IO Error: " + err.Error())
  55. }
  56. if info.IsDir() {
  57. return nil
  58. }
  59. //Push the current file into the filelist
  60. if info.Size() > 0 {
  61. vpath, _ := fsh.FileSystemAbstraction.RealPathToVirtualPath(path, userinfo.Username)
  62. fileList = append(fileList, &FileObject{
  63. Filename: filepath.Base(path),
  64. Filepath: vpath,
  65. realpath: path,
  66. thisfsh: fsh,
  67. Size: info.Size(),
  68. IsOwner: false,
  69. })
  70. }
  71. return nil
  72. })
  73. /*
  74. if err != nil {
  75. sendErrorResponse(w, "Failed to scan emulated storage device: "+fsh.Name)
  76. return
  77. }
  78. */
  79. }
  80. //Sort the fileList
  81. sort.Slice(fileList, func(i, j int) bool {
  82. return fileList[i].Size > fileList[j].Size
  83. })
  84. //Set the max filecount to prevent slice bounds out of range
  85. if len(fileList) < limitInt {
  86. limitInt = len(fileList)
  87. }
  88. //Only check ownership of those requested
  89. for _, file := range fileList[:limitInt] {
  90. if userinfo.IsOwnerOfFile(file.thisfsh, file.Filepath) {
  91. file.IsOwner = true
  92. } else {
  93. file.IsOwner = false
  94. }
  95. }
  96. //Format the results and return
  97. jsonString, _ := json.Marshal(fileList[:limitInt])
  98. utils.SendJSONResponse(w, string(jsonString))
  99. }