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.

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