imagefile endpunkt
This commit is contained in:
47
Webshop.Api/Controllers/Admin/FileUploadController.cs
Normal file
47
Webshop.Api/Controllers/Admin/FileUploadController.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
// 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<ActionResult<FileUploadResultDto>> 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 });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -74,6 +74,8 @@ builder.Services.AddAuthentication(options =>
|
||||
});
|
||||
builder.Services.AddAuthorization();
|
||||
|
||||
builder.Services.AddHttpContextAccessor();
|
||||
|
||||
// Repositories
|
||||
builder.Services.AddScoped<IProductRepository, ProductRepository>();
|
||||
builder.Services.AddScoped<ISupplierRepository, SupplierRepository>();
|
||||
@@ -106,6 +108,7 @@ builder.Services.AddScoped<IOrderService, OrderService>();
|
||||
builder.Services.AddScoped<IAddressService, AddressService>();
|
||||
builder.Services.AddScoped<ICheckoutService, CheckoutService>();
|
||||
builder.Services.AddScoped<IReviewService, ReviewService>();
|
||||
builder.Services.AddScoped<IFileStorageService, LocalFileStorageService>();
|
||||
|
||||
// Externe Dienste (Resend)
|
||||
builder.Services.AddHttpClient<ResendClient>();
|
||||
@@ -286,6 +289,9 @@ app.UseForwardedHeaders(new ForwardedHeadersOptions
|
||||
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
|
||||
});
|
||||
|
||||
// << NEU: Statische Dateien aus wwwroot bereitstellen (z.B. /uploads/xyz.jpg) >>
|
||||
app.UseStaticFiles();
|
||||
|
||||
// Swagger/SwaggerUI f<>r API-Dokumentation aktivieren
|
||||
//if (app.Environment.IsDevelopment())
|
||||
//{
|
||||
|
||||
8
Webshop.Application/DTOs/Images/FileUploadResultDto.cs
Normal file
8
Webshop.Application/DTOs/Images/FileUploadResultDto.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
// src/Webshop.Application/DTOs/Images/FileUploadResultDto.cs
|
||||
namespace Webshop.Application.DTOs
|
||||
{
|
||||
public class FileUploadResultDto
|
||||
{
|
||||
public string Url { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
18
Webshop.Domain/Interfaces/IFileStorageService.cs
Normal file
18
Webshop.Domain/Interfaces/IFileStorageService.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
// src/Webshop.Domain/Interfaces/IFileStorageService.cs
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Webshop.Domain.Interfaces
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
51
Webshop.Infrastructure/Services/LocalFileStorageService.cs
Normal file
51
Webshop.Infrastructure/Services/LocalFileStorageService.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
// 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<string> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user