47 lines
1.6 KiB
C#
47 lines
1.6 KiB
C#
// 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/[controller]")]
|
|
[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 });
|
|
}
|
|
}
|
|
} |