This commit is contained in:
Tizian.Breuch
2025-09-25 14:57:13 +02:00
parent db2073dbd1
commit 195d794703
4 changed files with 80 additions and 30 deletions

View File

@@ -2,10 +2,10 @@
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.IO;
using System.Threading.Tasks; using System.Threading.Tasks;
using Webshop.Application;
using Webshop.Application.DTOs; using Webshop.Application.DTOs;
using Webshop.Domain.Interfaces; using Webshop.Application.Services; // Namespace für den neuen Service
namespace Webshop.Api.Controllers.Admin namespace Webshop.Api.Controllers.Admin
{ {
@@ -14,34 +14,27 @@ namespace Webshop.Api.Controllers.Admin
[Authorize(Roles = "Admin")] [Authorize(Roles = "Admin")]
public class FileUploadController : ControllerBase public class FileUploadController : ControllerBase
{ {
private readonly IFileStorageService _fileStorageService; private readonly IFileUploadService _fileUploadService;
public FileUploadController(IFileStorageService fileStorageService) public FileUploadController(IFileUploadService fileUploadService)
{ {
_fileStorageService = fileStorageService; _fileUploadService = fileUploadService;
} }
[HttpPost("upload")] [HttpPost("upload")]
public async Task<ActionResult<FileUploadResultDto>> UploadImage(IFormFile file) [ProducesResponseType(typeof(FileUploadResultDto), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> UploadImage(IFormFile file)
{ {
if (file == null || file.Length == 0) var result = await _fileUploadService.UploadFileAsync(file);
return result.Type switch
{ {
return BadRequest(new { Message = "Es wurde keine Datei hochgeladen." }); ServiceResultType.Success => Ok(result.Value),
} ServiceResultType.InvalidInput => BadRequest(new { Message = result.ErrorMessage }),
_ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." })
// Optional: Validierung des Dateityps };
if (!file.ContentType.StartsWith("image/"))
{
return BadRequest(new { Message = "Nur Bilddateien sind erlaubt." });
}
// Öffne einen Stream aus der hochgeladenen Datei
await using var stream = file.OpenReadStream();
// Speichere die Datei mit dem Service und erhalte die URL
var fileUrl = await _fileStorageService.SaveFileAsync(stream, file.FileName, file.ContentType);
return Ok(new FileUploadResultDto { Url = fileUrl });
} }
} }
} }

View File

@@ -0,0 +1,46 @@
// src/Webshop.Application/Services/FileUploadService.cs
using Microsoft.AspNetCore.Http;
using System;
using System.Threading.Tasks;
using Webshop.Application.DTOs;
using Webshop.Domain.Interfaces;
namespace Webshop.Application.Services
{
public class FileUploadService : IFileUploadService
{
private readonly IFileStorageService _fileStorageService;
public FileUploadService(IFileStorageService fileStorageService)
{
_fileStorageService = fileStorageService;
}
public async Task<ServiceResult<FileUploadResultDto>> UploadFileAsync(IFormFile file)
{
if (file == null || file.Length == 0)
{
return ServiceResult.Fail<FileUploadResultDto>(ServiceResultType.InvalidInput, "Es wurde keine Datei hochgeladen.");
}
if (!file.ContentType.StartsWith("image/"))
{
return ServiceResult.Fail<FileUploadResultDto>(ServiceResultType.InvalidInput, "Nur Bilddateien sind erlaubt.");
}
try
{
await using var stream = file.OpenReadStream();
var fileUrl = await _fileStorageService.SaveFileAsync(stream, file.FileName, file.ContentType);
var resultDto = new FileUploadResultDto { Url = fileUrl };
return ServiceResult.Ok(resultDto);
}
catch (Exception ex)
{
// Hier könntest du den Fehler loggen (z.B. bei Festplattenproblemen)
return ServiceResult.Fail<FileUploadResultDto>(ServiceResultType.Failure, $"Beim Speichern der Datei ist ein interner Fehler aufgetreten: {ex.Message}");
}
}
}
}

View File

@@ -0,0 +1,18 @@
// src/Webshop.Application/Services/IFileUploadService.cs
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;
using Webshop.Application;
using Webshop.Application.DTOs;
namespace Webshop.Application.Services
{
public interface IFileUploadService
{
/// <summary>
/// Validiert und speichert eine hochgeladene Datei.
/// </summary>
/// <param name="file">Die per HTTP-Request hochgeladene Datei.</param>
/// <returns>Ein ServiceResult, das bei Erfolg die URL der Datei enthält.</returns>
Task<ServiceResult<FileUploadResultDto>> UploadFileAsync(IFormFile file);
}
}

View File

@@ -6,13 +6,6 @@ namespace Webshop.Domain.Interfaces
{ {
public interface IFileStorageService public interface IFileStorageService
{ {
/// <summary>
/// Speichert eine Datei und gibt die öffentlich zugängliche URL zurück.
/// </summary>
/// <param name="fileStream">Der Stream der Datei.</param>
/// <param name="fileName">Der ursprüngliche Dateiname (zur Ermittlung der Erweiterung).</param>
/// <param name="contentType">Der MIME-Typ der Datei.</param>
/// <returns>Die öffentliche URL der gespeicherten Datei.</returns>
Task<string> SaveFileAsync(Stream fileStream, string fileName, string contentType); Task<string> SaveFileAsync(Stream fileStream, string fileName, string contentType);
} }
} }