Ahmed0Tawfik.Services.Images 1.0.0

dotnet add package Ahmed0Tawfik.Services.Images --version 1.0.0
                    
NuGet\Install-Package Ahmed0Tawfik.Services.Images -Version 1.0.0
                    
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="Ahmed0Tawfik.Services.Images" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Ahmed0Tawfik.Services.Images" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Ahmed0Tawfik.Services.Images" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Ahmed0Tawfik.Services.Images --version 1.0.0
                    
#r "nuget: Ahmed0Tawfik.Services.Images, 1.0.0"
                    
#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.
#:package Ahmed0Tawfik.Services.Images@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Ahmed0Tawfik.Services.Images&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Ahmed0Tawfik.Services.Images&version=1.0.0
                    
Install as a Cake Tool

MyImageService

Overview

MyImageService is a simple .NET Core service for handling image uploads and deletions. It includes file validation, size checks, and URL generation for stored images.

Features

  • Upload images with validation for file type and size.
  • Delete images by filename.
  • Generate accessible URLs for stored images.

Installation

  1. Install the package (if applicable) or add the service to your project.
  2. Register the service in your Startup.cs or Program.cs file.

Usage

1. Register the Service

In your .NET application, register the image service by modifying Program.cs:

using MyImageService;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container
builder.Services.RegisterImageServices();

var app = builder.Build();
app.Run();

2. Inject and Use the Service

You can inject IUploadFileService into any controller or service where you need file upload functionality.

using Microsoft.AspNetCore.Mvc;
using MyImageService.Interfaces;

[ApiController]
[Route("api/images")]
public class ImageController : ControllerBase
{
    private readonly IUploadFileService _uploadFileService;

    public ImageController(IUploadFileService uploadFileService)
    {
        _uploadFileService = uploadFileService;
    }

    [HttpPost("upload")]
    public IActionResult UploadImage([FromForm] IFormFile file)
    {
        try
        {
            var url = _uploadFileService.UploadFile(file, Request.Scheme, Request.Host.Value);
            return Ok(new { ImageUrl = url });
        }
        catch (Exception ex)
        {
            return BadRequest(new { Message = ex.Message });
        }
    }

    [HttpDelete("delete")]
    public IActionResult DeleteImage([FromQuery] string fileName)
    {
        try
        {
            _uploadFileService.DeleteImage(fileName);
            return Ok(new { Message = "Image deleted successfully" });
        }
        catch (Exception ex)
        {
            return BadRequest(new { Message = ex.Message });
        }
    }
}

Service Logic Explained

UploadFile Method

  1. File Type Validation: Ensures only .jpg, .jpeg, .png, and .svg files are uploaded.
  2. File Size Validation: Ensures file size is between 0 and 2MB.
  3. File Storage: Saves the file under the Images directory with a unique GUID filename.
  4. URL Generation: Returns a publicly accessible URL for the uploaded image.

DeleteImage Method

  1. Extracts the filename from the given URL.
  2. Checks if the file exists in the Images directory.
  3. Deletes the file if it exists; otherwise, throws an exception.

Exception Handling

Exception Reason
InvalidFileExtensionException If the uploaded file has an invalid extension
InvalidFileSizeException If the file size is not within the allowed limit
FileDoesNotExistException If the file to delete does not exist

License

This project is open-source and available under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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.0 197 2/19/2025