1 // Copyright (C) MongoDB, Inc. 2023-present. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may 4 // not use this file except in compliance with the License. You may obtain 5 // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Based on github.com/aws/aws-sdk-go by Amazon.com, Inc. with code from: 8 // - github.com/aws/aws-sdk-go/blob/v1.44.225/aws/types.go 9 // See THIRD-PARTY-NOTICES for original license terms 10 11 package aws 12 13 import ( 14 "io" 15 ) 16 17 // ReadSeekCloser wraps a io.Reader returning a ReaderSeekerCloser. Allows the 18 // SDK to accept an io.Reader that is not also an io.Seeker for unsigned 19 // streaming payload API operations. 20 // 21 // A ReadSeekCloser wrapping an nonseekable io.Reader used in an API 22 // operation's input will prevent that operation being retried in the case of 23 // network errors, and cause operation requests to fail if the operation 24 // requires payload signing. 25 // 26 // Note: If using With S3 PutObject to stream an object upload The SDK's S3 27 // Upload manager (s3manager.Uploader) provides support for streaming with the 28 // ability to retry network errors. 29 func ReadSeekCloser(r io.Reader) ReaderSeekerCloser { 30 return ReaderSeekerCloser{r} 31 } 32 33 // ReaderSeekerCloser represents a reader that can also delegate io.Seeker and 34 // io.Closer interfaces to the underlying object if they are available. 35 type ReaderSeekerCloser struct { 36 r io.Reader 37 } 38 39 // IsReaderSeekable returns if the underlying reader type can be seeked. A 40 // io.Reader might not actually be seekable if it is the ReaderSeekerCloser 41 // type. 42 func IsReaderSeekable(r io.Reader) bool { 43 switch v := r.(type) { 44 case ReaderSeekerCloser: 45 return v.IsSeeker() 46 case *ReaderSeekerCloser: 47 return v.IsSeeker() 48 case io.ReadSeeker: 49 return true 50 default: 51 return false 52 } 53 } 54 55 // Read reads from the reader up to size of p. The number of bytes read, and 56 // error if it occurred will be returned. 57 // 58 // If the reader is not an io.Reader zero bytes read, and nil error will be 59 // returned. 60 // 61 // Performs the same functionality as io.Reader Read 62 func (r ReaderSeekerCloser) Read(p []byte) (int, error) { 63 switch t := r.r.(type) { 64 case io.Reader: 65 return t.Read(p) 66 } 67 return 0, nil 68 } 69 70 // Seek sets the offset for the next Read to offset, interpreted according to 71 // whence: 0 means relative to the origin of the file, 1 means relative to the 72 // current offset, and 2 means relative to the end. Seek returns the new offset 73 // and an error, if any. 74 // 75 // If the ReaderSeekerCloser is not an io.Seeker nothing will be done. 76 func (r ReaderSeekerCloser) Seek(offset int64, whence int) (int64, error) { 77 switch t := r.r.(type) { 78 case io.Seeker: 79 return t.Seek(offset, whence) 80 } 81 return int64(0), nil 82 } 83 84 // IsSeeker returns if the underlying reader is also a seeker. 85 func (r ReaderSeekerCloser) IsSeeker() bool { 86 _, ok := r.r.(io.Seeker) 87 return ok 88 } 89 90 // HasLen returns the length of the underlying reader if the value implements 91 // the Len() int method. 92 func (r ReaderSeekerCloser) HasLen() (int, bool) { 93 type lenner interface { 94 Len() int 95 } 96 97 if lr, ok := r.r.(lenner); ok { 98 return lr.Len(), true 99 } 100 101 return 0, false 102 } 103 104 // GetLen returns the length of the bytes remaining in the underlying reader. 105 // Checks first for Len(), then io.Seeker to determine the size of the 106 // underlying reader. 107 // 108 // Will return -1 if the length cannot be determined. 109 func (r ReaderSeekerCloser) GetLen() (int64, error) { 110 if l, ok := r.HasLen(); ok { 111 return int64(l), nil 112 } 113 114 if s, ok := r.r.(io.Seeker); ok { 115 return seekerLen(s) 116 } 117 118 return -1, nil 119 } 120 121 // SeekerLen attempts to get the number of bytes remaining at the seeker's 122 // current position. Returns the number of bytes remaining or error. 123 func SeekerLen(s io.Seeker) (int64, error) { 124 // Determine if the seeker is actually seekable. ReaderSeekerCloser 125 // hides the fact that a io.Readers might not actually be seekable. 126 switch v := s.(type) { 127 case ReaderSeekerCloser: 128 return v.GetLen() 129 case *ReaderSeekerCloser: 130 return v.GetLen() 131 } 132 133 return seekerLen(s) 134 } 135 136 func seekerLen(s io.Seeker) (int64, error) { 137 curOffset, err := s.Seek(0, io.SeekCurrent) 138 if err != nil { 139 return 0, err 140 } 141 142 endOffset, err := s.Seek(0, io.SeekEnd) 143 if err != nil { 144 return 0, err 145 } 146 147 _, err = s.Seek(curOffset, io.SeekStart) 148 if err != nil { 149 return 0, err 150 } 151 152 return endOffset - curOffset, nil 153 } 154