mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-02-14 19:41:37 +00:00
Add error handling for oversized image files
This commit is contained in:
parent
c0c439da02
commit
f7748be845
@ -1,7 +1,11 @@
|
|||||||
|
using AIStudio.Tools.PluginSystem;
|
||||||
|
|
||||||
namespace AIStudio.Chat;
|
namespace AIStudio.Chat;
|
||||||
|
|
||||||
public static class IImageSourceExtensions
|
public static class IImageSourceExtensions
|
||||||
{
|
{
|
||||||
|
private static string TB(string fallbackEN) => I18N.I.T(fallbackEN, typeof(IImageSourceExtensions).Namespace, nameof(IImageSourceExtensions));
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Read the image content as a base64 string.
|
/// Read the image content as a base64 string.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -33,8 +37,11 @@ public static class IImageSourceExtensions
|
|||||||
// Read the length of the content:
|
// Read the length of the content:
|
||||||
var lengthBytes = response.Content.Headers.ContentLength;
|
var lengthBytes = response.Content.Headers.ContentLength;
|
||||||
if(lengthBytes > 10_000_000)
|
if(lengthBytes > 10_000_000)
|
||||||
|
{
|
||||||
|
await MessageBus.INSTANCE.SendError(new(Icons.Material.Filled.ImageNotSupported, TB("The image at the URL is too large (>10 MB). Skipping the image.")));
|
||||||
return string.Empty;
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
var bytes = await response.Content.ReadAsByteArrayAsync(token);
|
var bytes = await response.Content.ReadAsByteArrayAsync(token);
|
||||||
return Convert.ToBase64String(bytes);
|
return Convert.ToBase64String(bytes);
|
||||||
}
|
}
|
||||||
@ -48,8 +55,11 @@ public static class IImageSourceExtensions
|
|||||||
// Read the content length:
|
// Read the content length:
|
||||||
var length = new FileInfo(image.Source).Length;
|
var length = new FileInfo(image.Source).Length;
|
||||||
if(length > 10_000_000)
|
if(length > 10_000_000)
|
||||||
|
{
|
||||||
|
await MessageBus.INSTANCE.SendError(new(Icons.Material.Filled.ImageNotSupported, TB("The local image file is too large (>10 MB). Skipping the image.")));
|
||||||
return string.Empty;
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
var bytes = await File.ReadAllBytesAsync(image.Source, token);
|
var bytes = await File.ReadAllBytesAsync(image.Source, token);
|
||||||
return Convert.ToBase64String(bytes);
|
return Convert.ToBase64String(bytes);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user