aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Services
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-03-09 00:21:28 +0800
committercrupest <crupest@outlook.com>2020-03-09 00:21:28 +0800
commitc3c9618c8493349fb3d6464d69359f7be6e8ce24 (patch)
tree16c0dc235cfba2ec1fa17e3cb3bfbbead348cb3e /Timeline/Services
parent540baa40d0d32d87fbba0ad351ff534fd43f2749 (diff)
downloadtimeline-c3c9618c8493349fb3d6464d69359f7be6e8ce24.tar.gz
timeline-c3c9618c8493349fb3d6464d69359f7be6e8ce24.tar.bz2
timeline-c3c9618c8493349fb3d6464d69359f7be6e8ce24.zip
...
Diffstat (limited to 'Timeline/Services')
-rw-r--r--Timeline/Services/TimelinePostNotExistException.cs11
-rw-r--r--Timeline/Services/TimelineService.cs55
2 files changed, 59 insertions, 7 deletions
diff --git a/Timeline/Services/TimelinePostNotExistException.cs b/Timeline/Services/TimelinePostNotExistException.cs
index 97e5d550..c542e63e 100644
--- a/Timeline/Services/TimelinePostNotExistException.cs
+++ b/Timeline/Services/TimelinePostNotExistException.cs
@@ -12,12 +12,17 @@ namespace Timeline.Services
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
- public TimelinePostNotExistException(long id) : base(Resources.Services.Exception.TimelinePostNotExistException) { Id = id; }
+ public TimelinePostNotExistException(string timelineName, long id, bool isDelete = false) : base(Resources.Services.Exception.TimelinePostNotExistException) { TimelineName = timelineName; Id = id; IsDelete = isDelete; }
- public TimelinePostNotExistException(long id, string message) : base(message) { Id = id; }
+ public TimelinePostNotExistException(string timelineName, long id, bool isDelete, string message) : base(message) { TimelineName = timelineName; Id = id; IsDelete = isDelete; }
- public TimelinePostNotExistException(long id, string message, Exception inner) : base(message, inner) { Id = id; }
+ public TimelinePostNotExistException(string timelineName, long id, bool isDelete, string message, Exception inner) : base(message, inner) { TimelineName = timelineName; Id = id; IsDelete = isDelete; }
+ public string TimelineName { get; set; } = "";
public long Id { get; set; }
+ /// <summary>
+ /// True if the post is deleted. False if the post does not exist at all.
+ /// </summary>
+ public bool IsDelete { get; set; } = false;
}
}
diff --git a/Timeline/Services/TimelineService.cs b/Timeline/Services/TimelineService.cs
index ff2532ea..a2b7b5ea 100644
--- a/Timeline/Services/TimelineService.cs
+++ b/Timeline/Services/TimelineService.cs
@@ -1,6 +1,7 @@
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
+using SixLabors.ImageSharp;
using System;
using System.Collections.Generic;
using System.Globalization;
@@ -105,7 +106,7 @@ namespace Timeline.Services
/// <returns>The data and its type.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="name"/> is null.</exception>
/// <exception cref="ArgumentException">Thrown when <paramref name="name"/> is illegal. It is not a valid timeline name (for normal timeline service) or a valid username (for personal timeline service).</exception>
- /// <exception cref="TimelinePostNotExistException">Thrown when post of <paramref name="postId"/> does not exist.</exception>
+ /// <exception cref="TimelinePostNotExistException">Thrown when post of <paramref name="postId"/> does not exist or has been deleted.</exception>
/// <exception cref="InvalidOperationException">Thrown when post has no data. See remarks.</exception>
/// <remarks>
/// Use this method to retrieve the image of image post.
@@ -332,6 +333,7 @@ namespace Timeline.Services
{
protected BaseTimelineService(ILoggerFactory loggerFactory, DatabaseContext database, IImageValidator imageValidator, IDataManager dataManager, IUserService userService, IMapper mapper, IClock clock)
{
+ _logger = loggerFactory.CreateLogger<BaseTimelineService>();
Clock = clock;
Database = database;
ImageValidator = imageValidator;
@@ -340,6 +342,8 @@ namespace Timeline.Services
Mapper = mapper;
}
+ private ILogger<BaseTimelineService> _logger;
+
protected IClock Clock { get; }
protected UsernameValidator UsernameValidator { get; } = new UsernameValidator();
@@ -422,7 +426,6 @@ namespace Timeline.Services
if (name == null)
throw new ArgumentNullException(nameof(name));
-
var timelineId = await FindTimelineId(name);
var postEntities = await Database.TimelinePosts.OrderBy(p => p.Time).Where(p => p.TimelineId == timelineId && p.Content != null).ToListAsync();
@@ -454,6 +457,50 @@ namespace Timeline.Services
}
return posts;
}
+ public async Task<DataWithType> GetPostData(string name, long postId)
+ {
+ if (name == null)
+ throw new ArgumentNullException(nameof(name));
+
+ var timelineId = await FindTimelineId(name);
+ var postEntity = await Database.TimelinePosts.Where(p => p.LocalId == postId).SingleOrDefaultAsync();
+
+ if (postEntity == null)
+ throw new TimelinePostNotExistException(name, postId);
+
+ if (postEntity.Content == null)
+ throw new TimelinePostNotExistException(name, postId, true);
+
+ if (postEntity.ContentType != TimelinePostContentTypes.Image)
+ throw new InvalidOperationException(ExceptionGetDataNonImagePost);
+
+ var tag = postEntity.Content;
+
+ byte[] data;
+
+ try
+ {
+ data = await DataManager.GetEntry(tag);
+ }
+ catch (InvalidOperationException e)
+ {
+ throw new DatabaseCorruptedException(ExceptionGetDataDataEntryNotExist, e);
+ }
+
+ if (postEntity.ExtraContent == null)
+ {
+ _logger.LogWarning(LogGetDataNoFormat);
+ var format = Image.DetectFormat(data);
+ postEntity.ExtraContent = format.DefaultMimeType;
+ await Database.SaveChangesAsync();
+ }
+
+ return new DataWithType
+ {
+ Data = data,
+ Type = postEntity.ExtraContent
+ };
+ }
public async Task<TimelinePostInfo> CreateTextPost(string name, long authorId, string text, DateTime? time)
{
@@ -552,7 +599,7 @@ namespace Timeline.Services
var post = await Database.TimelinePosts.Where(p => p.TimelineId == timelineId && p.LocalId == id).SingleOrDefaultAsync();
if (post == null)
- throw new TimelinePostNotExistException(id);
+ throw new TimelinePostNotExistException(name, id);
string? dataTag = null;
@@ -721,7 +768,7 @@ namespace Timeline.Services
var postEntity = await Database.TimelinePosts.Where(p => p.Id == id).Select(p => new { p.AuthorId }).SingleOrDefaultAsync();
if (postEntity == null)
- throw new TimelinePostNotExistException(id);
+ throw new TimelinePostNotExistException(name, id);
return timelineEntity.OwnerId == modifierId || postEntity.AuthorId == modifierId;
}