diff --git a/Controllers/UserContentController.cs b/Controllers/UserContentController.cs index 3ca1457..01347f3 100644 --- a/Controllers/UserContentController.cs +++ b/Controllers/UserContentController.cs @@ -71,7 +71,7 @@ public class UserContentController : ControllerBase [ProducesResponseType(200)] [ProducesResponseType(typeof(ErrorDTO), 400)] [ProducesResponseType(typeof(ErrorDTO), 406)] - public IActionResult PostNewImage(IFormFile file) + /*public IActionResult PostNewImage(IFormFile file) { // Ideally, a hash of the file would be stored somewhere @@ -113,6 +113,76 @@ public class UserContentController : ControllerBase // Zwróć powyższy URL return Ok(new { Status = "ok", Filepath = "miejsce na wspomniany URL" }); + }*/ + + + //[RequestSizeLimit(10_000_000)] // np. limit 10 MB – możesz zmienić lub pobierać z configu + public IActionResult PostNewImage(IFormFile file) + { + // Obsługa braku pliku + if (file == null || file.Length == 0) + { + return BadRequest(new ErrorDTO + { + Status = "error", + Error_msg = "No file was uploaded." + }); + } + + // Dozwolone rozszerzenia + List allowedExtensions = new List() { ".jpg", ".jpeg", ".jfif", ".png", ".gif", ".avif", ".webp" }; + string fileExtension = Path.GetExtension(file.FileName).ToLower(); + + if (!allowedExtensions.Contains(fileExtension)) + { + return BadRequest(new ErrorDTO + { + Status = "error", + Error_msg = $"Unknown file extension. Allowed: {string.Join(", ", allowedExtensions)}" + }); + } + + // Sprawdzenie typu MIME (opcjonalnie dokładniejsze) + if (!file.ContentType.StartsWith("image/")) + { + return BadRequest(new ErrorDTO + { + Status = "error", + Error_msg = "Uploaded file is not an image." + }); + } + + // Ograniczenie rozmiaru pliku – przykładowo 5 MB + const long MaxFileSize = 5 * 1024 * 1024; + if (file.Length > MaxFileSize) + { + return BadRequest(new ErrorDTO + { + Status = "error", + Error_msg = $"File size exceeds {MaxFileSize / 1024 / 1024} MB." + }); + } + + // Generowanie unikalnej nazwy + string uniqueFileName = $"{Guid.NewGuid()}{fileExtension}"; + string relativePath = $"/uploads/images/{uniqueFileName}"; + string absolutePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "uploads", "images", uniqueFileName); + + // Upewnij się, że katalog istnieje + Directory.CreateDirectory(Path.GetDirectoryName(absolutePath)!); + + // Zapis pliku na dysk + using (var stream = new FileStream(absolutePath, FileMode.Create)) + { + file.CopyTo(stream); + } + + // Zwracany adres URL (np. do użytku w cytacie) + return Ok(new + { + Status = "ok", + Filepath = relativePath + }); } } diff --git a/Program.cs b/Program.cs index f7a0092..88deaa2 100644 --- a/Program.cs +++ b/Program.cs @@ -151,5 +151,5 @@ app.UseAuthentication(); app.UseAuthorization(); app.MapControllers(); - +app.UseStaticFiles(); app.Run(); diff --git a/wwwroot/uploads/images/42cbadf4-7804-4fde-991c-d56eb1f4a1b4.png b/wwwroot/uploads/images/42cbadf4-7804-4fde-991c-d56eb1f4a1b4.png new file mode 100644 index 0000000..bbc0464 Binary files /dev/null and b/wwwroot/uploads/images/42cbadf4-7804-4fde-991c-d56eb1f4a1b4.png differ