From 038e13c65e1f460ffb0af604575ffe5a5105880e Mon Sep 17 00:00:00 2001 From: "Tizian.Breuch" Date: Fri, 8 Aug 2025 14:59:59 +0200 Subject: [PATCH] File ruas --- .../Controllers/Admin/FileUploadController.cs | 47 ----------------- Webshop.Api/Program.cs | 1 + .../DTOs/Images/FileUploadResultDto.cs | 8 --- .../Webshop.Application.csproj | 4 ++ .../Interfaces/IFileStorageService.cs | 18 ------- .../Services/LocalFileStorageService.cs | 51 ------------------- .../Webshop.Infrastructure.csproj | 4 ++ 7 files changed, 9 insertions(+), 124 deletions(-) delete mode 100644 Webshop.Api/Controllers/Admin/FileUploadController.cs delete mode 100644 Webshop.Application/DTOs/Images/FileUploadResultDto.cs delete mode 100644 Webshop.Domain/Interfaces/IFileStorageService.cs delete mode 100644 Webshop.Infrastructure/Services/LocalFileStorageService.cs diff --git a/Webshop.Api/Controllers/Admin/FileUploadController.cs b/Webshop.Api/Controllers/Admin/FileUploadController.cs deleted file mode 100644 index 85b2695..0000000 --- a/Webshop.Api/Controllers/Admin/FileUploadController.cs +++ /dev/null @@ -1,47 +0,0 @@ -// src/Webshop.Api/Controllers/Admin/FileUploadController.cs -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; -using System.IO; -using System.Threading.Tasks; -using Webshop.Application.DTOs; -using Webshop.Domain.Interfaces; - -namespace Webshop.Api.Controllers.Admin -{ - [ApiController] - [Route("api/v1/admin/files")] - [Authorize(Roles = "Admin")] - public class FileUploadController : ControllerBase - { - private readonly IFileStorageService _fileStorageService; - - public FileUploadController(IFileStorageService fileStorageService) - { - _fileStorageService = fileStorageService; - } - - [HttpPost("upload")] - public async Task> UploadImage(IFormFile file) - { - if (file == null || file.Length == 0) - { - 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 }); - } - } -} \ No newline at end of file diff --git a/Webshop.Api/Program.cs b/Webshop.Api/Program.cs index 8158db2..804a813 100644 --- a/Webshop.Api/Program.cs +++ b/Webshop.Api/Program.cs @@ -289,6 +289,7 @@ var forwardedHeadersOptions = new ForwardedHeadersOptions ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto }; + // << Statische Dateien aus wwwroot bereitstellen (z.B. /uploads/xyz.jpg) >> app.UseStaticFiles(); diff --git a/Webshop.Application/DTOs/Images/FileUploadResultDto.cs b/Webshop.Application/DTOs/Images/FileUploadResultDto.cs deleted file mode 100644 index 85f737f..0000000 --- a/Webshop.Application/DTOs/Images/FileUploadResultDto.cs +++ /dev/null @@ -1,8 +0,0 @@ -// src/Webshop.Application/DTOs/Images/FileUploadResultDto.cs -namespace Webshop.Application.DTOs -{ - public class FileUploadResultDto - { - public string Url { get; set; } = string.Empty; - } -} \ No newline at end of file diff --git a/Webshop.Application/Webshop.Application.csproj b/Webshop.Application/Webshop.Application.csproj index 649d1f2..96e600f 100644 --- a/Webshop.Application/Webshop.Application.csproj +++ b/Webshop.Application/Webshop.Application.csproj @@ -23,4 +23,8 @@ + + + + \ No newline at end of file diff --git a/Webshop.Domain/Interfaces/IFileStorageService.cs b/Webshop.Domain/Interfaces/IFileStorageService.cs deleted file mode 100644 index 16af061..0000000 --- a/Webshop.Domain/Interfaces/IFileStorageService.cs +++ /dev/null @@ -1,18 +0,0 @@ -// src/Webshop.Domain/Interfaces/IFileStorageService.cs -using System.IO; -using System.Threading.Tasks; - -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 diff --git a/Webshop.Infrastructure/Services/LocalFileStorageService.cs b/Webshop.Infrastructure/Services/LocalFileStorageService.cs deleted file mode 100644 index 4e1bdce..0000000 --- a/Webshop.Infrastructure/Services/LocalFileStorageService.cs +++ /dev/null @@ -1,51 +0,0 @@ -// src/Webshop.Infrastructure/Services/LocalFileStorageService.cs -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Http; -using System; -using System.IO; -using System.Threading.Tasks; -using Webshop.Domain.Interfaces; - -namespace Webshop.Infrastructure.Services -{ - public class LocalFileStorageService : IFileStorageService - { - private readonly IWebHostEnvironment _env; - private readonly IHttpContextAccessor _httpContextAccessor; - - public LocalFileStorageService(IWebHostEnvironment env, IHttpContextAccessor httpContextAccessor) - { - _env = env; - _httpContextAccessor = httpContextAccessor; - } - - public async Task SaveFileAsync(Stream fileStream, string fileName, string contentType) - { - // Erstelle einen eindeutigen Dateinamen, um Kollisionen zu vermeiden - var fileExtension = Path.GetExtension(fileName); - var uniqueFileName = $"{Guid.NewGuid()}{fileExtension}"; - - // Definiere den Speicherort im wwwroot-Ordner - var uploadsFolderPath = Path.Combine(_env.WebRootPath, "uploads"); - if (!Directory.Exists(uploadsFolderPath)) - { - Directory.CreateDirectory(uploadsFolderPath); - } - - var filePath = Path.Combine(uploadsFolderPath, uniqueFileName); - - // Speichere den Stream in die Datei - await using (var outputStream = new FileStream(filePath, FileMode.Create)) - { - await fileStream.CopyToAsync(outputStream); - } - - // Erstelle die öffentliche URL - var request = _httpContextAccessor.HttpContext.Request; - var baseUrl = $"{request.Scheme}://{request.Host}"; - var fileUrl = $"{baseUrl}/uploads/{uniqueFileName}"; - - return fileUrl; - } - } -} \ No newline at end of file diff --git a/Webshop.Infrastructure/Webshop.Infrastructure.csproj b/Webshop.Infrastructure/Webshop.Infrastructure.csproj index 567e0e4..540032e 100644 --- a/Webshop.Infrastructure/Webshop.Infrastructure.csproj +++ b/Webshop.Infrastructure/Webshop.Infrastructure.csproj @@ -24,4 +24,8 @@ + + + +