mirror of
https://jihulab.com/DGP-Studio/Snap.Hutao.git
synced 2025-11-19 21:02:53 +08:00
add static resource hint for more situation
This commit is contained in:
@@ -18,6 +18,12 @@ internal sealed class HutaoException : Exception
|
||||
|
||||
public HutaoExceptionKind Kind { get; private set; }
|
||||
|
||||
[DoesNotReturn]
|
||||
public static HutaoException Throw(HutaoExceptionKind kind, string message, Exception? innerException = default)
|
||||
{
|
||||
throw new HutaoException(kind, message, innerException);
|
||||
}
|
||||
|
||||
public static void ThrowIf(bool condition, HutaoExceptionKind kind, string message, Exception? innerException = default)
|
||||
{
|
||||
if (condition)
|
||||
@@ -25,4 +31,10 @@ internal sealed class HutaoException : Exception
|
||||
throw new HutaoException(kind, message, innerException);
|
||||
}
|
||||
}
|
||||
|
||||
public static HutaoException ServiceTypeCastFailed<TFrom, TTo>(string name, Exception? innerException = default)
|
||||
{
|
||||
string message = $"This instance of '{typeof(TFrom).FullName}' '{name}' doesn't implement '{typeof(TTo).FullName}'";
|
||||
throw new HutaoException(HutaoExceptionKind.ServiceTypeCastFailed, message, innerException);
|
||||
}
|
||||
}
|
||||
@@ -6,4 +6,5 @@ namespace Snap.Hutao.Core.ExceptionService;
|
||||
internal enum HutaoExceptionKind
|
||||
{
|
||||
None,
|
||||
ServiceTypeCastFailed,
|
||||
}
|
||||
@@ -1691,6 +1691,9 @@
|
||||
<data name="ViewModelWelcomeDownloadSummaryComplete" xml:space="preserve">
|
||||
<value>完成</value>
|
||||
</data>
|
||||
<data name="ViewModelWelcomeDownloadSummaryContentTypeNotMatch" xml:space="preserve">
|
||||
<value>响应内容不是有效的文件字节流</value>
|
||||
</data>
|
||||
<data name="ViewModelWelcomeDownloadSummaryDefault" xml:space="preserve">
|
||||
<value>等待中</value>
|
||||
</data>
|
||||
|
||||
@@ -5,6 +5,7 @@ using CommunityToolkit.Common;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using Snap.Hutao.Core;
|
||||
using Snap.Hutao.Core.Caching;
|
||||
using Snap.Hutao.Core.ExceptionService;
|
||||
using Snap.Hutao.Core.IO;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
@@ -74,6 +75,14 @@ internal sealed class DownloadSummary : ObservableObject
|
||||
try
|
||||
{
|
||||
HttpResponseMessage response = await httpClient.GetAsync(fileUrl, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);
|
||||
|
||||
if (response.Content.Headers.ContentType?.MediaType is not "application/octet-stream")
|
||||
{
|
||||
logger.LogWarning("Download Static Zip failed, Content-Type is {Type}", response.Content.Headers.ContentType);
|
||||
Description = SH.ViewModelWelcomeDownloadSummaryContentTypeNotMatch;
|
||||
return false;
|
||||
}
|
||||
|
||||
long contentLength = response.Content.Headers.ContentLength ?? 0;
|
||||
logger.LogInformation("Begin download, length: {length}", contentLength);
|
||||
using (Stream content = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
|
||||
@@ -95,7 +104,7 @@ internal sealed class DownloadSummary : ObservableObject
|
||||
logger.LogError(ex, "Download Static Zip failed");
|
||||
await taskContext.SwitchToMainThreadAsync();
|
||||
Description = ex is HttpRequestException httpRequestException
|
||||
? $"{SH.ViewModelWelcomeDownloadSummaryException} - HTTP {httpRequestException.HttpRequestError} {httpRequestException.StatusCode:D}"
|
||||
? $"{SH.ViewModelWelcomeDownloadSummaryException} - [HTTP '{httpRequestException.StatusCode:D}'] [Error '{httpRequestException.HttpRequestError}']"
|
||||
: ex.Message;
|
||||
return false;
|
||||
}
|
||||
@@ -109,8 +118,10 @@ internal sealed class DownloadSummary : ObservableObject
|
||||
|
||||
private void ExtractFiles(Stream stream)
|
||||
{
|
||||
IImageCacheFilePathOperation? imageCacheFilePathOperation = imageCache.As<IImageCacheFilePathOperation>();
|
||||
ArgumentNullException.ThrowIfNull(imageCacheFilePathOperation);
|
||||
if (imageCache is not IImageCacheFilePathOperation imageCacheFilePathOperation)
|
||||
{
|
||||
throw HutaoException.ServiceTypeCastFailed<IImageCache, IImageCacheFilePathOperation>(nameof(imageCache));
|
||||
}
|
||||
|
||||
using (ZipArchive archive = new(stream))
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user