MCPcopy
hub / github.com/rclone/rclone / Open

Method Open

backend/filelu/filelu_object.go:59–139  ·  view source on GitHub ↗

Open opens the object for reading

(ctx context.Context, options ...fs.OpenOption)

Source from the content-addressed store, hash-verified

57
58// Open opens the object for reading
59func (o *Object) Open(ctx context.Context, options ...fs.OpenOption) (io.ReadCloser, error) {
60 filePath := path.Join(o.fs.root, o.remote)
61 // Get direct link
62 directLink, size, err := o.fs.getDirectLink(ctx, filePath)
63 if err != nil {
64 return nil, fmt.Errorf("failed to get direct link: %w", err)
65 }
66
67 o.size = size
68
69 // Offset and Count for range download
70 var offset int64
71 var count int64
72 fs.FixRangeOption(options, o.size)
73 for _, option := range options {
74 switch x := option.(type) {
75 case *fs.RangeOption:
76 offset, count = x.Decode(o.size)
77 if count < 0 {
78 count = o.size - offset
79 }
80 case *fs.SeekOption:
81 offset = x.Offset
82 count = o.size
83 default:
84 if option.Mandatory() {
85 fs.Logf(o, "Unsupported mandatory option: %v", option)
86 }
87 }
88 }
89
90 // Wrap the response body to handle offset and count
91 var reader io.ReadCloser
92 err = o.fs.pacer.Call(func() (bool, error) {
93 req, err := http.NewRequestWithContext(ctx, "GET", directLink, nil)
94 if err != nil {
95 return false, fmt.Errorf("failed to create download request: %w", err)
96 }
97
98 resp, err := o.fs.client.Do(req)
99 if err != nil {
100 return shouldRetry(err), fmt.Errorf("failed to download file: %w", err)
101 }
102
103 if resp.StatusCode != http.StatusOK {
104 defer func() {
105 if err := resp.Body.Close(); err != nil {
106 fs.Logf(nil, "Failed to close response body: %v", err)
107 }
108 }()
109 return false, fmt.Errorf("failed to download file: HTTP %d", resp.StatusCode)
110 }
111
112 if offset > 0 {
113 _, err = io.CopyN(io.Discard, resp.Body, offset)
114 if err != nil {
115 _ = resp.Body.Close()
116 return false, fmt.Errorf("failed to skip offset: %w", err)

Callers

nothing calls this directly

Calls 11

FixRangeOptionFunction · 0.92
LogfFunction · 0.92
JoinMethod · 0.80
getDirectLinkMethod · 0.80
shouldRetryFunction · 0.70
DecodeMethod · 0.65
MandatoryMethod · 0.65
DoMethod · 0.65
CloseMethod · 0.65
ErrorfMethod · 0.45
CallMethod · 0.45

Tested by

no test coverage detected