read_write.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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)
  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.FastGet(target)
  31. return data, err
  32. }
  33. func SaveAs(host string, port int, dir, name string, contentType string, byteBuffer *bytes.Buffer) error {
  34. target := fmt.Sprintf("http://%s:%d%s/%s", host, port, dir, name)
  35. // set the HTTP method, url, and request body
  36. req, err := http.NewRequest(http.MethodPut, target, byteBuffer)
  37. if err != nil {
  38. return err
  39. }
  40. // set the request header Content-Type for json
  41. if contentType != "" {
  42. req.Header.Set("Content-Type", contentType)
  43. }
  44. resp, err := http.DefaultClient.Do(req)
  45. if err != nil {
  46. return err
  47. }
  48. defer util.CloseResponse(resp)
  49. b, err := ioutil.ReadAll(resp.Body)
  50. if err != nil {
  51. return err
  52. }
  53. if resp.StatusCode >= 400 {
  54. return fmt.Errorf("%s: %s %v", target, resp.Status, string(b))
  55. }
  56. return nil
  57. }
  58. func SaveInsideFiler(client filer_pb.SeaweedFilerClient, dir, name string, content []byte) error {
  59. resp, err := filer_pb.LookupEntry(client, &filer_pb.LookupDirectoryEntryRequest{
  60. Directory: dir,
  61. Name: name,
  62. })
  63. if err == filer_pb.ErrNotFound {
  64. err = filer_pb.CreateEntry(client, &filer_pb.CreateEntryRequest{
  65. Directory: dir,
  66. Entry: &filer_pb.Entry{
  67. Name: name,
  68. IsDirectory: false,
  69. Attributes: &filer_pb.FuseAttributes{
  70. Mtime: time.Now().Unix(),
  71. Crtime: time.Now().Unix(),
  72. FileMode: uint32(0644),
  73. Collection: "",
  74. Replication: "",
  75. FileSize: uint64(len(content)),
  76. },
  77. Content: content,
  78. },
  79. })
  80. } else if err == nil {
  81. entry := resp.Entry
  82. entry.Content = content
  83. entry.Attributes.Mtime = time.Now().Unix()
  84. entry.Attributes.FileSize = uint64(len(content))
  85. err = filer_pb.UpdateEntry(client, &filer_pb.UpdateEntryRequest{
  86. Directory: dir,
  87. Entry: entry,
  88. })
  89. }
  90. return err
  91. }