filer_grpc_server.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package weed_server
  2. import (
  3. "context"
  4. "strconv"
  5. "github.com/chrislusf/seaweedfs/weed/operation"
  6. "github.com/chrislusf/seaweedfs/weed/util"
  7. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  8. "fmt"
  9. )
  10. func (fs *FilerServer) LookupDirectoryEntry(ctx context.Context, req *filer_pb.LookupDirectoryEntryRequest) (*filer_pb.LookupDirectoryEntryResponse, error) {
  11. found, fileId, err := fs.filer.LookupDirectoryEntry(req.Directory, req.Name)
  12. if err != nil {
  13. return nil, err
  14. }
  15. if !found {
  16. return nil, fmt.Errorf("%s not found under %s", req.Name, req.Directory)
  17. }
  18. return &filer_pb.LookupDirectoryEntryResponse{
  19. Entry: &filer_pb.Entry{
  20. Name: req.Name,
  21. IsDirectory: fileId == "",
  22. FileId: fileId,
  23. },
  24. }, nil
  25. }
  26. func (fs *FilerServer) ListEntries(ctx context.Context, req *filer_pb.ListEntriesRequest) (*filer_pb.ListEntriesResponse, error) {
  27. directoryNames, err := fs.filer.ListDirectories(req.Directory)
  28. if err != nil {
  29. return nil, err
  30. }
  31. files, err := fs.filer.ListFiles(req.Directory, "", 1000)
  32. if err != nil {
  33. return nil, err
  34. }
  35. resp := &filer_pb.ListEntriesResponse{}
  36. for _, dir := range directoryNames {
  37. resp.Entries = append(resp.Entries, &filer_pb.Entry{
  38. Name: string(dir),
  39. IsDirectory: true,
  40. })
  41. }
  42. for _, fileEntry := range files {
  43. resp.Entries = append(resp.Entries, &filer_pb.Entry{
  44. Name: fileEntry.Name,
  45. IsDirectory: false,
  46. FileId: string(fileEntry.Id),
  47. })
  48. }
  49. return resp, nil
  50. }
  51. func (fs *FilerServer) GetFileAttributes(ctx context.Context, req *filer_pb.GetFileAttributesRequest) (*filer_pb.GetFileAttributesResponse, error) {
  52. attributes := &filer_pb.FuseAttributes{}
  53. server, err := operation.LookupFileId(fs.getMasterNode(), req.FileId)
  54. if err != nil {
  55. return nil, err
  56. }
  57. head, err := util.Head(server)
  58. if err != nil {
  59. return nil, err
  60. }
  61. attributes.FileSize, err = strconv.ParseUint(head.Get("Content-Length"), 10, 0)
  62. if err != nil {
  63. return nil, err
  64. }
  65. return &filer_pb.GetFileAttributesResponse{
  66. Attributes: attributes,
  67. }, nil
  68. }
  69. func (fs *FilerServer) GetFileContent(ctx context.Context, req *filer_pb.GetFileContentRequest) (*filer_pb.GetFileContentResponse, error) {
  70. server, err := operation.LookupFileId(fs.getMasterNode(), req.FileId)
  71. if err != nil {
  72. return nil, err
  73. }
  74. content, err := util.Get(server)
  75. if err != nil {
  76. return nil, err
  77. }
  78. return &filer_pb.GetFileContentResponse{
  79. Content: content,
  80. }, nil
  81. }
  82. func (fs *FilerServer) DeleteEntry(ctx context.Context, req *filer_pb.DeleteEntryRequest) (resp *filer_pb.DeleteEntryResponse, err error) {
  83. if req.IsDirectory {
  84. err = fs.filer.DeleteDirectory(req.Directory+req.Name, false)
  85. } else {
  86. fid, err := fs.filer.DeleteFile(req.Directory + req.Name)
  87. if err == nil && fid != "" {
  88. err = operation.DeleteFile(fs.getMasterNode(), fid, fs.jwt(fid))
  89. }
  90. }
  91. return nil, err
  92. }