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.

123 lines
3.0 KiB

  1. package command
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/filer"
  6. "github.com/chrislusf/seaweedfs/weed/pb"
  7. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  8. "github.com/chrislusf/seaweedfs/weed/wdclient"
  9. "google.golang.org/grpc"
  10. "math"
  11. "net/url"
  12. "os"
  13. "strings"
  14. "github.com/chrislusf/seaweedfs/weed/security"
  15. "github.com/chrislusf/seaweedfs/weed/util"
  16. )
  17. var (
  18. filerCat FilerCatOptions
  19. )
  20. type FilerCatOptions struct {
  21. grpcDialOption grpc.DialOption
  22. filerAddress pb.ServerAddress
  23. filerClient filer_pb.SeaweedFilerClient
  24. output *string
  25. }
  26. func (fco *FilerCatOptions) GetLookupFileIdFunction() wdclient.LookupFileIdFunctionType {
  27. return func(fileId string) (targetUrls []string, err error) {
  28. vid := filer.VolumeId(fileId)
  29. resp, err := fco.filerClient.LookupVolume(context.Background(), &filer_pb.LookupVolumeRequest{
  30. VolumeIds: []string{vid},
  31. })
  32. if err != nil {
  33. return nil, err
  34. }
  35. locations := resp.LocationsMap[vid]
  36. for _, loc := range locations.Locations {
  37. targetUrls = append(targetUrls, fmt.Sprintf("http://%s/%s", loc.Url, fileId))
  38. }
  39. return
  40. }
  41. }
  42. func init() {
  43. cmdFilerCat.Run = runFilerCat // break init cycle
  44. filerCat.output = cmdFilerCat.Flag.String("o", "", "write to file instead of stdout")
  45. }
  46. var cmdFilerCat = &Command{
  47. UsageLine: "filer.cat [-o <file>] http://localhost:8888/path/to/file",
  48. Short: "copy one file to local",
  49. Long: `read one file to stdout or write to a file
  50. `,
  51. }
  52. func runFilerCat(cmd *Command, args []string) bool {
  53. util.LoadConfiguration("security", false)
  54. if len(args) == 0 {
  55. return false
  56. }
  57. filerSource := args[len(args)-1]
  58. filerUrl, err := url.Parse(filerSource)
  59. if err != nil {
  60. fmt.Printf("The last argument should be a URL on filer: %v\n", err)
  61. return false
  62. }
  63. urlPath := filerUrl.Path
  64. if strings.HasSuffix(urlPath, "/") {
  65. fmt.Printf("The last argument should be a file: %v\n", err)
  66. return false
  67. }
  68. filerCat.filerAddress = pb.ServerAddress(filerUrl.Host)
  69. filerCat.grpcDialOption = security.LoadClientTLS(util.GetViper(), "grpc.client")
  70. dir, name := util.FullPath(urlPath).DirAndName()
  71. writer := os.Stdout
  72. if *filerCat.output != "" {
  73. fmt.Printf("saving %s to %s\n", filerSource, *filerCat.output)
  74. f, err := os.OpenFile(*filerCat.output, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755)
  75. if err != nil {
  76. fmt.Printf("open file %s: %v\n", *filerCat.output, err)
  77. return false
  78. }
  79. defer f.Close()
  80. writer = f
  81. }
  82. pb.WithFilerClient(false, filerCat.filerAddress, filerCat.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  83. request := &filer_pb.LookupDirectoryEntryRequest{
  84. Name: name,
  85. Directory: dir,
  86. }
  87. respLookupEntry, err := filer_pb.LookupEntry(client, request)
  88. if err != nil {
  89. return err
  90. }
  91. if len(respLookupEntry.Entry.Content) > 0 {
  92. _, err = writer.Write(respLookupEntry.Entry.Content)
  93. return err
  94. }
  95. filerCat.filerClient = client
  96. return filer.StreamContent(&filerCat, writer, respLookupEntry.Entry.Chunks, 0, math.MaxInt64)
  97. })
  98. return true
  99. }