Skip to content

Commit

Permalink
fix: GetVideoPathAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
LeZi9916 committed Feb 7, 2025
1 parent 5043df5 commit 719cf7d
Show file tree
Hide file tree
Showing 2 changed files with 241 additions and 124 deletions.
109 changes: 107 additions & 2 deletions Assets/Scripts/Misc/Types/DataFormats/OnlineSongDetail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Buffers;
using System.Threading;
using Unity.VisualScripting.Antlr3.Runtime;
using System.Security.Policy;
#nullable enable
namespace MajdataPlay.Types
{
Expand Down Expand Up @@ -45,6 +46,7 @@ internal class OnlineSongDetail: ISongDetail
readonly string _fullSizeCoverUriStr = string.Empty;
readonly string _coverUriStr = string.Empty;

string? _videoPath = null;
AudioSampleWrap? _audioTrack = null;
AudioSampleWrap? _previewAudioTrack = null;
Sprite? _cover = null;
Expand Down Expand Up @@ -175,11 +177,47 @@ public async UniTask<string> GetVideoPathAsync(CancellationToken token = default
using (await _videoPathLock.LockAsync(token))
{
token.ThrowIfCancellationRequested();

if(_videoPath is not null)
return _videoPath;
var savePath = Path.Combine(_cachePath, "bg.mp4");
var cacheFlagPath = Path.Combine(_cachePath, $"bg.mp4.cache");

await CheckAndDownloadFile(_videoUri, savePath, token);
if (File.Exists(cacheFlagPath) && !File.Exists(savePath))
{
_videoPath = string.Empty;
return _videoPath;
}
for (var i = 0; i <= MajEnv.HTTP_REQUEST_MAX_RETRY; i++)
{
try
{
var httpClient = MajEnv.SharedHttpClient;
using var rsp = await httpClient.GetAsync(_videoUri, HttpCompletionOption.ResponseHeadersRead, token);

return savePath;
if (rsp.StatusCode != System.Net.HttpStatusCode.OK)
{
using var _ = File.Create(cacheFlagPath);
_videoPath = string.Empty;
return _videoPath;
}
else
{
break;
}
}
catch (Exception e)
{
if (i == MajEnv.HTTP_REQUEST_MAX_RETRY)
{
MajDebug.LogError($"Failed to request resource: {_coverUri}\n{e}");
throw;
}
}
}
await CheckAndDownloadFile(_videoUri, savePath, token);
_videoPath = savePath;
return _videoPath;
}
}
finally
Expand Down Expand Up @@ -230,7 +268,40 @@ async UniTask<Sprite> GetCompressedCoverAsync(CancellationToken token = default)
if (_cover is not null)
return _cover;
var savePath = Path.Combine(_cachePath, "bg.jpg");
var cacheFlagPath = Path.Combine(_cachePath, $"bg.jpg.cache");

if (File.Exists(cacheFlagPath) && !File.Exists(savePath))
{
_cover = MajEnv.EmptySongCover;
return _cover;
}
for (var i = 0; i <= MajEnv.HTTP_REQUEST_MAX_RETRY; i++)
{
try
{
var httpClient = MajEnv.SharedHttpClient;
using var rsp = await httpClient.GetAsync(_coverUri, HttpCompletionOption.ResponseHeadersRead, token);

if (rsp.StatusCode != System.Net.HttpStatusCode.OK)
{
using var _ = File.Create(cacheFlagPath);
_cover = MajEnv.EmptySongCover;
return _cover;
}
else
{
break;
}
}
catch (Exception e)
{
if (i == MajEnv.HTTP_REQUEST_MAX_RETRY)
{
MajDebug.LogError($"Failed to request resource: {_coverUri}\n{e}");
throw;
}
}
}
await CheckAndDownloadFile(_coverUri, savePath, token);
token.ThrowIfCancellationRequested();
_cover = await SpriteLoader.LoadAsync(savePath, token);
Expand All @@ -253,7 +324,40 @@ async UniTask<Sprite> GetFullSizeCoverAsync(CancellationToken token = default)
if (_fullSizeCover is not null)
return _fullSizeCover;
var savePath = Path.Combine(_cachePath, "bg_fullSize.jpg");
var cacheFlagPath = Path.Combine(_cachePath, $"bg_fullSize.jpg.cache");

if(File.Exists(cacheFlagPath) && !File.Exists(savePath))
{
_fullSizeCover = MajEnv.EmptySongCover;
return _fullSizeCover;
}
for (var i = 0; i <= MajEnv.HTTP_REQUEST_MAX_RETRY; i++)
{
try
{
var httpClient = MajEnv.SharedHttpClient;
using var rsp = await httpClient.GetAsync(_fullSizeCoverUri, HttpCompletionOption.ResponseHeadersRead, token);

if (rsp.StatusCode != System.Net.HttpStatusCode.OK)
{
using var _ = File.Create(cacheFlagPath);
_fullSizeCover = MajEnv.EmptySongCover;
return _fullSizeCover;
}
else
{
break;
}
}
catch (Exception e)
{
if (i == MajEnv.HTTP_REQUEST_MAX_RETRY)
{
MajDebug.LogError($"Failed to request resource: {_fullSizeCoverUri}\n{e}");
throw;
}
}
}
await CheckAndDownloadFile(_fullSizeCoverUri, savePath, token);
token.ThrowIfCancellationRequested();
_fullSizeCover = await SpriteLoader.LoadAsync(savePath, token);
Expand Down Expand Up @@ -286,6 +390,7 @@ await Task.Run(async () =>
return;
}
using var rsp = await httpClient.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead, token);
rsp.EnsureSuccessStatusCode();
token.ThrowIfCancellationRequested();
MajDebug.Log($"Received http response header from: {uri}");

Expand Down
Loading

0 comments on commit 719cf7d

Please sign in to comment.