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 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -289,7 +289,6 @@ var forwardedHeadersOptions = new ForwardedHeadersOptions
|
|||||||
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
|
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// << Statische Dateien aus wwwroot bereitstellen (z.B. /uploads/xyz.jpg) >>
|
// << Statische Dateien aus wwwroot bereitstellen (z.B. /uploads/xyz.jpg) >>
|
||||||
app.UseStaticFiles();
|
app.UseStaticFiles();
|
||||||
|
|
||||||
|
|||||||
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -23,8 +23,4 @@
|
|||||||
<ProjectReference Include="..\Webshop.Domain\Webshop.Domain.csproj" />
|
<ProjectReference Include="..\Webshop.Domain\Webshop.Domain.csproj" />
|
||||||
<ProjectReference Include="..\Webshop.Infrastructure\Webshop.Infrastructure.csproj" />
|
<ProjectReference Include="..\Webshop.Infrastructure\Webshop.Infrastructure.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Folder Include="DTOs\Images\" />
|
|
||||||
</ItemGroup>
|
|
||||||
</Project>
|
</Project>
|
||||||
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -24,8 +24,4 @@
|
|||||||
<ProjectReference Include="..\Webshop.Domain\Webshop.Domain.csproj" />
|
<ProjectReference Include="..\Webshop.Domain\Webshop.Domain.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Folder Include="Services\" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
Reference in New Issue
Block a user