Skip to content

Commit

Permalink
Merge branch 'release/v1.3.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
tor4kichi committed Feb 18, 2022
2 parents 3723eaf + 94cab9a commit f69f10c
Show file tree
Hide file tree
Showing 104 changed files with 1,294 additions and 1,040 deletions.
1 change: 1 addition & 0 deletions Hohoema.Models/Hohoema.Models.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
<Compile Include="Models.Domain\Application\HohoemaAppServiceLevel.cs" />
<Compile Include="Models.Domain\Application\LisenceSummary.cs" />
<Compile Include="Models.Domain\Application\PlayerDisplayView.cs" />
<Compile Include="Models.Domain\Application\ThumbnailCacheManager.cs" />
<Compile Include="Models.Domain\Application\ToastNotificationConstants.cs" />
<Compile Include="Models.Domain\Niconico\AccountManager.cs" />
<Compile Include="Models.Domain\Niconico\Channel\ChannelNameCacheRepository.cs" />
Expand Down
144 changes: 144 additions & 0 deletions Hohoema.Models/Models.Domain/Application/ThumbnailCacheManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
using LiteDB;
using Microsoft.Toolkit.Mvvm.DependencyInjection;
using System;
using System.Buffers;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media.Imaging;
using Windows.Web.Http;

namespace Hohoema.Models.Domain.Application
{
public class UrlToCachedImageConverter : IValueConverter
{
private readonly ThumbnailCacheManager _thumbnailCacheManager;

public UrlToCachedImageConverter()
{
_thumbnailCacheManager = Ioc.Default.GetService<ThumbnailCacheManager>();
}

public object Convert(object value, Type targetType, object parameter, string language)
{
if (value is string url)
{
var bitmap = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
_thumbnailCacheManager.ResolveImage(bitmap, url);
return bitmap;
}
else if (value is Uri uri)
{
var bitmap = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
_thumbnailCacheManager.ResolveImage(bitmap, uri.OriginalString);
return bitmap;
}
else
{
throw new NotSupportedException();
}
}

public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}

public sealed class ThumbnailCacheManager
{
private readonly LiteStorage _fileStorage;
private readonly HttpClient _httpClient;

public ThumbnailCacheManager(
LiteDatabase liteDatabase
)
{
_fileStorage = liteDatabase.FileStorage;
#if DEBUG && true
foreach (var file in _fileStorage.FindAll().ToArray())
{
_fileStorage.Delete(file.Id);
}
#endif
_httpClient = new HttpClient();
}

public void Maitenance(TimeSpan expiredTime, int maxCount)
{
DateTime expiredDateTime = DateTime.Now - expiredTime;
foreach (var fileInfo in _fileStorage.FindAll().Where(x => x.Metadata.TryGetValue("updateAt", out var val) && (DateTime)val < expiredDateTime).ToArray())
{
_fileStorage.Delete(fileInfo.Id);
}

foreach (var fileInfo in _fileStorage.FindAll().OrderByDescending(x => x.UploadDate).Skip(maxCount).ToArray())
{
_fileStorage.Delete(fileInfo.Id);
}
}

public async void ResolveImage(BitmapImage image, string imageUrl)
{
var id = imageUrl.Replace("https://nicovideo.cdn.nimg.jp/thumbnails/", "");
if (TryGetCacheImageStream(id, imageUrl, out var stream))
{
using var _ = stream;
image.SetSource(stream.AsRandomAccessStream());
}
else
{
using var res = await _httpClient.GetAsync(new Uri(imageUrl));
if (!res.Content.TryComputeLength(out var length)) { throw new InvalidOperationException(); }
using var memoryStream = new MemoryStream((int)length);
await res.Content.WriteToStreamAsync(memoryStream.AsOutputStream());
memoryStream.Seek(0, SeekOrigin.Begin);
image.SetSource(memoryStream.AsRandomAccessStream());
memoryStream.Seek(0, SeekOrigin.Begin);
SetCacheImage(id, imageUrl, memoryStream.AsInputStream());
}
}

private bool TryGetCacheImageStream(string id, string imageUrl, out Stream outImageStream)
{
id = $"$/{id}";
if (_fileStorage.Exists(id))
{
var file = _fileStorage.FindById(id);
var stream = new MemoryStream((int)file.Length);
try
{
file.CopyTo(stream);
stream.Seek(0, SeekOrigin.Begin);
_fileStorage.SetMetadata(id, new BsonDocument(new Dictionary<string, BsonValue>() { { "updateAt", DateTime.Now } }));
outImageStream = stream;
return true;
}
catch
{
stream.Dispose();
throw;
}
}
else
{
outImageStream = null;
return false;
}

}


private bool SetCacheImage(string id, string imageUrl, IInputStream stream)
{
id = $"$/{id}";
var file = _fileStorage.Upload(id, imageUrl, stream.AsStreamForRead());
_fileStorage.SetMetadata(id, new BsonDocument(new Dictionary<string, BsonValue>() { { "updateAt", DateTime.Now } }));
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ public struct CommentPostResult
public ChatResultCode Status => StatusCode;
}

public interface ICommentSession : IDisposable
public interface ICommentSession<TComment> : IDisposable where TComment : IComment
{
string ContentId { get; }
string UserId { get; }

event EventHandler<IComment> RecieveComment;
event EventHandler<TComment> RecieveComment;

Task<IReadOnlyCollection<IComment>> GetInitialComments();
Task<IEnumerable<TComment>> GetInitialComments();

bool CanPostComment { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

namespace Hohoema.Models.Domain.Player
{
public interface INiconicoCommentSessionProvider
public interface INiconicoCommentSessionProvider<TComment> where TComment : IComment
{
VideoId ContentId { get; }
Task<ICommentSession> CreateCommentSessionAsync();
Task<ICommentSession<TComment>> CreateCommentSessionAsync();
}

public interface INiconicoVideoSessionProvider
Expand Down
26 changes: 6 additions & 20 deletions Hohoema.Models/Models.Domain/Player/Video/Comment/CommentClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,36 +93,22 @@ public bool CanSubmitComment
}


public async Task<List<IComment>> GetCommentsAsync()
public async Task<IEnumerable<VideoComment>> GetCommentsAsync()
{
List<IComment> comments = null;

if (CanGetCommentsFromNMSG)
{
try
{
var res = await GetCommentsFromNMSG();

var rawComments = res.Comments;

comments = rawComments.Select(x => ChatToComment(x)).ToList();
}
catch
{
}
var res = await GetCommentsFromNMSG();
return res.Comments.Select(x => ChatToComment(x));
}

if (comments == null)
else
{
throw new HohoemaExpception("コメント取得に失敗");
throw new NotSupportedException();
}

return comments;
}

public string VideoOwnerId { get; set; }

private IComment ChatToComment(NMSG_Chat rawComment)
private VideoComment ChatToComment(NMSG_Chat rawComment)
{
return new VideoComment()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Hohoema.Models.Domain.Player.Video.Comment
{
public class VideoCommentService : ICommentSession
public class VideoCommentService : ICommentSession<VideoComment>
{
CommentClient CommentClient;

Expand All @@ -19,14 +19,14 @@ public VideoCommentService(CommentClient commentClient, string userId)
}


public event EventHandler<IComment> RecieveComment;
public event EventHandler<VideoComment> RecieveComment;

void IDisposable.Dispose()
{

}

public async Task<IReadOnlyCollection<IComment>> GetInitialComments()
public async Task<IEnumerable<VideoComment>> GetInitialComments()
{
return await CommentClient.GetCommentsAsync();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public enum PreparePlayVideoFailedReason
NotPlayPermit_RequirePremiumMember,
}

public class PreparePlayVideoResult : INiconicoVideoSessionProvider, INiconicoCommentSessionProvider
public class PreparePlayVideoResult : INiconicoVideoSessionProvider, INiconicoCommentSessionProvider<VideoComment>
{
public Exception Exception { get; }
public bool IsSuccess { get; }
Expand Down Expand Up @@ -272,7 +272,7 @@ public async Task<IStreamingSession> CreateVideoSessionAsync(NicoVideoQuality qu



public Task<ICommentSession> CreateCommentSessionAsync()
public Task<ICommentSession<VideoComment>> CreateCommentSessionAsync()
{
if (_dmcWatchData != null)
{
Expand All @@ -284,7 +284,7 @@ public Task<ICommentSession> CreateCommentSessionAsync()
}
}

Task<ICommentSession> CreateCommentSession(string contentId, DmcWatchApiData watchData)
Task<ICommentSession<VideoComment>> CreateCommentSession(string contentId, DmcWatchApiData watchData)
{
var commentClient = new CommentClient(_niconicoSession, contentId);
var dmcRes = watchData;
Expand All @@ -308,7 +308,7 @@ Task<ICommentSession> CreateCommentSession(string contentId, DmcWatchApiData wat
commentClient.CommentServerInfo.CommunityThreadId = communityThread.Id;
}

return Task.FromResult(new VideoCommentService(commentClient, _niconicoSession.UserId) as ICommentSession);
return Task.FromResult(new VideoCommentService(commentClient, _niconicoSession.UserId) as ICommentSession<VideoComment>);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ internal PlayingOrchestrateResult(Exception exception)
Exception = exception;
}

internal PlayingOrchestrateResult(INiconicoVideoSessionProvider vss, INiconicoCommentSessionProvider cs, INicoVideoDetails videoDetails)
internal PlayingOrchestrateResult(INiconicoVideoSessionProvider vss, INiconicoCommentSessionProvider<VideoComment> cs, INicoVideoDetails videoDetails)
{
IsSuccess = vss != null;
VideoSessionProvider = vss;
Expand All @@ -88,7 +88,7 @@ internal PlayingOrchestrateResult(INiconicoVideoSessionProvider vss, INiconicoCo


public INiconicoVideoSessionProvider VideoSessionProvider { get; }
public INiconicoCommentSessionProvider CommentSessionProvider { get; }
public INiconicoCommentSessionProvider<VideoComment> CommentSessionProvider { get; }

public INicoVideoDetails VideoDetails { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -599,8 +599,7 @@ private void StopPlaybackMedia()

public async Task ReopenAsync(TimeSpan? position = null)
{
if (CurrentPlaylist != null
&& CurrentPlaylistItem != null)
if (CurrentPlaylistItem != null)
{
if (position == null)
{
Expand Down
4 changes: 2 additions & 2 deletions Hohoema.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29025.244
# Visual Studio Version 17
VisualStudioVersion = 17.0.32126.317
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hohoema", "Hohoema\Hohoema.csproj", "{17BCCDA1-CE22-4306-8845-FDD7B346944D}"
EndProject
Expand Down
Loading

0 comments on commit f69f10c

Please sign in to comment.