@@ -181,28 +181,31 @@ func (s *RepositoriesService) DownloadContents(ctx context.Context, owner, repo,
181181func (s * RepositoriesService ) DownloadContentsWithMeta (ctx context.Context , owner , repo , filepath string , opts * RepositoryContentGetOptions ) (io.ReadCloser , * RepositoryContent , * Response , error ) {
182182 dir := path .Dir (filepath )
183183 filename := path .Base (filepath )
184- _ , dirContents , resp , err := s .GetContents (ctx , owner , repo , dir , opts )
184+ fileContent , _ , resp , err := s .GetContents (ctx , owner , repo , filepath , opts )
185185 if err != nil {
186186 return nil , nil , resp , err
187187 }
188188
189- for _ , contents := range dirContents {
190- if * contents . Name == filename {
191- if contents . DownloadURL == nil || * contents . DownloadURL = = "" {
192- return nil , contents , resp , fmt . Errorf ( "no download link found for %s" , filepath )
193- }
189+ if fileContent != nil {
190+ content , err := fileContent . GetContent ()
191+ if err == nil && content ! = "" {
192+ return io . NopCloser ( strings . NewReader ( content )), fileContent , resp , nil
193+ }
194194
195- dlReq , err := http .NewRequestWithContext (ctx , http .MethodGet , * contents .DownloadURL , nil )
196- if err != nil {
197- return nil , contents , resp , err
198- }
199- dlResp , err := s .client .client .Do (dlReq )
200- if err != nil {
201- return nil , contents , & Response {Response : dlResp }, err
202- }
195+ if fileContent .DownloadURL == nil || * fileContent .DownloadURL == "" {
196+ return nil , fileContent , resp , fmt .Errorf ("no download link found for %s" , filepath )
197+ }
203198
204- return dlResp .Body , contents , & Response {Response : dlResp }, nil
199+ dlReq , err := http .NewRequestWithContext (ctx , http .MethodGet , * fileContent .DownloadURL , nil )
200+ if err != nil {
201+ return nil , fileContent , resp , err
202+ }
203+ dlResp , err := s .client .client .Do (dlReq )
204+ if err != nil {
205+ return nil , fileContent , & Response {Response : dlResp }, err
205206 }
207+
208+ return dlResp .Body , fileContent , & Response {Response : dlResp }, nil
206209 }
207210
208211 return nil , nil , resp , fmt .Errorf ("no file named %s found in %s" , filename , dir )
0 commit comments