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:
+14
-2
@@ -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,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user