WSM.Media.Management 1.0.2

dotnet add package WSM.Media.Management --version 1.0.2                
NuGet\Install-Package WSM.Media.Management -Version 1.0.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="WSM.Media.Management" Version="1.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add WSM.Media.Management --version 1.0.2                
#r "nuget: WSM.Media.Management, 1.0.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install WSM.Media.Management as a Cake Addin
#addin nuget:?package=WSM.Media.Management&version=1.0.2

// Install WSM.Media.Management as a Cake Tool
#tool nuget:?package=WSM.Media.Management&version=1.0.2                

WSM Media Management Library

A simple library to manage images or media files in ASP.NET Core with EF Core integration.

Features

  • Upload and store media files using EF Core.
  • Generate URLs for uploaded media files.
  • Easy integration with ASP.NET Core through dependency injection.

Installation

  1. Install the necessary NuGet packages:
dotnet add package WSM.Media.Management
dotnet add package Microsoft.EntityFrameworkCore.InMemory
  1. Add the necessary dependencies to your project:
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using WSM.Media.Management;
using WSM.Media.Management.Media;
using WSM.Media.Management.Models;
using WSMMediaSample1;

Setup

1. Create AppDbContext

Your AppDbContext must inherit from IMediaDbContext and include the MediaFiles property:

using Microsoft.EntityFrameworkCore;
using WSM.Media.Management.Db;
using WSM.Media.Management.Models;

public class AppDbContext : DbContext, IMediaDbContext
{
    public AppDbContext(DbContextOptions options) : base(options) { }

    public DbSet<MediaFile> MediaFiles { get; set; }
}

2. Register Services in Program.cs

To enable media management, add the AddMediaFile and UseMediaFile services in the Program.cs:

var builder = WebApplication.CreateBuilder(args);

// Register DbContext
builder.Services.AddDbContext<AppDbContext>(options =>
    options.UseInMemoryDatabase(nameof(AppDbContext)));

// Register MediaFile management
builder.Services.AddMediaFile<AppDbContext>();

var app = builder.Build();

// Enable MediaFile middleware
app.UseMediaFile();
app.UseHttpsRedirection();

3. Upload and Manage Media Files

To upload media files, use the FileManager class in your API. Here’s an example of how to add media in a controller or endpoint:

var mediaFileId = await fileManager.UploadFileAsync<WeatherForecast>(new()
{
   Bytes = Convert.FromBase64String(fileRequest.Base64),
   Extension = fileRequest.Extension,
});

var mediaFileUrl = await fileManager.GetUrlAsync(mediaFileId);
return new
{
   ResponseUrl = mediaFileUrl,
   MediaFileId = mediaFileId,
};

4. Example File Request Model

Create a simple request model for handling incoming files:

public class ExampleFileRequest
{
    public string Extension { get; set; }
    public string Base64 { get; set; }
}

5. Don't forgot to create App_Data folder and follow the sample project for more details

mkdir App_Data

This setup allows you to upload media files, generate URLs, and store them using EF Core.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.2 108 9/21/2024
1.0.1 96 9/21/2024
1.0.0 94 9/19/2024