diff --git a/Webshop.Api/Controllers/Admin/FileUploadController.cs b/Webshop.Api/Controllers/Admin/FileUploadController.cs index 970e942..f1d05d7 100644 --- a/Webshop.Api/Controllers/Admin/FileUploadController.cs +++ b/Webshop.Api/Controllers/Admin/FileUploadController.cs @@ -2,10 +2,10 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; -using System.IO; using System.Threading.Tasks; +using Webshop.Application; using Webshop.Application.DTOs; -using Webshop.Domain.Interfaces; +using Webshop.Application.Services; // Namespace für den neuen Service namespace Webshop.Api.Controllers.Admin { @@ -14,34 +14,27 @@ namespace Webshop.Api.Controllers.Admin [Authorize(Roles = "Admin")] 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")] - public async Task> UploadImage(IFormFile file) + [ProducesResponseType(typeof(FileUploadResultDto), StatusCodes.Status200OK)] + [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)] + [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)] + public async Task 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." }); - } - - // 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 }); + ServiceResultType.Success => Ok(result.Value), + ServiceResultType.InvalidInput => BadRequest(new { Message = result.ErrorMessage }), + _ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." }) + }; } } } \ No newline at end of file diff --git a/Webshop.Application/Services/FileUploadService.cs b/Webshop.Application/Services/FileUploadService.cs new file mode 100644 index 0000000..d78728a --- /dev/null +++ b/Webshop.Application/Services/FileUploadService.cs @@ -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> UploadFileAsync(IFormFile file) + { + if (file == null || file.Length == 0) + { + return ServiceResult.Fail(ServiceResultType.InvalidInput, "Es wurde keine Datei hochgeladen."); + } + + if (!file.ContentType.StartsWith("image/")) + { + return ServiceResult.Fail(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(ServiceResultType.Failure, $"Beim Speichern der Datei ist ein interner Fehler aufgetreten: {ex.Message}"); + } + } + } +} \ No newline at end of file diff --git a/Webshop.Application/Services/IFileUploadService.cs b/Webshop.Application/Services/IFileUploadService.cs new file mode 100644 index 0000000..441a8f7 --- /dev/null +++ b/Webshop.Application/Services/IFileUploadService.cs @@ -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 + { + /// + /// Validiert und speichert eine hochgeladene Datei. + /// + /// Die per HTTP-Request hochgeladene Datei. + /// Ein ServiceResult, das bei Erfolg die URL der Datei enthält. + Task> UploadFileAsync(IFormFile file); + } +} \ No newline at end of file diff --git a/Webshop.Domain/Interfaces/IFileStorageService.cs b/Webshop.Domain/Interfaces/IFileStorageService.cs index 16af061..bae5ebc 100644 --- a/Webshop.Domain/Interfaces/IFileStorageService.cs +++ b/Webshop.Domain/Interfaces/IFileStorageService.cs @@ -6,13 +6,6 @@ namespace Webshop.Domain.Interfaces { public interface IFileStorageService { - /// - /// Speichert eine Datei und gibt die öffentlich zugängliche URL zurück. - /// - /// Der Stream der Datei. - /// Der ursprüngliche Dateiname (zur Ermittlung der Erweiterung). - /// Der MIME-Typ der Datei. - /// Die öffentliche URL der gespeicherten Datei. Task SaveFileAsync(Stream fileStream, string fileName, string contentType); } } \ No newline at end of file