diff --git a/Controllers/UserContentController.cs b/Controllers/UserContentController.cs
index 01347f3..59c39bb 100644
--- a/Controllers/UserContentController.cs
+++ b/Controllers/UserContentController.cs
@@ -60,129 +60,93 @@ public class UserContentController : ControllerBase
///
///
/// Allows authorized users to publish images.
- /// A user-reachable path is returned on success.
+ /// A user-reachable path and image id is returned on success.
///
/// Returned on valid request
- /// Returned when file extension is unknown
- /// Returned when request does not follow user-provided config
+ /// Returned when request does not contain a file or the file is blank
+ /// Returned when image size is too large
+ /// Returned when file extension/mimetype is unknown
[HttpPost]
[Authorize]
[EnableCors]
[ProducesResponseType(200)]
[ProducesResponseType(typeof(ErrorDTO), 400)]
- [ProducesResponseType(typeof(ErrorDTO), 406)]
- /*public IActionResult PostNewImage(IFormFile file)
+ [ProducesResponseType(typeof(ErrorDTO), 413)]
+ [ProducesResponseType(typeof(ErrorDTO), 415)]
+ public IActionResult PostNewImage(IFormFile file)
{
-
- // Ideally, a hash of the file would be stored somewhere
- // in the database to have a basic redundancy check,
- // but this will do for now. ~eee4
-
- // A good idea would be to also check the Content-Type
- // of submitted files. ~eee4
-
- List allowedExtensions = new List() { ".jpg", ".jpeg", ".jfif", ".png", ".gif", ".avif", ".webp" };
-
- string fileExtension = Path.GetExtension(file.FileName);
- if (!allowedExtensions.Contains(fileExtension.ToLower())) {
- return BadRequest(new ErrorDTO {
+ // Obsługa braku pliku
+ if (file == null || file.Length == 0)
+ {
+ return BadRequest(new ErrorDTO
+ {
Status = "error",
- Error_msg = $"Unknown file extension. Please use one of the following: {string.Join(", ", allowedExtensions)}"
+ Error_msg = "No file was uploaded."
});
}
- // TODO:
- // https://www.youtube.com/watch?v=6-FNejMrVuk
+ // Dozwolone rozszerzenia
+ List allowedExtensions = new List() { ".jpg", ".jpeg", ".jfif", ".png", ".gif", ".avif", ".webp" };
+ string fileExtension = Path.GetExtension(file.FileName).ToLower();
- // Sprawdź, czy plik spełnia ograniczenia:
- // 1. Czy rozmiar jest mniejszy od _appsettings["UserContent"]["MaxFileSize"] ?
+ if (!allowedExtensions.Contains(fileExtension))
+ {
+ return StatusCode(415, 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 StatusCode(415, new ErrorDTO
+ {
+ Status = "error",
+ Error_msg = "Uploaded file is not an image."
+ });
+ }
- // Jeśli nie, zwróć ErrorDTO ze wiadomością: $"File size exceeds {_appsettings["UserContent"]["MaxFileSize"]}"
+ // Ograniczenie rozmiaru pliku do tego, ustawionego przez użytkownika
+ int MaxFileSize = int.TryParse(_appsettings.GetSection("UserContent")["MaxFileSize"], out int r)
+ ? r
+ : 5 * 1024 * 1024;
+ if (file.Length > MaxFileSize)
+ {
+ return StatusCode(413, 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);
- // Zapisz plik na dysku z pseudolosową nazwą GUID
+ // 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);
+ }
- // Wrzucić go do folderu "uploads/images/"
+ // Dodaj do bazy
+ Image image = new Image { Url = relativePath };
+ _db.Images.Add(image);
+ _db.SaveChanges();
-
- // Stwórz URL postaci: "/uploads/images/."
-
-
- // 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
- });
+ // Zwracany adres URL (np. do użytku w cytacie)
+ return Ok(new
+ {
+ Status = "ok",
+ Filepath = relativePath,
+ ImageId = image.Id
+ });
}
}
diff --git a/QuotifyBE.csproj b/QuotifyBE.csproj
index de69528..edb7c11 100644
--- a/QuotifyBE.csproj
+++ b/QuotifyBE.csproj
@@ -36,7 +36,7 @@
-
+