aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Helpers
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-10-27 19:21:35 +0800
committercrupest <crupest@outlook.com>2020-10-27 19:21:35 +0800
commitac769e656b122ff569c3f1534701b71e00fed586 (patch)
tree72966645ff1e25139d3995262e1c4349f2c14733 /Timeline/Helpers
parent14e5848c23c643cea9b5d709770747d98c3d75e2 (diff)
downloadtimeline-ac769e656b122ff569c3f1534701b71e00fed586.tar.gz
timeline-ac769e656b122ff569c3f1534701b71e00fed586.tar.bz2
timeline-ac769e656b122ff569c3f1534701b71e00fed586.zip
Split front and back end.
Diffstat (limited to 'Timeline/Helpers')
-rw-r--r--Timeline/Helpers/DataCacheHelper.cs125
-rw-r--r--Timeline/Helpers/DateTimeExtensions.cs14
-rw-r--r--Timeline/Helpers/InvalidModelResponseFactory.cs25
-rw-r--r--Timeline/Helpers/LanguageHelper.cs12
-rw-r--r--Timeline/Helpers/Log.cs22
5 files changed, 0 insertions, 198 deletions
diff --git a/Timeline/Helpers/DataCacheHelper.cs b/Timeline/Helpers/DataCacheHelper.cs
deleted file mode 100644
index 1ad69708..00000000
--- a/Timeline/Helpers/DataCacheHelper.cs
+++ /dev/null
@@ -1,125 +0,0 @@
-using Microsoft.AspNetCore.Http;
-using Microsoft.AspNetCore.Mvc;
-using Microsoft.Extensions.DependencyInjection;
-using Microsoft.Extensions.Logging;
-using Microsoft.Net.Http.Headers;
-using System;
-using System.Linq;
-using System.Threading.Tasks;
-using Timeline.Models.Http;
-using static Timeline.Resources.Helper.DataCacheHelper;
-
-namespace Timeline.Helpers
-{
- public interface ICacheableData
- {
- string Type { get; }
-#pragma warning disable CA1819 // Properties should not return arrays
- byte[] Data { get; }
-#pragma warning restore CA1819 // Properties should not return arrays
- DateTime? LastModified { get; }
- }
-
- public class CacheableData : ICacheableData
- {
- public CacheableData(string type, byte[] data, DateTime? lastModified)
- {
- Type = type;
- Data = data;
- LastModified = lastModified;
- }
-
- public string Type { 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 DateTime? LastModified { get; set; }
- }
-
- public interface ICacheableDataProvider
- {
- Task<string> GetDataETag();
- Task<ICacheableData> GetData();
- }
-
- public class DelegateCacheableDataProvider : ICacheableDataProvider
- {
- private readonly Func<Task<string>> _getDataETagDelegate;
- private readonly Func<Task<ICacheableData>> _getDataDelegate;
-
- public DelegateCacheableDataProvider(Func<Task<string>> getDataETagDelegate, Func<Task<ICacheableData>> getDataDelegate)
- {
- _getDataETagDelegate = getDataETagDelegate;
- _getDataDelegate = getDataDelegate;
- }
-
- public Task<ICacheableData> GetData()
- {
- return _getDataDelegate();
- }
-
- public Task<string> GetDataETag()
- {
- return _getDataETagDelegate();
- }
- }
-
- public static class DataCacheHelper
- {
- public static async Task<ActionResult> GenerateActionResult(Controller controller, ICacheableDataProvider provider, TimeSpan? maxAge = null)
- {
- const string CacheControlHeaderKey = "Cache-Control";
- const string IfNonMatchHeaderKey = "If-None-Match";
- const string ETagHeaderKey = "ETag";
-
- string GenerateCacheControlHeaderValue()
- {
- var cacheControlHeader = new CacheControlHeaderValue()
- {
- NoCache = true,
- NoStore = false,
- MaxAge = maxAge ?? TimeSpan.FromDays(14),
- Private = true,
- MustRevalidate = true
- };
- return cacheControlHeader.ToString();
- }
-
- var loggerFactory = controller.HttpContext.RequestServices.GetRequiredService<ILoggerFactory>();
- var logger = loggerFactory.CreateLogger(typeof(DataCacheHelper));
-
- var eTagValue = await provider.GetDataETag();
- eTagValue = '"' + eTagValue + '"';
- var eTag = new EntityTagHeaderValue(eTagValue);
-
-
- if (controller.Request.Headers.TryGetValue(IfNonMatchHeaderKey, out var value))
- {
- if (!EntityTagHeaderValue.TryParseStrictList(value, out var eTagList))
- {
- logger.LogInformation(Log.Format(LogBadIfNoneMatch, ("Header Value", value)));
- return controller.BadRequest(ErrorResponse.Common.Header.IfNonMatch_BadFormat());
- }
-
- if (eTagList.FirstOrDefault(e => e.Equals(eTag)) != null)
- {
- logger.LogInformation(LogResultNotModified);
- controller.Response.Headers.Add(ETagHeaderKey, eTagValue);
- controller.Response.Headers.Add(CacheControlHeaderKey, GenerateCacheControlHeaderValue());
-
- return controller.StatusCode(StatusCodes.Status304NotModified, null);
- }
- }
-
- var data = await provider.GetData();
- logger.LogInformation(LogResultData);
- controller.Response.Headers.Add(CacheControlHeaderKey, GenerateCacheControlHeaderValue());
- return controller.File(data.Data, data.Type, data.LastModified, eTag);
- }
-
- public static Task<ActionResult> GenerateActionResult(Controller controller, Func<Task<string>> getDataETagDelegate, Func<Task<ICacheableData>> getDataDelegate, TimeSpan? maxAge = null)
- {
- return GenerateActionResult(controller, new DelegateCacheableDataProvider(getDataETagDelegate, getDataDelegate), maxAge);
- }
- }
-}
diff --git a/Timeline/Helpers/DateTimeExtensions.cs b/Timeline/Helpers/DateTimeExtensions.cs
deleted file mode 100644
index 374f3bc9..00000000
--- a/Timeline/Helpers/DateTimeExtensions.cs
+++ /dev/null
@@ -1,14 +0,0 @@
-using System;
-
-namespace Timeline.Helpers
-{
- public static class DateTimeExtensions
- {
- public static DateTime MyToUtc(this DateTime dateTime)
- {
- if (dateTime.Kind == DateTimeKind.Utc) return dateTime;
- if (dateTime.Kind == DateTimeKind.Local) return dateTime.ToUniversalTime();
- return DateTime.SpecifyKind(dateTime, DateTimeKind.Utc);
- }
- }
-}
diff --git a/Timeline/Helpers/InvalidModelResponseFactory.cs b/Timeline/Helpers/InvalidModelResponseFactory.cs
deleted file mode 100644
index 9b253e7d..00000000
--- a/Timeline/Helpers/InvalidModelResponseFactory.cs
+++ /dev/null
@@ -1,25 +0,0 @@
-using Microsoft.AspNetCore.Mvc;
-using System.Text;
-using Timeline.Models.Http;
-
-namespace Timeline.Helpers
-{
- public static class InvalidModelResponseFactory
- {
- public static IActionResult Factory(ActionContext context)
- {
- var modelState = context.ModelState;
-
- var messageBuilder = new StringBuilder();
- foreach (var model in modelState)
- foreach (var error in model.Value.Errors)
- {
- messageBuilder.Append(model.Key);
- messageBuilder.Append(" : ");
- messageBuilder.AppendLine(error.ErrorMessage);
- }
-
- return new BadRequestObjectResult(ErrorResponse.Common.CustomMessage_InvalidModel(messageBuilder.ToString()));
- }
- }
-}
diff --git a/Timeline/Helpers/LanguageHelper.cs b/Timeline/Helpers/LanguageHelper.cs
deleted file mode 100644
index b0156b8b..00000000
--- a/Timeline/Helpers/LanguageHelper.cs
+++ /dev/null
@@ -1,12 +0,0 @@
-using System.Linq;
-
-namespace Timeline.Helpers
-{
- public static class LanguageHelper
- {
- public static bool AreSame(this bool firstBool, params bool[] otherBools)
- {
- return otherBools.All(b => b == firstBool);
- }
- }
-}
diff --git a/Timeline/Helpers/Log.cs b/Timeline/Helpers/Log.cs
deleted file mode 100644
index af0b7e13..00000000
--- a/Timeline/Helpers/Log.cs
+++ /dev/null
@@ -1,22 +0,0 @@
-using System.Text;
-
-namespace Timeline.Helpers
-{
- public static class Log
- {
- public static string Format(string summary, params (string, object?)[] properties)
- {
- var builder = new StringBuilder();
- builder.Append(summary);
- foreach (var property in properties)
- {
- var (key, value) = property;
- builder.AppendLine();
- builder.Append(key);
- builder.Append(" : ");
- builder.Append(value);
- }
- return builder.ToString();
- }
- }
-}