mirror of
https://github.com/QuotifyTeam/QuotifyBE.git
synced 2025-12-16 22:10:05 +01:00
zdjęcia
This commit is contained in:
@@ -71,7 +71,7 @@ public class UserContentController : ControllerBase
|
|||||||
[ProducesResponseType(200)]
|
[ProducesResponseType(200)]
|
||||||
[ProducesResponseType(typeof(ErrorDTO), 400)]
|
[ProducesResponseType(typeof(ErrorDTO), 400)]
|
||||||
[ProducesResponseType(typeof(ErrorDTO), 406)]
|
[ProducesResponseType(typeof(ErrorDTO), 406)]
|
||||||
public IActionResult PostNewImage(IFormFile file)
|
/*public IActionResult PostNewImage(IFormFile file)
|
||||||
{
|
{
|
||||||
|
|
||||||
// Ideally, a hash of the file would be stored somewhere
|
// Ideally, a hash of the file would be stored somewhere
|
||||||
@@ -113,6 +113,76 @@ public class UserContentController : ControllerBase
|
|||||||
// Zwróć powyższy URL
|
// Zwróć powyższy URL
|
||||||
return Ok(new { Status = "ok", Filepath = "miejsce na wspomniany 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<string> allowedExtensions = new List<string>() { ".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
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -151,5 +151,5 @@ app.UseAuthentication();
|
|||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
|
|
||||||
app.MapControllers();
|
app.MapControllers();
|
||||||
|
app.UseStaticFiles();
|
||||||
app.Run();
|
app.Run();
|
||||||
|
|||||||
BIN
wwwroot/uploads/images/42cbadf4-7804-4fde-991c-d56eb1f4a1b4.png
Normal file
BIN
wwwroot/uploads/images/42cbadf4-7804-4fde-991c-d56eb1f4a1b4.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 141 KiB |
Reference in New Issue
Block a user