Problem/Motivation

The current implementation of GenerateAltText::generateAltText() doesn't validate whether the image file exists on the file system before attempting to generate alt text. This can lead to fatal errors or unexpected behavior when the physical image file has been deleted or moved but the file entity still exists in the database. This situation can occur due to manual file deletion, file system corruption, or synchronization issues between environments.

Steps to reproduce

Upload an image file through Drupal's file upload system
Successfully generate alt text for the image
Manually delete the physical image file from the file system (while keeping the file entity in the database)
Attempt to regenerate alt text for the same file entity
Observe error or unexpected behavior due to the missing physical file

Proposed resolution

Add file existence validation before processing the image for alt text generation. The solution includes:

Add FileSystemInterface as a dependency to access file system methods
Implement file existence check using $this->fileSystem->realPath() and file_exists()
Return appropriate JSON error response (HTTP 403) when the file doesn't exist
Provide clear error message: "The file does not exist."
Improve code documentation and fix PHPDoc annotations
This ensures the module gracefully handles edge cases where file entities exist in the database but their corresponding physical files are missing from the file system.

Remaining tasks

Review and test the proposed patch
Confirm error handling approach and HTTP status code
Consider if additional logging should be added for debugging purposes
Test with various file storage configurations (local, remote, etc.)

User interface changes

Users will now receive a clear error message "The file does not exist." instead of encountering a fatal error when attempting to generate alt text for missing image files.

API changes

Added FileSystemInterface $fileSystem property to the GenerateAltText controller
Modified the dependency injection in create() method to include file system service
The generateAltText() method now returns a JSON error response when files are missing

Data model changes

None.

CommentFileSizeAuthor
added-check-for-path.patch2.35 KBremco hoeneveld
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

remco hoeneveld created an issue. See original summary.

anybody’s picture

Assigned: remco hoeneveld » Unassigned
Status: Active » Reviewed & tested by the community

Great work @remco hoeneveld perfectly solved! RTBC!