filer_pb_helper.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package filer_pb
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  6. )
  7. func toFileIdObject(fileIdStr string) (*FileId, error) {
  8. t, err := needle.ParseFileIdFromString(fileIdStr)
  9. if err != nil {
  10. return nil, err
  11. }
  12. return &FileId{
  13. VolumeId: uint32(t.VolumeId),
  14. Cookie: uint32(t.Cookie),
  15. FileKey: uint64(t.Key),
  16. }, nil
  17. }
  18. func (fid *FileId) toFileIdString() string {
  19. return needle.NewFileId(needle.VolumeId(fid.VolumeId), fid.FileKey, fid.Cookie).String()
  20. }
  21. func (c *FileChunk) GetFileIdString() string {
  22. if c.FileId != "" {
  23. return c.FileId
  24. }
  25. if c.Fid != nil {
  26. c.FileId = c.Fid.toFileIdString()
  27. return c.FileId
  28. }
  29. return ""
  30. }
  31. func BeforeEntrySerialization(chunks []*FileChunk) {
  32. for _, chunk := range chunks {
  33. if chunk.FileId != "" {
  34. if fid, err := toFileIdObject(chunk.FileId); err == nil {
  35. chunk.Fid = fid
  36. chunk.FileId = ""
  37. }
  38. }
  39. if chunk.SourceFileId != "" {
  40. if fid, err := toFileIdObject(chunk.SourceFileId); err == nil {
  41. chunk.SourceFid = fid
  42. chunk.SourceFileId = ""
  43. }
  44. }
  45. }
  46. }
  47. func AfterEntryDeserialization(chunks []*FileChunk) {
  48. for _, chunk := range chunks {
  49. if chunk.Fid != nil && chunk.FileId == "" {
  50. chunk.FileId = chunk.Fid.toFileIdString()
  51. }
  52. if chunk.SourceFid != nil && chunk.SourceFileId == "" {
  53. chunk.SourceFileId = chunk.SourceFid.toFileIdString()
  54. }
  55. }
  56. }
  57. func CreateEntry(ctx context.Context, client SeaweedFilerClient, request *CreateEntryRequest) error {
  58. resp, err := client.CreateEntry(ctx, request)
  59. if err == nil && resp.Error != "" {
  60. return fmt.Errorf("CreateEntry: %v", resp.Error)
  61. }
  62. if err != nil {
  63. return fmt.Errorf("CreateEntry: %v", err)
  64. }
  65. return err
  66. }