In a software kitchen, like the Restaurant of Mistaken Orders, where unexpected dishes (files) are often served, managing the menu (assets) can be a challenging task. In this article, we'll learn how to create or update an asset's main file from an external URL, using a recipe that even the most distracted waiter can follow.
Ingredients
- Sitecore Content Hub SDK: A rich library to manage your digital assets.
- C#: Our programming language to cook the code.
- HttpClient: To fetch the external URL.
- MemoryStream: To hold the content of the file.
- A Mistaken Order (External URL): The URL pointing to the new main file.
The Recipe
Step 1: Preparing the Mistaken Order
First, we need a method to fetch the content from the mistaken order (external URL). Think of it as a waiter running to grab a dish from a neighboring restaurant.
public class MemoryStreamHelper
{
public static async Task<MemoryStream> GetMemoryStreamFromUrlAsync(Uri uri, CancellationToken cancellationToken)
{
using (var httpClient = new HttpClient())
using (var response = await httpClient.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead, cancellationToken))
{
response.EnsureSuccessStatusCode();
var memoryStream = new MemoryStream();
await response.Content.CopyToAsync(memoryStream);
memoryStream.Position = 0;
return memoryStream;
}
}
}
Step 2: Cooking the New MainFile (Asset Creation)
If the order is new and the asset doesn't exist yet, we'll create a new dish.
public async Task<long> CreateNewAsset(string videoBlobUrl)
{
var uri = new Uri(videoBlobUrl, UriKind.Absolute);
var memoryStream = await MemoryStreamHelper.GetMemoryStreamFromUrlAsync(uri, CancellationToken.None);
var request = new UploadRequest(new StreamUploadSource(memoryStream, "video/mp4", "video.mp4"), "AssetUploadConfiguration", "NewAsset")
{
// Add any additional parameters as needed
};
var response = await mClient.Uploads.UploadAsync(request, CancellationToken.None);
return (long)await mClient.LinkHelper.IdFromEntityAsync(response.Headers.Location);
}
Step 3: Updating the MainFile (Asset Update)
If the asset already exists, and we want to replace the main file, we'll need to follow a different recipe.
public async Task UpdateAssetMainFile(long assetId, string videoBlobUrl)
{
var uri = new Uri(videoBlobUrl, UriKind.Absolute);
var memoryStream = await MemoryStreamHelper.GetMemoryStreamFromUrlAsync(uri, CancellationToken.None);
var request = new UploadRequest(new StreamUploadSource(memoryStream, "video/mp4", "video.mp4"), "AssetUploadConfiguration", "NewMainFile")
{
ActionParameters = new Dictionary<string, object>
{
{ "AssetId", assetId }, // Specify the asset ID to update
}
};
await mClient.Uploads.UploadAsync(request, CancellationToken.None);
}
Conclusion
In the Restaurant of Mistaken Orders, managing assets and main files can be as delightful as tasting a new dish. Whether it's creating a new asset or updating an existing one, the ingredients and steps outlined in this article provide a nourishing recipe for success.
So the next time a customer (user) comes in with a mistaken order (external URL), you'll know exactly how to cook up the perfect solution. Bon appétit! 🍽️
0 comments:
Post a Comment