I uploaded a *.riv file through the media center and that wasn’t too difficult. I created a media type with a file upload property and set the correct file extension and Umbraco took care of the rest!
But when it came time to serve the file, the server returned a 404.
Now I looked for the “right” way to configure/extend Umbraco to support this file but the only thing I could come up with that worked is below. It seems obtuse to add my own static file handler in front of Umbraco’s. I don’t know what the overhead is, but it’s probably minimal. However, if this is a code smell now, I don’t want to repeat it across other packages that also need to add file extensions.
Is this the “right” way? Is there a “better” way?
Thanks!
using Microsoft.AspNetCore.StaticFiles;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Web.Common.ApplicationBuilder;
public class RiveStaticFileComposer : IComposer
{
public void Compose(IUmbracoBuilder builder)
{
builder.Services.Configure<UmbracoPipelineOptions>(opts =>
{
opts.AddFilter(new UmbracoPipelineFilter("Rive", prePipeline: app =>
{
var provider = new FileExtensionContentTypeProvider
{
Mappings =
{
[".riv"] = "application/octet-stream"
}
};
app.UseStaticFiles(new StaticFileOptions
{
ContentTypeProvider = provider,
ServeUnknownFileTypes = false
});
}));
});
}
}
I’m having a little trouble running the example as-is. With the following change, I was able to serve *.riv files again.
using Microsoft.AspNetCore.StaticFiles;
using Umbraco.Cms.Core.Composing;
public class RiveStaticFileComposer : IComposer
{
public void Compose(IUmbracoBuilder builder)
{
builder.Services.Configure<StaticFileOptions>(options =>
{
var provider = options.ContentTypeProvider as FileExtensionContentTypeProvider
?? new FileExtensionContentTypeProvider();
provider.Mappings[".riv"] = "application/octet-stream";
options.ContentTypeProvider = provider;
});
}
}
(Hopefully I haven’t misunderstood what’s happening under the hood here…)
I did find a shorter method, but it would drop any other customizations that other code would’ve made (so if anyone else added mappings then it would drop those and overwrite them with your mappings. So this seems like the best and shortest way to do it.