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.

63 lines
1.5 KiB

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. "math"
  9. "net/http"
  10. )
  11. func ReadEntry(masterClient *wdclient.MasterClient, filerClient filer_pb.SeaweedFilerClient, dir, name string, byteBuffer *bytes.Buffer) error {
  12. request := &filer_pb.LookupDirectoryEntryRequest{
  13. Directory: dir,
  14. Name: name,
  15. }
  16. respLookupEntry, err := filer_pb.LookupEntry(filerClient, request)
  17. if err != nil {
  18. return err
  19. }
  20. if len(respLookupEntry.Entry.Content) > 0 {
  21. _, err = byteBuffer.Write(respLookupEntry.Entry.Content)
  22. return err
  23. }
  24. return StreamContent(masterClient, byteBuffer, respLookupEntry.Entry.Chunks, 0, math.MaxInt64)
  25. }
  26. func ReadContent(filerAddress string, dir, name string) ([]byte, error) {
  27. target := fmt.Sprintf("http://%s%s/%s", filerAddress, dir, name)
  28. data, _, err := util.Get(target)
  29. return data, err
  30. }
  31. func SaveAs(host string, port int, dir, name string, contentType string, byteBuffer *bytes.Buffer) error {
  32. target := fmt.Sprintf("http://%s:%d%s/%s", host, port, dir, name)
  33. // set the HTTP method, url, and request body
  34. req, err := http.NewRequest(http.MethodPut, target, byteBuffer)
  35. if err != nil {
  36. return err
  37. }
  38. // set the request header Content-Type for json
  39. if contentType != "" {
  40. req.Header.Set("Content-Type", contentType)
  41. }
  42. resp, err := http.DefaultClient.Do(req)
  43. if err != nil {
  44. return err
  45. }
  46. util.CloseResponse(resp)
  47. return nil
  48. }