Additional checking for post uploads

This commit is contained in:
2025-10-13 10:14:57 +03:00
parent 30cb0521f6
commit cadb8453a2
3 changed files with 19 additions and 1488 deletions

View File

@@ -10,7 +10,7 @@ Sistema bus kuriama naudojant modernias technologijas, o diegimas bus supaprasti
### Funkciniai reikalavimai ### Funkciniai reikalavimai
#### Bendrieji reikalavimai #### Bendrieji reikalavimai
- Sistema privalo turėti vartotojo sąsają, kuri leistų peržiūrėti, įkelti, ir tvarkyti nuotraukas bei komentarus. - Sistema privalo turėti naudotojo sąsają, kuri leistų peržiūrėti, įkelti, ir tvarkyti nuotraukas bei komentarus.
- Duomenų bazė turi būti lengvai keičiama dėl pasirinkto _ORM_ (angl. Object-Relational Mapping) sluoksnio. - Duomenų bazė turi būti lengvai keičiama dėl pasirinkto _ORM_ (angl. Object-Relational Mapping) sluoksnio.
- Serverio ir klientinės dalys turi būti supakuotos į vieną diegimo vienetą (binary), siekiant supaprastinti diegiamosios sistemos procesą. - Serverio ir klientinės dalys turi būti supakuotos į vieną diegimo vienetą (binary), siekiant supaprastinti diegiamosios sistemos procesą.
@@ -81,7 +81,5 @@ Automatiškai bus sukurta administratoriaus paskyra:
## API dokumentacija ## API dokumentacija
API dokumentacija yra pasiekiama naudojant `Development` versiją šia nuoroda: API dokumentacija yra pasiekiama OpenAPI 3 JSON formatu naudojant `Development` versiją per šią nuorodą:
http://localhost:5259/swagger/v1/swagger.json http://localhost:5259/swagger/v1/swagger.json
Failo kopija yra pateikiama repozitorijoje pavadinta `swagger.json`. Nebūtinai naujausia versija.

View File

@@ -22,7 +22,8 @@ public class PostController(
ITagService tagService, ITagService tagService,
ICommentService commentService, ICommentService commentService,
IFileService fileService, IFileService fileService,
IWebHostEnvironment env IWebHostEnvironment env,
ILogger<PostController> logger
): ControllerBase ): ControllerBase
{ {
@@ -117,6 +118,7 @@ public class PostController(
/// <response code="400">If request is malformed</response> /// <response code="400">If request is malformed</response>
/// <response code="401">If authentication is missing</response> /// <response code="401">If authentication is missing</response>
/// <response code="403">If authorization is missing</response> /// <response code="403">If authorization is missing</response>
/// <response code="422">If data provided does not fit constraints</response>
[HttpPost] [HttpPost]
[Authorize(Roles = UserRoles.Regular)] [Authorize(Roles = UserRoles.Regular)]
[ProducesResponseType(StatusCodes.Status201Created)] [ProducesResponseType(StatusCodes.Status201Created)]
@@ -129,6 +131,11 @@ public class PostController(
var userId = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value; var userId = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value;
var user = await userService.GetUserById(userId); var user = await userService.GetUserById(userId);
if (user == null) return Unauthorized(); if (user == null) return Unauthorized();
if (dto.FileSize.Value > 10*1024*1024) return Problem("File cannot exceed 10MB", statusCode: StatusCodes.Status422UnprocessableEntity);
if (dto.FileMimeType != "image/png" && dto.FileMimeType != "image/jpeg")
return Problem("File must be image", statusCode: StatusCodes.Status422UnprocessableEntity);
var maybeTags = await TagNamesToTags(dto.Tags); var maybeTags = await TagNamesToTags(dto.Tags);
List<Tag> tags; List<Tag> tags;
@@ -164,6 +171,7 @@ public class PostController(
/// <response code="401">If authentication is missing</response> /// <response code="401">If authentication is missing</response>
/// <response code="403">If authorization is missing</response> /// <response code="403">If authorization is missing</response>
/// <response code="404">If post or file is not found</response> /// <response code="404">If post or file is not found</response>
/// <response code="409">If a chunk was already uploaded</response>
/// <response code="415">If finished upload mime does not match provided</response> /// <response code="415">If finished upload mime does not match provided</response>
[HttpPatch("{postId:int}/files/{fileId:int}")] [HttpPatch("{postId:int}/files/{fileId:int}")]
[Authorize(Roles = UserRoles.Regular)] [Authorize(Roles = UserRoles.Regular)]
@@ -214,11 +222,18 @@ public class PostController(
// Append the chunk to the temporary file // Append the chunk to the temporary file
var tempFilePath = fileRecord.FilePath; var tempFilePath = fileRecord.FilePath;
await using (var stream = new FileStream(tempFilePath, FileMode.Append, FileAccess.Write, FileShare.None, bufferSize: 4096, useAsync: true)) try
{ {
await using var stream = new FileStream(tempFilePath, FileMode.Append, FileAccess.Write, FileShare.None,
bufferSize: 4096, useAsync: true);
stream.Seek(start, SeekOrigin.Begin); stream.Seek(start, SeekOrigin.Begin);
await Request.Body.CopyToAsync(stream); await Request.Body.CopyToAsync(stream);
} }
catch (Exception e)
{
logger.LogCritical(e.ToString());
return Problem("Chunk has already been uploaded", statusCode: StatusCodes.Status409Conflict);
}
// Check if the upload is complete // Check if the upload is complete
// Return 202 Accepted for a successful intermediate chunk // Return 202 Accepted for a successful intermediate chunk

File diff suppressed because it is too large Load Diff