Skip to content

DownloadContents can't always download files in large directories #3810

@ran-arigur

Description

@ran-arigur

*github.RepositoriesService provides a special helper method called DownloadContents [code link] to get the contents of a file even if it's big enough that the result from GetContents method won't include them. DownloadContents works by first trying GetContents on the file itself, and then if that doesn't work (or doesn't seem to work, e.g. because the file is just genuinely empty), it falls back to calling GetContents on the containing directory, iterating over the results until it gets to the desired file, and then using the DownloadUrl for that desired file. (There's also another helper method called DownloadContentsWithMeta [code link] that does the same thing but returns a bit more information from the API calls; everything here applies to that method as well.)

The problem is, when you call GetContents on a directory, it only returns the first 1000 files. [doc link] So if the initial GetContents call (the one for the specific file) didn't work or didn't seem to work, and if the file isn't one of the first 1000 in the directory, then this approach will fail with the error "no file named %s found in %s".

The good news is, there's a simple fix: we don't actually need to call GetContents on the containing directory at all, because the initial call to GetContents on the individual file already gives the same information (including the DownloadUrl), even if the file is big enough that it doesn't contain the actual file contents. So we can just use that. In addition to fixing this issue, that would also fix #2480. (In fact, I see that this same solution has also been suggested in comments there.)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions