-
Notifications
You must be signed in to change notification settings - Fork 5.9k
fix: handle mac screenshots with the image tool #1622
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -805,46 +805,90 @@ impl DeveloperRouter { | |
| ]) | ||
| } | ||
|
|
||
| // Helper function to handle Mac screenshot filenames that contain U+202F (narrow no-break space) | ||
| fn normalize_mac_screenshot_path(&self, path: &Path) -> PathBuf { | ||
| // Only process if the path has a filename | ||
| if let Some(filename) = path.file_name().and_then(|f| f.to_str()) { | ||
| // Check if this matches Mac screenshot pattern: | ||
| // "Screenshot YYYY-MM-DD at H.MM.SS AM/PM.png" | ||
| if let Some(captures) = regex::Regex::new(r"^Screenshot \d{4}-\d{2}-\d{2} at \d{1,2}\.\d{2}\.\d{2} (AM|PM)(?: \(\d+\))?\.png$") | ||
| .ok() | ||
| .and_then(|re| re.captures(filename)) | ||
| { | ||
|
|
||
| // Get the AM/PM part | ||
| let meridian = captures.get(1).unwrap().as_str(); | ||
|
|
||
| // Find the last space before AM/PM and replace it with U+202F | ||
| let space_pos = filename.rfind(meridian) | ||
| .map(|pos| filename[..pos].trim_end().len()) | ||
| .unwrap_or(0); | ||
|
|
||
| if space_pos > 0 { | ||
| let parent = path.parent().unwrap_or(Path::new("")); | ||
| let new_filename = format!( | ||
| "{}{}{}", | ||
| &filename[..space_pos], | ||
| '\u{202F}', | ||
| &filename[space_pos+1..] | ||
| ); | ||
| let new_path = parent.join(new_filename); | ||
|
|
||
| // If the original file exists and paths are different, rename it | ||
| if path.exists() && path != new_path { | ||
| if let Err(e) = std::fs::rename(path, &new_path) { | ||
| eprintln!("Warning: Failed to normalize Mac screenshot filename: {}", e); | ||
| return path.to_path_buf(); | ||
| } | ||
| } | ||
| return new_path; | ||
| } | ||
| } | ||
| } | ||
| path.to_path_buf() | ||
| } | ||
|
|
||
| async fn image_processor(&self, params: Value) -> Result<Vec<Content>, ToolError> { | ||
| let path_str = params | ||
| .get("path") | ||
| .and_then(|v| v.as_str()) | ||
| .ok_or_else(|| ToolError::InvalidParameters("Missing 'path' parameter".into()))?; | ||
|
|
||
| let path = self.resolve_path(path_str)?; | ||
| let normalized_path = self.normalize_mac_screenshot_path(&path); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we do something like #[cfg(target_os = "macos")]or something that only runs this logic on mac and just skips this step in linux/windows?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yea, that makes sense |
||
|
|
||
| // Check if file is ignored before proceeding | ||
| if self.is_ignored(&path) { | ||
| if self.is_ignored(&normalized_path) { | ||
| return Err(ToolError::ExecutionError(format!( | ||
| "Access to '{}' is restricted by .gooseignore", | ||
| path.display() | ||
| normalized_path.display() | ||
| ))); | ||
| } | ||
|
|
||
| // Check if file exists | ||
| if !path.exists() { | ||
| if !normalized_path.exists() { | ||
| return Err(ToolError::ExecutionError(format!( | ||
| "File '{}' does not exist", | ||
| path.display() | ||
| normalized_path.display() | ||
| ))); | ||
| } | ||
|
|
||
| // Check file size (10MB limit for image files) | ||
| const MAX_FILE_SIZE: u64 = 10 * 1024 * 1024; // 10MB in bytes | ||
| let file_size = std::fs::metadata(&path) | ||
| let file_size = std::fs::metadata(&normalized_path) | ||
| .map_err(|e| ToolError::ExecutionError(format!("Failed to get file metadata: {}", e)))? | ||
| .len(); | ||
|
|
||
| if file_size > MAX_FILE_SIZE { | ||
| return Err(ToolError::ExecutionError(format!( | ||
| "File '{}' is too large ({:.2}MB). Maximum size is 10MB.", | ||
| path.display(), | ||
| normalized_path.display(), | ||
| file_size as f64 / (1024.0 * 1024.0) | ||
| ))); | ||
| } | ||
|
|
||
| // Open and decode the image | ||
| let image = xcap::image::open(&path) | ||
| let image = xcap::image::open(&normalized_path) | ||
| .map_err(|e| ToolError::ExecutionError(format!("Failed to open image file: {}", e)))?; | ||
|
|
||
| // Resize if necessary (same logic as screen_capture) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
don't think we need the rename anymore? or did we change our mind here?
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.
removing it