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.

116 lines
2.8 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package filer
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  6. "github.com/chrislusf/seaweedfs/weed/util"
  7. "github.com/chrislusf/seaweedfs/weed/wdclient"
  8. "io/ioutil"
  9. "math"
  10. "net/http"
  11. "time"
  12. )
  13. func ReadEntry(masterClient *wdclient.MasterClient, filerClient filer_pb.SeaweedFilerClient, dir, name string, byteBuffer *bytes.Buffer) error {
  14. request := &filer_pb.LookupDirectoryEntryRequest{
  15. Directory: dir,
  16. Name: name,
  17. }
  18. respLookupEntry, err := filer_pb.LookupEntry(filerClient, request)
  19. if err != nil {
  20. return err
  21. }
  22. if len(respLookupEntry.Entry.Content) > 0 {
  23. _, err = byteBuffer.Write(respLookupEntry.Entry.Content)
  24. return err
  25. }
  26. return StreamContent(masterClient, byteBuffer, respLookupEntry.Entry.Chunks, 0, math.MaxInt64, false)
  27. }
  28. func ReadContent(filerAddress string, dir, name string) ([]byte, error) {
  29. target := fmt.Sprintf("http://%s%s/%s", filerAddress, dir, name)
  30. data, _, err := util.Get(target)
  31. return data, err
  32. }
  33. func SaveAs(host string, port int, dir, name string, contentType string, byteBuffer *bytes.Buffer) error {
  34. var target string
  35. if port == 0 {
  36. target = fmt.Sprintf("http://%s%s/%s", host, dir, name)
  37. } else {
  38. target = fmt.Sprintf("http://%s:%d%s/%s", host, port, dir, name)
  39. }
  40. // set the HTTP method, url, and request body
  41. req, err := http.NewRequest(http.MethodPut, target, byteBuffer)
  42. if err != nil {
  43. return err
  44. }
  45. // set the request header Content-Type for json
  46. if contentType != "" {
  47. req.Header.Set("Content-Type", contentType)
  48. }
  49. resp, err := http.DefaultClient.Do(req)
  50. if err != nil {
  51. return err
  52. }
  53. defer util.CloseResponse(resp)
  54. b, err := ioutil.ReadAll(resp.Body)
  55. if err != nil {
  56. return err
  57. }
  58. if resp.StatusCode >= 400 {
  59. return fmt.Errorf("%s: %s %v", target, resp.Status, string(b))
  60. }
  61. return nil
  62. }
  63. func SaveInsideFiler(client filer_pb.SeaweedFilerClient, dir, name string, content []byte) error {
  64. resp, err := filer_pb.LookupEntry(client, &filer_pb.LookupDirectoryEntryRequest{
  65. Directory: dir,
  66. Name: name,
  67. })
  68. if err == filer_pb.ErrNotFound {
  69. err = filer_pb.CreateEntry(client, &filer_pb.CreateEntryRequest{
  70. Directory: dir,
  71. Entry: &filer_pb.Entry{
  72. Name: name,
  73. IsDirectory: false,
  74. Attributes: &filer_pb.FuseAttributes{
  75. Mtime: time.Now().Unix(),
  76. Crtime: time.Now().Unix(),
  77. FileMode: uint32(0644),
  78. Collection: "",
  79. Replication: "",
  80. FileSize: uint64(len(content)),
  81. },
  82. Content: content,
  83. },
  84. })
  85. } else if err == nil {
  86. entry := resp.Entry
  87. entry.Content = content
  88. entry.Attributes.Mtime = time.Now().Unix()
  89. entry.Attributes.FileSize = uint64(len(content))
  90. err = filer_pb.UpdateEntry(client, &filer_pb.UpdateEntryRequest{
  91. Directory: dir,
  92. Entry: entry,
  93. })
  94. }
  95. return err
  96. }