Improve image upload error handling and validation (#17188)

- Add URL validation in getImageBufferFromUrl utility
- Add response status validation and content-type checking
- Add timeout and connection error handling with specific error messages
- Validate buffer is not empty before processing
- Validate file type detection results before proceeding
- Ensure detected file type is actually an image format
- Add proper type safety for Axios error handling

This improves robustness when uploading images from URLs by:
- Preventing invalid URLs from being processed
- Providing clear error messages for different failure scenarios
- Ensuring only valid image files are processed
- Handling network errors gracefully

---------

Co-authored-by: GitTensor Miner <miner@gittensor.io>
This commit is contained in:
Angel98518
2026-01-19 16:40:02 +08:00
committed by GitHub
parent 5cef07af45
commit 92a080b704
4 changed files with 82 additions and 7 deletions
@@ -120,10 +120,22 @@ export class FileUploadService {
const type = await FileType.fromBuffer(buffer);
if (!type || !type.ext || !type.mime) {
throw new Error(
'Unable to detect image type from buffer. The file may not be a valid image format.',
);
}
if (!type.mime.startsWith('image/')) {
throw new Error(
`Detected file type is not an image: ${type.mime}. Please provide a valid image URL.`,
);
}
return await this.uploadImage({
file: buffer,
filename: `${v4()}.${type?.ext}`,
mimeType: type?.mime,
filename: `${v4()}.${type.ext}`,
mimeType: type.mime,
fileFolder,
workspaceId,
});