I am trying to download and store remote images locally as media, as a part of a Recurring Hosted Service (see here). I am following the example for Umbraco 9.0 for creating a media item from a stream:
This is how my method looks:
private void ConvertImageToLocalImage(string remoteImageUrl, string title)
{
var downloadFolder = $"{_hostingEnvironment.ContentRootPath}\\App_Data\\TEMP\\Images";
var fileName = Path.GetFileName(remoteImageUrl.Split('?')[0]);
var fileExt = Path.GetExtension(fileName);
var downloadPath = $"{downloadFolder}\\{title.Replace(" ", "_")}{fileExt}";
if (Directory.Exists(downloadFolder) == false)
{
Directory.CreateDirectory(downloadFolder);
}
using (var client = new WebClient())
client.DownloadFile(remoteImageUrl, downloadPath);
using FileStream stream = System.IO.File.OpenRead(downloadPath);
IMedia media = _mediaService.CreateMedia(title, -1, Constants.Conventions.MediaTypes.Image);
media.SetValue(_mediaFileManager, _shortStringHelper, _contentTypeBaseServiceProvider, _serializer, Constants.Conventions.MediaTypes.File, fileName, stream);
_mediaService.Save(media);
}
However, I get the error:
No overload for method 'SetValue' takes 7 arguments
The only 'SetValue' method I appear to have access to is:
void SetValue(string propertyTypeAlias, object value, string culture = null, string segment = null);
I'm not sure what I'm doing wrong...
I have the following using statements at the top of the file:
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Infrastructure.HostedServices;
Worked it out...
I needed to include:
using Umbraco.Extensions;
And then the signiture for the overloaded method is actually really different to what the docs say, this is the signiture of the method:
void SetValue(this IContentBase content, MediaFileManager mediaFileManager, MediaUrlGeneratorCollection mediaUrlGenerators, IShortStringHelper shortStringHelper, IContentTypeBaseServiceProvider contentTypeBaseServiceProvider, string propertyTypeAlias, string filename, Stream filestream, string culture = null, string segment = null)
Which ended up looking like this:
media.SetValue(_mediaFileManager, _mediaUrlGenerators, _shortStringHelper, _contentTypeBaseServiceProvider, "umbracoFile", fileName, stream)