-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor DownloadImage and DownloadThumbnail methods for improved error handling and file management #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
spiegel-im-spiegel
commented
Nov 28, 2025
- Updated DownloadImage to use a package-level createFile helper for file creation, allowing for better testing and error simulation.
- Enhanced error handling in DownloadImage to cover scenarios like directory creation failures and read errors during HTTP response processing.
- Refactored DownloadThumbnail to utilize the createFile helper and improved error handling for file creation and image processing.
- Added comprehensive unit tests for DownloadImage and DownloadThumbnail to cover various edge cases, including error scenarios and temporary file handling.
- Improved documentation for both methods to clarify behavior, parameters, and error handling.
… image handling notes
…m a specified URL
…or handling and file management - Updated DownloadImage to use a package-level createFile helper for file creation, allowing for better testing and error simulation. - Enhanced error handling in DownloadImage to cover scenarios like directory creation failures and read errors during HTTP response processing. - Refactored DownloadThumbnail to utilize the createFile helper and improved error handling for file creation and image processing. - Added comprehensive unit tests for DownloadImage and DownloadThumbnail to cover various edge cases, including error scenarios and temporary file handling. - Improved documentation for both methods to clarify behavior, parameters, and error handling.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR refactors the DownloadImage and DownloadThumbnail methods to improve testability and error handling. The main changes introduce package-level function variables (createFile, decodeImage, outputImage) that tests can override to simulate failure scenarios. The PR adds comprehensive test coverage for edge cases including file creation failures, read errors, and encoding issues. Documentation has been enhanced with detailed API notes and test examples showing how to use the new test hooks.
Key changes:
- Introduced testable dependency injection via package-level function variables for file creation, image decoding, and encoding
- Removed hardcoded 15-second HTTP timeout from
DownloadImage(now relies on context for cancellation) - Unified file creation and closing logic for both temporary and permanent files in both methods
- Added 20+ new test cases covering error paths and edge cases
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| webinfo.go | Refactored DownloadImage and DownloadThumbnail to use createFile, decodeImage, and outputImage function variables; removed HTTP timeout; simplified defer/close logic |
| webinfo_test.go | Added extensive test coverage including helper types (errReader, roundTripperFunc, failingBody, zeroImg) and 20+ new test cases for error scenarios |
| fetch_test.go | Added tests for custom User-Agent and body close errors |
| README.md | Enhanced documentation with API notes section explaining test hooks and added comprehensive test examples |
| .github/copilot-instructions.md | Updated with detailed image download/thumbnail notes and clarifications on I/O patterns |
| Taskfile.yml | Added coverage profile generation and reporting to the test task |
| sample/sample1.go | New example demonstrating basic Fetch usage with context timeout |
| sample/sample2.go | New example demonstrating DownloadThumbnail usage |
Co-authored-by: Copilot <[email protected]>
|
@copilot open a new pull request to apply changes based on the comments in this thread |
|
@spiegel-im-spiegel I've opened a new pull request, #3, to work on those changes. Once the pull request is ready, I'll request review from you. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.
Co-authored-by: spiegel-im-spiegel <[email protected]>
Fix review comments: remove io.NopCloser wrapper, add HTTP timeout, fix typos
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.