Skip to content

Commit

Permalink
Add custom handler for image uploading
Browse files Browse the repository at this point in the history
  • Loading branch information
pathartl committed Aug 23, 2024
1 parent 00eed45 commit 028ed3c
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion PSC.Blazor.Components.MarkdownEditor/MarkdownEditor.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,9 @@ public partial class MarkdownEditor : IDisposable
[Parameter]
public Func<FileStartedEventArgs, Task>? ImageUploadStarted { get; set; }

[Parameter]
public Func<MarkdownEditor, FileEntry, Task<FileEntry>>? CustomImageUpload { get; set; }

/// <summary>
/// If set to true, enables line numbers in the editor.
/// </summary>
Expand Down Expand Up @@ -651,6 +654,8 @@ public async Task UpdateInternalValue(string value, string valueHTML)
[JSInvokable]
public async Task UploadFile(FileEntry fileInfo)
{
bool success = false;

if (fileInfo is null || string.IsNullOrEmpty(fileInfo.ContentBase64))
await JSModule.NotifyImageUploadError(ElementId, $"Can't upload an empty file");

Expand All @@ -660,6 +665,27 @@ public async Task UploadFile(FileEntry fileInfo)
if (ImageUploadStarted is not null)
await ImageUploadStarted.Invoke(new(fileInfo));

if (CustomImageUpload is not null)
{
try
{
fileInfo = await CustomImageUpload.Invoke(this, fileInfo);

success = true;
}
catch (Exception ex)
{
fileInfo.ErrorMessage = ex.Message;
}

if (ImageUploadProgressed is not null)
await ImageUploadProgressed.Invoke(new(fileInfo, 100));

await UpdateFileEndedAsync(fileInfo, success, fileInfo.ErrorMessage);

return;
}

using (HttpClient httpClient = new HttpClient())
{
if (!string.IsNullOrWhiteSpace(ImageUploadAuthenticationSchema) && !string.IsNullOrWhiteSpace(ImageUploadAuthenticationToken))
Expand All @@ -680,7 +706,6 @@ public async Task UploadFile(FileEntry fileInfo)
if (ImageUploadProgressed is not null)
await ImageUploadProgressed.Invoke(new(fileInfo, 50.0));

bool success = false;
try
{
var response = await httpClient.PostAsync(ImageUploadEndpoint, form);
Expand Down

0 comments on commit 028ed3c

Please sign in to comment.