Luuk Peters (ProudNerds)
01/06/2025, 2:44 PMLuuk Peters (ProudNerds)
01/06/2025, 2:51 PMcsharp
public class Controller (...): UmbracoApiController
{
public async Task<IActionResult> Webhook()
{
try
{
using var reader = new StreamReader(Request.Body);
var body = await reader.ReadToEndAsync();
A.DoSomething(body);
return Ok("OK");
}
catch (Exception e)
{
return BadRequest();
}
}
}
When called, an action is performed on package A (non-Umbraco) and A dispatches an event:
csharp
public void HandleNotification(string body)
{
var bodyModel = JsonSerializer.Deserialize<Contract>(body);
eventService.OnEvent(new EventModel {Details = bodyModel});
}
Package B (Umbraco) listens to the event:
csharp
internal class NotificationsHandler(...) : INotificationsHandler
{
public void Initialize()
{
notificationManager.Event += NotificationService_Event;
}
private void NotificationService_Event(object? sender, Args e)
{
HandleEventAsync(e).ConfigureAwait(false);
}
private async Task HandleEventAsync(PdfChangedEventArgs e)
{
await SomeService.HandleUpdate(e);
}
}
On that event, a mediaNode is found (or created if it didn't exist), the file is uploaded to the media node and after that a notification is published:
csharp
private async Task HandleUpdate(eventdata)
{
var mediaNode = GetOrCreateDocumentMediaNode(eventdata.someId, eventdata.documentName);
_ = await UpdateMediaItemAsync(mediaNode, eventdata.title, eventdata.documentUrl);
await eventAggregator.PublishAsync(new CustomNotification(mediaNode, eventdata.someId, eventdata));
}
Luuk Peters (ProudNerds)
01/06/2025, 3:14 PMcsharp
private async Task<string?> UpdateMediaItemAsync(IMedia mediaItem, string title, string fileUrl)
{
using var httpClient = new HttpClient();
try
{
// Update the file
var fileBytes = await httpClient.GetByteArrayAsync(mcmFileUrl);
var fileName = Path.GetFileName(new Uri(fileUrl).LocalPath);
using var stream = new MemoryStream(fileBytes);
mediaItem.SetValue(mediaFileManager, mediaUrlGeneratorCollection, shortStringHelper,
contentTypeBaseServiceProvider, MediaAliases.PlusDocumentPropertyAlias, fileName, stream);
//Update other fields
mediaItem.SetValue(MediaAliases.TitlePropertyAlias, title);
mediaItem.SetValue(MediaAliases.LastUpdatePropertyAlias, DateTime.Now);
//Save the changes
mediaService.Save(mediaItem);
return fileName;
}
catch (Exception ex)
{
logger.LogError(ex, "Error uploading file from URL: {Url}", mcmFileUrl);
throw;
}
}
Now the consuming Umbraco instance can handle the notification. In this case it simply takes the IMedia from the notification and updates a few fields and saves it.
csharp
public void Handle(CustomNotification notification)
{
var mediaNode = notification.mediaNode;
mediaNode.SetValue(propertyAlias, $"[{string.Join(",", matchingNodes.ToArray())}]");
var result = mediaService.Save(mediaNode);
//This last line causes the errors....
}
Luuk Peters (ProudNerds)
01/07/2025, 3:17 PMLuuk Peters (ProudNerds)
01/09/2025, 3:39 PMA hub and casual space for you to interact with fellow community members and learn more about Umbraco!
Powered by