You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
2.8 KiB

9 years ago
10 years ago
9 years ago
9 years ago
  1. package operation
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "net/url"
  7. "strings"
  8. "sync"
  9. "net/http"
  10. "github.com/chrislusf/seaweedfs/go/security"
  11. "github.com/chrislusf/seaweedfs/go/util"
  12. )
  13. type DeleteResult struct {
  14. Fid string `json:"fid"`
  15. Size int `json:"size"`
  16. Status int `json:"status"`
  17. Error string `json:"error,omitempty"`
  18. }
  19. func DeleteFile(master string, fileId string, jwt security.EncodedJwt) error {
  20. fileUrl, err := LookupFileId(master, fileId)
  21. if err != nil {
  22. return fmt.Errorf("Failed to lookup %s:%v", fileId, err)
  23. }
  24. err = util.Delete(fileUrl, jwt)
  25. if err != nil {
  26. return fmt.Errorf("Failed to delete %s:%v", fileUrl, err)
  27. }
  28. return nil
  29. }
  30. func ParseFileId(fid string) (vid string, key_cookie string, err error) {
  31. commaIndex := strings.Index(fid, ",")
  32. if commaIndex <= 0 {
  33. return "", "", errors.New("Wrong fid format.")
  34. }
  35. return fid[:commaIndex], fid[commaIndex+1:], nil
  36. }
  37. type DeleteFilesResult struct {
  38. Errors []string
  39. Results []DeleteResult
  40. }
  41. func DeleteFiles(master string, fileIds []string) (*DeleteFilesResult, error) {
  42. vid_to_fileIds := make(map[string][]string)
  43. ret := &DeleteFilesResult{}
  44. var vids []string
  45. for _, fileId := range fileIds {
  46. vid, _, err := ParseFileId(fileId)
  47. if err != nil {
  48. ret.Results = append(ret.Results, DeleteResult{
  49. Fid: vid,
  50. Status: http.StatusBadRequest,
  51. Error: err.Error()},
  52. )
  53. continue
  54. }
  55. if _, ok := vid_to_fileIds[vid]; !ok {
  56. vid_to_fileIds[vid] = make([]string, 0)
  57. vids = append(vids, vid)
  58. }
  59. vid_to_fileIds[vid] = append(vid_to_fileIds[vid], fileId)
  60. }
  61. lookupResults, err := LookupVolumeIds(master, vids)
  62. if err != nil {
  63. return ret, err
  64. }
  65. server_to_fileIds := make(map[string][]string)
  66. for vid, result := range lookupResults {
  67. if result.Error != "" {
  68. ret.Errors = append(ret.Errors, result.Error)
  69. continue
  70. }
  71. for _, location := range result.Locations {
  72. if _, ok := server_to_fileIds[location.Url]; !ok {
  73. server_to_fileIds[location.Url] = make([]string, 0)
  74. }
  75. server_to_fileIds[location.Url] = append(
  76. server_to_fileIds[location.Url], vid_to_fileIds[vid]...)
  77. }
  78. }
  79. var wg sync.WaitGroup
  80. for server, fidList := range server_to_fileIds {
  81. wg.Add(1)
  82. go func(server string, fidList []string) {
  83. defer wg.Done()
  84. values := make(url.Values)
  85. for _, fid := range fidList {
  86. values.Add("fid", fid)
  87. }
  88. jsonBlob, err := util.Post("http://"+server+"/delete", values)
  89. if err != nil {
  90. ret.Errors = append(ret.Errors, err.Error()+" "+string(jsonBlob))
  91. return
  92. }
  93. var result []DeleteResult
  94. err = json.Unmarshal(jsonBlob, &result)
  95. if err != nil {
  96. ret.Errors = append(ret.Errors, err.Error()+" "+string(jsonBlob))
  97. return
  98. }
  99. ret.Results = append(ret.Results, result...)
  100. }(server, fidList)
  101. }
  102. wg.Wait()
  103. return ret, nil
  104. }