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.

71 lines
2.2 KiB

  1. package filesys
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "github.com/chrislusf/seaweedfs/weed/filer"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/operation"
  9. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  10. "github.com/chrislusf/seaweedfs/weed/security"
  11. "github.com/chrislusf/seaweedfs/weed/util"
  12. )
  13. func (wfs *WFS) saveDataAsChunk(fullPath util.FullPath) filer.SaveDataAsChunkFunctionType {
  14. return func(reader io.Reader, filename string, offset int64) (chunk *filer_pb.FileChunk, collection, replication string, err error) {
  15. var fileId, host string
  16. var auth security.EncodedJwt
  17. if err := wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  18. request := &filer_pb.AssignVolumeRequest{
  19. Count: 1,
  20. Replication: wfs.option.Replication,
  21. Collection: wfs.option.Collection,
  22. TtlSec: wfs.option.TtlSec,
  23. DataCenter: wfs.option.DataCenter,
  24. Path: string(fullPath),
  25. }
  26. resp, err := client.AssignVolume(context.Background(), request)
  27. if err != nil {
  28. glog.V(0).Infof("assign volume failure %v: %v", request, err)
  29. return err
  30. }
  31. if resp.Error != "" {
  32. return fmt.Errorf("assign volume failure %v: %v", request, resp.Error)
  33. }
  34. fileId, auth = resp.FileId, security.EncodedJwt(resp.Auth)
  35. loc := &filer_pb.Location{
  36. Url: resp.Url,
  37. PublicUrl: resp.PublicUrl,
  38. }
  39. host = wfs.AdjustedUrl(loc)
  40. collection, replication = resp.Collection, resp.Replication
  41. return nil
  42. }); err != nil {
  43. return nil, "", "", fmt.Errorf("filerGrpcAddress assign volume: %v", err)
  44. }
  45. fileUrl := fmt.Sprintf("http://%s/%s", host, fileId)
  46. uploadResult, err, data := operation.Upload(fileUrl, filename, wfs.option.Cipher, reader, false, "", nil, auth)
  47. if err != nil {
  48. glog.V(0).Infof("upload data %v to %s: %v", filename, fileUrl, err)
  49. return nil, "", "", fmt.Errorf("upload data: %v", err)
  50. }
  51. if uploadResult.Error != "" {
  52. glog.V(0).Infof("upload failure %v to %s: %v", filename, fileUrl, err)
  53. return nil, "", "", fmt.Errorf("upload result: %v", uploadResult.Error)
  54. }
  55. wfs.chunkCache.SetChunk(fileId, data)
  56. chunk = uploadResult.ToPbFileChunk(fileId, offset)
  57. return chunk, collection, replication, nil
  58. }
  59. }