From 4ea535d93753826ec900879560d876cec4d58c38 Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 10 Feb 2021 02:03:06 +0800 Subject: ... --- BackEnd/Timeline/Models/Http/HttpTimelinePost.cs | 11 ++++--- .../Models/Http/HttpTimelinePostContent.cs | 35 ---------------------- .../Models/Http/HttpTimelinePostCreateRequest.cs | 22 ++++++++++++-- .../Http/HttpTimelinePostCreateRequestContent.cs | 26 ---------------- .../Models/Http/HttpTimelinePostDataDigest.cs | 23 ++++++++++++++ BackEnd/Timeline/Models/Mapper/TimelineMapper.cs | 33 ++++---------------- .../Timeline/Models/TimelinePostContentTypes.cs | 7 ++--- .../Validation/TimelinePostContentTypeValidator.cs | 9 +++--- 8 files changed, 61 insertions(+), 105 deletions(-) delete mode 100644 BackEnd/Timeline/Models/Http/HttpTimelinePostContent.cs delete mode 100644 BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequestContent.cs create mode 100644 BackEnd/Timeline/Models/Http/HttpTimelinePostDataDigest.cs (limited to 'BackEnd/Timeline/Models') diff --git a/BackEnd/Timeline/Models/Http/HttpTimelinePost.cs b/BackEnd/Timeline/Models/Http/HttpTimelinePost.cs index 5981d7a4..165c92da 100644 --- a/BackEnd/Timeline/Models/Http/HttpTimelinePost.cs +++ b/BackEnd/Timeline/Models/Http/HttpTimelinePost.cs @@ -1,7 +1,9 @@ using System; +using System.Collections.Generic; namespace Timeline.Models.Http { + /// /// Info of a post. /// @@ -9,10 +11,10 @@ namespace Timeline.Models.Http { public HttpTimelinePost() { } - public HttpTimelinePost(long id, HttpTimelinePostContent? content, bool deleted, DateTime time, HttpUser? author, string? color, DateTime lastUpdated) + public HttpTimelinePost(long id, List dataList, bool deleted, DateTime time, HttpUser? author, string? color, DateTime lastUpdated) { Id = id; - Content = content; + DataList = dataList; Deleted = deleted; Time = time; Author = author; @@ -24,10 +26,7 @@ namespace Timeline.Models.Http /// Post id. /// public long Id { get; set; } - /// - /// Content of the post. May be null if post is deleted. - /// - public HttpTimelinePostContent? Content { get; set; } + public List DataList { get; set; } = default!; /// /// True if post is deleted. /// diff --git a/BackEnd/Timeline/Models/Http/HttpTimelinePostContent.cs b/BackEnd/Timeline/Models/Http/HttpTimelinePostContent.cs deleted file mode 100644 index 55ff1ac2..00000000 --- a/BackEnd/Timeline/Models/Http/HttpTimelinePostContent.cs +++ /dev/null @@ -1,35 +0,0 @@ -namespace Timeline.Models.Http -{ - /// - /// Info of post content. - /// - public class HttpTimelinePostContent - { - public HttpTimelinePostContent() { } - - public HttpTimelinePostContent(string type, string? text, string? url, string? eTag) - { - Type = type; - Text = text; - Url = url; - ETag = eTag; - } - - /// - /// Type of the post content. - /// - public string Type { get; set; } = default!; - /// - /// If post is of text type. This is the text. - /// - public string? Text { get; set; } - /// - /// If post is of image type. This is the image url. - /// - public string? Url { get; set; } - /// - /// If post has data (currently it means it's a image post), this is the data etag. - /// - public string? ETag { get; set; } - } -} diff --git a/BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequest.cs b/BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequest.cs index b25adf36..20d1a25b 100644 --- a/BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequest.cs +++ b/BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequest.cs @@ -1,16 +1,34 @@ using System; +using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using Timeline.Models.Validation; namespace Timeline.Models.Http { + public class HttpTimelinePostCreateRequestData + { + /// + /// Kind of the data. + /// + [Required] + [TimelinePostDataKind] + public string Kind { get; set; } = default!; + + /// + /// The true data. If kind is text or markdown, this is a string. If kind is image, this is base64 of data. + /// + [Required] + public string Data { get; set; } = default!; + } + public class HttpTimelinePostCreateRequest { /// - /// Content of the new post. + /// Data list of the new content. /// [Required] - public HttpTimelinePostCreateRequestContent Content { get; set; } = default!; + [MinLength(1)] + public List DataList { get; set; } = default!; /// /// Time of the post. If not set, current time will be used. diff --git a/BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequestContent.cs b/BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequestContent.cs deleted file mode 100644 index 12ab407f..00000000 --- a/BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequestContent.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System.ComponentModel.DataAnnotations; -using Timeline.Models.Validation; - -namespace Timeline.Models.Http -{ - /// - /// Content of post create request. - /// - public class HttpTimelinePostCreateRequestContent - { - /// - /// Type of post content. - /// - [Required] - [TimelinePostContentType] - public string Type { get; set; } = default!; - /// - /// If post is of text type, this is the text. - /// - public string? Text { get; set; } - /// - /// If post is of image type, this is base64 of image data. - /// - public string? Data { get; set; } - } -} diff --git a/BackEnd/Timeline/Models/Http/HttpTimelinePostDataDigest.cs b/BackEnd/Timeline/Models/Http/HttpTimelinePostDataDigest.cs new file mode 100644 index 00000000..61d35e15 --- /dev/null +++ b/BackEnd/Timeline/Models/Http/HttpTimelinePostDataDigest.cs @@ -0,0 +1,23 @@ +using System; + +namespace Timeline.Models.Http +{ + public class HttpTimelinePostDataDigest + { + public HttpTimelinePostDataDigest() + { + + } + + public HttpTimelinePostDataDigest(string kind, string eTag, DateTime lastUpdated) + { + Kind = kind; + ETag = eTag; + LastUpdated = lastUpdated; + } + + public string Kind { get; set; } = default!; + public string ETag { get; set; } = default!; + public DateTime LastUpdated { get; set; } + } +} diff --git a/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs b/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs index 88c96d8a..33ee9593 100644 --- a/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs +++ b/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs @@ -1,7 +1,6 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using System.Collections.Generic; -using System.Globalization; using System.Linq; using System.Threading.Tasks; using Timeline.Controllers; @@ -67,34 +66,12 @@ namespace Timeline.Models.Mapper public async Task MapToHttp(TimelinePostEntity entity, string timelineName, IUrlHelper urlHelper) { - HttpTimelinePostContent? content = null; - - if (entity.Content != null) - { - content = entity.ContentType switch - { - TimelinePostContentTypes.Text => new HttpTimelinePostContent - ( - type: TimelinePostContentTypes.Text, - text: entity.Content, - url: null, - eTag: null - ), - TimelinePostContentTypes.Image => new HttpTimelinePostContent - ( - type: TimelinePostContentTypes.Image, - text: null, - url: urlHelper.ActionLink(nameof(TimelinePostController.DataGet), nameof(TimelinePostController)[0..^nameof(Controller).Length], new { timeline = timelineName, post = entity.LocalId }), - eTag: $"\"{entity.Content}\"" - ), - _ => throw new DatabaseCorruptedException(string.Format(CultureInfo.InvariantCulture, "Unknown timeline post type {0}.", entity.ContentType)) - }; - } - + await _database.Entry(entity).Collection(p => p.DataList).LoadAsync(); await _database.Entry(entity).Reference(e => e.Author).LoadAsync(); - HttpUser? author = null; + List dataDigestList = entity.DataList.OrderBy(d => d.Index).Select(d => new HttpTimelinePostDataDigest(d.Kind, d.DataTag, d.LastUpdated)).ToList(); + HttpUser? author = null; if (entity.Author is not null) { author = await _userMapper.MapToHttp(entity.Author, urlHelper); @@ -102,11 +79,11 @@ namespace Timeline.Models.Mapper return new HttpTimelinePost( id: entity.LocalId, - content: content, - deleted: content is null, + dataList: dataDigestList, time: entity.Time, author: author, color: entity.Color, + deleted: entity.Deleted, lastUpdated: entity.LastUpdated ); } diff --git a/BackEnd/Timeline/Models/TimelinePostContentTypes.cs b/BackEnd/Timeline/Models/TimelinePostContentTypes.cs index ca5e79e1..d432e03c 100644 --- a/BackEnd/Timeline/Models/TimelinePostContentTypes.cs +++ b/BackEnd/Timeline/Models/TimelinePostContentTypes.cs @@ -2,13 +2,12 @@ namespace Timeline.Models { - public static class TimelinePostContentTypes + public static class TimelinePostDataKind { -#pragma warning disable CA1819 // Properties should not return arrays - public static string[] AllTypes { get; } = new string[] { Text, Image }; -#pragma warning restore CA1819 // Properties should not return arrays + public static IReadOnlyList AllTypes { get; } = new List { Text, Image, Markdown }; public const string Text = "text"; public const string Image = "image"; + public const string Markdown = "markdown"; } } diff --git a/BackEnd/Timeline/Models/Validation/TimelinePostContentTypeValidator.cs b/BackEnd/Timeline/Models/Validation/TimelinePostContentTypeValidator.cs index 483cce06..b65c846c 100644 --- a/BackEnd/Timeline/Models/Validation/TimelinePostContentTypeValidator.cs +++ b/BackEnd/Timeline/Models/Validation/TimelinePostContentTypeValidator.cs @@ -1,16 +1,17 @@ using System; +using System.Linq; namespace Timeline.Models.Validation { - public class TimelinePostContentTypeValidator : StringSetValidator + public class TimelinePostDataKindValidator : StringSetValidator { - public TimelinePostContentTypeValidator() : base(TimelinePostContentTypes.AllTypes) { } + public TimelinePostDataKindValidator() : base(TimelinePostDataKind.AllTypes.ToArray()) { } } [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)] - public class TimelinePostContentTypeAttribute : ValidateWithAttribute + public class TimelinePostDataKindAttribute : ValidateWithAttribute { - public TimelinePostContentTypeAttribute() : base(typeof(TimelinePostContentTypeValidator)) + public TimelinePostDataKindAttribute() : base(typeof(TimelinePostDataKindValidator)) { } -- cgit v1.2.3 From 98df0fb9fe94b703325f09bf765904a11e8a7d4c Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 10 Feb 2021 14:31:31 +0800 Subject: ... --- BackEnd/Timeline.ErrorCodes/ErrorCodes.cs | 2 +- .../Timeline/Controllers/TimelinePostController.cs | 158 ++++++++------------- .../CatchTimelineNotExistExceptionAttribute.cs | 32 +++++ ...chTimelinePostDataNotExistExceptionAttribute.cs | 24 ++++ .../CatchTimelinePostNotExistExceptionAttribute.cs | 24 ++++ BackEnd/Timeline/Filters/Timeline.cs | 32 ----- .../Timeline/Formatters/ByteDataInputFormatter.cs | 80 +++++++++++ BackEnd/Timeline/Formatters/BytesInputFormatter.cs | 79 ----------- .../Migrations/20200312112552_AddImagePost.cs | 2 +- BackEnd/Timeline/Models/Http/ErrorResponse.cs | 11 -- .../Models/Http/HttpTimelinePostCreateRequest.cs | 17 +-- .../Http/HttpTimelinePostCreateRequestData.cs | 19 +++ BackEnd/Timeline/Models/MimeTypes.cs | 14 ++ .../Timeline/Models/TimelinePostContentTypes.cs | 13 -- .../Validation/TimelinePostContentTypeValidator.cs | 19 --- .../Exceptions/TimelinePostNoDataException.cs | 15 -- .../Services/TimelinePostCreateDataException.cs | 16 +++ .../Services/TimelinePostDataNotExistException.cs | 15 ++ BackEnd/Timeline/Services/TimelinePostService.cs | 68 +++------ BackEnd/Timeline/Startup.cs | 2 +- 20 files changed, 308 insertions(+), 334 deletions(-) create mode 100644 BackEnd/Timeline/Filters/CatchTimelineNotExistExceptionAttribute.cs create mode 100644 BackEnd/Timeline/Filters/CatchTimelinePostDataNotExistExceptionAttribute.cs create mode 100644 BackEnd/Timeline/Filters/CatchTimelinePostNotExistExceptionAttribute.cs delete mode 100644 BackEnd/Timeline/Filters/Timeline.cs create mode 100644 BackEnd/Timeline/Formatters/ByteDataInputFormatter.cs delete mode 100644 BackEnd/Timeline/Formatters/BytesInputFormatter.cs create mode 100644 BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequestData.cs create mode 100644 BackEnd/Timeline/Models/MimeTypes.cs delete mode 100644 BackEnd/Timeline/Models/TimelinePostContentTypes.cs delete mode 100644 BackEnd/Timeline/Models/Validation/TimelinePostContentTypeValidator.cs delete mode 100644 BackEnd/Timeline/Services/Exceptions/TimelinePostNoDataException.cs create mode 100644 BackEnd/Timeline/Services/TimelinePostCreateDataException.cs create mode 100644 BackEnd/Timeline/Services/TimelinePostDataNotExistException.cs (limited to 'BackEnd/Timeline/Models') diff --git a/BackEnd/Timeline.ErrorCodes/ErrorCodes.cs b/BackEnd/Timeline.ErrorCodes/ErrorCodes.cs index 8211a0cc..4c3b6cd8 100644 --- a/BackEnd/Timeline.ErrorCodes/ErrorCodes.cs +++ b/BackEnd/Timeline.ErrorCodes/ErrorCodes.cs @@ -61,7 +61,7 @@ public const int NotExist = 1_104_02_01; public const int QueryRelateNotExist = 1_104_04_01; public const int PostNotExist = 1_104_05_01; - public const int PostNoData = 1_104_05_02; + public const int PostDataNotExist = 1_104_05_02; } public static class HighlightTimelineController diff --git a/BackEnd/Timeline/Controllers/TimelinePostController.cs b/BackEnd/Timeline/Controllers/TimelinePostController.cs index a0fd1687..06082f0f 100644 --- a/BackEnd/Timeline/Controllers/TimelinePostController.cs +++ b/BackEnd/Timeline/Controllers/TimelinePostController.cs @@ -4,14 +4,14 @@ using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Threading.Tasks; +using System.ComponentModel.DataAnnotations; using Timeline.Filters; -using Timeline.Helpers; +using Timeline.Helpers.Cache; using Timeline.Models; using Timeline.Models.Http; using Timeline.Models.Mapper; using Timeline.Models.Validation; using Timeline.Services; -using Timeline.Services.Exceptions; namespace Timeline.Controllers { @@ -21,6 +21,8 @@ namespace Timeline.Controllers [ApiController] [Route("timelines/{timeline}/posts")] [CatchTimelineNotExistException] + [CatchTimelinePostNotExistException] + [CatchTimelinePostDataNotExistException] [ProducesErrorResponseType(typeof(CommonResponse))] public class TimelinePostController : Controller { @@ -86,57 +88,27 @@ namespace Timeline.Controllers return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); } - try - { - var post = await _postService.GetPost(timelineId, postId); - var result = await _timelineMapper.MapToHttp(post, timeline, Url); - return result; - } - catch (TimelinePostNotExistException) - { - return NotFound(ErrorResponse.TimelineController.PostNotExist()); - } + var post = await _postService.GetPost(timelineId, postId); + var result = await _timelineMapper.MapToHttp(post, timeline, Url); + return result; } /// - /// Get the data of a post. Usually a image post. + /// Get the first data of a post. /// /// Timeline name. /// The id of the post. - /// If-None-Match header. /// The data. [HttpGet("{post}/data")] - [Produces("image/png", "image/jpeg", "image/gif", "image/webp", "application/json", "text/json")] + [Produces(MimeTypes.ImagePng, MimeTypes.ImageJpeg, MimeTypes.ImageGif, MimeTypes.ImageWebp, MimeTypes.TextPlain, MimeTypes.TextMarkdown, MimeTypes.TextPlain, MimeTypes.ApplicationJson)] [ProducesResponseType(typeof(byte[]), StatusCodes.Status200OK)] [ProducesResponseType(typeof(void), StatusCodes.Status304NotModified)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(StatusCodes.Status404NotFound)] - public async Task DataIndexGet([FromRoute][GeneralTimelineName] string timeline, [FromRoute] long post, [FromHeader(Name = "If-None-Match")] string? ifNoneMatch) + public async Task> DataIndexGet([FromRoute][GeneralTimelineName] string timeline, [FromRoute] long post) { - _ = ifNoneMatch; - - var timelineId = await _timelineService.GetTimelineIdByName(timeline); - - if (!UserHasAllTimelineManagementPermission && !await _timelineService.HasReadPermission(timelineId, this.GetOptionalUserId())) - { - return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); - } - - try - { - return await DataCacheHelper.GenerateActionResult(this, - () => _postService.GetPostDataETag(timelineId, post), - async () => await _postService.GetPostData(timelineId, post)); - } - catch (TimelinePostNotExistException) - { - return NotFound(ErrorResponse.TimelineController.PostNotExist()); - } - catch (TimelinePostNoDataException) - { - return BadRequest(ErrorResponse.TimelineController.PostNoData()); - } + return await DataGet(timeline, post, 0); } /// @@ -144,17 +116,28 @@ namespace Timeline.Controllers /// /// Timeline name. /// The id of the post. - /// If-None-Match header. + /// Index of the data. /// The data. [HttpGet("{post}/data/{data_index}")] - [Produces("image/png", "image/jpeg", "image/gif", "image/webp", "application/json", "text/json")] + [Produces(MimeTypes.ImagePng, MimeTypes.ImageJpeg, MimeTypes.ImageGif, MimeTypes.ImageWebp, MimeTypes.TextPlain, MimeTypes.TextMarkdown, MimeTypes.TextPlain, MimeTypes.ApplicationJson)] [ProducesResponseType(typeof(byte[]), StatusCodes.Status200OK)] [ProducesResponseType(typeof(void), StatusCodes.Status304NotModified)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(StatusCodes.Status404NotFound)] - public async Task DataGet([FromRoute][GeneralTimelineName] string timeline, [FromRoute] long post, [FromHeader(Name = "If-None-Match")] string? ifNoneMatch) + public async Task DataGet([FromRoute][GeneralTimelineName] string timeline, [FromRoute] long post, [FromRoute(Name = "data_index")][Range(0, 100)] long dataIndex) { + var timelineId = await _timelineService.GetTimelineIdByName(timeline); + + if (!UserHasAllTimelineManagementPermission && !await _timelineService.HasReadPermission(timelineId, this.GetOptionalUserId())) + { + return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); + } + + return await DataCacheHelper.GenerateActionResult(this, + () => _postService.GetPostDataDigest(timelineId, post, dataIndex), + () => _postService.GetPostData(timelineId, post, dataIndex) + ); } /// @@ -179,50 +162,36 @@ namespace Timeline.Controllers return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); } - var requestContent = body.Content; - - TimelinePostCreateRequestData createContent; - - switch (requestContent.Type) + var createRequest = new TimelinePostCreateRequest() { - case TimelinePostDataKind.Text: - if (requestContent.Text is null) - { - return BadRequest(ErrorResponse.Common.CustomMessage_InvalidModel(Resources.Messages.TimelineController_TextContentTextRequired)); - } - createContent = new TimelinePostCreateRequestTextContent(requestContent.Text); - break; - case TimelinePostDataKind.Image: - if (requestContent.Data is null) - return BadRequest(ErrorResponse.Common.CustomMessage_InvalidModel(Resources.Messages.TimelineController_ImageContentDataRequired)); - - // decode base64 - byte[] data; - try - { - data = Convert.FromBase64String(requestContent.Data); - } - catch (FormatException) - { - return BadRequest(ErrorResponse.Common.CustomMessage_InvalidModel(Resources.Messages.TimelineController_ImageContentDataNotBase64)); - } - - createContent = new TimelinePostCreateRequestImageData(data); - break; - default: - return BadRequest(ErrorResponse.Common.CustomMessage_InvalidModel(Resources.Messages.TimelineController_ContentUnknownType)); + Time = body.Time, + Color = body.Color + }; + for (int i = 0; i < body.DataList.Count; i++) + { + var data = body.DataList[i]; + try + { + var d = Convert.FromBase64String(data.Data); + createRequest.DataList.Add(new TimelinePostCreateRequestData(data.ContentType, d)); + } + catch (FormatException) + { + return BadRequest(new CommonResponse(ErrorCodes.Common.InvalidModel, $"Data at index {i} is not a valid base64 string.")); + } } + try { - var post = await _postService.CreatePost(timelineId, userId, new TimelinePostCreateRequest(createContent) { Time = body.Time, Color = body.Color }); + var post = await _postService.CreatePost(timelineId, userId, createRequest); var result = await _timelineMapper.MapToHttp(post, timeline, Url); return result; } - catch (ImageException) + catch (TimelinePostCreateDataException e) { - return BadRequest(ErrorResponse.Common.CustomMessage_InvalidModel(Resources.Messages.TimelineController_ImageContentDataNotImage)); + return BadRequest(new CommonResponse(ErrorCodes.Common.InvalidModel, $"Data at index {e.Index} is invalid. {e.Message}")); } } @@ -243,21 +212,15 @@ namespace Timeline.Controllers { var timelineId = await _timelineService.GetTimelineIdByName(timeline); - try + if (!UserHasAllTimelineManagementPermission && !await _postService.HasPostModifyPermission(timelineId, post, this.GetUserId(), true)) { - if (!UserHasAllTimelineManagementPermission && !await _postService.HasPostModifyPermission(timelineId, post, this.GetUserId(), true)) - { - return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); - } - - var entity = await _postService.PatchPost(timelineId, post, new TimelinePostPatchRequest { Time = body.Time, Color = body.Color }); - var result = await _timelineMapper.MapToHttp(entity, timeline, Url); - return Ok(result); - } - catch (TimelinePostNotExistException) - { - return BadRequest(ErrorResponse.TimelineController.PostNotExist()); + return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); } + + var entity = await _postService.PatchPost(timelineId, post, new TimelinePostPatchRequest { Time = body.Time, Color = body.Color }); + var result = await _timelineMapper.MapToHttp(entity, timeline, Url); + + return Ok(result); } /// @@ -276,19 +239,14 @@ namespace Timeline.Controllers { var timelineId = await _timelineService.GetTimelineIdByName(timeline); - try + if (!UserHasAllTimelineManagementPermission && !await _postService.HasPostModifyPermission(timelineId, post, this.GetUserId(), true)) { - if (!UserHasAllTimelineManagementPermission && !await _postService.HasPostModifyPermission(timelineId, post, this.GetUserId(), true)) - { - return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); - } - await _postService.DeletePost(timelineId, post); - return Ok(); - } - catch (TimelinePostNotExistException) - { - return BadRequest(ErrorResponse.TimelineController.PostNotExist()); + return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); } + + await _postService.DeletePost(timelineId, post); + + return Ok(); } } } diff --git a/BackEnd/Timeline/Filters/CatchTimelineNotExistExceptionAttribute.cs b/BackEnd/Timeline/Filters/CatchTimelineNotExistExceptionAttribute.cs new file mode 100644 index 00000000..857d1d2b --- /dev/null +++ b/BackEnd/Timeline/Filters/CatchTimelineNotExistExceptionAttribute.cs @@ -0,0 +1,32 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; +using Timeline.Models.Http; +using Timeline.Services.Exceptions; + +namespace Timeline.Filters +{ + public class CatchTimelineNotExistExceptionAttribute : ExceptionFilterAttribute + { + public override void OnException(ExceptionContext context) + { + if (context.Exception is TimelineNotExistException e) + { + if (e.InnerException is UserNotExistException) + { + if (HttpMethods.IsGet(context.HttpContext.Request.Method)) + context.Result = new NotFoundObjectResult(ErrorResponse.UserCommon.NotExist()); + else + context.Result = new BadRequestObjectResult(ErrorResponse.UserCommon.NotExist()); + } + else + { + if (HttpMethods.IsGet(context.HttpContext.Request.Method)) + context.Result = new NotFoundObjectResult(ErrorResponse.TimelineController.NotExist()); + else + context.Result = new BadRequestObjectResult(ErrorResponse.TimelineController.NotExist()); + } + } + } + } +} diff --git a/BackEnd/Timeline/Filters/CatchTimelinePostDataNotExistExceptionAttribute.cs b/BackEnd/Timeline/Filters/CatchTimelinePostDataNotExistExceptionAttribute.cs new file mode 100644 index 00000000..8b5868aa --- /dev/null +++ b/BackEnd/Timeline/Filters/CatchTimelinePostDataNotExistExceptionAttribute.cs @@ -0,0 +1,24 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; +using Timeline.Models.Http; +using Timeline.Services; + +namespace Timeline.Filters +{ + public class CatchTimelinePostDataNotExistExceptionAttribute : ExceptionFilterAttribute + { + public override void OnException(ExceptionContext context) + { + const string message = "Timeline post data does not exist."; + + if (context.Exception is TimelinePostDataNotExistException e) + { + if (HttpMethods.IsGet(context.HttpContext.Request.Method)) + context.Result = new NotFoundObjectResult(new CommonResponse(ErrorCodes.TimelineController.PostNotExist, message)); + else + context.Result = new BadRequestObjectResult(new CommonResponse(ErrorCodes.TimelineController.PostNotExist, message)); + } + } + } +} diff --git a/BackEnd/Timeline/Filters/CatchTimelinePostNotExistExceptionAttribute.cs b/BackEnd/Timeline/Filters/CatchTimelinePostNotExistExceptionAttribute.cs new file mode 100644 index 00000000..ac3789c7 --- /dev/null +++ b/BackEnd/Timeline/Filters/CatchTimelinePostNotExistExceptionAttribute.cs @@ -0,0 +1,24 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; +using Timeline.Models.Http; +using Timeline.Services.Exceptions; + +namespace Timeline.Filters +{ + public class CatchTimelinePostNotExistExceptionAttribute : ExceptionFilterAttribute + { + public override void OnException(ExceptionContext context) + { + const string message = "Timeline post does not exist."; + + if (context.Exception is TimelinePostNotExistException e) + { + if (HttpMethods.IsGet(context.HttpContext.Request.Method)) + context.Result = new NotFoundObjectResult(new CommonResponse(ErrorCodes.TimelineController.PostNotExist, message)); + else + context.Result = new BadRequestObjectResult(new CommonResponse(ErrorCodes.TimelineController.PostNotExist, message)); + } + } + } +} diff --git a/BackEnd/Timeline/Filters/Timeline.cs b/BackEnd/Timeline/Filters/Timeline.cs deleted file mode 100644 index 6a730ee7..00000000 --- a/BackEnd/Timeline/Filters/Timeline.cs +++ /dev/null @@ -1,32 +0,0 @@ -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.Filters; -using Timeline.Models.Http; -using Timeline.Services.Exceptions; - -namespace Timeline.Filters -{ - public class CatchTimelineNotExistExceptionAttribute : ExceptionFilterAttribute - { - public override void OnException(ExceptionContext context) - { - if (context.Exception is TimelineNotExistException e) - { - if (e.InnerException is UserNotExistException) - { - if (HttpMethods.IsGet(context.HttpContext.Request.Method)) - context.Result = new NotFoundObjectResult(ErrorResponse.UserCommon.NotExist()); - else - context.Result = new BadRequestObjectResult(ErrorResponse.UserCommon.NotExist()); - } - else - { - if (HttpMethods.IsGet(context.HttpContext.Request.Method)) - context.Result = new NotFoundObjectResult(ErrorResponse.TimelineController.NotExist()); - else - context.Result = new BadRequestObjectResult(ErrorResponse.TimelineController.NotExist()); - } - } - } - } -} diff --git a/BackEnd/Timeline/Formatters/ByteDataInputFormatter.cs b/BackEnd/Timeline/Formatters/ByteDataInputFormatter.cs new file mode 100644 index 00000000..2451ead6 --- /dev/null +++ b/BackEnd/Timeline/Formatters/ByteDataInputFormatter.cs @@ -0,0 +1,80 @@ +using Microsoft.AspNetCore.Mvc.Formatters; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using System; +using System.Threading.Tasks; +using Timeline.Models; + +namespace Timeline.Formatters +{ + /// + /// Formatter that reads body as byte data. + /// + public class ByteDataInputFormatter : InputFormatter + { + /// + /// + /// + public ByteDataInputFormatter() + { + SupportedMediaTypes.Add(MimeTypes.ImagePng); + SupportedMediaTypes.Add(MimeTypes.ImageJpeg); + SupportedMediaTypes.Add(MimeTypes.ImageGif); + SupportedMediaTypes.Add(MimeTypes.ImageWebp); + SupportedMediaTypes.Add(MimeTypes.TextPlain); + SupportedMediaTypes.Add(MimeTypes.TextMarkdown); + } + + /// + public override bool CanRead(InputFormatterContext context) + { + if (context == null) throw new ArgumentNullException(nameof(context)); + + if (context.ModelType == typeof(ByteData)) + return true; + + return false; + } + + /// + public override async Task ReadRequestBodyAsync(InputFormatterContext context) + { + var request = context.HttpContext.Request; + var contentLength = request.ContentLength; + + var logger = context.HttpContext.RequestServices.GetRequiredService>(); + + if (contentLength == null) + { + logger.LogInformation("Failed to read body as bytes. Content-Length is not set."); + return await InputFormatterResult.FailureAsync(); + } + + if (contentLength == 0) + { + logger.LogInformation("Failed to read body as bytes. Content-Length is 0."); + return await InputFormatterResult.FailureAsync(); + } + + var bodyStream = request.Body; + + var data = new byte[contentLength.Value]; + var bytesRead = await bodyStream.ReadAsync(data); + + if (bytesRead != contentLength) + { + logger.LogInformation("Failed to read body as bytes. Actual length of body is smaller than Content-Length."); + return await InputFormatterResult.FailureAsync(); + } + + var extraByte = new byte[1]; + if (await bodyStream.ReadAsync(extraByte) != 0) + { + logger.LogInformation("Failed to read body as bytes. Actual length of body is greater than Content-Length."); + return await InputFormatterResult.FailureAsync(); + } + + return await InputFormatterResult.SuccessAsync(new ByteData(data, request.ContentType)); + } + } +} diff --git a/BackEnd/Timeline/Formatters/BytesInputFormatter.cs b/BackEnd/Timeline/Formatters/BytesInputFormatter.cs deleted file mode 100644 index ac6537c9..00000000 --- a/BackEnd/Timeline/Formatters/BytesInputFormatter.cs +++ /dev/null @@ -1,79 +0,0 @@ -using Microsoft.AspNetCore.Mvc.Formatters; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using Microsoft.Net.Http.Headers; -using System; -using System.Threading.Tasks; -using Timeline.Models; - -namespace Timeline.Formatters -{ - /// - /// Formatter that reads body as bytes. - /// - public class BytesInputFormatter : InputFormatter - { - /// - /// - /// - public BytesInputFormatter() - { - SupportedMediaTypes.Add(new MediaTypeHeaderValue("image/png")); - SupportedMediaTypes.Add(new MediaTypeHeaderValue("image/jpeg")); - SupportedMediaTypes.Add(new MediaTypeHeaderValue("image/gif")); - SupportedMediaTypes.Add(new MediaTypeHeaderValue("image/webp")); - } - - /// - public override bool CanRead(InputFormatterContext context) - { - if (context == null) throw new ArgumentNullException(nameof(context)); - - if (context.ModelType == typeof(ByteData)) - return true; - - return false; - } - - /// - public override async Task ReadRequestBodyAsync(InputFormatterContext context) - { - var request = context.HttpContext.Request; - var contentLength = request.ContentLength; - - var logger = context.HttpContext.RequestServices.GetRequiredService>(); - - if (contentLength == null) - { - logger.LogInformation("Failed to read body as bytes. Content-Length is not set."); - return await InputFormatterResult.FailureAsync(); - } - - if (contentLength == 0) - { - logger.LogInformation("Failed to read body as bytes. Content-Length is 0."); - return await InputFormatterResult.FailureAsync(); - } - - var bodyStream = request.Body; - - var data = new byte[contentLength.Value]; - var bytesRead = await bodyStream.ReadAsync(data); - - if (bytesRead != contentLength) - { - logger.LogInformation("Failed to read body as bytes. Actual length of body is smaller than Content-Length."); - return await InputFormatterResult.FailureAsync(); - } - - var extraByte = new byte[1]; - if (await bodyStream.ReadAsync(extraByte) != 0) - { - logger.LogInformation("Failed to read body as bytes. Actual length of body is greater than Content-Length."); - return await InputFormatterResult.FailureAsync(); - } - - return await InputFormatterResult.SuccessAsync(new ByteData(data, request.ContentType)); - } - } -} diff --git a/BackEnd/Timeline/Migrations/20200312112552_AddImagePost.cs b/BackEnd/Timeline/Migrations/20200312112552_AddImagePost.cs index b6cc29a3..7d9c6614 100644 --- a/BackEnd/Timeline/Migrations/20200312112552_AddImagePost.cs +++ b/BackEnd/Timeline/Migrations/20200312112552_AddImagePost.cs @@ -20,7 +20,7 @@ namespace Timeline.Migrations migrationBuilder.Sql($@" UPDATE timeline_posts -SET content_type = '{TimelinePostDataKind.Text}'; +SET content_type = 'text'; "); } diff --git a/BackEnd/Timeline/Models/Http/ErrorResponse.cs b/BackEnd/Timeline/Models/Http/ErrorResponse.cs index 1bc46680..3812471d 100644 --- a/BackEnd/Timeline/Models/Http/ErrorResponse.cs +++ b/BackEnd/Timeline/Models/Http/ErrorResponse.cs @@ -253,17 +253,6 @@ namespace Timeline.Models.Http { return new CommonResponse(ErrorCodes.TimelineController.PostNotExist, string.Format(message, formatArgs)); } - - public static CommonResponse PostNoData(params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.TimelineController.PostNoData, string.Format(TimelineController_PostNoData, formatArgs)); - } - - public static CommonResponse CustomMessage_PostNoData(string message, params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.TimelineController.PostNoData, string.Format(message, formatArgs)); - } - } } diff --git a/BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequest.cs b/BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequest.cs index 20d1a25b..07d823ad 100644 --- a/BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequest.cs +++ b/BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequest.cs @@ -5,22 +5,6 @@ using Timeline.Models.Validation; namespace Timeline.Models.Http { - public class HttpTimelinePostCreateRequestData - { - /// - /// Kind of the data. - /// - [Required] - [TimelinePostDataKind] - public string Kind { get; set; } = default!; - - /// - /// The true data. If kind is text or markdown, this is a string. If kind is image, this is base64 of data. - /// - [Required] - public string Data { get; set; } = default!; - } - public class HttpTimelinePostCreateRequest { /// @@ -28,6 +12,7 @@ namespace Timeline.Models.Http /// [Required] [MinLength(1)] + [MaxLength(100)] public List DataList { get; set; } = default!; /// diff --git a/BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequestData.cs b/BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequestData.cs new file mode 100644 index 00000000..94ee5aa7 --- /dev/null +++ b/BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequestData.cs @@ -0,0 +1,19 @@ +using System.ComponentModel.DataAnnotations; + +namespace Timeline.Models.Http +{ + public class HttpTimelinePostCreateRequestData + { + /// + /// Mime type of the data. + /// + [Required] + public string ContentType { get; set; } = default!; + + /// + /// Base64 of data. + /// + [Required] + public string Data { get; set; } = default!; + } +} diff --git a/BackEnd/Timeline/Models/MimeTypes.cs b/BackEnd/Timeline/Models/MimeTypes.cs new file mode 100644 index 00000000..37d3a893 --- /dev/null +++ b/BackEnd/Timeline/Models/MimeTypes.cs @@ -0,0 +1,14 @@ +namespace Timeline.Models +{ + public static class MimeTypes + { + public const string ImagePng = "image/png"; + public const string ImageJpeg = "image/jpeg"; + public const string ImageGif = "image/gif"; + public const string ImageWebp = "image/webp"; + public const string TextPlain = "text/plain"; + public const string TextMarkdown = "text/markdown"; + public const string TextJson = "text/json"; + public const string ApplicationJson = "application/json"; + } +} diff --git a/BackEnd/Timeline/Models/TimelinePostContentTypes.cs b/BackEnd/Timeline/Models/TimelinePostContentTypes.cs deleted file mode 100644 index d432e03c..00000000 --- a/BackEnd/Timeline/Models/TimelinePostContentTypes.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System.Collections.Generic; - -namespace Timeline.Models -{ - public static class TimelinePostDataKind - { - public static IReadOnlyList AllTypes { get; } = new List { Text, Image, Markdown }; - - public const string Text = "text"; - public const string Image = "image"; - public const string Markdown = "markdown"; - } -} diff --git a/BackEnd/Timeline/Models/Validation/TimelinePostContentTypeValidator.cs b/BackEnd/Timeline/Models/Validation/TimelinePostContentTypeValidator.cs deleted file mode 100644 index b65c846c..00000000 --- a/BackEnd/Timeline/Models/Validation/TimelinePostContentTypeValidator.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using System.Linq; - -namespace Timeline.Models.Validation -{ - public class TimelinePostDataKindValidator : StringSetValidator - { - public TimelinePostDataKindValidator() : base(TimelinePostDataKind.AllTypes.ToArray()) { } - } - - [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)] - public class TimelinePostDataKindAttribute : ValidateWithAttribute - { - public TimelinePostDataKindAttribute() : base(typeof(TimelinePostDataKindValidator)) - { - - } - } -} diff --git a/BackEnd/Timeline/Services/Exceptions/TimelinePostNoDataException.cs b/BackEnd/Timeline/Services/Exceptions/TimelinePostNoDataException.cs deleted file mode 100644 index c4b6bf62..00000000 --- a/BackEnd/Timeline/Services/Exceptions/TimelinePostNoDataException.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; - -namespace Timeline.Services.Exceptions -{ - [Serializable] - public class TimelinePostNoDataException : Exception - { - public TimelinePostNoDataException() : this(null, null) { } - public TimelinePostNoDataException(string? message) : this(message, null) { } - public TimelinePostNoDataException(string? message, Exception? inner) : base(Resources.Services.Exceptions.TimelineNoDataException.AppendAdditionalMessage(message), inner) { } - protected TimelinePostNoDataException( - System.Runtime.Serialization.SerializationInfo info, - System.Runtime.Serialization.StreamingContext context) : base(info, context) { } - } -} diff --git a/BackEnd/Timeline/Services/TimelinePostCreateDataException.cs b/BackEnd/Timeline/Services/TimelinePostCreateDataException.cs new file mode 100644 index 00000000..fd1e6664 --- /dev/null +++ b/BackEnd/Timeline/Services/TimelinePostCreateDataException.cs @@ -0,0 +1,16 @@ +namespace Timeline.Services +{ + [System.Serializable] + public class TimelinePostCreateDataException : System.Exception + { + public TimelinePostCreateDataException() { } + public TimelinePostCreateDataException(string message) : base(message) { } + public TimelinePostCreateDataException(string message, System.Exception inner) : base(message, inner) { } + public TimelinePostCreateDataException(long index, string? message, System.Exception? inner = null) : base(message, inner) { Index = index; } + protected TimelinePostCreateDataException( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) : base(info, context) { } + + public long Index { get; } + } +} diff --git a/BackEnd/Timeline/Services/TimelinePostDataNotExistException.cs b/BackEnd/Timeline/Services/TimelinePostDataNotExistException.cs new file mode 100644 index 00000000..eac7a771 --- /dev/null +++ b/BackEnd/Timeline/Services/TimelinePostDataNotExistException.cs @@ -0,0 +1,15 @@ +using System; + +namespace Timeline.Services +{ + [Serializable] + public class TimelinePostDataNotExistException : Exception + { + public TimelinePostDataNotExistException() : this(null, null) { } + public TimelinePostDataNotExistException(string? message) : this(message, null) { } + public TimelinePostDataNotExistException(string? message, Exception? inner) : base(message, inner) { } + protected TimelinePostDataNotExistException( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) : base(info, context) { } + } +} diff --git a/BackEnd/Timeline/Services/TimelinePostService.cs b/BackEnd/Timeline/Services/TimelinePostService.cs index 98841478..cea702a1 100644 --- a/BackEnd/Timeline/Services/TimelinePostService.cs +++ b/BackEnd/Timeline/Services/TimelinePostService.cs @@ -7,6 +7,7 @@ using System.Linq; using System.Threading.Tasks; using Timeline.Entities; using Timeline.Helpers; +using Timeline.Helpers.Cache; using Timeline.Models; using Timeline.Models.Validation; using Timeline.Services.Exceptions; @@ -14,49 +15,15 @@ using static Timeline.Resources.Services.TimelineService; namespace Timeline.Services { - public class TimelinePostDataDigest - { - public TimelinePostDataDigest(string kind, string eTag, DateTime lastModified) - { - Kind = kind; - ETag = eTag; - LastModified = lastModified; - } - - public string Kind { get; set; } - public string ETag { get; set; } - public DateTime LastModified { get; set; } - } - - public class TimelinePostData - { - public TimelinePostData(string kind, byte[] data, string eTag, DateTime lastModified) - { - Kind = kind; - Data = data; - ETag = eTag; - LastModified = lastModified; - } - - public string Kind { get; set; } - -#pragma warning disable CA1819 // Properties should not return arrays - public byte[] Data { get; set; } -#pragma warning restore CA1819 // Properties should not return arrays - - public string ETag { get; set; } - public DateTime LastModified { get; set; } - } - public class TimelinePostCreateRequestData { - public TimelinePostCreateRequestData(string kind, byte[] data) + public TimelinePostCreateRequestData(string contentType, byte[] data) { - Kind = kind; + ContentType = contentType; Data = data; } - public string Kind { get; set; } + public string ContentType { get; set; } #pragma warning disable CA1819 // Properties should not return arrays public byte[] Data { get; set; } #pragma warning restore CA1819 // Properties should not return arrays @@ -69,14 +36,13 @@ namespace Timeline.Services /// If not set, current time is used. public DateTime? Time { get; set; } - public List Content { get; set; } = new List(); + public List DataList { get; set; } = new List(); } public class TimelinePostPatchRequest { public string? Color { get; set; } public DateTime? Time { get; set; } - public List? Content { get; set; } } public interface ITimelinePostService @@ -102,18 +68,29 @@ namespace Timeline.Services /// Thrown when post of does not exist or has been deleted. Task GetPost(long timelineId, long postId, bool includeDelete = false); - Task GetPostDataDigest(long timelineId, long postId, long dataIndex); + /// + /// Get the data digest of a post. + /// + /// The timeline id. + /// The post id. + /// The index of the data. + /// The data digest. + /// Thrown when timeline does not exist. + /// Thrown when post of does not exist or has been deleted. + /// Thrown when data of that index does not exist. + Task GetPostDataDigest(long timelineId, long postId, long dataIndex); /// /// Get the data of a post. /// - /// The id of the timeline of the post. - /// The id of the post. - /// The etag of the data. + /// The timeline id. + /// The post id. + /// The index of the data. + /// The data. /// Thrown when timeline does not exist. /// Thrown when post of does not exist or has been deleted. - /// Thrown when post has no data. - Task GetPostData(long timelineId, long postId, long dataIndex); + /// Thrown when data of that index does not exist. + Task GetPostData(long timelineId, long postId, long dataIndex); /// /// Create a new post in timeline. @@ -140,7 +117,6 @@ namespace Timeline.Services /// Thrown when is of invalid format. /// Thrown when timeline does not exist. /// Thrown when post does not exist. - /// Thrown if data is not a image. Validated by . Task PatchPost(long timelineId, long postId, TimelinePostPatchRequest request); /// diff --git a/BackEnd/Timeline/Startup.cs b/BackEnd/Timeline/Startup.cs index 0fab798b..5951dc80 100644 --- a/BackEnd/Timeline/Startup.cs +++ b/BackEnd/Timeline/Startup.cs @@ -61,7 +61,7 @@ namespace Timeline services.AddControllers(setup => { setup.InputFormatters.Add(new StringInputFormatter()); - setup.InputFormatters.Add(new BytesInputFormatter()); + setup.InputFormatters.Add(new ByteDataInputFormatter()); setup.Filters.Add(new ConsumesAttribute(MediaTypeNames.Application.Json, "text/json")); setup.Filters.Add(new ProducesAttribute(MediaTypeNames.Application.Json, "text/json")); setup.UseApiRoutePrefix("api"); -- cgit v1.2.3 From e21b5f85f0d66f51e23a7c1cbf260f2981a83a49 Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 10 Feb 2021 19:17:08 +0800 Subject: ... --- .../Timeline/Helpers/Cache/CacheableDataDigest.cs | 2 +- BackEnd/Timeline/Models/ByteData.cs | 8 +- BackEnd/Timeline/Services/BasicUserService.cs | 29 +++ BackEnd/Timeline/Services/DataManager.cs | 27 ++- BackEnd/Timeline/Services/UserAvatarService.cs | 245 +++++++++++---------- 5 files changed, 189 insertions(+), 122 deletions(-) (limited to 'BackEnd/Timeline/Models') diff --git a/BackEnd/Timeline/Helpers/Cache/CacheableDataDigest.cs b/BackEnd/Timeline/Helpers/Cache/CacheableDataDigest.cs index 3b5bcf52..18a6c894 100644 --- a/BackEnd/Timeline/Helpers/Cache/CacheableDataDigest.cs +++ b/BackEnd/Timeline/Helpers/Cache/CacheableDataDigest.cs @@ -2,7 +2,7 @@ using System; namespace Timeline.Helpers.Cache { - public class CacheableDataDigest + public class CacheableDataDigest : ICacheableDataDigest { public CacheableDataDigest(string eTag, DateTime lastModified) { diff --git a/BackEnd/Timeline/Models/ByteData.cs b/BackEnd/Timeline/Models/ByteData.cs index 7b832eb5..a1a0c238 100644 --- a/BackEnd/Timeline/Models/ByteData.cs +++ b/BackEnd/Timeline/Models/ByteData.cs @@ -1,4 +1,5 @@ -using NSwag.Annotations; +using System; +using NSwag.Annotations; namespace Timeline.Models { @@ -14,6 +15,11 @@ namespace Timeline.Models /// The content type. public ByteData(byte[] data, string contentType) { + if (data is null) + throw new ArgumentNullException(nameof(data)); + if (contentType is null) + throw new ArgumentNullException(nameof(contentType)); + Data = data; ContentType = contentType; } diff --git a/BackEnd/Timeline/Services/BasicUserService.cs b/BackEnd/Timeline/Services/BasicUserService.cs index fbbb6677..de0829ee 100644 --- a/BackEnd/Timeline/Services/BasicUserService.cs +++ b/BackEnd/Timeline/Services/BasicUserService.cs @@ -29,6 +29,14 @@ namespace Timeline.Services /// Thrown when is of bad format. /// Thrown when the user with given username does not exist. Task GetUserIdByUsername(string username); + + /// + /// Get the username modified time of a user. + /// + /// User id. + /// The time. + /// Thrown when user does not exist. + Task GetUsernameLastModifiedTime(long userId); } public class BasicUserService : IBasicUserService @@ -62,5 +70,26 @@ namespace Timeline.Services return entity.Id; } + + public async Task GetUsernameLastModifiedTime(long userId) + { + var entity = await _database.Users.Where(u => u.Id == userId).Select(u => new { u.UsernameChangeTime }).SingleOrDefaultAsync(); + + if (entity is null) + throw new UserNotExistException(userId); + + return entity.UsernameChangeTime; + } + } + + public static class BasicUserServiceExtensions + { + public static async Task ThrowIfUserNotExist(this IBasicUserService service, long userId) + { + if (!await service.CheckUserExistence(userId)) + { + throw new UserNotExistException(userId); + } + } } } diff --git a/BackEnd/Timeline/Services/DataManager.cs b/BackEnd/Timeline/Services/DataManager.cs index d447b0d5..f24bb59b 100644 --- a/BackEnd/Timeline/Services/DataManager.cs +++ b/BackEnd/Timeline/Services/DataManager.cs @@ -38,13 +38,12 @@ namespace Timeline.Services public Task FreeEntry(string tag); /// - /// Retrieve the entry with given tag. + /// Retrieve the entry with given tag. If not exist, returns null. /// /// The tag of the entry. - /// The data of the entry. + /// The data of the entry. If not exist, returns null. /// Thrown when is null. - /// Thrown when entry with given tag does not exist. - public Task GetEntry(string tag); + public Task GetEntry(string tag); } public class DataManager : IDataManager @@ -106,17 +105,31 @@ namespace Timeline.Services } } - public async Task GetEntry(string tag) + public async Task GetEntry(string tag) { if (tag == null) throw new ArgumentNullException(nameof(tag)); var entity = await _database.Data.Where(d => d.Tag == tag).Select(d => new { d.Data }).SingleOrDefaultAsync(); - if (entity == null) - throw new InvalidOperationException(Resources.Services.DataManager.ExceptionEntryNotExist); + if (entity is null) + return null; return entity.Data; } } + + public static class DataManagerExtensions + { + /// + /// Try to get an entry and throw if not exist. + /// + public static async Task GetEntryAndCheck(this IDataManager dataManager, string tag, string notExistMessage) + { + var data = await dataManager.GetEntry(tag); + if (data is null) + throw new DatabaseCorruptedException($"Can't get data of tag {tag}. {notExistMessage}"); + return data; + } + } } diff --git a/BackEnd/Timeline/Services/UserAvatarService.cs b/BackEnd/Timeline/Services/UserAvatarService.cs index afd6cf0a..5a6d013e 100644 --- a/BackEnd/Timeline/Services/UserAvatarService.cs +++ b/BackEnd/Timeline/Services/UserAvatarService.cs @@ -2,12 +2,12 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using SixLabors.ImageSharp; using System; using System.IO; using System.Linq; using System.Threading.Tasks; using Timeline.Entities; -using Timeline.Helpers; using Timeline.Helpers.Cache; using Timeline.Models; using Timeline.Services.Exceptions; @@ -23,39 +23,53 @@ namespace Timeline.Services public interface IDefaultUserAvatarProvider { /// - /// Get the etag of default avatar. + /// Get the digest of default avatar. /// - /// - Task GetDefaultAvatarETag(); + /// The digest. + Task GetDefaultAvatarDigest(); /// /// Get the default avatar. /// + /// The avatar. Task GetDefaultAvatar(); } public interface IUserAvatarService { - Task GetAvatarDigest(long id); + /// + /// Get avatar digest of a user. + /// + /// User id. + /// The avatar digest. + /// Thrown when user does not exist. + Task GetAvatarDigest(long userId); /// - /// Get avatar of a user. If the user has no avatar set, a default one is returned. Warning: This method does not check the user existence. + /// Get avatar of a user. If the user has no avatar set, a default one is returned. /// - /// The id of the user to get avatar of. - /// The avatar info. - Task GetAvatar(long id); + /// User id. + /// The avatar. + /// Thrown when user does not exist. + Task GetAvatar(long userId); /// - /// Set avatar for a user. Warning: This method does not check the user existence. + /// Set avatar for a user. /// - /// The id of the user to set avatar for. - /// The avatar. Can be null to delete the saved avatar. - /// The etag of the avatar. - /// Thrown if any field in is null when is not null. + /// User id. + /// The new avatar data. + /// The digest of the avatar. + /// Thrown if is null. + /// Thrown when user does not exist. /// Thrown if avatar is of bad format. - Task SetAvatar(long id, ByteData avatar); + Task SetAvatar(long userId, ByteData avatar); - Task DeleteAvatar(long id); + /// + /// Remove avatar of a user. + /// + /// User id. + /// Thrown when user does not exist. + Task DeleteAvatar(long userId); } // TODO! : Make this configurable. @@ -65,9 +79,8 @@ namespace Timeline.Services private readonly string _avatarPath; - private byte[] _cacheData = default!; - private DateTime _cacheLastModified; - private string _cacheETag = default!; + private CacheableDataDigest? _cacheDigest; + private ByteData? _cacheData; public DefaultUserAvatarProvider(IWebHostEnvironment environment, IETagGenerator eTagGenerator) { @@ -78,53 +91,42 @@ namespace Timeline.Services private async Task CheckAndInit() { var path = _avatarPath; - if (_cacheData == null || File.GetLastWriteTime(path) > _cacheLastModified) + if (_cacheData == null || File.GetLastWriteTime(path) > _cacheDigest!.LastModified) { - _cacheData = await File.ReadAllBytesAsync(path); - _cacheLastModified = File.GetLastWriteTime(path); - _cacheETag = await _eTagGenerator.Generate(_cacheData); + var data = await File.ReadAllBytesAsync(path); + _cacheDigest = new CacheableDataDigest(await _eTagGenerator.Generate(data), File.GetLastWriteTime(path)); + Image.Identify(data, out var format); + _cacheData = new ByteData(data, format.DefaultMimeType); } } - public async Task GetDefaultAvatarETag() + public async Task GetDefaultAvatarDigest() { await CheckAndInit(); - return _cacheETag; + return _cacheDigest!; } - public async Task GetDefaultAvatar() + public async Task GetDefaultAvatar() { await CheckAndInit(); - return new AvatarInfo - { - Avatar = new Avatar - { - Type = "image/png", - Data = _cacheData - }, - LastModified = _cacheLastModified - }; + return _cacheData!; } } public class UserAvatarService : IUserAvatarService { - private readonly ILogger _logger; - private readonly DatabaseContext _database; - + private readonly IBasicUserService _basicUserService; private readonly IDefaultUserAvatarProvider _defaultUserAvatarProvider; - private readonly IImageValidator _imageValidator; - private readonly IDataManager _dataManager; - private readonly IClock _clock; public UserAvatarService( ILogger logger, DatabaseContext database, + IBasicUserService basicUserService, IDefaultUserAvatarProvider defaultUserAvatarProvider, IImageValidator imageValidator, IDataManager dataManager, @@ -132,106 +134,123 @@ namespace Timeline.Services { _logger = logger; _database = database; + _basicUserService = basicUserService; _defaultUserAvatarProvider = defaultUserAvatarProvider; _imageValidator = imageValidator; _dataManager = dataManager; _clock = clock; } - public async Task GetAvatarETag(long id) + public async Task GetAvatarDigest(long userId) { - var eTag = (await _database.UserAvatars.Where(a => a.UserId == id).Select(a => new { a.DataTag }).SingleOrDefaultAsync())?.DataTag; - if (eTag == null) - return await _defaultUserAvatarProvider.GetDefaultAvatarETag(); + var usernameChangeTime = await _basicUserService.GetUsernameLastModifiedTime(userId); + + var entity = await _database.UserAvatars.Where(a => a.UserId == userId).Select(a => new { a.DataTag, a.LastModified }).SingleOrDefaultAsync(); + + if (entity is null) + { + var defaultAvatarDigest = await _defaultUserAvatarProvider.GetDefaultAvatarDigest(); + return new CacheableDataDigest(defaultAvatarDigest.ETag, new DateTime[] { usernameChangeTime, defaultAvatarDigest.LastModified }.Max()); + } + else if (entity.DataTag is null) + { + var defaultAvatarDigest = await _defaultUserAvatarProvider.GetDefaultAvatarDigest(); + return new CacheableDataDigest(defaultAvatarDigest.ETag, new DateTime[] { usernameChangeTime, defaultAvatarDigest.LastModified, entity.LastModified }.Max()); + } else - return eTag; + { + return new CacheableDataDigest(entity.DataTag, new DateTime[] { usernameChangeTime, entity.LastModified }.Max()); + } } - public async Task GetAvatar(long id) + public async Task GetAvatar(long userId) { - var avatarEntity = await _database.UserAvatars.Where(a => a.UserId == id).Select(a => new { a.Type, a.DataTag, a.LastModified }).SingleOrDefaultAsync(); + await _basicUserService.ThrowIfUserNotExist(userId); + + var entity = await _database.UserAvatars.Where(a => a.UserId == userId).SingleOrDefaultAsync(); - if (avatarEntity != null) + if (entity is null || entity.DataTag is null) { - if (!LanguageHelper.AreSame(avatarEntity.DataTag == null, avatarEntity.Type == null)) - { - var message = Resources.Services.UserAvatarService.ExceptionDatabaseCorruptedDataAndTypeNotSame; - _logger.LogCritical(message); - throw new DatabaseCorruptedException(message); - } + return await _defaultUserAvatarProvider.GetDefaultAvatar(); + } + var data = await _dataManager.GetEntryAndCheck(entity.DataTag, $"This is required by avatar of {userId}."); - if (avatarEntity.DataTag != null) - { - var data = await _dataManager.GetEntry(avatarEntity.DataTag); - return new AvatarInfo - { - Avatar = new Avatar - { - Type = avatarEntity.Type!, - Data = data - }, - LastModified = avatarEntity.LastModified - }; - } + if (entity.Type is null) + { + Image.Identify(data, out var format); + entity.Type = format.DefaultMimeType; + await _database.SaveChangesAsync(); } - var defaultAvatar = await _defaultUserAvatarProvider.GetDefaultAvatar(); - if (avatarEntity != null) - defaultAvatar.LastModified = defaultAvatar.LastModified > avatarEntity.LastModified ? defaultAvatar.LastModified : avatarEntity.LastModified; - return defaultAvatar; + + return new ByteData(data, entity.Type); } - public async Task SetAvatar(long id, Avatar? avatar) + public async Task SetAvatar(long userId, ByteData avatar) { - if (avatar != null) - { - if (avatar.Data == null) - throw new ArgumentException(Resources.Services.UserAvatarService.ExceptionAvatarDataNull, nameof(avatar)); - if (string.IsNullOrEmpty(avatar.Type)) - throw new ArgumentException(Resources.Services.UserAvatarService.ExceptionAvatarTypeNullOrEmpty, nameof(avatar)); - } + if (avatar is null) + throw new ArgumentNullException(nameof(avatar)); + + await _imageValidator.Validate(avatar.Data, avatar.ContentType, true); + + await _basicUserService.ThrowIfUserNotExist(userId); + + var entity = await _database.UserAvatars.Where(a => a.UserId == userId).SingleOrDefaultAsync(); - var avatarEntity = await _database.UserAvatars.Where(a => a.UserId == id).SingleOrDefaultAsync(); + await using var transaction = await _database.Database.BeginTransactionAsync(); - if (avatar == null) + var tag = await _dataManager.RetainEntry(avatar.Data); + + var now = _clock.GetCurrentTime(); + + if (entity is null) { - if (avatarEntity != null && avatarEntity.DataTag != null) + var newEntity = new UserAvatarEntity { - await _dataManager.FreeEntry(avatarEntity.DataTag); - avatarEntity.DataTag = null; - avatarEntity.Type = null; - avatarEntity.LastModified = _clock.GetCurrentTime(); - await _database.SaveChangesAsync(); - _logger.LogInformation(Resources.Services.UserAvatarService.LogUpdateEntity); - } - return await _defaultUserAvatarProvider.GetDefaultAvatarETag(); + DataTag = tag, + Type = avatar.ContentType, + LastModified = now, + UserId = userId + }; + _database.Add(newEntity); } else { - await _imageValidator.Validate(avatar.Data, avatar.Type, true); - var tag = await _dataManager.RetainEntry(avatar.Data); - var oldTag = avatarEntity?.DataTag; - var create = avatarEntity == null; - if (avatarEntity == null) - { - avatarEntity = new UserAvatarEntity(); - _database.UserAvatars.Add(avatarEntity); - } - avatarEntity.DataTag = tag; - avatarEntity.Type = avatar.Type; - avatarEntity.LastModified = _clock.GetCurrentTime(); - avatarEntity.UserId = id; - await _database.SaveChangesAsync(); - _logger.LogInformation(create ? - Resources.Services.UserAvatarService.LogCreateEntity - : Resources.Services.UserAvatarService.LogUpdateEntity); - if (oldTag != null) - { - await _dataManager.FreeEntry(oldTag); - } + if (entity.DataTag is not null) + await _dataManager.FreeEntry(entity.DataTag); - return avatarEntity.DataTag; + entity.DataTag = tag; + entity.Type = avatar.ContentType; + entity.LastModified = now; } + + await _database.SaveChangesAsync(); + + await transaction.CommitAsync(); + + return new CacheableDataDigest(tag, now); + } + + public async Task DeleteAvatar(long userId) + { + await _basicUserService.ThrowIfUserNotExist(userId); + + var entity = await _database.UserAvatars.Where(a => a.UserId == userId).SingleOrDefaultAsync(); + + if (entity is null || entity.DataTag is null) + return; + + await using var transaction = await _database.Database.BeginTransactionAsync(); + + await _dataManager.FreeEntry(entity.DataTag); + + entity.DataTag = null; + entity.Type = null; + entity.LastModified = _clock.GetCurrentTime(); + + await _database.SaveChangesAsync(); + + await transaction.CommitAsync(); } } -- cgit v1.2.3 From b5376f71157f68f06aa04bde79389f9ab291d84a Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 11 Feb 2021 22:21:12 +0800 Subject: ... --- .../IntegratedTests/TimelinePostTest.cs | 338 ++++----------------- .../IntegratedTests/UserAvatarTest.cs | 17 +- .../Timeline/Controllers/UserAvatarController.cs | 4 +- BackEnd/Timeline/Entities/TimelinePostEntity.cs | 2 + .../Timeline/Formatters/ByteDataInputFormatter.cs | 6 + BackEnd/Timeline/Models/Http/HttpTimelinePost.cs | 5 + .../Models/Http/HttpTimelinePostCreateRequest.cs | 2 + BackEnd/Timeline/Models/Mapper/TimelineMapper.cs | 2 + BackEnd/Timeline/Services/TimelinePostService.cs | 4 +- 9 files changed, 87 insertions(+), 293 deletions(-) (limited to 'BackEnd/Timeline/Models') diff --git a/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs index 4caff416..bd79ae18 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs @@ -1,44 +1,43 @@ using FluentAssertions; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; -using SixLabors.ImageSharp; -using SixLabors.ImageSharp.Formats.Png; using System; using System.Collections.Generic; using System.Globalization; using System.Linq; +using System.Text; using System.Threading.Tasks; -using Timeline.Entities; using Timeline.Models; using Timeline.Models.Http; -using Timeline.Tests.Helpers; using Xunit; +using Xunit.Abstractions; namespace Timeline.Tests.IntegratedTests { - public static class TimelineHelper + public class TimelinePostTest : BaseTimelineTest { - public static HttpTimelinePostContent TextPostContent(string text) + private static HttpTimelinePostCreateRequest CreateTextPostRequest(string text, DateTime? time = null, string? color = null) { - return new HttpTimelinePostContent("text", text, null, null); - } - - public static HttpTimelinePostCreateRequest TextPostCreateRequest(string text, DateTime? time = null) - { - return new HttpTimelinePostCreateRequest + return new HttpTimelinePostCreateRequest() { - Content = new HttpTimelinePostCreateRequestContent + Time = time, + Color = color, + DataList = new List() { - Type = "text", - Text = text - }, - Time = time + new HttpTimelinePostCreateRequestData() + { + ContentType = MimeTypes.TextPlain, + Data = Convert.ToBase64String(Encoding.UTF8.GetBytes(text)) + } + } }; } - } - public class TimelinePostTest : BaseTimelineTest - { + private readonly ITestOutputHelper _outputHelper; + + public TimelinePostTest(ITestOutputHelper outputHelper) + { + _outputHelper = outputHelper; + } + [Theory] [MemberData(nameof(TimelineNameGeneratorTestData))] public async Task GetPostsAndVisibility_Should_Work(TimelineNameGenerator generator) @@ -102,15 +101,13 @@ namespace Timeline.Tests.IntegratedTests { using var client = await CreateClientAsUser(); - var postContentList = new List { "a", "b", "c", "d" }; var posts = new List(); - foreach (var content in postContentList) + for (int i = 0; i < 4; i++) { - var post = await client.TestPostAsync($"timelines/{generator(1)}/posts", - new HttpTimelinePostCreateRequest { Content = new HttpTimelinePostCreateRequestContent { Text = content, Type = TimelinePostDataKind.Text } }); + var post = await client.TestPostAsync($"timelines/{generator(1)}/posts", CreateTextPostRequest("a")); posts.Add(post); - await Task.Delay(1000); + await Task.Delay(TimeSpan.FromSeconds(1)); } await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{posts[2].Id}"); @@ -118,64 +115,57 @@ namespace Timeline.Tests.IntegratedTests { var body = await client.TestGetAsync>($"timelines/{generator(1)}/posts?modifiedSince={posts[1].LastUpdated.ToString("s", CultureInfo.InvariantCulture) }"); body.Should().HaveCount(2) - .And.Subject.Select(p => p.Content!.Text).Should().Equal("b", "d"); + .And.Subject.Select(p => p.Id).Should().Equal(posts[1].Id, posts[3].Id); } } [Theory] [MemberData(nameof(TimelineNameGeneratorTestData))] - public async Task Post_ModifiedSince_And_IncludeDeleted(TimelineNameGenerator generator) + public async Task PostList_IncludeDeleted(TimelineNameGenerator generator) { using var client = await CreateClientAsUser(); - var postContentList = new List { "a", "b", "c", "d" }; var posts = new List(); - foreach (var (content, index) in postContentList.Select((v, i) => (v, i))) + for (int i = 0; i < 4; i++) { - var post = await client.TestPostAsync($"timelines/{generator(1)}/posts", - new HttpTimelinePostCreateRequest { Content = new HttpTimelinePostCreateRequestContent { Text = content, Type = TimelinePostDataKind.Text } }); - posts.Add(post); - await Task.Delay(1000); + var body = await client.TestPostAsync($"timelines/{generator(1)}/posts", CreateTextPostRequest("a")); + posts.Add(body); } - await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{posts[2].Id}"); - + foreach (var id in new long[] { posts[0].Id, posts[2].Id }) { + await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{id}"); + } - posts = await client.TestGetAsync>($"timelines/{generator(1)}/posts?modifiedSince={posts[1].LastUpdated.ToString("s", CultureInfo.InvariantCulture)}&includeDeleted=true"); - posts.Should().HaveCount(3); - posts.Select(p => p.Deleted).Should().Equal(false, true, false); - posts.Select(p => p.Content == null).Should().Equal(false, true, false); + { + posts = await client.TestGetAsync>($"timelines/{generator(1)}/posts?includeDeleted=true"); + posts.Should().HaveCount(4); + posts.Select(p => p.Deleted).Should().Equal(true, false, true, false); } } [Theory] [MemberData(nameof(TimelineNameGeneratorTestData))] - public async Task PostList_IncludeDeleted(TimelineNameGenerator generator) + public async Task Post_ModifiedSince_And_IncludeDeleted(TimelineNameGenerator generator) { using var client = await CreateClientAsUser(); - var postContentList = new List { "a", "b", "c", "d" }; var posts = new List(); - foreach (var content in postContentList) + for (int i = 0; i < 4; i++) { - var body = await client.TestPostAsync($"timelines/{generator(1)}/posts", - new HttpTimelinePostCreateRequest { Content = new HttpTimelinePostCreateRequestContent { Text = content, Type = TimelinePostDataKind.Text } }); - posts.Add(body); + var post = await client.TestPostAsync($"timelines/{generator(1)}/posts", CreateTextPostRequest("a")); + posts.Add(post); + await Task.Delay(TimeSpan.FromSeconds(1)); } - foreach (var id in new long[] { posts[0].Id, posts[2].Id }) - { - await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{id}"); - } + await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{posts[2].Id}"); { - posts = await client.TestGetAsync>($"timelines/{generator(1)}/posts?includeDeleted=true"); - posts.Should().HaveCount(4); - posts.Select(p => p.Deleted).Should().Equal(true, false, true, false); - posts.Select(p => p.Content == null).Should().Equal(true, false, true, false); + posts = await client.TestGetAsync>($"timelines/{generator(1)}/posts?modifiedSince={posts[1].LastUpdated.ToString("s", CultureInfo.InvariantCulture)}&includeDeleted=true"); + posts.Should().HaveCount(3); + posts.Select(p => p.Deleted).Should().Equal(false, true, false); } } @@ -190,25 +180,25 @@ namespace Timeline.Tests.IntegratedTests using (var client = await CreateDefaultClient()) { // no auth should get 401 - await client.TestPostAssertUnauthorizedAsync($"timelines/{generator(1)}/posts", TimelineHelper.TextPostCreateRequest("aaa")); + await client.TestPostAssertUnauthorizedAsync($"timelines/{generator(1)}/posts", CreateTextPostRequest("aaa")); } using (var client = await CreateClientAsUser()) { // post self's - await client.TestPostAsync($"timelines/{generator(1)}/posts", TimelineHelper.TextPostCreateRequest("aaa")); + await client.TestPostAsync($"timelines/{generator(1)}/posts", CreateTextPostRequest("aaa")); // post other not as a member should get 403 - await client.TestPostAssertForbiddenAsync($"timelines/{generator(0)}/posts", TimelineHelper.TextPostCreateRequest("aaa")); + await client.TestPostAssertForbiddenAsync($"timelines/{generator(0)}/posts", CreateTextPostRequest("aaa")); } using (var client = await CreateClientAsAdministrator()) { // post as admin - await client.TestPostAsync($"timelines/{generator(1)}/posts", TimelineHelper.TextPostCreateRequest("aaa")); + await client.TestPostAsync($"timelines/{generator(1)}/posts", CreateTextPostRequest("aaa")); } using (var client = await CreateClientAs(2)) { // post as member - await client.TestPostAsync($"timelines/{generator(1)}/posts", TimelineHelper.TextPostCreateRequest("aaa")); + await client.TestPostAsync($"timelines/{generator(1)}/posts", CreateTextPostRequest("aaa")); } } @@ -219,7 +209,7 @@ namespace Timeline.Tests.IntegratedTests async Task CreatePost(int userNumber) { using var client = await CreateClientAs(userNumber); - var body = await client.TestPostAsync($"timelines/{generator(1)}/posts", TimelineHelper.TextPostCreateRequest("aaa")); + var body = await client.TestPostAsync($"timelines/{generator(1)}/posts", CreateTextPostRequest("aaa")); return body.Id; } @@ -265,57 +255,6 @@ namespace Timeline.Tests.IntegratedTests } } - [Theory] - [MemberData(nameof(TimelineNameGeneratorTestData))] - public async Task TextPost_Should_Work(TimelineNameGenerator generator) - { - using var client = await CreateClientAsUser(); - - { - var body = await client.TestGetAsync>($"timelines/{generator(1)}/posts"); - body.Should().BeEmpty(); - } - - const string mockContent = "aaa"; - HttpTimelinePost createRes; - { - var body = await client.TestPostAsync($"timelines/{generator(1)}/posts", TimelineHelper.TextPostCreateRequest(mockContent)); - body.Content.Should().BeEquivalentTo(TimelineHelper.TextPostContent(mockContent)); - body.Author.Should().BeEquivalentTo(await client.GetUserAsync("user1")); - body.Deleted.Should().BeFalse(); - createRes = body; - } - { - var body = await client.TestGetAsync>($"timelines/{generator(1)}/posts"); - body.Should().BeEquivalentTo(createRes); - } - const string mockContent2 = "bbb"; - var mockTime2 = DateTime.UtcNow.AddDays(-1); - HttpTimelinePost createRes2; - { - var body = await client.TestPostAsync($"timelines/{generator(1)}/posts", TimelineHelper.TextPostCreateRequest(mockContent2, mockTime2)); - body.Should().NotBeNull(); - body.Content.Should().BeEquivalentTo(TimelineHelper.TextPostContent(mockContent2)); - body.Author.Should().BeEquivalentTo(await client.GetUserAsync("user1")); - body.Time.Should().BeCloseTo(mockTime2, 1000); - body.Deleted.Should().BeFalse(); - createRes2 = body; - } - { - var body = await client.TestGetAsync>($"timelines/{generator(1)}/posts"); - body.Should().BeEquivalentTo(createRes, createRes2); - } - { - await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{createRes.Id}"); - await client.TestDeleteAssertErrorAsync($"timelines/{generator(1)}/posts/{createRes.Id}"); - await client.TestDeleteAssertErrorAsync($"timelines/{generator(1)}/posts/30000"); - } - { - var body = await client.TestGetAsync>($"timelines/{generator(1)}/posts"); - body.Should().BeEquivalentTo(createRes2); - } - } - [Theory] [MemberData(nameof(TimelineNameGeneratorTestData))] public async Task GetPost_Should_Ordered(TimelineNameGenerator generator) @@ -324,7 +263,7 @@ namespace Timeline.Tests.IntegratedTests async Task CreatePost(DateTime time) { - var body = await client.TestPostAsync($"timelines/{generator(1)}/posts", TimelineHelper.TextPostCreateRequest("aaa", time)); + var body = await client.TestPostAsync($"timelines/{generator(1)}/posts", CreateTextPostRequest("aaa", time)); return body.Id; } @@ -339,166 +278,19 @@ namespace Timeline.Tests.IntegratedTests } } - [Theory] - [MemberData(nameof(TimelineNameGeneratorTestData))] - public async Task CreatePost_InvalidModel(TimelineNameGenerator generator) - { - using var client = await CreateClientAsUser(); - var postUrl = $"timelines/{generator(1)}/posts"; - await client.TestPostAssertInvalidModelAsync(postUrl, new HttpTimelinePostCreateRequest { Content = null! }); - await client.TestPostAssertInvalidModelAsync(postUrl, new HttpTimelinePostCreateRequest { Content = new HttpTimelinePostCreateRequestContent { Type = null! } }); - await client.TestPostAssertInvalidModelAsync(postUrl, new HttpTimelinePostCreateRequest { Content = new HttpTimelinePostCreateRequestContent { Type = "hahaha" } }); - await client.TestPostAssertInvalidModelAsync(postUrl, new HttpTimelinePostCreateRequest { Content = new HttpTimelinePostCreateRequestContent { Type = "text", Text = null } }); - await client.TestPostAssertInvalidModelAsync(postUrl, new HttpTimelinePostCreateRequest { Content = new HttpTimelinePostCreateRequestContent { Type = "image", Data = null } }); - // image not base64 - await client.TestPostAssertInvalidModelAsync(postUrl, new HttpTimelinePostCreateRequest { Content = new HttpTimelinePostCreateRequestContent { Type = "image", Data = "!!!" } }); - // image base64 not image - await client.TestPostAssertInvalidModelAsync(postUrl, new HttpTimelinePostCreateRequest { Content = new HttpTimelinePostCreateRequestContent { Type = "image", Data = Convert.ToBase64String(new byte[] { 0x01, 0x02, 0x03 }) } }); - } - - [Theory] - [MemberData(nameof(TimelineNameGeneratorTestData))] - public async Task ImagePost_ShouldWork(TimelineNameGenerator generator) - { - var imageData = ImageHelper.CreatePngWithSize(100, 200); - - long postId; - string postImageUrl; - - void AssertPostContent(HttpTimelinePostContent content) - { - content.Type.Should().Be(TimelinePostDataKind.Image); - content.Url.Should().EndWith($"timelines/{generator(1)}/posts/{postId}/data"); - content.Text.Should().Be(null); - } - - using var client = await CreateClientAsUser(); - - { - var body = await client.TestPostAsync($"timelines/{generator(1)}/posts", - new HttpTimelinePostCreateRequest - { - Content = new HttpTimelinePostCreateRequestContent - { - Type = TimelinePostDataKind.Image, - Data = Convert.ToBase64String(imageData) - } - }); - postId = body.Id; - postImageUrl = body.Content!.Url!; - AssertPostContent(body.Content); - } - - { - var body = await client.TestGetAsync>($"timelines/{generator(1)}/posts"); - body.Should().HaveCount(1); - var post = body[0]; - post.Id.Should().Be(postId); - AssertPostContent(post.Content!); - } - - { - var res = await client.GetAsync($"timelines/{generator(1)}/posts/{postId}/data"); - res.Content.Headers.ContentType!.MediaType.Should().Be("image/png"); - var data = await res.Content.ReadAsByteArrayAsync(); - var image = Image.Load(data, out var format); - image.Width.Should().Be(100); - image.Height.Should().Be(200); - format.Name.Should().Be(PngFormat.Instance.Name); - } - - await CacheTestHelper.TestCache(client, $"timelines/{generator(1)}/posts/{postId}/data"); - await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{postId}"); - await client.TestDeleteAssertErrorAsync($"timelines/{generator(1)}/posts/{postId}"); - - { - var body = await client.TestGetAsync>($"timelines/{generator(1)}/posts"); - body.Should().BeEmpty(); - } - - { - using var scope = TestApp.Host.Services.CreateScope(); - var database = scope.ServiceProvider.GetRequiredService(); - var count = await database.Data.CountAsync(); - count.Should().Be(0); - } - } - - [Theory] - [MemberData(nameof(TimelineNameGeneratorTestData))] - public async Task ImagePost_400(TimelineNameGenerator generator) - { - using var client = await CreateClientAsUser(); - - await client.TestGetAssertNotFoundAsync($"timelines/{generator(1)}/posts/11234/data", errorCode: ErrorCodes.TimelineController.PostNotExist); - - long postId; - { - var body = await client.TestPostAsync($"timelines/{generator(1)}/posts", TimelineHelper.TextPostCreateRequest("aaa")); - postId = body.Id; - } - - await client.TestGetAssertErrorAsync($"timelines/{generator(1)}/posts/{postId}/data", errorCode: ErrorCodes.TimelineController.PostNoData); - } - - [Theory] - [MemberData(nameof(TimelineNameGeneratorTestData))] - public async Task PostDataETag(TimelineNameGenerator generator) - { - using var client = await CreateClientAsUser(); - - long id; - string etag; - - { - var body = await client.TestPostAsync($"timelines/{generator(1)}/posts", new HttpTimelinePostCreateRequest - { - Content = new HttpTimelinePostCreateRequestContent - { - Type = TimelinePostDataKind.Image, - Data = Convert.ToBase64String(ImageHelper.CreatePngWithSize(100, 50)) - } - }); - body.Content!.ETag.Should().NotBeNullOrEmpty(); - - id = body.Id; - etag = body.Content.ETag!; - } - - { - var res = await client.GetAsync($"timelines/{generator(1)}/posts/{id}/data"); - res.StatusCode.Should().Be(200); - res.Headers.ETag.Should().NotBeNull(); - res.Headers.ETag!.ToString().Should().Be(etag); - } - } - [Theory] [MemberData(nameof(TimelineNameGeneratorTestData))] public async Task Color(TimelineNameGenerator generator) { using var client = await CreateClientAsUser(); - HttpTimelinePostCreateRequestContent CreateRequestContent() => new() - { - Type = "text", - Text = "aaa" - }; - - await client.TestPostAssertInvalidModelAsync($"timelines/{generator(1)}/posts", new HttpTimelinePostCreateRequest - { - Content = CreateRequestContent(), - Color = "#1" - }); + await client.TestPostAssertInvalidModelAsync($"timelines/{generator(1)}/posts", CreateTextPostRequest("a", color: "aa")); long id; { - var post = await client.TestPostAsync($"timelines/{generator(1)}/posts", new HttpTimelinePostCreateRequest - { - Content = CreateRequestContent(), - Color = "#aabbcc" - }); + var post = await client.TestPostAsync($"timelines/{generator(1)}/posts", + CreateTextPostRequest("a", color: "#aabbcc")); post.Color.Should().Be("#aabbcc"); id = post.Id; } @@ -515,18 +307,10 @@ namespace Timeline.Tests.IntegratedTests { using var client = await CreateClientAsUser(); - HttpTimelinePostCreateRequestContent CreateRequestContent() => new() - { - Type = "text", - Text = "aaa" - }; await client.TestGetAssertNotFoundAsync($"timelines/{generator(1)}/posts/1"); - var post = await client.TestPostAsync($"timelines/{generator(1)}/posts", new HttpTimelinePostCreateRequest - { - Content = CreateRequestContent(), - }); + var post = await client.TestPostAsync($"timelines/{generator(1)}/posts", CreateTextPostRequest("a")); var post2 = await client.TestGetAsync($"timelines/{generator(1)}/posts/{post.Id}"); post2.Should().BeEquivalentTo(post); @@ -542,14 +326,8 @@ namespace Timeline.Tests.IntegratedTests { using var client = await CreateClientAsUser(); - var post = await client.TestPostAsync($"timelines/{generator(1)}/posts", new HttpTimelinePostCreateRequest - { - Content = new() - { - Type = "text", - Text = "aaa" - } - }); + var post = await client.TestPostAsync($"timelines/{generator(1)}/posts", + CreateTextPostRequest("a")); var date = new DateTime(2000, 10, 1); diff --git a/BackEnd/Timeline.Tests/IntegratedTests/UserAvatarTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/UserAvatarTest.cs index 893a5d28..708120b1 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/UserAvatarTest.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/UserAvatarTest.cs @@ -11,8 +11,8 @@ using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; +using Timeline.Models; using Timeline.Models.Http; -using Timeline.Services; using Timeline.Tests.Helpers; using Xunit; @@ -23,11 +23,10 @@ namespace Timeline.Tests.IntegratedTests [Fact] public async Task Test() { - Avatar mockAvatar = new Avatar - { - Data = ImageHelper.CreatePngWithSize(100, 100), - Type = PngFormat.Instance.DefaultMimeType - }; + ByteData mockAvatar = new ByteData( + ImageHelper.CreatePngWithSize(100, 100), + PngFormat.Instance.DefaultMimeType + ); using (var client = await CreateClientAsUser()) { @@ -106,7 +105,7 @@ namespace Timeline.Tests.IntegratedTests } { - await client.TestPutByteArrayAsync("users/user1/avatar", mockAvatar.Data, mockAvatar.Type); + await client.TestPutByteArrayAsync("users/user1/avatar", mockAvatar.Data, mockAvatar.ContentType); await TestAvatar("user1", mockAvatar.Data); } @@ -137,7 +136,7 @@ namespace Timeline.Tests.IntegratedTests // Authorization check. using (var client = await CreateClientAsAdministrator()) { - await client.TestPutByteArrayAsync("users/user1/avatar", mockAvatar.Data, mockAvatar.Type); + await client.TestPutByteArrayAsync("users/user1/avatar", mockAvatar.Data, mockAvatar.ContentType); await client.TestDeleteAsync("users/user1/avatar"); await client.TestPutByteArrayAssertErrorAsync("users/usernotexist/avatar", new[] { (byte)0x00 }, "image/png", errorCode: ErrorCodes.UserCommon.NotExist); await client.TestDeleteAssertErrorAsync("users/usernotexist/avatar", errorCode: ErrorCodes.UserCommon.NotExist); @@ -175,4 +174,4 @@ namespace Timeline.Tests.IntegratedTests } } } -} \ No newline at end of file +} diff --git a/BackEnd/Timeline/Controllers/UserAvatarController.cs b/BackEnd/Timeline/Controllers/UserAvatarController.cs index 8ac2d21a..180d1f9b 100644 --- a/BackEnd/Timeline/Controllers/UserAvatarController.cs +++ b/BackEnd/Timeline/Controllers/UserAvatarController.cs @@ -102,12 +102,12 @@ namespace Timeline.Controllers try { - var etag = await _service.SetAvatar(id, body); + var digest = await _service.SetAvatar(id, body); _logger.LogInformation(Log.Format(LogPutSuccess, ("Username", username), ("Mime Type", Request.ContentType))); - Response.Headers.Append("ETag", new EntityTagHeaderValue($"\"{etag}\"").ToString()); + Response.Headers.Append("ETag", new EntityTagHeaderValue($"\"{digest.ETag}\"").ToString()); return Ok(); } diff --git a/BackEnd/Timeline/Entities/TimelinePostEntity.cs b/BackEnd/Timeline/Entities/TimelinePostEntity.cs index c65ef929..1f0270cb 100644 --- a/BackEnd/Timeline/Entities/TimelinePostEntity.cs +++ b/BackEnd/Timeline/Entities/TimelinePostEntity.cs @@ -37,6 +37,8 @@ namespace Timeline.Entities [Column("last_updated")] public DateTime LastUpdated { get; set; } +#pragma warning disable CA2227 public List DataList { get; set; } = default!; +#pragma warning restore CA2227 } } diff --git a/BackEnd/Timeline/Formatters/ByteDataInputFormatter.cs b/BackEnd/Timeline/Formatters/ByteDataInputFormatter.cs index 2451ead6..49f8221a 100644 --- a/BackEnd/Timeline/Formatters/ByteDataInputFormatter.cs +++ b/BackEnd/Timeline/Formatters/ByteDataInputFormatter.cs @@ -44,6 +44,12 @@ namespace Timeline.Formatters var logger = context.HttpContext.RequestServices.GetRequiredService>(); + if (request.ContentType is null) + { + logger.LogInformation("Failed to read body as bytes. Content-Type is not set."); + return await InputFormatterResult.FailureAsync(); + } + if (contentLength == null) { logger.LogInformation("Failed to read body as bytes. Content-Length is not set."); diff --git a/BackEnd/Timeline/Models/Http/HttpTimelinePost.cs b/BackEnd/Timeline/Models/Http/HttpTimelinePost.cs index 165c92da..26e1a92d 100644 --- a/BackEnd/Timeline/Models/Http/HttpTimelinePost.cs +++ b/BackEnd/Timeline/Models/Http/HttpTimelinePost.cs @@ -26,7 +26,12 @@ namespace Timeline.Models.Http /// Post id. /// public long Id { get; set; } + /// + /// The data list. + /// +#pragma warning disable CA2227 public List DataList { get; set; } = default!; +#pragma warning restore CA2227 /// /// True if post is deleted. /// diff --git a/BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequest.cs b/BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequest.cs index 07d823ad..2a973c72 100644 --- a/BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequest.cs +++ b/BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequest.cs @@ -13,7 +13,9 @@ namespace Timeline.Models.Http [Required] [MinLength(1)] [MaxLength(100)] +#pragma warning disable CA2227 public List DataList { get; set; } = default!; +#pragma warning restore CA2227 /// /// Time of the post. If not set, current time will be used. diff --git a/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs b/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs index 33ee9593..1f10c123 100644 --- a/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs +++ b/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs @@ -66,6 +66,8 @@ namespace Timeline.Models.Mapper public async Task MapToHttp(TimelinePostEntity entity, string timelineName, IUrlHelper urlHelper) { + _ = timelineName; + await _database.Entry(entity).Collection(p => p.DataList).LoadAsync(); await _database.Entry(entity).Reference(e => e.Author).LoadAsync(); diff --git a/BackEnd/Timeline/Services/TimelinePostService.cs b/BackEnd/Timeline/Services/TimelinePostService.cs index 8afd0770..62bc43cc 100644 --- a/BackEnd/Timeline/Services/TimelinePostService.cs +++ b/BackEnd/Timeline/Services/TimelinePostService.cs @@ -1,6 +1,5 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; -using SixLabors.ImageSharp; using System; using System.Collections.Generic; using System.Linq; @@ -12,7 +11,6 @@ using Timeline.Helpers.Cache; using Timeline.Models; using Timeline.Models.Validation; using Timeline.Services.Exceptions; -using static Timeline.Resources.Services.TimelineService; namespace Timeline.Services { @@ -37,7 +35,9 @@ namespace Timeline.Services /// If not set, current time is used. public DateTime? Time { get; set; } +#pragma warning disable CA2227 public List DataList { get; set; } = new List(); +#pragma warning restore CA2227 } public class TimelinePostPatchRequest -- cgit v1.2.3 From b5b758c41a01ab7f78f0711debe92f6add470c64 Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 12 Feb 2021 17:45:09 +0800 Subject: test: Add create post integrated tests. --- .../IntegratedTests/TimelinePostTest.cs | 70 ++++++++++++++++++++++ .../Timeline/Controllers/UserAvatarController.cs | 2 +- BackEnd/Timeline/Helpers/Cache/DataCacheHelper.cs | 2 +- BackEnd/Timeline/Models/Mapper/TimelineMapper.cs | 2 +- 4 files changed, 73 insertions(+), 3 deletions(-) (limited to 'BackEnd/Timeline/Models') diff --git a/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs index 85db0908..4563db3a 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs @@ -10,6 +10,10 @@ using Timeline.Models.Http; using Timeline.Tests.Helpers; using Xunit; using Xunit.Abstractions; +using SixLabors.ImageSharp.Formats.Gif; +using SixLabors.ImageSharp.Formats.Png; +using SixLabors.ImageSharp.Formats.Jpeg; +using System.Net; namespace Timeline.Tests.IntegratedTests { @@ -418,5 +422,71 @@ namespace Timeline.Tests.IntegratedTests } ); } + + public static IEnumerable CreatePost_ShouldWork_TestData() + { + var testByteDatas = new List() + { + new ByteData(Encoding.UTF8.GetBytes("aaa"), MimeTypes.TextPlain), + new ByteData(Encoding.UTF8.GetBytes("aaa"), MimeTypes.TextMarkdown), + new ByteData(ImageHelper.CreateImageWithSize(100, 50, PngFormat.Instance), MimeTypes.ImagePng), + new ByteData(ImageHelper.CreateImageWithSize(100, 50, JpegFormat.Instance), MimeTypes.ImageJpeg), + new ByteData(ImageHelper.CreateImageWithSize(100, 50, GifFormat.Instance), MimeTypes.ImageGif), + }; + + return TimelineNameGeneratorTestData().AppendTestData(testByteDatas); + } + + [Theory] + [MemberData(nameof(CreatePost_ShouldWork_TestData))] + public async Task CreatePost_ShouldWork(TimelineNameGenerator generator, ByteData data) + { + using var client = await CreateClientAsUser(); + + var post = await client.TestPostAsync( + $"timelines/{generator(1)}/posts", + new HttpTimelinePostCreateRequest + { + DataList = new List + { + new HttpTimelinePostCreateRequestData + { + ContentType = data.ContentType, + Data = Convert.ToBase64String(data.Data) + } + } + } + ); + + post.DataList.Should().NotBeNull().And.HaveCount(1); + var postData = post.DataList[0]; + postData.Should().NotBeNull(); + var postDataEtag = postData.ETag; + postDataEtag.Should().NotBeNullOrEmpty(); + + { + var response = await client.GetAsync($"timelines/{generator(1)}/posts/{post.Id}/data"); + response.StatusCode.Should().Be(HttpStatusCode.OK); + response.Headers.ETag.Should().NotBeNull(); + response.Headers.ETag!.Tag.Should().Be(postDataEtag); + response.Content.Headers.ContentType.Should().NotBeNull(); + response.Content.Headers.ContentType!.MediaType.Should().Be(data.ContentType); + + var body = await response.Content.ReadAsByteArrayAsync(); + body.Should().Equal(data.Data); + } + + { + var response = await client.GetAsync($"timelines/{generator(1)}/posts/{post.Id}/data/0"); + response.StatusCode.Should().Be(HttpStatusCode.OK); + response.Headers.ETag.Should().NotBeNull(); + response.Headers.ETag!.Tag.Should().Be(postDataEtag); + response.Content.Headers.ContentType.Should().NotBeNull(); + response.Content.Headers.ContentType!.MediaType.Should().Be(data.ContentType); + + var body = await response.Content.ReadAsByteArrayAsync(); + body.Should().Equal(data.Data); + } + } } } diff --git a/BackEnd/Timeline/Controllers/UserAvatarController.cs b/BackEnd/Timeline/Controllers/UserAvatarController.cs index 180d1f9b..fa13f0f6 100644 --- a/BackEnd/Timeline/Controllers/UserAvatarController.cs +++ b/BackEnd/Timeline/Controllers/UserAvatarController.cs @@ -107,7 +107,7 @@ namespace Timeline.Controllers _logger.LogInformation(Log.Format(LogPutSuccess, ("Username", username), ("Mime Type", Request.ContentType))); - Response.Headers.Append("ETag", new EntityTagHeaderValue($"\"{digest.ETag}\"").ToString()); + Response.Headers.Append("ETag", $"\"{digest.ETag}\""); return Ok(); } diff --git a/BackEnd/Timeline/Helpers/Cache/DataCacheHelper.cs b/BackEnd/Timeline/Helpers/Cache/DataCacheHelper.cs index c26bdddc..b7d86b18 100644 --- a/BackEnd/Timeline/Helpers/Cache/DataCacheHelper.cs +++ b/BackEnd/Timeline/Helpers/Cache/DataCacheHelper.cs @@ -33,7 +33,7 @@ namespace Timeline.Helpers.Cache } var digest = await provider.GetDigest(); - var eTagValue = '"' + digest.ETag + '"'; + var eTagValue = $"\"{digest.ETag}\""; var eTag = new EntityTagHeaderValue(eTagValue); ActionResult Generate304Result() diff --git a/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs b/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs index 1f10c123..5c46fa81 100644 --- a/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs +++ b/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs @@ -71,7 +71,7 @@ namespace Timeline.Models.Mapper await _database.Entry(entity).Collection(p => p.DataList).LoadAsync(); await _database.Entry(entity).Reference(e => e.Author).LoadAsync(); - List dataDigestList = entity.DataList.OrderBy(d => d.Index).Select(d => new HttpTimelinePostDataDigest(d.Kind, d.DataTag, d.LastUpdated)).ToList(); + List dataDigestList = entity.DataList.OrderBy(d => d.Index).Select(d => new HttpTimelinePostDataDigest(d.Kind, $"\"{d.DataTag}\"", d.LastUpdated)).ToList(); HttpUser? author = null; if (entity.Author is not null) -- cgit v1.2.3